-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.go
138 lines (126 loc) · 3.45 KB
/
script.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
137
138
package canada
import (
"context"
"errors"
"fmt"
"math"
"sync"
"github.com/whitewater-guide/gorge/core"
"golang.org/x/text/encoding/charmap"
)
type optionsCanada struct {
Provinces string `desc:"Comma-separated list of province codes"`
Timeout int64 `desc:"Custom http request timeout in seconds for listing gauges"`
}
type scriptCanada struct {
name string
baseURL string
numWokers int
provinces map[string]bool
timeoutSec int64
core.LoggingScript
}
func (s *scriptCanada) ListGauges() (result core.Gauges, err error) {
client := core.Client
if s.timeoutSec != 0 {
client = core.NewClient(core.ClientOptions{
UserAgent: "whitewater.guide robot",
Timeout: s.timeoutSec,
}, s.GetLogger())
}
err = client.StreamCSV(
s.baseURL+"/doc/hydrometric_StationList.csv",
func(row []string) error {
g, err := s.gaugeFromRow(row)
if err != nil {
s.GetLogger().Error(fmt.Errorf("failed to convert row to gauge: %w", err))
}
if _, ok := s.provinces[row[4]]; err == nil && ok {
result = append(result, *g)
}
return nil
},
core.CSVStreamOptions{
NumColumns: 6,
HeaderHeight: 1,
Decoder: charmap.Windows1252.NewDecoder(),
},
)
core.CloseTimezoneDb()
return
}
func (s *scriptCanada) Harvest(ctx context.Context, recv chan<- *core.Measurement, errs chan<- error, codes core.StringSet, since int64) {
defer close(recv)
defer close(errs)
// Sometimes gauge list would contain main gauge, but measurements list would contain auxiliary gauge for it or vise versa.
// Auxiliary gauges are marked with X character, main gauges have 0. Some gauges have 1, I dont't know what it means
// I could not find any documentation on this, so I know this by trial and error
remapCodes := make(map[string]string)
if len(codes) > 0 {
for code := range codes {
code2 := getPairedGauge(code)
if _, ok := codes[code2]; !ok && code2 != code {
remapCodes[code2] = code
}
}
}
jobsCh := make(chan string, len(s.provinces))
resultsCh := make(chan *core.Measurement, len(s.provinces))
var wg sync.WaitGroup
numWorkers := s.numWokers
if numWorkers == 0 {
numWorkers = int(math.Min(3, float64(len(s.provinces))))
}
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go s.provinceWorker(ctx, jobsCh, resultsCh, &wg)
}
for prov := range s.provinces {
jobsCh <- prov
}
close(jobsCh)
go func() {
wg.Wait()
close(resultsCh)
}()
for m := range resultsCh {
if code2, ok := remapCodes[m.GaugeID.Code]; ok {
m.GaugeID.Code = code2
}
recv <- m
}
}
func (s *scriptCanada) provinceWorker(ctx context.Context, provinces <-chan string, results chan<- *core.Measurement, wg *sync.WaitGroup) {
for province := range provinces {
logger := s.GetLogger().WithField("province", province)
var m *core.Measurement
err := core.Client.StreamCSV(
fmt.Sprintf("%s/csv/%s/hourly/%s_hourly_hydrometric.csv", s.baseURL, province, province),
func(row []string) error {
select {
case <-ctx.Done():
return fmt.Errorf("context canceled: %w", ctx.Err())
default:
var e error
m, e = s.measurementFromRow(row)
if e == nil {
results <- m
}
if e != nil {
logger.Error(core.WrapErr(e, "province worker error"))
}
}
return nil
},
core.CSVStreamOptions{
NumColumns: 10,
HeaderHeight: 1,
Decoder: charmap.Windows1252.NewDecoder(),
},
)
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
break
}
}
wg.Done()
}