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_doses_administered"` TotalDosesAdministered int `json:"total_doses_administered"` 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 { TotalDosesAdministered func() prometheus.Gauge `name:"total_doses_administered" help:"Total doses administered"` PreviousDayDosesAdministered func() prometheus.Gauge `name:"previous_day_doses_administered" help:"Previous day doses administered"` TotalDosesInFullyVaccinatedIndividuals func() prometheus.Gauge `name:"total_doses_in_fully_vaccinated_individuals" help:"Total doses 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(12455397) 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.TotalDosesAdministered().Set(float64(data.Result.Records[0].TotalDosesAdministered)) records.PreviousDayDosesAdministered().Set(float64(data.Result.Records[0].PreviousDayDosesAdministered)) 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)) }