Changed endpoints and db queries, chart now shows suggested mins and maxes
This commit is contained in:
24
Database.go
24
Database.go
@@ -26,17 +26,23 @@ func CloseDb() {
|
||||
}
|
||||
}
|
||||
|
||||
func writeSnapshotToDb(snapshotSub *SnapshotSubmission) error {
|
||||
query := fmt.Sprintf("INSERT INTO `snapshots` (`temp`, `humidity`, `co2`, `time`, `id`) VALUES (%v, %v, %v, '%v', NULL);",
|
||||
func writeSnapshotToDb(snapshotSub *SnapshotSubmission) (int64, error) {
|
||||
queryStr := "INSERT INTO `snapshots` (`temp`, `humidity`, `co2`, `time`, `id`) VALUES (%v, %v, %v, '%v', NULL);"
|
||||
query, err := ClimateDb.Prepare(queryStr)
|
||||
if err != nil {
|
||||
return -1, fmt.Errorf("couldn't prepare snapshot query: %w", err)
|
||||
}
|
||||
result, err := query.Exec(
|
||||
snapshotSub.Temp,
|
||||
snapshotSub.Humidity,
|
||||
snapshotSub.Co2,
|
||||
snapshotSub.Timestamp)
|
||||
_, err := ClimateDb.Query(query)
|
||||
snapshotSub.Timestamp,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
return -1, fmt.Errorf("couldn't execute query to write snapshot to db: %w", err)
|
||||
}
|
||||
return nil
|
||||
id, _ := result.LastInsertId()
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func getLastSnapshotRecordFromDb() (*SnapshotRecord, error) {
|
||||
@@ -55,7 +61,7 @@ func getLastSnapshotRecordFromDb() (*SnapshotRecord, error) {
|
||||
&snapshotRecord.Timestamp,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("couldn't read last snapshot from the database: %w", err)
|
||||
}
|
||||
return &snapshotRecord, nil
|
||||
}
|
||||
@@ -67,7 +73,7 @@ func getSnapshotRecordsFromDb(minuteAgo int) ([]*SnapshotRecord, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := 0; rows.Next(); i++ {
|
||||
for rows.Next() {
|
||||
var snapshotRecord SnapshotRecord
|
||||
err = rows.Scan(
|
||||
&snapshotRecord.Id,
|
||||
@@ -77,7 +83,7 @@ func getSnapshotRecordsFromDb(minuteAgo int) ([]*SnapshotRecord, error) {
|
||||
&snapshotRecord.Timestamp,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("couldn't read rows from the database: %w", err)
|
||||
}
|
||||
snapshots = append(snapshots, &snapshotRecord)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user