Add derived device snapshots for summary dashboard

This commit is contained in:
2026-03-15 21:00:39 +01:00
parent 7a8a1cc855
commit b9789430a1
3 changed files with 188 additions and 22 deletions
+41 -2
View File
@@ -55,6 +55,17 @@ func (writer *fakeWriter) allBatches() [][]model.Record {
return copyBatches
}
func findRecord(records []model.Record, measurement string, device string) *model.Record {
for index := range records {
record := &records[index]
if record.Measurement == measurement && record.Tags["device"] == device {
return record
}
}
return nil
}
func TestServiceFlushesParsedRecords(t *testing.T) {
fake := newFakeWriter()
service := NewService(config.Config{
@@ -118,8 +129,8 @@ func TestServiceFlushesParsedRecords(t *testing.T) {
}
batch := batches[1]
if len(batch) != 2 {
t.Fatalf("expected 2 records in flushed batch, got %d", len(batch))
if len(batch) != 4 {
t.Fatalf("expected 4 records in flushed batch, got %d", len(batch))
}
if batch[0].Measurement != "tasmota_lwt" {
@@ -142,6 +153,34 @@ func TestServiceFlushesParsedRecords(t *testing.T) {
t.Fatalf("unexpected sensor device_alias field: %#v", batch[1].Fields["device_alias"])
}
garageSnapshot := findRecord(batch, "tasmota_device_snapshot", "tasmota_896001")
if garageSnapshot == nil {
t.Fatal("expected garage snapshot record in flushed batch")
}
if garageSnapshot.Fields["device_alias"] != "Garage Plug" {
t.Fatalf("unexpected garage snapshot alias: %#v", garageSnapshot.Fields["device_alias"])
}
if _, ok := garageSnapshot.Fields["sensor_last_seen_unix"]; ok {
t.Fatalf("did not expect sensor timestamp on lwt-only snapshot: %#v", garageSnapshot.Fields["sensor_last_seen_unix"])
}
officeSnapshot := findRecord(batch, "tasmota_device_snapshot", "tasmota_c88994")
if officeSnapshot == nil {
t.Fatal("expected office snapshot record in flushed batch")
}
if officeSnapshot.Fields["device_alias"] != "Office Plug" {
t.Fatalf("unexpected office snapshot alias: %#v", officeSnapshot.Fields["device_alias"])
}
if officeSnapshot.Fields["energy_total"] != float64(41.385) {
t.Fatalf("unexpected office snapshot energy_total: %#v", officeSnapshot.Fields["energy_total"])
}
if officeSnapshot.Fields["energy_power"] != float64(1) {
t.Fatalf("unexpected office snapshot energy_power: %#v", officeSnapshot.Fields["energy_power"])
}
if officeSnapshot.Fields["sensor_last_seen_unix"] != time.Date(2026, time.March, 12, 16, 23, 13, 0, time.UTC).Unix() {
t.Fatalf("unexpected office snapshot sensor timestamp: %#v", officeSnapshot.Fields["sensor_last_seen_unix"])
}
cancel()
select {
case err := <-errCh: