113 lines
4.9 KiB
Go
113 lines
4.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/cabify/gotoprom"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
"github.com/robfig/cron/v3"
|
|
)
|
|
|
|
var addr = flag.String("listen-address", "127.0.0.1:8080", "The address to listen on for HTTP requests.")
|
|
|
|
type OntCovidData struct {
|
|
Help string `json:"help"`
|
|
Success bool `json:"success"`
|
|
Result struct {
|
|
Sort string `json:"sort"`
|
|
IncludeTotal bool `json:"include_total"`
|
|
ResourceID string `json:"resource_id"`
|
|
Fields []struct {
|
|
Type string `json:"type"`
|
|
ID string `json:"id"`
|
|
Info struct {
|
|
Notes string `json:"notes"`
|
|
TypeOverride string `json:"type_override"`
|
|
Label string `json:"label"`
|
|
} `json:"info,omitempty"`
|
|
} `json:"fields"`
|
|
RecordsFormat string `json:"records_format"`
|
|
Records []struct {
|
|
ID int `json:"_id"`
|
|
ReportDate string `json:"report_date"`
|
|
PreviousDayDosesAdministered int `json:"previous_day_total_doses_administered"`
|
|
PreviousDayAtLeastOne int `json:"previous_day_at_least_one"`
|
|
PreviousDayFullyVaccinated int `json:"previous_day_fully_vaccinated"`
|
|
TotalDosesAdministered int `json:"total_doses_administered"`
|
|
TotalDosesAtLeastOne int `json:"total_individuals_at_least_one"`
|
|
TotalDosesInFullyVaccinatedIndividuals int `json:"total_doses_in_fully_vaccinated_individuals"`
|
|
TotalIndividualsFullyVaccinated int `json:"total_individuals_fully_vaccinated"`
|
|
} `json:"records"`
|
|
Limit int `json:"limit"`
|
|
Links struct {
|
|
Start string `json:"start"`
|
|
Next string `json:"next"`
|
|
} `json:"_links"`
|
|
Total int `json:"total"`
|
|
} `json:"result"`
|
|
}
|
|
|
|
var records struct {
|
|
PreviousDayDosesAdministered func() prometheus.Gauge `name:"previous_day_doses_administered" help:"Previous day doses administered"`
|
|
PreviousDayAtLeastOne func() prometheus.Gauge `name:"previous_day_at_least_one" help:"Previous day doses administered at least one shot"`
|
|
PreviousDayFullyVaccinated func() prometheus.Gauge `name:"previous_day_fully_vaccinated" help:"Previous day doses administered fully vaccinated"`
|
|
TotalDosesAdministered func() prometheus.Gauge `name:"total_doses_administered" help:"Total doses administered"`
|
|
TotalDosesAtLeastOne func() prometheus.Gauge `name:"total_individuals_at_least_one" help:"Total at least one dose in individuals"`
|
|
TotalDosesInFullyVaccinatedIndividuals func() prometheus.Gauge `name:"total_doses_in_fully_vaccinated_individuals" help:"Total does in fully vaccinated individuals"`
|
|
TotalIndividualsFullyVaccinated func() prometheus.Gauge `name:"total_individuals_fully_vaccinated" help:"Total individuals fully vaccinated"`
|
|
EligiblePopulation func() prometheus.Gauge `name:"eligible_population" help:"Total population eligible for the vaccination"`
|
|
}
|
|
|
|
func init() {
|
|
gotoprom.MustInit(&records, "ontvacstat")
|
|
}
|
|
|
|
func updateMetrics() {
|
|
data_url := "https://data.ontario.ca/api/3/action/datastore_search?sort=report_date+desc&limit=1&resource_id=8a89caa9-511c-4568-af89-7f2174b4378c"
|
|
eligiblePopulation := int64(12932471) // Ontario population 12+ (https://www150.statcan.gc.ca/t1/tbl1/en/cv!recreate.action?pid=1710000501&selectedNodeIds=1D7,3D16,3D17,3D18,3D20,3D21,3D22,3D101&checkedLevels=1D1&refPeriods=20160101,20200101&dimensionLayouts=layout2,layout2,layout3,layout2&vectorDisplay=false)
|
|
resp, getErr := http.Get(data_url)
|
|
if getErr != nil {
|
|
log.Println(getErr)
|
|
return
|
|
}
|
|
body, readErr := ioutil.ReadAll(resp.Body)
|
|
if readErr != nil {
|
|
log.Println(readErr)
|
|
return
|
|
}
|
|
data := OntCovidData{}
|
|
err := json.Unmarshal(body, &data)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
records.PreviousDayDosesAdministered().Set(float64(data.Result.Records[0].PreviousDayDosesAdministered))
|
|
records.PreviousDayAtLeastOne().Set(float64(data.Result.Records[0].PreviousDayAtLeastOne))
|
|
records.PreviousDayFullyVaccinated().Set(float64(data.Result.Records[0].PreviousDayFullyVaccinated))
|
|
records.TotalDosesAdministered().Set(float64(data.Result.Records[0].TotalDosesAdministered))
|
|
records.TotalDosesAtLeastOne().Set(float64(data.Result.Records[0].TotalDosesAtLeastOne))
|
|
records.TotalDosesInFullyVaccinatedIndividuals().Set(float64(data.Result.Records[0].TotalDosesInFullyVaccinatedIndividuals))
|
|
records.TotalIndividualsFullyVaccinated().Set(float64(data.Result.Records[0].TotalIndividualsFullyVaccinated))
|
|
records.EligiblePopulation().Set(float64(eligiblePopulation))
|
|
}
|
|
|
|
func main() {
|
|
c := cron.New()
|
|
c.AddFunc("@hourly", func() { updateMetrics() })
|
|
c.Start()
|
|
|
|
updateMetrics()
|
|
|
|
flag.Parse()
|
|
http.Handle("/metrics", promhttp.Handler())
|
|
fmt.Println("Listening on port 8080")
|
|
log.Fatal(http.ListenAndServe(*addr, nil))
|
|
}
|