74 lines
2.2 KiB
Go
74 lines
2.2 KiB
Go
package application
|
|
|
|
import (
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func (h *Handler) DashboardHTTPHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
// Parse driver address geography filter
|
|
driverAddressGeo := r.URL.Query().Get("driver_address_geo")
|
|
driverAddressGeoLayer, driverAddressGeoCode := "", ""
|
|
if driverAddressGeo != "" {
|
|
parts := strings.SplitN(driverAddressGeo, ":", 2)
|
|
if len(parts) == 2 {
|
|
driverAddressGeoLayer, driverAddressGeoCode = parts[0], parts[1]
|
|
}
|
|
}
|
|
|
|
result, err := h.applicationHandler.GetDashboardData(r.Context(), driverAddressGeoLayer, driverAddressGeoCode)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error retrieving dashboard data")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Enrich geography filters with names from geography service
|
|
var enrichedGeoFilters []map[string]string
|
|
if h.cfg.GetBool("geography.filters.enabled") {
|
|
geoFilters := h.cfg.Get("geography.filters.geographies")
|
|
if geoList, ok := geoFilters.([]any); ok {
|
|
for _, geoItem := range geoList {
|
|
if geoMap, ok := geoItem.(map[string]any); ok {
|
|
layer := ""
|
|
code := ""
|
|
if l, ok := geoMap["layer"].(string); ok {
|
|
layer = l
|
|
}
|
|
if c, ok := geoMap["code"].(string); ok {
|
|
code = c
|
|
}
|
|
|
|
enrichedGeo := map[string]string{
|
|
"layer": layer,
|
|
"code": code,
|
|
"name": code, // Default to code if name fetch fails
|
|
}
|
|
|
|
// Fetch name from geography service
|
|
if layer != "" && code != "" {
|
|
if geoFeature, err := h.services.Geography.Find(layer, code); err == nil {
|
|
if name := geoFeature.Properties.MustString("nom"); name != "" {
|
|
enrichedGeo["name"] = name
|
|
}
|
|
}
|
|
}
|
|
|
|
enrichedGeoFilters = append(enrichedGeoFilters, enrichedGeo)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Sort by name
|
|
sort.Slice(enrichedGeoFilters, func(i, j int) bool {
|
|
return enrichedGeoFilters[i]["name"] < enrichedGeoFilters[j]["name"]
|
|
})
|
|
}
|
|
|
|
h.renderer.Dashboard(w, r, result.Accounts, len(result.Accounts), len(result.Members), result.Events, result.Bookings, result.SolidarityDrivers, result.OrganizedCarpoolDrivers, driverAddressGeo, enrichedGeoFilters)
|
|
}
|
|
} |