big update
This commit is contained in:
88
database.go
88
database.go
@@ -49,8 +49,8 @@ func writeSnapshotToDb(snapshotSub *SnapshotSubmission) (int64, error) {
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func getLastSnapshotRecordFromDb() (*SnapshotRecord, error) {
|
||||
var snapshotRecord SnapshotRecord
|
||||
func fetchLastSnapshotWithDatetime() (*DatetimeSnapshotRecord, error) {
|
||||
var snapshotRecord DatetimeSnapshotRecord
|
||||
query := "SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `snapshots` ORDER BY `id` DESC LIMIT 1;"
|
||||
rows, err := ClimateDb.Query(fmt.Sprintf(query, "id", "temp", "humidity", "co2", "time"))
|
||||
if err != nil {
|
||||
@@ -70,15 +70,15 @@ func getLastSnapshotRecordFromDb() (*SnapshotRecord, error) {
|
||||
return &snapshotRecord, nil
|
||||
}
|
||||
|
||||
func getSnapshotRecordsFromDb(dateSince string) ([]*SnapshotRecord, error) {
|
||||
snapshots := make([]*SnapshotRecord, 0)
|
||||
query := "SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `snapshots` WHERE TIMESTAMPDIFF(SECOND, `%s`, '%s') < 0 ORDER BY `id` DESC;"
|
||||
func fetchUnixTimeSnapshotsSince(dateSince int) ([]*DatetimeSnapshotRecord, error) {
|
||||
snapshots := make([]*DatetimeSnapshotRecord, 0)
|
||||
query := "SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `snapshots` WHERE UNIX(SECOND, `%s`, '%s') < 0 ORDER BY `id` DESC;"
|
||||
rows, err := ClimateDb.Query(fmt.Sprintf(query, "id", "temp", "humidity", "co2", "time", "time", dateSince))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't execute select query: %w", err)
|
||||
}
|
||||
for rows.Next() {
|
||||
var snapshotRecord SnapshotRecord
|
||||
var snapshotRecord DatetimeSnapshotRecord
|
||||
err = rows.Scan(
|
||||
&snapshotRecord.Id,
|
||||
&snapshotRecord.Temp,
|
||||
@@ -87,7 +87,81 @@ func getSnapshotRecordsFromDb(dateSince string) ([]*SnapshotRecord, error) {
|
||||
&snapshotRecord.Timestamp,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't read rows from the database: %w", err)
|
||||
return nil, fmt.Errorf("error reading rows since %v with unixtime from the database: %w", dateSince, err)
|
||||
}
|
||||
snapshots = append(snapshots, &snapshotRecord)
|
||||
}
|
||||
return snapshots, nil
|
||||
}
|
||||
|
||||
func fetchDatetimeSnapshotsSince(dateSince string) ([]*DatetimeSnapshotRecord, error) {
|
||||
snapshots := make([]*DatetimeSnapshotRecord, 0)
|
||||
query := "SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `snapshots` WHERE TIMESTAMPDIFF(SECOND, `%s`, '%s') < 0 ORDER BY `id` DESC;"
|
||||
rows, err := ClimateDb.Query(fmt.Sprintf(query, "id", "temp", "humidity", "co2", "time", "time", dateSince))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't execute select query: %w", err)
|
||||
}
|
||||
for rows.Next() {
|
||||
var snapshotRecord DatetimeSnapshotRecord
|
||||
err = rows.Scan(
|
||||
&snapshotRecord.Id,
|
||||
&snapshotRecord.Temp,
|
||||
&snapshotRecord.Humidity,
|
||||
&snapshotRecord.Co2,
|
||||
&snapshotRecord.Timestamp,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading rows since %s with datetime from the database: %w", dateSince, err)
|
||||
}
|
||||
snapshots = append(snapshots, &snapshotRecord)
|
||||
}
|
||||
return snapshots, nil
|
||||
}
|
||||
|
||||
func fetchUnixTimeSnapshotsBetween(startTime int, endTime int) ([]*DatetimeSnapshotRecord, error) {
|
||||
snapshots := make([]*DatetimeSnapshotRecord, 0)
|
||||
query := "SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `snapshots` WHERE TIMESTAMPDIFF(SECOND, `%s`, '%s') < 0 ORDER BY `id` DESC;"
|
||||
rows, err := ClimateDb.Query(fmt.Sprintf(query, "id", "temp", "humidity", "co2", "time", "time", startTime, endTime))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't execute select query: %w", err)
|
||||
}
|
||||
for rows.Next() {
|
||||
var snapshotRecord DatetimeSnapshotRecord
|
||||
err = rows.Scan(
|
||||
&snapshotRecord.Id,
|
||||
&snapshotRecord.Temp,
|
||||
&snapshotRecord.Humidity,
|
||||
&snapshotRecord.Co2,
|
||||
&snapshotRecord.Timestamp,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"couldn't read rows from the database b/w %v and %v with datetime: %w", startTime, endTime, err)
|
||||
}
|
||||
snapshots = append(snapshots, &snapshotRecord)
|
||||
}
|
||||
return snapshots, nil
|
||||
}
|
||||
|
||||
func fetchDatetimeSnapshotsBetween(startTime string, endTime string) ([]*DatetimeSnapshotRecord, error) {
|
||||
snapshots := make([]*DatetimeSnapshotRecord, 0)
|
||||
query := "SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `snapshots` WHERE TIMESTAMPDIFF(SECOND, `%s`, '%s') < 0 ORDER BY `id` DESC;"
|
||||
rows, err := ClimateDb.Query(fmt.Sprintf(query, "id", "temp", "humidity", "co2", "time", "time", startTime, endTime))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't execute select query: %w", err)
|
||||
}
|
||||
for rows.Next() {
|
||||
var snapshotRecord DatetimeSnapshotRecord
|
||||
err = rows.Scan(
|
||||
&snapshotRecord.Id,
|
||||
&snapshotRecord.Temp,
|
||||
&snapshotRecord.Humidity,
|
||||
&snapshotRecord.Co2,
|
||||
&snapshotRecord.Timestamp,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"couldn't read rows from the database b/w %v and %v with unix time: %w", startTime, endTime, err)
|
||||
}
|
||||
snapshots = append(snapshots, &snapshotRecord)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user