Add daily filter to events count checker.
Some checks failed
Build Docker image / build (push) Failing after 51s
Build Golang packages / release (push) Has been skipped

This commit is contained in:
Pieter Hollander 2024-02-23 12:12:45 +01:00
parent 054a933def
commit d4028d37ec
Signed by: pieter
SSH key fingerprint: SHA256:HbX+9cBXsop9SuvL+mELd29sK+7DehFfdVweFVDtMSg

13
main.go
View file

@ -3,7 +3,7 @@
// TODO: Record Inverter struct data only on-change. // TODO: Record Inverter struct data only on-change.
// Idea: Make a full admin / config GUI and only configure through this utility. // Idea: Make a full admin / config GUI and only configure through this utility.
// Idea: Gather settings only on start-up. // 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: Only update meter readings such as yieldday, yieldtotal on-change.
// TODO: DB migrations. // TODO: DB migrations.
package main package main
@ -444,11 +444,16 @@ func queryEventsEndpoint(inverterSerial string) (*EventsResponse, error) {
return &eventsResponse, nil return &eventsResponse, nil
} }
// TODO: This function should probably be altered to only select from current day. // The events counter reported by OpenDTU resets every day.
// CHECK.
func getPreviousEventsCount(db *sql.DB, inverterSerial string) int { func getPreviousEventsCount(db *sql.DB, inverterSerial string) int {
var count 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 { if err != nil && err != sql.ErrNoRows {
logger.Error("Error querying previous events count", "error", err) logger.Error("Error querying previous events count", "error", err)
} }