-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.go
90 lines (72 loc) · 2 KB
/
core.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
package main
import "time"
func CreateHistoryDataEntry(location LocationEntry, source SourceEntry, measurements MeasurementArray, wtype string, url string, err error) (entry HistoryDataEntry) {
var status int64
var message string
if err != nil {
status = 500
message = err.Error()
} else {
status = 200
message = "OK"
}
entry = MakeHistoryDataEntry()
entry.Status = status
entry.Message = message
entry.Location = location
entry.Source = source
entry.Measurements = measurements
entry.WType = wtype
entry.Url = url
return entry
}
// StatSource polls single provider for data on specified location and wtype.
func StatSource(location LocationEntry, source SourceEntry, wtype string) (entry HistoryDataEntry) {
var err error
var url string
var raw string
measurements := make(MeasurementArray, 0)
adaptFunc, adaptFuncLookupErr := GetAdaptFunc(source.Name, wtype)
if adaptFuncLookupErr == nil {
url = MakeURL(source.Urls[wtype], UrlData{Source: source, Location: location})
var downloadErr error
raw, downloadErr = Download(url)
if downloadErr != nil {
measurements = AdaptStub(raw)
err = downloadErr
} else {
var adaptErr error
measurements, adaptErr = adaptFunc(raw)
err = adaptErr
}
} else {
err = adaptFuncLookupErr
}
entry = CreateHistoryDataEntry(location, source, measurements, wtype, url, err)
return entry
}
func PollAll(h *WeatherHistory, locations []LocationEntry, sources []SourceEntry, wtypes []string) (dataset []HistoryDataEntry) {
dt := time.Now().Unix()
dataChan := make(chan HistoryDataEntry, 9999)
doneChan := make(chan struct{})
go func() {
for entry := range dataChan {
h.Add(entry)
dataset = append(dataset, entry)
}
doneChan <- struct{}{}
}()
for _, location := range locations {
for _, source := range sources {
for _, wtype := range wtypes {
data := StatSource(location, source, wtype)
data.RequestTime = dt
dataChan <- data
}
}
time.Sleep(6 * time.Second)
}
close(dataChan)
<-doneChan
return dataset
}