Private
Public Access
1
0

scores: csv handler

This commit is contained in:
2023-12-10 21:02:04 -08:00
parent adab600e26
commit 61245cc77c
12 changed files with 750 additions and 2 deletions

View File

@@ -9,8 +9,139 @@ import (
"database/sql/driver"
"fmt"
"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 ServersIpVersion string
const (
@@ -95,6 +226,37 @@ func (ns NullZoneServerCountsIpVersion) Value() (driver.Value, error) {
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 {
ID uint32 `db:"id" json:"id"`
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 (
"context"
"database/sql"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
@@ -78,6 +79,52 @@ func (_d QuerierTxWithTracing) Commit(ctx context.Context) (err error) {
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, ids []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,
"ids": ids}, 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, ids)
}
// GetServerByID implements QuerierTx
func (_d QuerierTxWithTracing) GetServerByID(ctx context.Context, id uint32) (s1 Server, err error) {
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)
}
// 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
func (_d QuerierTxWithTracing) GetServerNetspeed(ctx context.Context, ip string) (u1 uint32, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetServerNetspeed")

View File

@@ -6,11 +6,16 @@ package ntpdb
import (
"context"
"database/sql"
)
type Querier interface {
GetMonitorByName(ctx context.Context, tlsName sql.NullString) (Monitor, error)
GetMonitorsByID(ctx context.Context, ids []uint32) ([]Monitor, error)
GetServerByID(ctx context.Context, id uint32) (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)
GetZoneStatsData(ctx context.Context) ([]GetZoneStatsDataRow, error)
GetZoneStatsV2(ctx context.Context, ip string) ([]GetZoneStatsV2Row, error)

View File

@@ -7,9 +7,94 @@ package ntpdb
import (
"context"
"database/sql"
"strings"
"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:ids*/?)
`
func (q *Queries) GetMonitorsByID(ctx context.Context, ids []uint32) ([]Monitor, error) {
query := getMonitorsByID
var queryParams []interface{}
if len(ids) > 0 {
for _, v := range ids {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:ids*/?", strings.Repeat(",?", len(ids))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:ids*/?", "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
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
@@ -68,6 +153,100 @@ func (q *Queries) GetServerByIP(ctx context.Context, ip string) (Server, error)
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
select netspeed from servers where ip = ?
`