Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOISSUE - Add Country, City, Service and Version filters to the UI #20

Merged
merged 18 commits into from
Aug 8, 2023
3 changes: 3 additions & 0 deletions api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func retrieveSummaryEndpoint(svc callhome.Service) endpoint.Endpoint {
}
return telemetrySummaryRes{
Countries: summary.Countries,
Cities: summary.Cities,
Services: summary.Services,
Versions: summary.Versions,
TotalDeployments: summary.TotalDeployments,
}, nil
}
Expand Down
3 changes: 3 additions & 0 deletions api/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func (res uiRes) Headers() map[string]string {

type telemetrySummaryRes struct {
Musilah marked this conversation as resolved.
Show resolved Hide resolved
Countries []callhome.CountrySummary `json:"countries,omitempty"`
Cities []string `json:"cities,omitempty"`
Services []string `json:"services,omitempty"`
Versions []string `json:"versions,omitempty"`
TotalDeployments int `json:"total_deployments,omitempty"`
}

Expand Down
12 changes: 12 additions & 0 deletions openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ components:
type: string
number_of_deployments:
type: integer
cities:
type: array
items:
type: string
services:
type: array
items:
type: string
versions:
type: array
items:
type: string
securitySchemes:
ApiKeyAuth:
type: apiKey
Expand Down
42 changes: 28 additions & 14 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ 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.RetrieveSummary(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.RetrieveSummary(ctx, filters)
if err != nil {
return nil, err
}
unfilteredSummary, err := ts.repo.RetrieveSummary(ctx, TelemetryFilters{})
if err != nil {
return nil, err
}
Expand All @@ -81,6 +85,7 @@ func (ts *telemetryService) ServeUI(ctx context.Context, filters TelemetryFilter
if err != nil {
return nil, err
}

Musilah marked this conversation as resolved.
Show resolved Hide resolved
var from, to string
if !filters.From.IsZero() {
from = filters.From.Format(time.DateOnly)
Expand All @@ -89,19 +94,28 @@ func (ts *telemetryService) ServeUI(ctx context.Context, filters TelemetryFilter
to = filters.To.Format(time.DateOnly)
}
data := struct {
Countries string
NoDeployments int
NoCountries int
MapData string
From string
To string
Countries string
Cities string
FilterCountries []CountrySummary
FilterCities []string
FilterServices []string
FilterVersions []string
NoDeployments int
NoCountries int
MapData string
From string
To string
}{
Countries: string(countries),
NoDeployments: summary.TotalDeployments,
NoCountries: len(summary.Countries),
MapData: string(pg),
From: from,
To: to,
Countries: string(countries),
FilterCountries: unfilteredSummary.Countries,
FilterCities: unfilteredSummary.Cities,
FilterServices: unfilteredSummary.Services,
FilterVersions: unfilteredSummary.Versions,
NoDeployments: summary.TotalDeployments,
NoCountries: len(summary.Countries),
MapData: string(pg),
From: from,
To: to,
}

var res bytes.Buffer
Expand Down
7 changes: 5 additions & 2 deletions telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type CountrySummary struct {

type TelemetrySummary struct {
Countries []CountrySummary `json:"countries,omitempty"`
Cities []string `json:"cities,omitempty"`
Services []string `json:"services,omitempty"`
Versions []string `json:"versions,omitempty"`
TotalDeployments int `json:"total_deployments,omitempty"`
}

Expand All @@ -58,6 +61,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)
// RetrieveSummary gets distinct countries, cities,services and versions in a summarised form.
RetrieveSummary(ctx context.Context, filters TelemetryFilters) (TelemetrySummary, error)
}
3 changes: 1 addition & 2 deletions timescale/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ 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) {
func (*mockRepo) RetrieveSummary(ctx context.Context, filter callhome.TelemetryFilters) (callhome.TelemetrySummary, error) {
return callhome.TelemetrySummary{}, nil
}

Expand Down
48 changes: 45 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) {
// RetrieveSummary retrieve distinct
Musilah marked this conversation as resolved.
Show resolved Hide resolved
func (r repo) RetrieveSummary(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,48 @@ func (r repo) RetrieveDistinctIPsCountries(ctx context.Context, filters callhome
for _, country := range summary.Countries {
summary.TotalDeployments += country.NoDeployments
}

q1 := fmt.Sprintf(`select distinct city from telemetry %s;`, filterQuery)
cityRows, err := r.db.NamedQuery(q1, params)
if err != nil {
return callhome.TelemetrySummary{}, err
}
defer cityRows.Close()
for cityRows.Next() {
var val string
if err := cityRows.Scan(&val); err != nil {
return callhome.TelemetrySummary{}, err
}
summary.Cities = append(summary.Cities, val)
}

q2 := fmt.Sprintf(`select distinct service from telemetry %s;`, filterQuery)
serviceRows, err := r.db.NamedQuery(q2, params)
if err != nil {
return callhome.TelemetrySummary{}, err
}
defer serviceRows.Close()
for serviceRows.Next() {
var val string
if err := serviceRows.Scan(&val); err != nil {
return callhome.TelemetrySummary{}, err
}
summary.Services = append(summary.Services, val)
}

q3 := fmt.Sprintf(`select distinct mf_version from telemetry %s;`, filterQuery)
versionRows, err := r.db.NamedQuery(q3, params)
if err != nil {
return callhome.TelemetrySummary{}, err
}
defer versionRows.Close()
for versionRows.Next() {
var val string
if err := versionRows.Scan(&val); err != nil {
return callhome.TelemetrySummary{}, err
}
summary.Versions = append(summary.Versions, val)
}
return summary, nil
}

Expand Down
14 changes: 7 additions & 7 deletions timescale/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

const (
retrieveAllOp = "retrieve_all_op"
retrieveDistinctIPsCountriesOp = "retrieve_distinct_IP_countries_op"
saveOp = "save_op"
retrieveAllOp = "retrieve_all_op"
retrieveSummaryOp = "retrieve_summary_op"
saveOp = "save_op"
)

var _ callhome.TelemetryRepo = (*repoTracer)(nil)
Expand All @@ -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) {
ctx, span := rt.tracer.Start(ctx, retrieveDistinctIPsCountriesOp)
// RetrieveSummary adds tracing middleware to retrieve summary method.
func (rt *repoTracer) RetrieveSummary(ctx context.Context, filter callhome.TelemetryFilters) (callhome.TelemetrySummary, error) {
ctx, span := rt.tracer.Start(ctx, retrieveSummaryOp)
defer span.End()
return rt.repo.RetrieveDistinctIPsCountries(ctx, filter)
return rt.repo.RetrieveSummary(ctx, filter)
}

// Save adds tracing middleware to save method.
Expand Down
38 changes: 24 additions & 14 deletions web/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,41 @@ body {
}
.main-content {
flex-grow: 1;
height: 95%;
width: 100%;

}
#map {
height: 100%;
width: 100%;
}
#filter-container {
.btn {
margin-left: 20px;
margin-right: 20px;
padding: 10px 20px;
text-align: center;
line-height: 10px;
margin-top: 10px;
}
.list-group-item{
display: flex;
justify-content: center;
margin-bottom: 10px;
margin-top: 10px;
height: 5%;
justify-content: space-between;
}
.badge {
max-height: 20px;
}
#map {
height: 100%;
width: 100%;
}

#filter-form {
display: flex;
display: fixed;
gap: 20px;
}

#myButton {
position: absolute;
top: 20px;
right: 20px;
z-index: 1000;
}
#from-date,
#to-date {
width: 200px;
width: 150px;
height: 30px;
border: 1px solid #ccc;
border-radius: 4px;
Expand Down
Loading