Skip to content

Commit

Permalink
added city filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Musilah committed Aug 3, 2023
1 parent fd0fdfc commit c36f5f9
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 19 deletions.
1 change: 1 addition & 0 deletions api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func retrieveSummaryEndpoint(svc callhome.Service) endpoint.Endpoint {
}
return telemetrySummaryRes{
Countries: summary.Countries,
Cities: summary.Cities,
TotalDeployments: summary.TotalDeployments,
}, nil
}
Expand Down
1 change: 1 addition & 0 deletions api/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (res uiRes) Headers() map[string]string {

type telemetrySummaryRes struct {
Countries []callhome.CountrySummary `json:"countries,omitempty"`
Cities []callhome.CitySummary `json:"cities,omitempty"`
TotalDeployments int `json:"total_deployments,omitempty"`
}

Expand Down
15 changes: 11 additions & 4 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,22 @@ func (ts *telemetryService) Save(ctx context.Context, t Telemetry) error {
}

func (ts *telemetryService) RetrieveSummary(ctx context.Context, filters TelemetryFilters) (TelemetrySummary, error) {
return ts.repo.RetrieveDistinctIPsCountries(ctx, filters)
return ts.repo.RetrieveDistinctIPs(ctx, filters)
}

// ServeUI gets the callhome index html page
func (ts *telemetryService) ServeUI(ctx context.Context, filters TelemetryFilters) ([]byte, error) {
tmpl := template.Must(template.ParseFiles("./web/template/index.html"))

summary, err := ts.repo.RetrieveDistinctIPsCountries(ctx, filters)
summary, err := ts.repo.RetrieveDistinctIPs(ctx, filters)
if err != nil {
return nil, err
}
allCountries, err := ts.repo.RetrieveDistinctIPsCountries(ctx, TelemetryFilters{})
allCountries, err := ts.repo.RetrieveDistinctIPs(ctx, TelemetryFilters{})
if err != nil {
return nil, err
}
allCities, err := ts.repo.RetrieveDistinctIPsCountries(ctx, filters)
allCities, err := ts.repo.RetrieveDistinctIPs(ctx, filters)
if err != nil {
return nil, err
}
Expand All @@ -92,6 +92,11 @@ func (ts *telemetryService) ServeUI(ctx context.Context, filters TelemetryFilter
return nil, err
}

cities, err := json.Marshal(summary.Cities)
if err != nil {
return nil, err
}

filterCountries := allCountries.Countries
filterCities := allCities.Cities

Expand All @@ -104,6 +109,7 @@ func (ts *telemetryService) ServeUI(ctx context.Context, filters TelemetryFilter
}
data := struct {
Countries string
Cities string
FilterCountries []CountrySummary
FilterCities []CitySummary
NoDeployments int
Expand All @@ -113,6 +119,7 @@ func (ts *telemetryService) ServeUI(ctx context.Context, filters TelemetryFilter
To string
}{
Countries: string(countries),
Cities: string(cities),
FilterCountries: filterCountries,
FilterCities: filterCities,
NoDeployments: summary.TotalDeployments,
Expand Down
11 changes: 5 additions & 6 deletions telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ type CitySummary struct {
}

type TelemetrySummary struct {
Countries []CountrySummary `json:"countries,omitempty"`
TotalDeployments int `json:"total_deployments,omitempty"`
Cities []CitySummary `json:"cities,omitempty"`
TotalCityDeployments int `json:"total_city_deployments,omitempty"`
Countries []CountrySummary `json:"countries,omitempty"`
Cities []CitySummary `json:"cities,omitempty"`
TotalDeployments int `json:"total_deployments,omitempty"`
}

// TelemetryRepository specifies an account persistence API.
Expand All @@ -65,6 +64,6 @@ type TelemetryRepo interface {

// RetrieveAll retrieves all telemetry events.
RetrieveAll(ctx context.Context, pm PageMetadata, filters TelemetryFilters) (TelemetryPage, error)
// RetrieveDistinctIPsCOuntries gets distinct ip addresses and countries from database.
RetrieveDistinctIPsCountries(ctx context.Context, filters TelemetryFilters) (TelemetrySummary, error)
// RetrieveDistinctIPs gets distinct ip addresses and countries from database.
RetrieveDistinctIPs(ctx context.Context, filters TelemetryFilters) (TelemetrySummary, error)
}
4 changes: 2 additions & 2 deletions timescale/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (mr *mockRepo) Save(ctx context.Context, t callhome.Telemetry) error {
return r0
}

// RetrieveDistinctIPsCountries retrieve distinct
func (*mockRepo) RetrieveDistinctIPsCountries(ctx context.Context, filter callhome.TelemetryFilters) (callhome.TelemetrySummary, error) {
// RetrieveDistinctIPs retrieve distinct
func (*mockRepo) RetrieveDistinctIPs(ctx context.Context, filter callhome.TelemetryFilters) (callhome.TelemetrySummary, error) {
return callhome.TelemetrySummary{}, nil
}

Expand Down
20 changes: 17 additions & 3 deletions timescale/timescale.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ func (r repo) Save(ctx context.Context, t callhome.Telemetry) error {

}

// RetrieveDistinctIPsCountries retrieve distinct
func (r repo) RetrieveDistinctIPsCountries(ctx context.Context, filters callhome.TelemetryFilters) (callhome.TelemetrySummary, error) {
// RetrieveDistinctIPs retrieve distinct
func (r repo) RetrieveDistinctIPs(ctx context.Context, filters callhome.TelemetryFilters) (callhome.TelemetrySummary, error) {
filterQuery, params := generateQuery(filters)
var summary callhome.TelemetrySummary
q := fmt.Sprintf(`select count(distinct ip_address), country from telemetry %s group by country;`, filterQuery)
rows, err := r.db.NamedQuery(q, params)
if err != nil {
return callhome.TelemetrySummary{}, err
}
defer rows.Close()
var summary callhome.TelemetrySummary
for rows.Next() {
var val callhome.CountrySummary
if err := rows.StructScan(&val); err != nil {
Expand All @@ -146,6 +146,20 @@ func (r repo) RetrieveDistinctIPsCountries(ctx context.Context, filters callhome
for _, country := range summary.Countries {
summary.TotalDeployments += country.NoDeployments
}

q1 := `select city from telemetry;`
cityRows, err := r.db.NamedQuery(q1, params)
if err != nil {
return callhome.TelemetrySummary{}, err
}
defer cityRows.Close()
for cityRows.Next() {
var val callhome.CitySummary
if err := cityRows.StructScan(&val); err != nil {
return callhome.TelemetrySummary{}, err
}
summary.Cities = append(summary.Cities, val)
}
return summary, nil
}

Expand Down
6 changes: 3 additions & 3 deletions timescale/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func (rt *repoTracer) RetrieveAll(ctx context.Context, pm callhome.PageMetadata,
return rt.repo.RetrieveAll(ctx, pm, filter)
}

// RetrieveDistinctIPsCountries adds tracing middleware to retrieve distinct ips countries method.
func (rt *repoTracer) RetrieveDistinctIPsCountries(ctx context.Context, filter callhome.TelemetryFilters) (callhome.TelemetrySummary, error) {
// RetrieveDistinctIPs adds tracing middleware to retrieve distinct ips countries method.
func (rt *repoTracer) RetrieveDistinctIPs(ctx context.Context, filter callhome.TelemetryFilters) (callhome.TelemetrySummary, error) {
ctx, span := rt.tracer.Start(ctx, retrieveDistinctIPsCountriesOp)
defer span.End()
return rt.repo.RetrieveDistinctIPsCountries(ctx, filter)
return rt.repo.RetrieveDistinctIPs(ctx, filter)
}

// Save adds tracing middleware to save method.
Expand Down
2 changes: 1 addition & 1 deletion web/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ body {
flex-grow: 1;
height: 95%;
width: 100%;

}

.btn {
Expand Down
2 changes: 2 additions & 0 deletions web/template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ <h1 class="modal-title fs-5" id="applyFiltersLabel">Apply Filters</h1>
var toDate = toDateInput ? new Date(toDateInput).toISOString() : '';

var url = `/?from=${encodeURIComponent(fromDate)}&to=${encodeURIComponent(toDate)}&country=${encodeURIComponent(selectedCountry)}&city=${encodeURIComponent(selectedCity)}`;


window.location.href = url;
}

Expand Down

0 comments on commit c36f5f9

Please sign in to comment.