Skip to content

Commit

Permalink
Fix on comments
Browse files Browse the repository at this point in the history
Signed-off-by: Musilah <[email protected]>
  • Loading branch information
Musilah committed Aug 3, 2023
1 parent 0d1a59a commit e4b5dfb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 38 deletions.
16 changes: 15 additions & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"context"
"encoding/json"

//"math/rand"
"text/template"
"time"
)
Expand Down Expand Up @@ -68,6 +70,14 @@ func (ts *telemetryService) ServeUI(ctx context.Context, filters TelemetryFilter
if err != nil {
return nil, err
}
allCountries, err := ts.repo.RetrieveDistinctIPsCountries(ctx, TelemetryFilters{})
if err != nil {
return nil, err
}
allCities, err := ts.repo.RetrieveDistinctIPsCountries(ctx, filters)
if err != nil {
return nil, err
}
telPage, err := ts.repo.RetrieveAll(ctx, PageMetadata{Limit: pageLimit}, filters)
if err != nil {
return nil, err
Expand All @@ -82,7 +92,9 @@ func (ts *telemetryService) ServeUI(ctx context.Context, filters TelemetryFilter
return nil, err
}

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

var from, to string
if !filters.From.IsZero() {
from = filters.From.Format(time.DateOnly)
Expand All @@ -93,6 +105,7 @@ func (ts *telemetryService) ServeUI(ctx context.Context, filters TelemetryFilter
data := struct {
Countries string
FilterCountries []CountrySummary
FilterCities []CitySummary
NoDeployments int
NoCountries int
MapData string
Expand All @@ -101,6 +114,7 @@ func (ts *telemetryService) ServeUI(ctx context.Context, filters TelemetryFilter
}{
Countries: string(countries),
FilterCountries: filterCountries,
FilterCities: filterCities,
NoDeployments: summary.TotalDeployments,
NoCountries: len(summary.Countries),
MapData: string(pg),
Expand Down
11 changes: 9 additions & 2 deletions telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ type CountrySummary struct {
NoDeployments int `json:"number_of_deployments" db:"count"`
}

type CitySummary struct {
City string `json:"city" db:"city"`
NoDeployments int `json:"number_of_deployments" db:"count"`
}

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

// TelemetryRepository specifies an account persistence API.
Expand Down
6 changes: 5 additions & 1 deletion web/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ body {
.btn {
margin-left: 20px;
margin-right: 20px;
padding: 5px 20px;
text-align: center;
line-height: 10px;

}

#map {
Expand All @@ -48,7 +52,7 @@ body {

#from-date,
#to-date {
width: 200px;
width: 150px;
height: 30px;
border: 1px solid #ccc;
border-radius: 4px;
Expand Down
61 changes: 27 additions & 34 deletions web/template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.5.1/leaflet.markercluster.js"></script>
<script src="https://d3js.org/d3.v3.min.js" type="text/javascript"></script>

<title>Mainflux Deployments</title>
</head>
<body>
Expand All @@ -26,16 +25,9 @@ <h2>Mainflux Deployment Summary</h2>
</div>
<div class="container">
<div id="filter-container">
<form id="filter-form" onsubmit="applyFilter(event)">
<label for="from-date">From:</label>
<input type="date" id="from-date" name="from-date" value="{{.From}}">
<label for="to-date">To:</label>
<input type="date" id="to-date" name="to-date" value="{{.To}}">
<input class="btn btn-primary" type="submit" value="Apply">
</form>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" id="btn" data-bs-toggle="modal" data-bs-target="#applyFilters">
More Filters
Filters
</button>
<!-- Modal -->
<div class="modal fade" id="applyFilters" tabindex="-1" aria-labelledby="applyFiltersLabel" aria-hidden="true">
Expand All @@ -45,27 +37,32 @@ <h2>Mainflux Deployment Summary</h2>
<h1 class="modal-title fs-5" id="applyFiltersLabel">Apply Filters</h1>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="country-filter" class="form-label">Country</label>
<div class="mb-3">
<form id="country-filter-form" onsubmit="applyFilter(event)">
<select id="country-filter" class="form-select">
<option value="">Select a country</option>
{{range $i, $country := .FilterCountries}}
<option value="{{$country.Country}}">{{$country.Country}}</option>
{{end}}
</select>
<button type="submit" class="btn btn-primary">Apply</button>
</form>
</div>
<div class="mb-3">
<label for="city-filter" class="form-label">City</label>
<form id="city-filter-form" onsubmit="applyFilter(event)">
<select id="city-filter" class="form-select">
<option value="">Select a city</option>

</select>
<button type="submit" class="btn btn-primary">Apply</button>
<div>
<label for="from-date">From:</label>
<input type="date" id="from-date" name="from-date" value="{{.From}}">
<label for="to-date">To:</label>
<input type="date" id="to-date" name="to-date" value="{{.To}}">
</div>
<label for="country-filter" class="form-label">Country</label>
<select id="country-filter" class="form-select">
<option value="">Select a country</option>
{{range $i, $country := .FilterCountries}}
<option value="{{$country.Country}}">{{$country.Country}}</option>
{{end}}
</select>
<label for="city-filter" class="form-label">City</label>
<select id="city-filter" class="form-select">
<option value="">Select a city</option>
{{range $i, $city := .FilterCities}}
<option value="{{$city.City}}">{{$city.City}}</option>
{{end}}
</select>
<button type="submit" class="btn btn-primary">Apply</button>

</form>

</div>
</div>
</div>
Expand All @@ -78,7 +75,6 @@ <h1 class="modal-title fs-5" id="applyFiltersLabel">Apply Filters</h1>
<div id="map"></div>
</div>
</div>

<script type="text/javascript">
//create map object and set default positions and zoom level
var map = L.map('map').setView([20, 0], 2);
Expand All @@ -90,6 +86,7 @@ <h1 class="modal-title fs-5" id="applyFiltersLabel">Apply Filters</h1>
var fromDateInput = document.getElementById('from-date').value;
var toDateInput = document.getElementById('to-date').value;
var selectedCountry = document.getElementById('country-filter').value;
var selectedCity = document.getElementById('city-filter').value
var fromDate = fromDateInput ? new Date(fromDateInput) : null;
var toDate = toDateInput ? new Date(toDateInput) : null;
if (fromDate && toDate && fromDate > toDate) {
Expand All @@ -105,7 +102,7 @@ <h1 class="modal-title fs-5" id="applyFiltersLabel">Apply Filters</h1>
var fromDate = fromDateInput ? new Date(fromDateInput).toISOString() : '';
var toDate = toDateInput ? new Date(toDateInput).toISOString() : '';

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

Expand Down Expand Up @@ -179,12 +176,8 @@ <h1 class="modal-title fs-5" id="applyFiltersLabel">Apply Filters</h1>
map.addLayer(countryMarkers);
});
}

logJSONData();

</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>

</body>

</html>

0 comments on commit e4b5dfb

Please sign in to comment.