Skip to content

Commit

Permalink
Added activity level metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenuhrbach committed Oct 31, 2024
1 parent 2acf15d commit 22196c5
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 17 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions internal/egym/activityLevel.go
Original file line number Diff line number Diff line change
@@ -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"`
}
22 changes: 20 additions & 2 deletions internal/egym/models.go → internal/egym/bioAge.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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"`
Expand Down
15 changes: 0 additions & 15 deletions internal/egym/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
68 changes: 68 additions & 0 deletions internal/exporter/activityLevel.go
Original file line number Diff line number Diff line change
@@ -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...,
)
}
2 changes: 2 additions & 0 deletions internal/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 22196c5

Please sign in to comment.