Optimize query for updating metricURLsTotalGauge
This commit is contained in:
parent
c4ff0ab1b7
commit
306705cb28
29
metrics.go
29
metrics.go
@ -87,19 +87,22 @@ func (mh *MetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (mh *MetricsHandler) updateMetrics() {
|
||||
// Update metricURLsTotalGauge
|
||||
for state := db.PasteStateUndef; state <= db.PasteStateDeleted; state++ {
|
||||
for ty := db.PasteTypeUndef; ty <= db.PasteTypeFileUpload; ty++ {
|
||||
var count int64
|
||||
query := mh.db.Unscoped().Model(&db.Paste{}).Where("type = ? AND state = ?", ty, state).Count(&count)
|
||||
if err := query.Error; err != nil {
|
||||
log.Printf("error: %v", errors.Wrap(err, "fetching pastes_total metric"))
|
||||
return
|
||||
}
|
||||
labels := map[string]string{
|
||||
"state": state.String(),
|
||||
"type": ty.String(),
|
||||
}
|
||||
metricURLsTotalGauge.With(labels).Set(float64(count))
|
||||
results := make([](struct {
|
||||
Type db.PasteType
|
||||
State db.PasteState
|
||||
Count float64
|
||||
}), 0)
|
||||
query := mh.db.Unscoped().Model(&db.Paste{}).Select("type", "state", "COUNT(*) as count").Group("type, state").Find(&results)
|
||||
if err := query.Error; err != nil {
|
||||
log.Printf("error: %v", errors.Wrap(err, "fetching pastes_total metric"))
|
||||
return
|
||||
}
|
||||
metricURLsTotalGauge.Reset()
|
||||
for _, r := range results {
|
||||
labels := map[string]string{
|
||||
"type": r.Type.String(),
|
||||
"state": r.State.String(),
|
||||
}
|
||||
metricURLsTotalGauge.With(labels).Set(r.Count)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user