Skip to content

Commit

Permalink
add a command for collecting regular metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
waisbrot committed Dec 31, 2023
1 parent 70a8464 commit 4c1c202
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 37 deletions.
30 changes: 19 additions & 11 deletions birdweather/forStation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,19 @@ import (

"github.com/Khan/genqlient/graphql"
"github.com/waisbrot/birdweather_daily_email/metrics"
"github.com/waisbrot/birdweather_daily_email/structs"
)

type BirdCount struct {
Name string
SciName string
ImageURL string
ImageCredit string
Count int
}

func BirdsForStation(stationid string) (string, []BirdCount) {
func BirdsForStation(stationid string) (string, []structs.BirdCount) {
client := graphql.NewClient("https://app.birdweather.com/graphql", http.DefaultClient)
counts, err := dailyCounts(context.Background(), client, stationid)
if err != nil {
panic(err)
}

var result = []BirdCount{}
var result = []structs.BirdCount{}
for _, count := range counts.Station.TopSpecies {
var bc BirdCount
var bc structs.BirdCount
bc.Name = count.Species.CommonName
bc.SciName = count.Species.ScientificName
bc.ImageURL = count.Species.ImageUrl
Expand All @@ -37,3 +30,18 @@ func BirdsForStation(stationid string) (string, []BirdCount) {
metrics.RecordFetch(counts.Station.Name, len(result))
return counts.Station.Name, result
}

func RecordCountsForStationPastMinutes(stationId string, minutes int) {
client := graphql.NewClient("https://app.birdweather.com/graphql", http.DefaultClient)
duration := InputDuration{
Count: minutes,
Unit: "minute",
}
counts, err := hourlyCounts(context.Background(), client, stationId, duration)
if err != nil {
panic(err)
}
for _, count := range counts.Station.TopSpecies {
metrics.RecordBird(counts.Station.Name, count.Species.CommonName, count.Breakdown.AlmostCertain)
}
}
146 changes: 146 additions & 0 deletions birdweather/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions birdweather/genqlient.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@ query dailyCounts($stationId: ID!) {
}
}
}

query hourlyCounts($stationId: ID!, $timePeriod: InputDuration!) {
station(id: $stationId) {
name
topSpecies(limit: 20, period: $timePeriod) {
breakdown {
almostCertain
}
species {
commonName
}
}
}
}

4 changes: 4 additions & 0 deletions birdweather/genqlient.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ operations:
- genqlient.graphql
generated: generated.go
package: birdweather

bindings:
ISO8601Date:
type: time.Time
39 changes: 39 additions & 0 deletions cmd/hourlyMetrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/waisbrot/birdweather_daily_email/birdweather"
)

// hourlyMetricsCmd represents the hourlyMetrics command
var hourlyMetricsCmd = &cobra.Command{
Use: "hourlyMetrics",
Short: "Fetch and push bird metrics",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
stationIds := viper.GetIntSlice("stations")
for _, stationId := range stationIds {
birdweather.RecordCountsForStationPastMinutes(fmt.Sprint(stationId), 30)
}
},
}

func init() {
rootCmd.AddCommand(hourlyMetricsCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// hourlyMetricsCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// hourlyMetricsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
7 changes: 4 additions & 3 deletions cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/spf13/viper"
"github.com/waisbrot/birdweather_daily_email/birdweather"
"github.com/waisbrot/birdweather_daily_email/email"
"github.com/waisbrot/birdweather_daily_email/structs"
)

// sendCmd represents the send command
Expand All @@ -24,11 +25,11 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
stations := []email.StationTemplate{}
stations := []structs.StationTemplate{}
stationIds := viper.GetIntSlice("stations")
for _, stationId := range stationIds {
stationName, counts := birdweather.BirdsForStation(fmt.Sprint(stationId))
templ := email.StationTemplate{}
templ := structs.StationTemplate{}
templ.Name = stationName
templ.Id = stationId
templ.Counts = counts
Expand All @@ -41,7 +42,7 @@ to quickly create a Cobra application.`,
}
yesterday := time.Now()
yesterday = yesterday.Add(time.Hour * -24)
templ := email.EmailTemplate{
templ := structs.EmailTemplate{
Day: yesterday.Weekday(),
Stations: stations,
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/templatetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"time"

"github.com/spf13/cobra"
"github.com/waisbrot/birdweather_daily_email/birdweather"
"github.com/waisbrot/birdweather_daily_email/email"
"github.com/waisbrot/birdweather_daily_email/structs"
)

// templatetestCmd represents the templatetest command
Expand All @@ -25,13 +25,13 @@ This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("templatetest called")
templ := email.EmailTemplate{
templ := structs.EmailTemplate{
Day: time.Sunday,
Stations: []email.StationTemplate{
Stations: []structs.StationTemplate{
{
Name: "Example station",
Id: 42,
Counts: []birdweather.BirdCount{
Counts: []structs.BirdCount{
{
Name: "Chickadee",
SciName: "Dee-dee-dee",
Expand Down
16 changes: 2 additions & 14 deletions email/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,11 @@ import (
"html/template"
"io"
"path"
"time"

"github.com/spf13/viper"
"github.com/waisbrot/birdweather_daily_email/birdweather"
"github.com/waisbrot/birdweather_daily_email/structs"
)

type EmailTemplate struct {
Day time.Weekday
Stations []StationTemplate
}

type StationTemplate struct {
Name string
Id int
Counts []birdweather.BirdCount
}

func readTemplate() *template.Template {
templateFile := viper.GetString("email.template")
base := path.Base(templateFile)
Expand All @@ -32,7 +20,7 @@ func readTemplate() *template.Template {
return template
}

func RenderTemplate(variables EmailTemplate) io.Reader {
func RenderTemplate(variables structs.EmailTemplate) io.Reader {
template := readTemplate()
buffer := new(bytes.Buffer)
err := template.Execute(buffer, variables)
Expand Down
Loading

0 comments on commit 4c1c202

Please sign in to comment.