From fc3c6f33fff3fb01d4f1ba99616710fdec39ea09 Mon Sep 17 00:00:00 2001 From: Tomi Hakala Date: Sat, 22 Jun 2024 16:38:05 +0300 Subject: [PATCH] feat: add hourly results for daily summary table by clicking hour column you now see all results of that specific hour --- internal/datastore/interfaces.go | 17 ++++- internal/httpcontroller/handlers.go | 32 ++++++++++ internal/httpcontroller/routes.go | 2 + views/fragments/birdsTableHTML.html | 10 ++- views/fragments/hourlyDetections.html | 90 +++++++++++++++++++++++++++ 5 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 views/fragments/hourlyDetections.html diff --git a/internal/datastore/interfaces.go b/internal/datastore/interfaces.go index 281aad3d..f38a9f57 100644 --- a/internal/datastore/interfaces.go +++ b/internal/datastore/interfaces.go @@ -1,4 +1,4 @@ -// interfaces.go this code defines the interface for the database operations +// interfaces.go: this code defines the interface for the database operations package datastore import ( @@ -36,6 +36,7 @@ type Interface interface { SaveHourlyWeather(hourlyWeather *HourlyWeather) error GetHourlyWeather(date string) ([]HourlyWeather, error) LatestHourlyWeather() (*HourlyWeather, error) + GetHourlyDetections(date, hour string) ([]Note, error) } // DataStore implements StoreInterface using a GORM database. @@ -442,3 +443,17 @@ func createGormLogger() logger.Interface { }, ) } + +// GetHourlyDetections retrieves bird detections for a specific date and hour. +func (ds *DataStore) GetHourlyDetections(date, hour string) ([]Note, error) { + var detections []Note + + startTime := hour + ":00:00" + endTime := hour + ":59:59" + + err := ds.DB.Where("date = ? AND time >= ? AND time <= ?", date, startTime, endTime). + Order("time ASC"). + Find(&detections).Error + + return detections, err +} diff --git a/internal/httpcontroller/handlers.go b/internal/httpcontroller/handlers.go index 939c9372..740c7502 100644 --- a/internal/httpcontroller/handlers.go +++ b/internal/httpcontroller/handlers.go @@ -399,3 +399,35 @@ func (s *Server) serveSpectrogramHandler(c echo.Context) error { // Serve the spectrogram image file return c.File(spectrogramPath) } + +// hourlyDetectionsHandler handles requests for hourly detections +func (s *Server) hourlyDetectionsHandler(c echo.Context) error { + date := c.QueryParam("date") + hour := c.QueryParam("hour") + + if date == "" || hour == "" { + return echo.NewHTTPError(http.StatusBadRequest, "Date and hour are required.") + } + + // Fetch all detections for the specified date and hour + detections, err := s.ds.GetHourlyDetections(date, hour) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) + } + + // Prepare data for rendering in the template + data := struct { + Date string + Hour string + Detections []datastore.Note + DashboardSettings *conf.Dashboard + }{ + Date: date, + Hour: hour, + Detections: detections, + DashboardSettings: s.DashboardSettings, + } + + // Render the hourlyDetections template with the data + return c.Render(http.StatusOK, "hourlyDetections", data) +} diff --git a/internal/httpcontroller/routes.go b/internal/httpcontroller/routes.go index a1bd2012..3a9c7cc9 100644 --- a/internal/httpcontroller/routes.go +++ b/internal/httpcontroller/routes.go @@ -28,6 +28,7 @@ var routes = []routeConfig{ {Path: "/logs", TemplateName: "logs", Title: "Logs"}, {Path: "/stats", TemplateName: "stats", Title: "Statistics"}, {Path: "/settings", TemplateName: "settings", Title: "General Settings"}, + {Path: "/hourly-detections", TemplateName: "hourlyDetections", Title: "Hourly Detections"}, } // initRoutes initializes the routes for the server. @@ -76,6 +77,7 @@ func (s *Server) initRoutes() { s.Echo.GET("/species-detections", s.speciesDetectionsHandler) s.Echo.GET("/search", s.searchHandler) s.Echo.GET("/spectrogram", s.serveSpectrogramHandler) + s.Echo.GET("/hourly-detections", s.hourlyDetectionsHandler) // Handle both GET and DELETE requests for the /note route s.Echo.Add("GET", "/note", s.getNoteHandler) diff --git a/views/fragments/birdsTableHTML.html b/views/fragments/birdsTableHTML.html index c3b4a799..a6734f44 100644 --- a/views/fragments/birdsTableHTML.html +++ b/views/fragments/birdsTableHTML.html @@ -10,7 +10,15 @@ {{end}} Detections {{range .Hours}} - {{printf "%02d" .}} + + + {{printf "%02d" .}} + + {{end}} diff --git a/views/fragments/hourlyDetections.html b/views/fragments/hourlyDetections.html new file mode 100644 index 00000000..1b7e9e41 --- /dev/null +++ b/views/fragments/hourlyDetections.html @@ -0,0 +1,90 @@ +{{define "hourlyDetections"}} +
+ + + + + + + + + + + + + + + + {{if .DashboardSettings.Thumbnails.Summary}} + + {{end}} + + + + + + + + + + {{range .Detections}} + + + + + + + + + + + + {{if $.DashboardSettings.Thumbnails.Summary}} + + {{end}} + + + + + + + + {{end}} + +
DateTimeCommon NameThumbnailConfidenceRecording
{{.Date}}{{.Time}} + + {{.CommonName}} + + +
+ + +
+
+ + +
+ + + Spectrogram Image + + + + +
+
+
+{{end}} \ No newline at end of file