Private
Public Access
1
0

5 Commits

Author SHA1 Message Date
f6b0f96a34 scores: json handler
Some checks failed
continuous-integration/drone/push Build was killed
2023-12-10 21:42:15 -08:00
61245cc77c scores: csv handler 2023-12-10 21:02:04 -08:00
adab600e26 Function to get server from IP or ID parameter
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-10 20:55:44 -08:00
9ef534eafa server: add url.path to traces
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-10 20:49:44 -08:00
9c6ea595f1 dnsanswers: tracing adjustments 2023-12-10 15:11:26 -08:00
15 changed files with 1079 additions and 28 deletions

2
go.mod
View File

@@ -13,7 +13,7 @@ require (
github.com/spf13/cobra v1.8.0 github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4 github.com/stretchr/testify v1.8.4
go.ntppool.org/api v0.1.8-0.20231210025001-f2c143296511 go.ntppool.org/api v0.1.8-0.20231210025001-f2c143296511
go.ntppool.org/common v0.2.5 go.ntppool.org/common v0.2.6-0.20231211031613-608f05d39551
go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.46.1 go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.46.1
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.46.1 go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.46.1
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1

2
go.sum
View File

@@ -142,6 +142,8 @@ go.ntppool.org/api v0.1.8-0.20231210025001-f2c143296511 h1:ilTOX5NQcdDiNohhDexUe
go.ntppool.org/api v0.1.8-0.20231210025001-f2c143296511/go.mod h1:9FKbwWfF7eRU7GZVEI3wUYv71ZKt16cLCGfxLayzS2Q= go.ntppool.org/api v0.1.8-0.20231210025001-f2c143296511/go.mod h1:9FKbwWfF7eRU7GZVEI3wUYv71ZKt16cLCGfxLayzS2Q=
go.ntppool.org/common v0.2.5 h1:fvuFrCCbmaRzZOSwv71+yhqVLOTDB/fD7YlscdGa6qs= go.ntppool.org/common v0.2.5 h1:fvuFrCCbmaRzZOSwv71+yhqVLOTDB/fD7YlscdGa6qs=
go.ntppool.org/common v0.2.5/go.mod h1:Cw8mq8jd2sLCxbTNzYXKXn3qKo2ZLERZ6V/eLcSgDHw= go.ntppool.org/common v0.2.5/go.mod h1:Cw8mq8jd2sLCxbTNzYXKXn3qKo2ZLERZ6V/eLcSgDHw=
go.ntppool.org/common v0.2.6-0.20231211031613-608f05d39551 h1:kJdF3U4KBuJJtbF04d5OA/QttxDY/fOgHe1oUDEgLGA=
go.ntppool.org/common v0.2.6-0.20231211031613-608f05d39551/go.mod h1:Cw8mq8jd2sLCxbTNzYXKXn3qKo2ZLERZ6V/eLcSgDHw=
go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.46.1 h1:yJWyqeE+8jdOJpt+ZFn7sX05EJAK/9C4jjNZyb61xZg= go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.46.1 h1:yJWyqeE+8jdOJpt+ZFn7sX05EJAK/9C4jjNZyb61xZg=
go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.46.1/go.mod h1:tlgpIvi6LCv4QIZQyBc8Gkr6HDxbJLTh9eQPNZAaljE= go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.46.1/go.mod h1:tlgpIvi6LCv4QIZQyBc8Gkr6HDxbJLTh9eQPNZAaljE=
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.46.1 h1:gbhw/u49SS3gkPWiYweQNJGm/uJN5GkI/FrosxSHT7A= go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.46.1 h1:gbhw/u49SS3gkPWiYweQNJGm/uJN5GkI/FrosxSHT7A=

82
logscores/history.go Normal file
View File

@@ -0,0 +1,82 @@
package logscores
import (
"context"
"database/sql"
"time"
"go.ntppool.org/common/logger"
"go.ntppool.org/common/tracing"
"go.ntppool.org/data-api/ntpdb"
"go.opentelemetry.io/otel/attribute"
)
type LogScoreHistory struct {
LogScores []ntpdb.LogScore
Monitors map[int]string
MonitorIDs []uint32
}
func GetHistory(ctx context.Context, db *sql.DB, serverID, monitorID uint32, since time.Time, count int) (*LogScoreHistory, error) {
log := logger.Setup()
ctx, span := tracing.Tracer().Start(ctx, "logscores.GetHistory")
defer span.End()
if count == 0 {
count = 200
}
span.SetAttributes(
attribute.Int("server", int(serverID)),
attribute.Int("monitor", int(monitorID)),
)
log.Debug("GetHistory", "server", serverID, "monitor", monitorID, "since", since, "count", count)
q := ntpdb.NewWrappedQuerier(ntpdb.New(db))
var ls []ntpdb.LogScore
var err error
if monitorID > 0 {
ls, err = q.GetServerLogScoresByMonitorID(ctx, ntpdb.GetServerLogScoresByMonitorIDParams{
ServerID: serverID,
MonitorID: sql.NullInt32{Int32: int32(monitorID), Valid: true},
Limit: int32(count),
})
} else {
ls, err = q.GetServerLogScores(ctx, ntpdb.GetServerLogScoresParams{
ServerID: serverID,
Limit: int32(count),
})
}
if err != nil {
return nil, err
}
monitors := map[int]string{}
monitorIDs := []uint32{}
for _, l := range ls {
if !l.MonitorID.Valid {
continue
}
mID := uint32(l.MonitorID.Int32)
if _, ok := monitors[int(mID)]; !ok {
monitors[int(mID)] = ""
monitorIDs = append(monitorIDs, mID)
}
}
dbmons, err := q.GetMonitorsByID(ctx, monitorIDs)
if err != nil {
return nil, err
}
for _, m := range dbmons {
monitors[int(m.ID)] = m.DisplayName()
}
return &LogScoreHistory{
LogScores: ls,
Monitors: monitors,
MonitorIDs: monitorIDs,
}, nil
}

View File

@@ -9,8 +9,182 @@ import (
"database/sql/driver" "database/sql/driver"
"fmt" "fmt"
"time" "time"
"go.ntppool.org/common/types"
) )
type MonitorsIpVersion string
const (
MonitorsIpVersionV4 MonitorsIpVersion = "v4"
MonitorsIpVersionV6 MonitorsIpVersion = "v6"
)
func (e *MonitorsIpVersion) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = MonitorsIpVersion(s)
case string:
*e = MonitorsIpVersion(s)
default:
return fmt.Errorf("unsupported scan type for MonitorsIpVersion: %T", src)
}
return nil
}
type NullMonitorsIpVersion struct {
MonitorsIpVersion MonitorsIpVersion `json:"monitors_ip_version"`
Valid bool `json:"valid"` // Valid is true if MonitorsIpVersion is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullMonitorsIpVersion) Scan(value interface{}) error {
if value == nil {
ns.MonitorsIpVersion, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.MonitorsIpVersion.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullMonitorsIpVersion) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return string(ns.MonitorsIpVersion), nil
}
type MonitorsStatus string
const (
MonitorsStatusPending MonitorsStatus = "pending"
MonitorsStatusTesting MonitorsStatus = "testing"
MonitorsStatusActive MonitorsStatus = "active"
MonitorsStatusPaused MonitorsStatus = "paused"
MonitorsStatusDeleted MonitorsStatus = "deleted"
)
func (e *MonitorsStatus) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = MonitorsStatus(s)
case string:
*e = MonitorsStatus(s)
default:
return fmt.Errorf("unsupported scan type for MonitorsStatus: %T", src)
}
return nil
}
type NullMonitorsStatus struct {
MonitorsStatus MonitorsStatus `json:"monitors_status"`
Valid bool `json:"valid"` // Valid is true if MonitorsStatus is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullMonitorsStatus) Scan(value interface{}) error {
if value == nil {
ns.MonitorsStatus, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.MonitorsStatus.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullMonitorsStatus) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return string(ns.MonitorsStatus), nil
}
type MonitorsType string
const (
MonitorsTypeMonitor MonitorsType = "monitor"
MonitorsTypeScore MonitorsType = "score"
)
func (e *MonitorsType) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = MonitorsType(s)
case string:
*e = MonitorsType(s)
default:
return fmt.Errorf("unsupported scan type for MonitorsType: %T", src)
}
return nil
}
type NullMonitorsType struct {
MonitorsType MonitorsType `json:"monitors_type"`
Valid bool `json:"valid"` // Valid is true if MonitorsType is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullMonitorsType) Scan(value interface{}) error {
if value == nil {
ns.MonitorsType, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.MonitorsType.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullMonitorsType) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return string(ns.MonitorsType), nil
}
type ServerScoresStatus string
const (
ServerScoresStatusNew ServerScoresStatus = "new"
ServerScoresStatusTesting ServerScoresStatus = "testing"
ServerScoresStatusActive ServerScoresStatus = "active"
)
func (e *ServerScoresStatus) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = ServerScoresStatus(s)
case string:
*e = ServerScoresStatus(s)
default:
return fmt.Errorf("unsupported scan type for ServerScoresStatus: %T", src)
}
return nil
}
type NullServerScoresStatus struct {
ServerScoresStatus ServerScoresStatus `json:"server_scores_status"`
Valid bool `json:"valid"` // Valid is true if ServerScoresStatus is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullServerScoresStatus) Scan(value interface{}) error {
if value == nil {
ns.ServerScoresStatus, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.ServerScoresStatus.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullServerScoresStatus) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return string(ns.ServerScoresStatus), nil
}
type ServersIpVersion string type ServersIpVersion string
const ( const (
@@ -95,6 +269,37 @@ func (ns NullZoneServerCountsIpVersion) Value() (driver.Value, error) {
return string(ns.ZoneServerCountsIpVersion), nil return string(ns.ZoneServerCountsIpVersion), nil
} }
type LogScore struct {
ID uint64 `db:"id" json:"id"`
MonitorID sql.NullInt32 `db:"monitor_id" json:"monitor_id"`
ServerID uint32 `db:"server_id" json:"server_id"`
Ts time.Time `db:"ts" json:"ts"`
Score float64 `db:"score" json:"score"`
Step float64 `db:"step" json:"step"`
Offset sql.NullFloat64 `db:"offset" json:"offset"`
Rtt sql.NullInt32 `db:"rtt" json:"rtt"`
Attributes types.LogScoreAttributes `db:"attributes" json:"attributes"`
}
type Monitor struct {
ID uint32 `db:"id" json:"id"`
Type MonitorsType `db:"type" json:"type"`
UserID sql.NullInt32 `db:"user_id" json:"user_id"`
AccountID sql.NullInt32 `db:"account_id" json:"account_id"`
Name string `db:"name" json:"name"`
Location string `db:"location" json:"location"`
Ip sql.NullString `db:"ip" json:"ip"`
IpVersion NullMonitorsIpVersion `db:"ip_version" json:"ip_version"`
TlsName sql.NullString `db:"tls_name" json:"tls_name"`
ApiKey sql.NullString `db:"api_key" json:"api_key"`
Status MonitorsStatus `db:"status" json:"status"`
Config string `db:"config" json:"config"`
ClientVersion string `db:"client_version" json:"client_version"`
LastSeen sql.NullTime `db:"last_seen" json:"last_seen"`
LastSubmit sql.NullTime `db:"last_submit" json:"last_submit"`
CreatedOn time.Time `db:"created_on" json:"created_on"`
}
type Server struct { type Server struct {
ID uint32 `db:"id" json:"id"` ID uint32 `db:"id" json:"id"`
Ip string `db:"ip" json:"ip"` Ip string `db:"ip" json:"ip"`

23
ntpdb/monitor.go Normal file
View File

@@ -0,0 +1,23 @@
package ntpdb
import (
"strconv"
"strings"
)
func (m *Monitor) DisplayName() string {
switch {
case len(m.Name) > 0:
return m.Name
case m.TlsName.Valid && len(m.TlsName.String) > 0:
name := m.TlsName.String
if idx := strings.Index(name, "."); idx > 0 {
name = name[0:idx]
}
return name
case len(m.Location) > 0:
return m.Location + " (" + strconv.Itoa(int(m.ID)) + ")" // todo: IDToken instead of ID
default:
return strconv.Itoa(int(m.ID)) // todo: IDToken
}
}

View File

@@ -8,6 +8,7 @@ package ntpdb
import ( import (
"context" "context"
"database/sql"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
@@ -78,6 +79,52 @@ func (_d QuerierTxWithTracing) Commit(ctx context.Context) (err error) {
return _d.QuerierTx.Commit(ctx) return _d.QuerierTx.Commit(ctx)
} }
// GetMonitorByName implements QuerierTx
func (_d QuerierTxWithTracing) GetMonitorByName(ctx context.Context, tlsName sql.NullString) (m1 Monitor, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetMonitorByName")
defer func() {
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
"tlsName": tlsName}, map[string]interface{}{
"m1": m1,
"err": err})
} else if err != nil {
_span.RecordError(err)
_span.SetAttributes(
attribute.String("event", "error"),
attribute.String("message", err.Error()),
)
}
_span.End()
}()
return _d.QuerierTx.GetMonitorByName(ctx, tlsName)
}
// GetMonitorsByID implements QuerierTx
func (_d QuerierTxWithTracing) GetMonitorsByID(ctx context.Context, monitorids []uint32) (ma1 []Monitor, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetMonitorsByID")
defer func() {
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
"monitorids": monitorids}, map[string]interface{}{
"ma1": ma1,
"err": err})
} else if err != nil {
_span.RecordError(err)
_span.SetAttributes(
attribute.String("event", "error"),
attribute.String("message", err.Error()),
)
}
_span.End()
}()
return _d.QuerierTx.GetMonitorsByID(ctx, monitorids)
}
// GetServerByID implements QuerierTx // GetServerByID implements QuerierTx
func (_d QuerierTxWithTracing) GetServerByID(ctx context.Context, id uint32) (s1 Server, err error) { func (_d QuerierTxWithTracing) GetServerByID(ctx context.Context, id uint32) (s1 Server, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetServerByID") ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetServerByID")
@@ -124,6 +171,52 @@ func (_d QuerierTxWithTracing) GetServerByIP(ctx context.Context, ip string) (s1
return _d.QuerierTx.GetServerByIP(ctx, ip) return _d.QuerierTx.GetServerByIP(ctx, ip)
} }
// GetServerLogScores implements QuerierTx
func (_d QuerierTxWithTracing) GetServerLogScores(ctx context.Context, arg GetServerLogScoresParams) (la1 []LogScore, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetServerLogScores")
defer func() {
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
"arg": arg}, map[string]interface{}{
"la1": la1,
"err": err})
} else if err != nil {
_span.RecordError(err)
_span.SetAttributes(
attribute.String("event", "error"),
attribute.String("message", err.Error()),
)
}
_span.End()
}()
return _d.QuerierTx.GetServerLogScores(ctx, arg)
}
// GetServerLogScoresByMonitorID implements QuerierTx
func (_d QuerierTxWithTracing) GetServerLogScoresByMonitorID(ctx context.Context, arg GetServerLogScoresByMonitorIDParams) (la1 []LogScore, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetServerLogScoresByMonitorID")
defer func() {
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
"arg": arg}, map[string]interface{}{
"la1": la1,
"err": err})
} else if err != nil {
_span.RecordError(err)
_span.SetAttributes(
attribute.String("event", "error"),
attribute.String("message", err.Error()),
)
}
_span.End()
}()
return _d.QuerierTx.GetServerLogScoresByMonitorID(ctx, arg)
}
// GetServerNetspeed implements QuerierTx // GetServerNetspeed implements QuerierTx
func (_d QuerierTxWithTracing) GetServerNetspeed(ctx context.Context, ip string) (u1 uint32, err error) { func (_d QuerierTxWithTracing) GetServerNetspeed(ctx context.Context, ip string) (u1 uint32, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetServerNetspeed") ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetServerNetspeed")
@@ -147,6 +240,29 @@ func (_d QuerierTxWithTracing) GetServerNetspeed(ctx context.Context, ip string)
return _d.QuerierTx.GetServerNetspeed(ctx, ip) return _d.QuerierTx.GetServerNetspeed(ctx, ip)
} }
// GetServerScores implements QuerierTx
func (_d QuerierTxWithTracing) GetServerScores(ctx context.Context, arg GetServerScoresParams) (ga1 []GetServerScoresRow, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetServerScores")
defer func() {
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
"arg": arg}, map[string]interface{}{
"ga1": ga1,
"err": err})
} else if err != nil {
_span.RecordError(err)
_span.SetAttributes(
attribute.String("event", "error"),
attribute.String("message", err.Error()),
)
}
_span.End()
}()
return _d.QuerierTx.GetServerScores(ctx, arg)
}
// GetZoneStatsData implements QuerierTx // GetZoneStatsData implements QuerierTx
func (_d QuerierTxWithTracing) GetZoneStatsData(ctx context.Context) (ga1 []GetZoneStatsDataRow, err error) { func (_d QuerierTxWithTracing) GetZoneStatsData(ctx context.Context) (ga1 []GetZoneStatsDataRow, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetZoneStatsData") ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetZoneStatsData")

View File

@@ -6,12 +6,18 @@ package ntpdb
import ( import (
"context" "context"
"database/sql"
) )
type Querier interface { type Querier interface {
GetMonitorByName(ctx context.Context, tlsName sql.NullString) (Monitor, error)
GetMonitorsByID(ctx context.Context, monitorids []uint32) ([]Monitor, error)
GetServerByID(ctx context.Context, id uint32) (Server, error) GetServerByID(ctx context.Context, id uint32) (Server, error)
GetServerByIP(ctx context.Context, ip string) (Server, error) GetServerByIP(ctx context.Context, ip string) (Server, error)
GetServerLogScores(ctx context.Context, arg GetServerLogScoresParams) ([]LogScore, error)
GetServerLogScoresByMonitorID(ctx context.Context, arg GetServerLogScoresByMonitorIDParams) ([]LogScore, error)
GetServerNetspeed(ctx context.Context, ip string) (uint32, error) GetServerNetspeed(ctx context.Context, ip string) (uint32, error)
GetServerScores(ctx context.Context, arg GetServerScoresParams) ([]GetServerScoresRow, error)
GetZoneStatsData(ctx context.Context) ([]GetZoneStatsDataRow, error) GetZoneStatsData(ctx context.Context) ([]GetZoneStatsDataRow, error)
GetZoneStatsV2(ctx context.Context, ip string) ([]GetZoneStatsV2Row, error) GetZoneStatsV2(ctx context.Context, ip string) ([]GetZoneStatsV2Row, error)
} }

View File

@@ -7,9 +7,94 @@ package ntpdb
import ( import (
"context" "context"
"database/sql"
"strings"
"time" "time"
) )
const getMonitorByName = `-- name: GetMonitorByName :one
select id, type, user_id, account_id, name, location, ip, ip_version, tls_name, api_key, status, config, client_version, last_seen, last_submit, created_on from monitors where tls_name = ?
`
func (q *Queries) GetMonitorByName(ctx context.Context, tlsName sql.NullString) (Monitor, error) {
row := q.db.QueryRowContext(ctx, getMonitorByName, tlsName)
var i Monitor
err := row.Scan(
&i.ID,
&i.Type,
&i.UserID,
&i.AccountID,
&i.Name,
&i.Location,
&i.Ip,
&i.IpVersion,
&i.TlsName,
&i.ApiKey,
&i.Status,
&i.Config,
&i.ClientVersion,
&i.LastSeen,
&i.LastSubmit,
&i.CreatedOn,
)
return i, err
}
const getMonitorsByID = `-- name: GetMonitorsByID :many
select id, type, user_id, account_id, name, location, ip, ip_version, tls_name, api_key, status, config, client_version, last_seen, last_submit, created_on from monitors
where id in (/*SLICE:MonitorIDs*/?)
`
func (q *Queries) GetMonitorsByID(ctx context.Context, monitorids []uint32) ([]Monitor, error) {
query := getMonitorsByID
var queryParams []interface{}
if len(monitorids) > 0 {
for _, v := range monitorids {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:MonitorIDs*/?", strings.Repeat(",?", len(monitorids))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:MonitorIDs*/?", "NULL", 1)
}
rows, err := q.db.QueryContext(ctx, query, queryParams...)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Monitor
for rows.Next() {
var i Monitor
if err := rows.Scan(
&i.ID,
&i.Type,
&i.UserID,
&i.AccountID,
&i.Name,
&i.Location,
&i.Ip,
&i.IpVersion,
&i.TlsName,
&i.ApiKey,
&i.Status,
&i.Config,
&i.ClientVersion,
&i.LastSeen,
&i.LastSubmit,
&i.CreatedOn,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getServerByID = `-- name: GetServerByID :one const getServerByID = `-- name: GetServerByID :one
select id, ip, ip_version, user_id, account_id, hostname, stratum, in_pool, in_server_list, netspeed, created_on, updated_on, score_ts, score_raw, deletion_on from servers select id, ip, ip_version, user_id, account_id, hostname, stratum, in_pool, in_server_list, netspeed, created_on, updated_on, score_ts, score_raw, deletion_on from servers
where where
@@ -68,6 +153,100 @@ func (q *Queries) GetServerByIP(ctx context.Context, ip string) (Server, error)
return i, err return i, err
} }
const getServerLogScores = `-- name: GetServerLogScores :many
select id, monitor_id, server_id, ts, score, step, offset, rtt, attributes from log_scores
where
server_id = ?
order by ts desc
limit ?
`
type GetServerLogScoresParams struct {
ServerID uint32 `db:"server_id" json:"server_id"`
Limit int32 `db:"limit" json:"limit"`
}
func (q *Queries) GetServerLogScores(ctx context.Context, arg GetServerLogScoresParams) ([]LogScore, error) {
rows, err := q.db.QueryContext(ctx, getServerLogScores, arg.ServerID, arg.Limit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []LogScore
for rows.Next() {
var i LogScore
if err := rows.Scan(
&i.ID,
&i.MonitorID,
&i.ServerID,
&i.Ts,
&i.Score,
&i.Step,
&i.Offset,
&i.Rtt,
&i.Attributes,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getServerLogScoresByMonitorID = `-- name: GetServerLogScoresByMonitorID :many
select id, monitor_id, server_id, ts, score, step, offset, rtt, attributes from log_scores
where
server_id = ? AND
monitor_id = ?
order by ts desc
limit ?
`
type GetServerLogScoresByMonitorIDParams struct {
ServerID uint32 `db:"server_id" json:"server_id"`
MonitorID sql.NullInt32 `db:"monitor_id" json:"monitor_id"`
Limit int32 `db:"limit" json:"limit"`
}
func (q *Queries) GetServerLogScoresByMonitorID(ctx context.Context, arg GetServerLogScoresByMonitorIDParams) ([]LogScore, error) {
rows, err := q.db.QueryContext(ctx, getServerLogScoresByMonitorID, arg.ServerID, arg.MonitorID, arg.Limit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []LogScore
for rows.Next() {
var i LogScore
if err := rows.Scan(
&i.ID,
&i.MonitorID,
&i.ServerID,
&i.Ts,
&i.Score,
&i.Step,
&i.Offset,
&i.Rtt,
&i.Attributes,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getServerNetspeed = `-- name: GetServerNetspeed :one const getServerNetspeed = `-- name: GetServerNetspeed :one
select netspeed from servers where ip = ? select netspeed from servers where ip = ?
` `
@@ -79,6 +258,77 @@ func (q *Queries) GetServerNetspeed(ctx context.Context, ip string) (uint32, err
return netspeed, err return netspeed, err
} }
const getServerScores = `-- name: GetServerScores :many
select
m.id, m.name, m.tls_name, m.location, m.type,
ss.score_raw, ss.score_ts, ss.status
from server_scores ss
inner join monitors m
on (m.id=ss.monitor_id)
where
server_id = ? AND
monitor_id in (/*SLICE:MonitorIDs*/?)
`
type GetServerScoresParams struct {
ServerID uint32 `db:"server_id" json:"server_id"`
MonitorIDs []uint32 `db:"MonitorIDs" json:"MonitorIDs"`
}
type GetServerScoresRow struct {
ID uint32 `db:"id" json:"id"`
Name string `db:"name" json:"name"`
TlsName sql.NullString `db:"tls_name" json:"tls_name"`
Location string `db:"location" json:"location"`
Type MonitorsType `db:"type" json:"type"`
ScoreRaw float64 `db:"score_raw" json:"score_raw"`
ScoreTs sql.NullTime `db:"score_ts" json:"score_ts"`
Status ServerScoresStatus `db:"status" json:"status"`
}
func (q *Queries) GetServerScores(ctx context.Context, arg GetServerScoresParams) ([]GetServerScoresRow, error) {
query := getServerScores
var queryParams []interface{}
queryParams = append(queryParams, arg.ServerID)
if len(arg.MonitorIDs) > 0 {
for _, v := range arg.MonitorIDs {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:MonitorIDs*/?", strings.Repeat(",?", len(arg.MonitorIDs))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:MonitorIDs*/?", "NULL", 1)
}
rows, err := q.db.QueryContext(ctx, query, queryParams...)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetServerScoresRow
for rows.Next() {
var i GetServerScoresRow
if err := rows.Scan(
&i.ID,
&i.Name,
&i.TlsName,
&i.Location,
&i.Type,
&i.ScoreRaw,
&i.ScoreTs,
&i.Status,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getZoneStatsData = `-- name: GetZoneStatsData :many const getZoneStatsData = `-- name: GetZoneStatsData :many
SELECT zc.date, z.name, zc.ip_version, count_active, count_registered, netspeed_active SELECT zc.date, z.name, zc.ip_version, count_active, count_registered, netspeed_active
FROM zone_server_counts zc USE INDEX (date_idx) FROM zone_server_counts zc USE INDEX (date_idx)

View File

@@ -42,8 +42,40 @@ select * from servers
where where
id = ?; id = ?;
-- name: GetServerByIP :one -- name: GetServerByIP :one
select * from servers select * from servers
where where
ip = sqlc.arg(ip); ip = sqlc.arg(ip);
-- name: GetMonitorByName :one
select * from monitors where tls_name = ?;
-- name: GetMonitorsByID :many
select * from monitors
where id in (sqlc.slice('MonitorIDs'));
-- name: GetServerScores :many
select
m.id, m.name, m.tls_name, m.location, m.type,
ss.score_raw, ss.score_ts, ss.status
from server_scores ss
inner join monitors m
on (m.id=ss.monitor_id)
where
server_id = ? AND
monitor_id in (sqlc.slice('MonitorIDs'));
-- name: GetServerLogScores :many
select * from log_scores
where
server_id = ?
order by ts desc
limit ?;
-- name: GetServerLogScoresByMonitorID :many
select * from log_scores
where
server_id = ? AND
monitor_id = ?
order by ts desc
limit ?;

View File

@@ -87,7 +87,7 @@ func (srv *Server) dnsAnswers(c echo.Context) error {
queryGroup.Go(func() error { queryGroup.Go(func() error {
var err error var err error
serverData, err = srv.ch.ServerAnswerCounts(c.Request().Context(), ip.String(), days) serverData, err = srv.ch.ServerAnswerCounts(ctx, ip.String(), days)
if err != nil { if err != nil {
log.Error("ServerUserCCData", "err", err) log.Error("ServerUserCCData", "err", err)
return err return err
@@ -105,7 +105,7 @@ func (srv *Server) dnsAnswers(c echo.Context) error {
qtype = "AAAA" qtype = "AAAA"
} }
totalData, err = srv.ch.AnswerTotals(c.Request().Context(), qtype, days) totalData, err = srv.ch.AnswerTotals(ctx, qtype, days)
if err != nil { if err != nil {
log.Error("AnswerTotals", "err", err) log.Error("AnswerTotals", "err", err)
} }

46
server/functions.go Normal file
View File

@@ -0,0 +1,46 @@
package server
import (
"context"
"database/sql"
"errors"
"net/netip"
"strconv"
"time"
"go.ntppool.org/common/logger"
"go.ntppool.org/common/tracing"
"go.ntppool.org/data-api/ntpdb"
)
func (srv *Server) FindServer(ctx context.Context, serverID string) (ntpdb.Server, error) {
log := logger.Setup()
ctx, span := tracing.Tracer().Start(ctx, "FindServer")
defer span.End()
q := ntpdb.NewWrappedQuerier(ntpdb.New(srv.db))
var serverData ntpdb.Server
var dberr error
if id, err := strconv.Atoi(serverID); id > 0 && err == nil {
serverData, dberr = q.GetServerByID(ctx, uint32(id))
} else {
ip, err := netip.ParseAddr(serverID)
if err != nil || !ip.IsValid() {
return ntpdb.Server{}, nil // 404 error
}
serverData, dberr = q.GetServerByIP(ctx, ip.String())
}
if dberr != nil {
if !errors.Is(dberr, sql.ErrNoRows) {
log.Error("could not query server id", "err", dberr)
return serverData, dberr
}
}
if serverData.ID == 0 || (serverData.DeletionOn.Valid && serverData.DeletionOn.Time.Before(time.Now().Add(-1*time.Hour*24*30*24))) {
// no data and no error to produce 404 errors
return ntpdb.Server{}, nil
}
return serverData, nil
}

View File

@@ -2,16 +2,12 @@ package server
import ( import (
"context" "context"
"database/sql"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/http/httptrace" "net/http/httptrace"
"net/url" "net/url"
"os" "os"
"strconv"
"time"
"github.com/hashicorp/go-retryablehttp" "github.com/hashicorp/go-retryablehttp"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@@ -22,8 +18,6 @@ import (
"go.ntppool.org/common/logger" "go.ntppool.org/common/logger"
"go.ntppool.org/common/tracing" "go.ntppool.org/common/tracing"
"go.ntppool.org/data-api/ntpdb"
) )
func (srv *Server) graphImage(c echo.Context) error { func (srv *Server) graphImage(c echo.Context) error {
@@ -51,24 +45,8 @@ func (srv *Server) graphImage(c echo.Context) error {
return c.Redirect(308, redirectURL.String()) return c.Redirect(308, redirectURL.String())
} }
q := ntpdb.NewWrappedQuerier(ntpdb.New(srv.db)) serverData, err := srv.FindServer(ctx, serverID)
if serverData.ID == 0 {
var serverData ntpdb.Server
var dberr error
if id, err := strconv.Atoi(serverID); id > 0 && err == nil {
serverData, dberr = q.GetServerByID(ctx, uint32(id))
} else {
serverData, dberr = q.GetServerByIP(ctx, serverID)
}
if dberr != nil {
if !errors.Is(dberr, sql.ErrNoRows) {
log.Error("could not query server id", "err", dberr)
return c.String(http.StatusInternalServerError, "server error")
}
return c.String(http.StatusNotFound, "not found")
}
if serverData.ID == 0 || (serverData.DeletionOn.Valid && serverData.DeletionOn.Time.Before(time.Now())) {
return c.String(http.StatusNotFound, "not found") return c.String(http.StatusNotFound, "not found")
} }

302
server/history.go Normal file
View File

@@ -0,0 +1,302 @@
package server
import (
"bytes"
"context"
"database/sql"
"encoding/csv"
"fmt"
"math"
"net/http"
"strconv"
"strings"
"time"
"github.com/labstack/echo/v4"
"go.ntppool.org/common/logger"
"go.ntppool.org/common/tracing"
"go.ntppool.org/data-api/logscores"
"go.ntppool.org/data-api/ntpdb"
)
type historyMode uint8
const (
historyModeUnknown historyMode = iota
historyModeLog
historyModeJSON
historyModeMonitor
)
func paramHistoryMode(s string) historyMode {
switch s {
case "log":
return historyModeLog
case "json":
return historyModeJSON
case "monitor":
return historyModeMonitor
default:
return historyModeUnknown
}
}
func (srv *Server) getHistory(ctx context.Context, c echo.Context, server ntpdb.Server) (*logscores.LogScoreHistory, error) {
log := logger.Setup()
limit := 0
if limitParam, err := strconv.Atoi(c.QueryParam("limit")); err == nil {
limit = limitParam
} else {
limit = 50
}
if limit > 4000 {
limit = 4000
}
since, _ := strconv.ParseInt(c.QueryParam("since"), 10, 64) // defaults to 0 so don't care if it parses
monitorParam := c.QueryParam("monitor")
if since > 0 {
c.Request().Header.Set("Cache-Control", "s-maxage=300")
}
q := ntpdb.NewWrappedQuerier(ntpdb.New(srv.db))
var monitorID uint32 = 0
switch monitorParam {
case "":
name := "recentmedian.scores.ntp.dev"
monitor, err := q.GetMonitorByName(ctx, sql.NullString{Valid: true, String: name})
if err != nil {
log.Warn("could not find monitor", "name", name, "err", err)
}
monitorID = monitor.ID
case "*":
monitorID = 0 // don't filter on monitor ID
default:
mID, err := strconv.ParseUint(monitorParam, 10, 32)
if err != nil {
log.InfoContext(ctx, "invalid monitor parameter", "monitor", monitorParam)
return nil, nil
}
monitorID = uint32(mID)
}
log.Info("monitor param", "monitor", monitorID)
sinceTime := time.Unix(since, 0)
if since > 0 {
log.Warn("monitor data requested with since parameter, not supported", "since", sinceTime)
}
ls, err := logscores.GetHistory(ctx, srv.db, server.ID, monitorID, sinceTime, limit)
return ls, err
}
func (srv *Server) history(c echo.Context) error {
log := logger.Setup()
ctx, span := tracing.Tracer().Start(c.Request().Context(), "history")
defer span.End()
// just cache for a short time by default
c.Response().Header().Set("Cache-Control", "public,max-age=240")
mode := paramHistoryMode(c.Param("mode"))
if mode == historyModeUnknown {
return c.String(http.StatusNotFound, "invalid mode")
}
server, err := srv.FindServer(ctx, c.Param("server"))
if err != nil {
log.Error("find server", "err", err)
span.RecordError(err)
return c.String(http.StatusInternalServerError, "internal error")
}
if server.ID == 0 {
span.AddEvent("server not found")
return c.String(http.StatusNotFound, "server not found")
}
history, err := srv.getHistory(ctx, c, server)
if err != nil {
log.Error("get history", "err", err)
span.RecordError(err)
return c.String(http.StatusInternalServerError, "internal error")
}
c.Response().Header().Set("Access-Control-Allow-Origin", "*")
switch mode {
case historyModeLog:
return srv.historyCSV(ctx, c, history)
case historyModeJSON:
return srv.historyJSON(ctx, c, server, history)
default:
return c.String(http.StatusNotFound, "not implemented")
}
}
func (srv *Server) historyJSON(ctx context.Context, c echo.Context, server ntpdb.Server, history *logscores.LogScoreHistory) error {
log := logger.Setup()
ctx, span := tracing.Tracer().Start(ctx, "history.json")
defer span.End()
type ScoresEntry struct {
TS int64 `json:"ts"`
Offset *float64 `json:"offset,omitempty"`
Step float64 `json:"step"`
Score float64 `json:"score"`
MonitorID int `json:"monitor_id"`
}
type MonitorEntry struct {
ID uint32 `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Ts string `json:"ts"`
Score float64 `json:"score"`
Status string `json:"status"`
}
res := struct {
History []ScoresEntry `json:"history"`
Monitors []MonitorEntry `json:"monitors"`
Server struct {
IP string `json:"ip"`
} `json:"server"`
}{
History: make([]ScoresEntry, len(history.LogScores)),
}
res.Server.IP = server.Ip
// log.InfoContext(ctx, "monitor id list", "ids", history.MonitorIDs)
q := ntpdb.NewWrappedQuerier(ntpdb.New(srv.db))
logScoreMonitors, err := q.GetServerScores(ctx,
ntpdb.GetServerScoresParams{
MonitorIDs: history.MonitorIDs,
ServerID: server.ID,
},
)
if err != nil {
span.RecordError(err)
log.ErrorContext(ctx, "GetServerScores", "err", err)
return c.String(http.StatusInternalServerError, "err")
}
// log.InfoContext(ctx, "got logScoreMonitors", "count", len(logScoreMonitors))
for _, lsm := range logScoreMonitors {
score := math.Round(lsm.ScoreRaw*10) / 10 // round to one decimal
tempMon := ntpdb.Monitor{
Name: lsm.Name,
TlsName: lsm.TlsName,
Location: lsm.Location,
ID: lsm.ID,
}
name := tempMon.DisplayName()
me := MonitorEntry{
ID: lsm.ID,
Name: name,
Type: string(lsm.Type),
Ts: lsm.ScoreTs.Time.Format(time.RFC3339),
Score: score,
Status: string(lsm.Status),
}
res.Monitors = append(res.Monitors, me)
}
for i, ls := range history.LogScores {
x := float64(1000000000000)
score := math.Round(ls.Score*x) / x
res.History[i] = ScoresEntry{
TS: ls.Ts.Unix(),
MonitorID: int(ls.MonitorID.Int32),
Step: ls.Step,
Score: score,
}
if ls.Offset.Valid {
offset := ls.Offset.Float64
res.History[i].Offset = &offset
}
}
if history.LogScores[len(history.LogScores)-1].Ts.After(time.Now().Add(-8 * time.Hour)) {
// cache for longer if data hasn't updated for a while
c.Request().Header.Set("Cache-Control", "s-maxage=3600,max-age=1800")
} else {
c.Request().Header.Set("Cache-Control", "s-maxage=300,max-age=240")
}
return c.JSON(http.StatusOK, res)
}
func (srv *Server) historyCSV(ctx context.Context, c echo.Context, history *logscores.LogScoreHistory) error {
log := logger.Setup()
ctx, span := tracing.Tracer().Start(ctx, "history.csv")
b := bytes.NewBuffer([]byte{})
w := csv.NewWriter(b)
ff := func(f float64) string {
s := fmt.Sprintf("%.9f", f)
s = strings.TrimRight(s, "0")
s = strings.TrimRight(s, ".")
return s
}
w.Write([]string{"ts_epoch", "ts", "offset", "step", "score", "monitor_id", "monitor_name", "leap", "error"})
for _, l := range history.LogScores {
// log.Debug("csv line", "id", l.ID, "n", i)
var offset string
if l.Offset.Valid {
offset = ff(l.Offset.Float64)
}
step := ff(l.Step)
score := ff(l.Score)
var monName string
if l.MonitorID.Valid {
monName = history.Monitors[int(l.MonitorID.Int32)]
}
var leap string
if l.Attributes.Leap != 0 {
leap = fmt.Sprintf("%d", l.Attributes.Leap)
}
err := w.Write([]string{
strconv.Itoa(int(l.Ts.Unix())),
// l.Ts.Format(time.RFC3339),
l.Ts.Format("2006-01-02 15:04:05"),
offset,
step,
score,
fmt.Sprintf("%d", l.MonitorID.Int32),
monName,
leap,
l.Attributes.Error,
})
if err != nil {
log.Warn("csv encoding error", "ls_id", l.ID, "err", err)
}
}
w.Flush()
if err := w.Error(); err != nil {
log.ErrorContext(ctx, "could not flush csv", "err", err)
span.End()
return c.String(http.StatusInternalServerError, "csv error")
}
// log.Info("entries", "count", len(history.LogScores), "out_bytes", b.Len())
c.Request().Header.Set("Cache-Control", "s-maxage=120,max-age=120")
return c.Blob(http.StatusOK, "text/csv", b.Bytes())
}

View File

@@ -137,6 +137,12 @@ func (srv *Server) Run() error {
span := trace.SpanFromContext(request.Context()) span := trace.SpanFromContext(request.Context())
span.SetAttributes(attribute.String("http.real_ip", c.RealIP())) span.SetAttributes(attribute.String("http.real_ip", c.RealIP()))
// since the Go library (temporarily?) isn't including this
span.SetAttributes(attribute.String("url.path", c.Path()))
if q := c.QueryString(); len(q) > 0 {
span.SetAttributes(attribute.String("url.query", q))
}
c.Response().Header().Set("Traceparent", span.SpanContext().TraceID().String()) c.Response().Header().Set("Traceparent", span.SpanContext().TraceID().String())
return next(c) return next(c)
@@ -182,6 +188,7 @@ func (srv *Server) Run() error {
e.GET("/api/usercc", srv.userCountryData) e.GET("/api/usercc", srv.userCountryData)
e.GET("/api/server/dns/answers/:server", srv.dnsAnswers) e.GET("/api/server/dns/answers/:server", srv.dnsAnswers)
e.GET("/api/server/scores/:server/:mode", srv.history)
e.GET("/graph/:server/:type", srv.graphImage) e.GET("/graph/:server/:type", srv.graphImage)
// e.GET("/api/server/scores/:server/:type", srv.logScores) // e.GET("/api/server/scores/:server/:type", srv.logScores)

View File

@@ -15,5 +15,7 @@ sql:
rename: rename:
servers.Ip: IP servers.Ip: IP
overrides: overrides:
- column: log_scores.attributes
go_type: go.ntppool.org/common/types.LogScoreAttributes
- column: "server_netspeed.netspeed_active" - column: "server_netspeed.netspeed_active"
go_type: "uint64" go_type: "uint64"