2021-04-08 20:50:25 +00:00
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"
)
2021-04-09 14:43:03 +00:00
var addr = flag . String ( "listen-address" , "127.0.0.1:8080" , "The address to listen on for HTTP requests." )
2021-04-08 20:50:25 +00:00
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" `
2021-06-03 15:09:27 +00:00
PreviousDayDosesAdministered int ` json:"previous_day_total_doses_administered" `
PreviousDayAtLeastOne int ` json:"previous_day_at_least_one" `
PreviousDayFullyVaccinated int ` json:"previous_day_fully_vaccinated" `
2021-04-29 15:51:57 +00:00
TotalDosesAdministered int ` json:"total_doses_administered" `
2021-06-03 15:09:27 +00:00
TotalDosesAtLeastOne int ` json:"total_individuals_at_least_one" `
2021-04-29 15:51:57 +00:00
TotalDosesInFullyVaccinatedIndividuals int ` json:"total_doses_in_fully_vaccinated_individuals" `
TotalIndividualsFullyVaccinated int ` json:"total_individuals_fully_vaccinated" `
2021-04-08 20:50:25 +00:00
} ` json:"records" `
Limit int ` json:"limit" `
Links struct {
Start string ` json:"start" `
Next string ` json:"next" `
} ` json:"_links" `
Total int ` json:"total" `
} ` json:"result" `
}
2021-04-09 14:18:28 +00:00
var records struct {
2021-04-09 15:03:27 +00:00
PreviousDayDosesAdministered func ( ) prometheus . Gauge ` name:"previous_day_doses_administered" help:"Previous day doses administered" `
2021-06-03 15:09:27 +00:00
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" `
2021-04-09 15:03:27 +00:00
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" `
2021-04-08 20:50:25 +00:00
}
func init ( ) {
2021-04-09 15:03:27 +00:00
gotoprom . MustInit ( & records , "ontvacstat" )
2021-04-08 20:50:25 +00:00
}
func updateMetrics ( ) {
2021-04-27 18:22:09 +00:00
data_url := "https://data.ontario.ca/api/3/action/datastore_search?sort=report_date+desc&limit=1&resource_id=8a89caa9-511c-4568-af89-7f2174b4378c"
2021-05-21 18:17:31 +00:00
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)
2021-04-08 20:50:25 +00:00
resp , getErr := http . Get ( data_url )
if getErr != nil {
2021-04-26 13:45:14 +00:00
log . Println ( getErr )
return
2021-04-08 20:50:25 +00:00
}
body , readErr := ioutil . ReadAll ( resp . Body )
if readErr != nil {
2021-04-26 13:45:14 +00:00
log . Println ( readErr )
return
2021-04-08 20:50:25 +00:00
}
data := OntCovidData { }
err := json . Unmarshal ( body , & data )
if err != nil {
2021-04-26 13:45:14 +00:00
log . Println ( err )
return
2021-04-08 20:50:25 +00:00
}
2021-04-26 13:45:14 +00:00
2021-06-03 15:09:27 +00:00
2021-04-29 15:51:57 +00:00
records . PreviousDayDosesAdministered ( ) . Set ( float64 ( data . Result . Records [ 0 ] . PreviousDayDosesAdministered ) )
2021-06-03 15:09:27 +00:00
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 ) )
2021-04-29 15:51:57 +00:00
records . TotalDosesInFullyVaccinatedIndividuals ( ) . Set ( float64 ( data . Result . Records [ 0 ] . TotalDosesInFullyVaccinatedIndividuals ) )
records . TotalIndividualsFullyVaccinated ( ) . Set ( float64 ( data . Result . Records [ 0 ] . TotalIndividualsFullyVaccinated ) )
2021-04-09 15:03:27 +00:00
records . EligiblePopulation ( ) . Set ( float64 ( eligiblePopulation ) )
2021-04-08 20:50:25 +00:00
}
func main ( ) {
c := cron . New ( )
2021-04-09 15:09:34 +00:00
c . AddFunc ( "@hourly" , func ( ) { updateMetrics ( ) } )
2021-04-08 20:50:25 +00:00
c . Start ( )
updateMetrics ( )
flag . Parse ( )
http . Handle ( "/metrics" , promhttp . Handler ( ) )
2021-04-09 14:18:28 +00:00
fmt . Println ( "Listening on port 8080" )
2021-04-08 20:50:25 +00:00
log . Fatal ( http . ListenAndServe ( * addr , nil ) )
}