Verified Commit 375739ff authored by blint's avatar blint 😶
Browse files

update prometheus metrics

parent f61516c9
......@@ -66,6 +66,12 @@ var db = pg.Connect(&pg.Options{
Database: env.String("POSTGRES_DB", "postgres"),
})
var spendsNow = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "becskasszasch",
Name: "products_bought",
Help: "Products bought",
}, []string{"product"})
func init() {
models := []interface{}{
(*Product)(nil),
......@@ -114,6 +120,18 @@ func init() {
panic(e)
}
}
prod, err := GetProducts()
if err != nil {
// TODO
}
for _, p := range prod {
s, _ := GetSpends(p.ID)
if s != nil {
spendsNow.With(prometheus.Labels{"product": p.Name}).Add(float64(s[0].Amount))
}
}
}
func StoreUserAndGiveCookie(SchAcc, Name string) (string, error) {
......@@ -204,6 +222,14 @@ func GetProducts() ([]*Product, error) {
return products, e
}
func GetSpends(typ string) ([]*Spend, error) {
var spends []*Spend
e := db.Model(&spends).Where("product_id = ?", typ).Select()
return spends, e
}
func update(something interface{}) error {
_, e := db.Model(something).Insert()
if e != nil {
......@@ -231,12 +257,6 @@ func (p *Product) Load() error {
return e
}
var spendsNow = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "becskasszasch",
Name: "products_bought",
Help: "Products bought",
}, []string{"product"})
func (sp *Spend) Save() error {
p := &Product{
ID: sp.ProdID,
......@@ -252,7 +272,7 @@ func (sp *Spend) Save() error {
}
helpers.SpendLog.Printf("%s %s %d %s %dx%d=%d\n", sp.SchAcc, action, sp.Amount, sp.ProdID, sp.Amount, p.Price, sp.Amount*p.Price)
spendsNow.With(prometheus.Labels{"product": p.ID}).Inc()
spendsNow.With(prometheus.Labels{"product": p.Name}).Inc()
return update(sp)
}
......
......@@ -7,5 +7,5 @@ POSTGRES: "${POSTGRES}"
POSTGRES_PASS: "${POSTGRES_PASS}"
POSTGRES_USER: "${POSTGRES_USER}"
POSTGRES_DB: "${POSTGRES_DB}"
Tag: "v1.5.5"
Tag: "v1.5.6"
REGISTRY_CONF: "${REGISTRY_CONF}"
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment