This commit is contained in:
idanoo 2023-02-13 08:47:08 +13:00
parent adfcf44890
commit 6b6037f735
Signed by: idanoo
GPG key ID: 387387CDBC02F132
2 changed files with 24 additions and 10 deletions

View file

@ -162,22 +162,15 @@ func getLastMetric(serviceName string) int {
func getLastWeekMetric(serviceName string) int {
monday := getStartofDayMonday()
log.Printf(
"SELECT metric_value FROM %s WHERE metric_name = '%s' AND service = '%s' AND metric_time = '%s' LIMIT 1",
POSTGRESQL_STATS_TABLE,
METRICNAME_USERCOUNT,
serviceName,
monday,
)
val, err := runIntQuery(
val, err := runIntQueryWithTime(
POSTGRESQL_STATS_DB,
fmt.Sprintf(
"SELECT metric_value FROM %s WHERE metric_name = '%s' AND service = '%s' AND metric_time = '%s' LIMIT 1",
"SELECT metric_value FROM %s WHERE metric_name = '%s' AND service = '%s' AND metric_time = '$1' LIMIT 1",
POSTGRESQL_STATS_TABLE,
METRICNAME_USERCOUNT,
serviceName,
monday,
),
monday,
)
if err != nil {

View file

@ -3,6 +3,7 @@ package gomastodonstats
import (
"database/sql"
"fmt"
"time"
_ "github.com/lib/pq"
)
@ -57,3 +58,23 @@ func runIntQuery(schema string, q string) (int, error) {
return res, err
}
func runIntQueryWithTime(schema string, q string, t time.Time) (int, error) {
var res int
db, err := getConnection(schema)
if err != nil {
return res, err
}
rows, err := db.Query(q, t)
if err != nil {
return res, err
}
defer rows.Close()
for rows.Next() {
rows.Scan(&res)
}
return res, err
}