From 22196c5955c1c8f4c4144ef8f05d5652a651fc0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Uhrbach?= Date: Thu, 31 Oct 2024 21:24:16 +0100 Subject: [PATCH] Added activity level metrics --- README.md | 7 +++ internal/egym/activityLevel.go | 29 +++++++++++ internal/egym/{models.go => bioAge.go} | 22 ++++++++- internal/egym/client.go | 15 ------ internal/exporter/activityLevel.go | 68 ++++++++++++++++++++++++++ internal/exporter/exporter.go | 2 + 6 files changed, 126 insertions(+), 17 deletions(-) create mode 100644 internal/egym/activityLevel.go rename internal/egym/{models.go => bioAge.go} (78%) create mode 100644 internal/exporter/activityLevel.go diff --git a/README.md b/README.md index faec114..b0a6b85 100644 --- a/README.md +++ b/README.md @@ -105,9 +105,16 @@ docker run \ |egym_bio_age_muscles_core|Core muscles bio age| |egym_bio_age_muscles_lower_body|Lower body muscles bio age| |egym_bio_age_muscles_upper_body|Upper body muscles bio age| +|egym_activity_points|Amount of activity points within the current period| +|egym_activity_points_goal|Amount of activity points to reach the next activity level| +|egym_activity_maintain_points|Amount of required activity points to maintain the activity level| ## Release Notes +### 0.2.0 + +Added metrics about the users activity level. + ### 0.1.0 Initial release diff --git a/internal/egym/activityLevel.go b/internal/egym/activityLevel.go new file mode 100644 index 0000000..afe7619 --- /dev/null +++ b/internal/egym/activityLevel.go @@ -0,0 +1,29 @@ +package egym + +import ( + "encoding/json" + "fmt" +) + +func (c *EgymClient) GetActivityLevel() (*GetActivityLevelsResponse, error) { + url := fmt.Sprintf("%s/analysis/api/v1.0/exercisers/%s/activitylevels", c.apiUrl, c.userId) + body, err := c.fetch(url, 1) + if err != nil { + return nil, err + } + + var response GetActivityLevelsResponse + err = json.Unmarshal(body, &response) + if err != nil { + return nil, err + } + return &response, nil +} + +type GetActivityLevelsResponse struct { + Points int `json:"points"` + DaysLeft int `json:"daysLeft"` + Level string `json:"level"` + Goal int `json:"goal"` + MaintainPoints int `json:"maintainPoints"` +} diff --git a/internal/egym/models.go b/internal/egym/bioAge.go similarity index 78% rename from internal/egym/models.go rename to internal/egym/bioAge.go index 78f222a..b96ec37 100644 --- a/internal/egym/models.go +++ b/internal/egym/bioAge.go @@ -1,5 +1,25 @@ package egym +import ( + "encoding/json" + "fmt" +) + +func (c *EgymClient) GetBioAge() (*GetBioAgeResponse, error) { + url := fmt.Sprintf("%s/analysis/api/v1.0/exercisers/%s/bioage", c.apiUrl, c.userId) + body, err := c.fetch(url, 1) + if err != nil { + return nil, err + } + + var response GetBioAgeResponse + err = json.Unmarshal(body, &response) + if err != nil { + return nil, err + } + return &response, nil +} + type GetBioAgeResponse struct { TotalDetails struct { TotalBioAge BioAgeDetail `json:"totalBioAge"` @@ -12,8 +32,6 @@ type GetBioAgeResponse struct { } `json:"muscleDetails"` MetabolicDetails struct { MetabolicAge BioAgeDetail `json:"metabolicAge"` - // BodyFat BioAgeDetail `json:"bodyFat"` - // BMI BioAgeDetail `json:"bmi"` } `json:"metabolicDetails"` CardioDetails struct { CardioAge BioAgeDetail `json:"cardioAge"` diff --git a/internal/egym/client.go b/internal/egym/client.go index 998ca58..b3d5525 100644 --- a/internal/egym/client.go +++ b/internal/egym/client.go @@ -102,18 +102,3 @@ func (c *EgymClient) fetch(url string, retryCount int) ([]byte, error) { return io.ReadAll(resp.Body) } - -func (c *EgymClient) GetBioAge() (*GetBioAgeResponse, error) { - url := fmt.Sprintf("%s/analysis/api/v1.0/exercisers/%s/bioage", c.apiUrl, c.userId) - body, err := c.fetch(url, 1) - if err != nil { - return nil, err - } - - var response GetBioAgeResponse - err = json.Unmarshal(body, &response) - if err != nil { - return nil, err - } - return &response, nil -} diff --git a/internal/exporter/activityLevel.go b/internal/exporter/activityLevel.go new file mode 100644 index 0000000..9af1dd9 --- /dev/null +++ b/internal/exporter/activityLevel.go @@ -0,0 +1,68 @@ +package exporter + +import ( + "strconv" + + "github.com/prometheus/client_golang/prometheus" + log "github.com/sirupsen/logrus" +) + +const activity_namespace = "activity" + +var ( + activity_labels = []string{"activity_level", "days_left"} + + activityPoints = prometheus.NewDesc( + prometheus.BuildFQName(namespace, activity_namespace, "points"), + "Current activity level", + append(labels, activity_labels...), + nil, + ) + activityMaintainPoints = prometheus.NewDesc( + prometheus.BuildFQName(namespace, activity_namespace, "maintain_points"), + "Current activity level", + append(labels, activity_labels...), + nil, + ) + activityGoal = prometheus.NewDesc( + prometheus.BuildFQName(namespace, activity_namespace, "points_goal"), + "Current activity level", + append(labels, activity_labels...), + nil, + ) +) + +func (e *EgymExporter) describeActivityLevelMetrics(ch chan<- *prometheus.Desc) { + ch <- activityPoints + ch <- activityMaintainPoints + ch <- activityGoal +} + +func (e *EgymExporter) collectActivityLevelMetrics(ch chan<- prometheus.Metric) { + data, err := e.client.GetActivityLevel() + if err != nil { + log.Error("Could not load activity level data", err) + return + } + + labelValues := []string{e.client.Username, data.Level, strconv.Itoa(data.DaysLeft)} + + ch <- prometheus.MustNewConstMetric( + activityPoints, + prometheus.GaugeValue, + float64(data.Points), + labelValues..., + ) + ch <- prometheus.MustNewConstMetric( + activityMaintainPoints, + prometheus.GaugeValue, + float64(data.MaintainPoints), + labelValues..., + ) + ch <- prometheus.MustNewConstMetric( + activityGoal, + prometheus.GaugeValue, + float64(data.Goal), + labelValues..., + ) +} diff --git a/internal/exporter/exporter.go b/internal/exporter/exporter.go index c917d89..bf05939 100644 --- a/internal/exporter/exporter.go +++ b/internal/exporter/exporter.go @@ -17,10 +17,12 @@ type EgymExporter struct { func (c *EgymExporter) Describe(ch chan<- *prometheus.Desc) { c.describeBioAgeMetrics(ch) + c.describeActivityLevelMetrics(ch) } func (c *EgymExporter) Collect(ch chan<- prometheus.Metric) { c.collectBioAgeMetrics(ch) + c.collectActivityLevelMetrics(ch) } func NewEgymExporter(client *egym.EgymClient) *EgymExporter {