-
Notifications
You must be signed in to change notification settings - Fork 5
/
storage_timescaledb_utils.go
120 lines (104 loc) · 4.22 KB
/
storage_timescaledb_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"time"
"go.uber.org/zap"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type TimescaleDBClient struct {
config *Config
logger *zap.SugaredLogger
db *gorm.DB
}
type FetchedBucketReading struct {
Bucket *time.Time `gorm:"column:bucket"`
StationName string `gorm:"column:stationname"`
Barometer float32 `gorm:"column:barometer"`
MaxBarometer float32 `gorm:"column:max_barometer"`
MinBarometer float32 `gorm:"column:min_barometer"`
InTemp float32 `gorm:"column:intemp"`
MaxInTemp float32 `gorm:"column:max_intemp"`
MinInTemp float32 `gorm:"column:max_intemp"`
ExtraTemp1 float32 `gorm:"column:extratemp1"`
MaxExtraTemp1 float32 `gorm:"column:min_extratemp1"`
MinExtraTemp1 float32 `gorm:"column:max_extratemp1"`
InHumidity float32 `gorm:"column:inhumidity"`
MaxInHumidity float32 `gorm:"column:max_inhumidity"`
MinInHumidity float32 `gorm:"column:min_inhumidity"`
SolarWatts float32 `gorm:"column:solarwatts"`
SolarJoules float32 `gorm:"column:solarjoules"`
OutTemp float32 `gorm:"column:outtemp"`
MaxOutTemp float32 `gorm:"column:max_outtemp"`
MinOutTemp float32 `gorm:"column:min_outtemp"`
OutHumidity float32 `gorm:"column:outhumidity"`
MinOutHumidity float32 `gorm:"column:min_outhumidity"`
MaxOutHumidity float32 `gorm:"column:max_outhumidity"`
WindSpeed float32 `gorm:"column:windspeed"`
MaxWindSpeed float32 `gorm:"column:max_windspeed"`
WindDir float32 `gorm:"column:winddir"`
WindChill float32 `gorm:"column:windchill"`
MinWindChill float32 `gorm:"column:min_windchill"`
HeatIndex float32 `gorm:"column:heatindex"`
MaxHeatIndex float32 `gorm:"column:max_heatindex"`
PeriodRain float32 `gorm:"column:period_rain"`
RainRate float32 `gorm:"column:rainrate"`
MaxRainRate float32 `gorm:"column:max_rainrate"`
DayRain float32 `gorm:"column:dayrain"`
MonthRain float32 `gorm:"column:monthrain"`
YearRain float32 `gorm:"column:yearrain"`
ConsBatteryVoltage float32 `gorm:"column:consbatteryvoltage"`
StationBatteryVoltage float32 `gorm:"column:stationbatteryvoltage"`
}
func NewTimescaleDBClient(c *Config, logger *zap.SugaredLogger) *TimescaleDBClient {
return &TimescaleDBClient{
config: c,
logger: logger,
}
}
// We implement the Tabler interface for the Reading struct
func (FetchedBucketReading) TableName() string {
return "weather_1m"
}
func (t *TimescaleDBClient) connectToTimescaleDB(s StorageConfig) error {
var err error
// Create a logger for gorm
dbLogger := logger.New(
zap.NewStdLog(zapLogger),
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logger.Warn, // Log level
IgnoreRecordNotFoundError: false, // Ignore ErrRecordNotFound error for logger
Colorful: true, // Use colors
},
)
config := &gorm.Config{
Logger: dbLogger,
}
log.Info("connecting to TimescaleDB...")
t.db, err = gorm.Open(postgres.Open(t.config.Storage.TimescaleDB.ConnectionString), config)
if err != nil {
log.Warn("warning: unable to create a TimescaleDB connection:", err)
return err
}
log.Info("TimescaleDB connection successful")
return nil
}
func (p *TimescaleDBClient) getReadingsFromTimescaleDB(pullFromDevice string) (FetchedBucketReading, error) {
var br FetchedBucketReading
if err := p.db.Table("weather_1m").Where("stationname=? AND bucket > NOW() - INTERVAL '2 minutes'", pullFromDevice).Limit(1).Find(&br).Error; err != nil {
return FetchedBucketReading{}, fmt.Errorf("error querying database for latest readings: %+v", err)
}
return br, nil
}
func (t *TimescaleDBClient) validatePullFromStation(pullFromDevice string) bool {
if len(t.config.Devices) > 0 {
for _, station := range t.config.Devices {
if station.Name == pullFromDevice {
return true
}
}
}
return false
}