-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.go
136 lines (129 loc) · 3.01 KB
/
weather.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package database
import (
"time"
)
type WeatherMeasurement struct {
Id uint `json:"id"`
Time time.Time `json:"time"`
WeatherTitle string `json:"weatherTitle"`
WeatherDescription string `json:"weatherDescription"`
Temperature float32 `json:"temperature"`
FeelsLike float32 `json:"feelsLike"`
Humidity uint8 `json:"humidity"`
}
func createWeatherTable() error {
if _, err := db.Exec(`
CREATE TABLE
IF NOT EXISTS
weather(
Id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Time DATETIME DEFAULT CURRENT_TIMESTAMP,
WeatherTitle TEXT,
WeatherDescription TEXT,
Temperature FLOAT(24),
FeelsLike FLOAT(24),
Humidity INT UNSIGNED
)
`); err != nil {
log.Error("Failed to create weather data table: executing query failed: ", err.Error())
return err
}
return nil
}
func GetWeatherDataRecords(maxAgeMinutes uint) ([]WeatherMeasurement, error) {
query, err := db.Prepare(`
SELECT
Id,
Time,
WeatherTitle,
WeatherDescription,
Temperature,
FeelsLike,
Humidity
From weather
WHERE
Time > NOW() - INTERVAL ? MINUTE
`)
if err != nil {
log.Error("Failed to get weather data records: preparing query failed: ", err.Error())
return nil, err
}
defer query.Close()
res, err := query.Query(maxAgeMinutes)
if err != nil {
log.Error("Failed to get weather data records: executing failed: ", err.Error())
return nil, err
}
results := make([]WeatherMeasurement, 0)
for res.Next() {
var row WeatherMeasurement
// Scan the current row
if err := res.Scan(
&row.Id,
&row.Time,
&row.WeatherTitle,
&row.WeatherDescription,
&row.Temperature,
&row.FeelsLike,
&row.Humidity,
); err != nil {
log.Error("Failed to get weather data records: scanning query results failed: ", err.Error())
return nil, err
}
// Append the current row to the results
results = append(results, row)
}
return results, nil
}
func AddWeatherDataRecord(
weatherTitle string,
weatherDescription string,
temperature float32,
feelsLike float32,
humidity uint8,
) (uint, error) {
query, err := db.Prepare(`
INSERT INTO
weather(
Id,
Time,
WeatherTitle,
WeatherDescription,
Temperature,
FeelsLike,
Humidity
)
VALUES(DEFAULT, DEFAULT, ?, ?, ?, ?, ?)
`)
if err != nil {
log.Error("Failed to add weather measurement: preparing query failed: ", err.Error())
return 0, err
}
res, err := query.Exec(
weatherTitle,
weatherDescription,
temperature,
feelsLike,
humidity,
)
if err != nil {
log.Error("Failed to add weather measurement: executing query failed: ", err.Error())
return 0, err
}
newId, err := res.LastInsertId()
if err != nil {
log.Error("Failed to add weather measurement: retrieving newly inserted id failed: ", err.Error())
return 0, err
}
return uint(newId), nil
}
func PurgeWeatherData() error {
if _, err := db.Exec(`
DELETE FROM
weather
`); err != nil {
log.Error("Failed to purge weather cache: executing query failed: ", err.Error())
return err
}
return nil
}