From d4028d37ec4b14be714a441c4ed499f23507b192 Mon Sep 17 00:00:00 2001 From: Pieter Hollander Date: Fri, 23 Feb 2024 12:12:45 +0100 Subject: [PATCH] Add daily filter to events count checker. --- main.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 170a479..5a0b625 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ // TODO: Record Inverter struct data only on-change. // Idea: Make a full admin / config GUI and only configure through this utility. // Idea: Gather settings only on start-up. -// TODO: handleMessage: Support older data age than 0, due to new OpenDTU WebSocket implementation. +// TODO: handleMessage: Support older data age than 0, due to new OpenDTU WebSocket implementation? // TODO: Only update meter readings such as yieldday, yieldtotal on-change. // TODO: DB migrations. package main @@ -444,11 +444,16 @@ func queryEventsEndpoint(inverterSerial string) (*EventsResponse, error) { return &eventsResponse, nil } -// TODO: This function should probably be altered to only select from current day. -// CHECK. +// The events counter reported by OpenDTU resets every day. func getPreviousEventsCount(db *sql.DB, inverterSerial string) int { var count int - err := db.QueryRow("SELECT COUNT(*) FROM opendtu_events WHERE inverter_serial = $1", inverterSerial).Scan(&count) + err := db.QueryRow(` + SELECT COUNT(*) + FROM opendtu_events + WHERE inverter_serial = $1 + AND timestamp >= CURRENT_DATE + AND timestamp < CURRENT_DATE + INTERVAL '1 day' + `, inverterSerial).Scan(&count) if err != nil && err != sql.ErrNoRows { logger.Error("Error querying previous events count", "error", err) }