Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Fix PostgreSQL integer logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
SMerrony committed May 8, 2022
1 parent e930fb6 commit c82db0c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
CHANGES file for AGHAST Server
==============================

AGHAST v0.5.1 (release 2022-05-08) - Fix PostgreSQL Logging Issue
- Bug Fix: Postgres integration was failing when expecting Integers and Floats arrived.

AGHAST v0.5.0 (released 2021-08-21) - Simpler Conditions
- New Feature: Conditions may now use a value from the triggering message

Expand Down
2 changes: 1 addition & 1 deletion cmd/aghastServer/aghastServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/SMerrony/aghast/server"
)

const SemVer = "v0.5.0" // TODO Update SemVer on each release
const SemVer = "v0.5.1" // TODO Update SemVer on each release

var (
configFlag = flag.String("configdir", "", "directory containing configuration files")
Expand Down
19 changes: 13 additions & 6 deletions integrations/postgres/postgres.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright ©2021 Steve Merrony
// Copyright ©2021,2022 Steve Merrony

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,8 +22,10 @@ package postgres
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"math"
"strconv"
"sync"

Expand Down Expand Up @@ -154,12 +156,12 @@ func (p *Postgres) logger(l loggerT) {
jsonMap := make(map[string]interface{})
err := json.Unmarshal([]byte(msg.Payload.([]uint8)), &jsonMap)
if err != nil {
log.Printf("ERROR: DataLogger - Could not understand JSON %s\n", msg.Payload.(string))
log.Printf("ERROR: Postgres Logger - Could not understand JSON %s\n", msg.Payload.(string))
return
}
v, found := jsonMap[l.Key]
if !found {
log.Printf("ERROR: DataLogger - Could find Key in JSON %s\n", msg.Payload.(string))
log.Printf("ERROR: Postgres Logger - Could find Key in JSON %s\n", msg.Payload.(string))
return
}
value = v
Expand All @@ -175,25 +177,30 @@ func (p *Postgres) logger(l loggerT) {
case string:
fl, err = strconv.ParseFloat(value.(string), 64)
if err != nil {
log.Printf("WARNING: Postgres logger could not parse float from %v\n", value.(string))
log.Printf("WARNING: Postgres logger could not parse float from %v\n", value)
continue
}
}
sql = fmt.Sprintf("INSERT INTO logged_floats(id, ts, float_val) VALUES( %s, NOW(), %f)", idString, fl)
case "integer":
var num int
switch value.(type) {
switch t := value.(type) {
case int:
num = value.(int)
case int32:
num = int(value.(int32))
case int64:
num = int(value.(int64))
case float64:
num = int(math.Round(value.(float64)))
case string:
num, err = strconv.Atoi(value.(string))
default:
log.Printf("WARNING: Postgres Logger expected integer type, got: %T\n", t)
err = errors.New("Type error")
}
if err != nil {
log.Printf("WARNING: Postgres logger could not parse integer from %v\n", value.(string))
log.Printf("WARNING: Postgres logger could not parse integer from %v\n", value)
continue
}
sql = fmt.Sprintf("INSERT INTO logged_integers(id, ts, int_val) VALUES( %s, NOW(), %d)", idString, num)
Expand Down

0 comments on commit c82db0c

Please sign in to comment.