-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ceb pollution data source
- Loading branch information
Showing
5 changed files
with
145 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package ceb | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/hg/airmon/influx" | ||
"github.com/hg/airmon/net" | ||
"github.com/hg/airmon/tm" | ||
influxdb2 "github.com/influxdata/influxdb-client-go/v2" | ||
"github.com/pkg/errors" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type measurement struct { | ||
Id string `json:"id"` | ||
Title string `json:"post_title"` | ||
Address string `json:"address"` | ||
Lat float64 `json:"lat,string"` | ||
Lon float64 `json:"lon,string"` | ||
Pollutant string `json:"desc"` | ||
PollutantFull string `json:"substance"` | ||
ValueMg float64 `json:"value"` | ||
Date tm.Time `json:"cdate"` | ||
} | ||
|
||
func Collect(sender *influx.MeasurementSender) { | ||
client := net.NewProxiedClient() | ||
lastAt := time.Time{} | ||
|
||
for { | ||
if measurements, err := getResponse(client); err == nil { | ||
latest := time.Time{} | ||
toSave := make([]measurement, len(measurements)) | ||
|
||
log.Print("found ", len(measurements), " ceb measurements") | ||
|
||
for _, meas := range measurements { | ||
if meas.Date.After(lastAt) { | ||
latest = meas.Date.Time | ||
toSave = append(toSave, meas) | ||
} | ||
} | ||
|
||
if !latest.IsZero() { | ||
lastAt = latest | ||
} | ||
|
||
go func() { | ||
for _, ms := range toSave { | ||
saveMeasurement(ms, sender) | ||
} | ||
}() | ||
} else { | ||
log.Print("could not load data: ", err) | ||
} | ||
|
||
time.Sleep(5 * time.Minute) | ||
} | ||
} | ||
|
||
func saveMeasurement(ms measurement, sender *influx.MeasurementSender) { | ||
endOfFormula := strings.Index(ms.PollutantFull, "-") | ||
if endOfFormula <= 0 { | ||
return | ||
} | ||
|
||
tags := map[string]string{ | ||
"station": ms.Title, | ||
"address": ms.Address, | ||
"formula": ms.PollutantFull[:endOfFormula], | ||
"pollutant": ms.Pollutant, | ||
} | ||
|
||
fields := map[string]interface{}{ | ||
"level_ug": ms.ValueMg * 1000, | ||
"lat": ms.Lat, | ||
"lon": ms.Lon, | ||
} | ||
|
||
sender.Send(influxdb2.NewPoint("ceb", tags, fields, ms.Date.Time)) | ||
} | ||
|
||
func getResponse(client *http.Client) ([]measurement, error) { | ||
resp, err := client.Get("https://ceb-uk.kz/map/ajax.php?markers") | ||
if err != nil { | ||
return nil, errors.Wrap(err, "data fetch failed") | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not read response") | ||
} | ||
|
||
var measurements []measurement | ||
err = json.Unmarshal(body, &measurements) | ||
return measurements, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package tm | ||
|
||
import ( | ||
"log" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const timeFormat = "2006-01-02 15:04:05" | ||
|
||
type Time struct { | ||
time.Time | ||
} | ||
|
||
var location *time.Location | ||
|
||
func init() { | ||
var err error | ||
if location, err = time.LoadLocation("Asia/Almaty"); err != nil { | ||
log.Fatal("could not find timezone") | ||
} | ||
} | ||
|
||
func (t *Time) UnmarshalJSON(b []byte) (err error) { | ||
s := strings.Trim(string(b), `"`) | ||
if s == "null" { | ||
t.Time = time.Time{} | ||
return nil | ||
} | ||
t.Time, err = time.ParseInLocation(timeFormat, s, location) | ||
return err | ||
} |