Skip to content

Commit

Permalink
add total payments metric
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkJD committed Dec 29, 2023
1 parent 7fa2339 commit b9a9591
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .munch-o-matic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Core:
schedule: "*/10 * * * * *"
type: CheckBalance
- name: Update Metrics
schedule: "*/10 * * * * *"
schedule: "0 * * * * *"
type: UpdateMetrics
- name: Order Food
params:
Expand Down
7 changes: 6 additions & 1 deletion core/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,12 @@ func (d Daemon) updateMetrics(ch chan jobStatus) func() {
ch <- jobStatus{JobId: jobId, Err: fmt.Errorf("could not load user: %w", err)}
return
}

UpdateAccountBalance(user.User.ID, user.User.FirstName, user.User.Customer.AccountBalance.Amount)

totalPayed := 0
for _, i := range user.User.Customer.Bookings {
totalPayed += i.BookingPrice
}
UpdatePaymentsTotal(user.User.ID, user.User.FirstName, totalPayed)
}
}
11 changes: 11 additions & 0 deletions core/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ var accountBalance = prometheus.NewGaugeVec(prometheus.GaugeOpts{
[]string{"user_id", "user_name"},
)

var paymentsTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "payments_total",
Help: "Tracks total payments"},
[]string{"user_id", "user_name"},
)

func init() {
// Register metrics with Prometheus's default registry.
prometheus.MustRegister(dishOrders)
prometheus.MustRegister(accountBalance)
prometheus.MustRegister(paymentsTotal)
}

func UpdateOrdersPlaced(OrderId int, DishName string, NumOrders int) {
Expand All @@ -34,6 +41,10 @@ func UpdateAccountBalance(UserId int, UserName string, Balance int) {
accountBalance.With(prometheus.Labels{"user_id": fmt.Sprint(UserId), "user_name": UserName}).Set(float64(Balance))
}

func UpdatePaymentsTotal(UserId int, UserName string, Value int) {
paymentsTotal.With(prometheus.Labels{"user_id": fmt.Sprint(UserId), "user_name": UserName}).Set(float64(Value))
}

// GetPrometheusHandler returns the HTTP handler for Prometheus metrics.
func GetPrometheusHandler() http.Handler {
return promhttp.Handler()
Expand Down

0 comments on commit b9a9591

Please sign in to comment.