-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
85 lines (68 loc) · 1.68 KB
/
data.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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/jackc/pgx/v4"
)
func getData() []Zone {
var zones []Zone
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close(context.Background())
zones, age, err := load(conn)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to load data: %v\n", err)
os.Exit(1)
}
if time.Now().After(age.Add(time.Hour * 3)) {
updateCurrency()
zones = scrapeAllofThem()
save(zones, conn)
}
return zones
}
type DataEntry struct {
Age time.Time `json:"age"`
Zones []Zone `json:"zones"`
}
func save(z []Zone, conn *pgx.Conn) {
j, err := json.Marshal(DataEntry{
time.Now(),
z,
})
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to marshal data: %v\n", err)
os.Exit(1)
}
_, err = conn.Exec(context.Background(), "DELETE FROM power")
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to delete data: %v\n", err)
os.Exit(1)
}
_, err = conn.Query(context.Background(), "INSERT INTO power (data) VALUES ($1)", j)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to insert data: %v\n", err)
os.Exit(1)
}
}
func load(conn *pgx.Conn) ([]Zone, time.Time, error) {
var data []byte
err := conn.QueryRow(context.Background(), "SELECT data FROM power LIMIT 1").Scan(&data)
if err != nil {
fmt.Fprintf(os.Stderr, "No data, go get new plz: %v\n", err)
return nil, time.Time{}, nil
}
var entry DataEntry
err = json.Unmarshal(data, &entry)
if err != nil {
fmt.Println("Error unmarshalling data: ", err)
return nil, time.Time{}, err
}
return entry.Zones, entry.Age, nil
}