Private
Public Access
1
0

scores: full_history option for internal clients
All checks were successful
continuous-integration/drone/push Build is passing

(somewhat inefficient, but for now rarely used ...)
This commit is contained in:
2024-01-20 23:21:21 -07:00
parent b786ed6986
commit e1398e7472
3 changed files with 59 additions and 34 deletions

View File

@@ -11,25 +11,27 @@ import (
"go.ntppool.org/data-api/ntpdb"
)
func (d *ClickHouse) Logscores(ctx context.Context, serverID, monitorID int, since time.Time, limit int) ([]ntpdb.LogScore, error) {
func (d *ClickHouse) Logscores(ctx context.Context, serverID, monitorID int, since time.Time, limit int, fullHistory bool) ([]ntpdb.LogScore, error) {
log := logger.Setup()
ctx, span := tracing.Tracer().Start(ctx, "CH Logscores")
defer span.End()
if since.IsZero() {
recentFirst := true
if since.IsZero() && !fullHistory {
log.WarnContext(ctx, "resetting since to 4 days ago")
since = time.Now().Add(4 * -24 * time.Hour)
} else {
recentFirst = false
}
args := []interface{}{serverID, since, limit}
args := []interface{}{serverID}
query := `select id,monitor_id,server_id,ts,
toFloat64(score),toFloat64(step),offset,
rtt,leap,warning,error
from log_scores
where
server_id = ?
and ts > ?
order by ts desc
limit ?;`
server_id = ?`
if monitorID > 0 {
query = `select id,monitor_id,server_id,ts,
@@ -38,13 +40,26 @@ func (d *ClickHouse) Logscores(ctx context.Context, serverID, monitorID int, sin
from log_scores
where
server_id = ?
and monitor_id = ?
and ts > ?
order by ts desc
limit ?;`
args = []interface{}{serverID, monitorID, since, limit}
and monitor_id = ?`
args = []interface{}{serverID, monitorID}
}
if fullHistory {
query += " order by ts"
if recentFirst {
query += " desc"
}
} else {
query += " and ts > ? order by ts "
if recentFirst {
query += "desc "
}
query += "limit ?"
args = append(args, since, limit)
}
log.DebugContext(ctx, "clickhouse query", "query", query, "args", args)
rows, err := d.Scores.Query(clickhouse.Context(ctx, clickhouse.WithSpan(span.SpanContext())),
query, args...,
)