Private
Public Access
1
0

18 Commits

Author SHA1 Message Date
47b96cd598 zones: per zone server counts API migrated
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-23 01:54:21 -08:00
19c02063e9 Make text/csv inline in the browser
All checks were successful
continuous-integration/drone/push Build is passing
... by using text/plain
https://bugs.chromium.org/p/chromium/issues/detail?id=152911
2023-12-22 09:33:14 -08:00
84523661e2 scores: Allow 10000 rows 2023-12-22 09:22:26 -08:00
6553b4711b scores: allow specifying the monitor by name
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-22 08:59:13 -08:00
9280668d28 scores: support requesting logs by monitor name 2023-12-22 08:15:13 -08:00
ccc2fd401f scores: better error when monitor parameter is invalid 2023-12-22 08:11:43 -08:00
f2e4530023 Update dependencies 2023-12-22 08:10:42 -08:00
8f333354d2 Register build_info metric 2023-12-15 00:10:39 -08:00
41e7585637 scores: redirect POST requests
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 23:57:36 -08:00
36f695c146 trace tweaks
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 23:17:44 -08:00
404c64b910 Return not found for more recently deleted servers, too 2023-12-14 23:17:44 -08:00
2cd4d8a35a Fix url.path in trace 2023-12-14 23:14:00 -08:00
bae726dba6 scores: handle IPs with no current history
All checks were successful
continuous-integration/drone/push Build is passing
2023-12-14 20:00:09 -08:00
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
17 changed files with 1482 additions and 37 deletions

26
go.mod
View File

@@ -5,15 +5,15 @@ go 1.21.3
// replace github.com/samber/slog-echo => github.com/abh/slog-echo v0.0.0-20231024051244-af740639893e // replace github.com/samber/slog-echo => github.com/abh/slog-echo v0.0.0-20231024051244-af740639893e
require ( require (
github.com/ClickHouse/clickhouse-go/v2 v2.16.0 github.com/ClickHouse/clickhouse-go/v2 v2.17.0
github.com/go-sql-driver/mysql v1.7.1 github.com/go-sql-driver/mysql v1.7.1
github.com/hashicorp/go-retryablehttp v0.7.5 github.com/hashicorp/go-retryablehttp v0.7.5
github.com/labstack/echo/v4 v4.11.3 github.com/labstack/echo/v4 v4.11.4
github.com/samber/slog-echo v1.9.0 github.com/samber/slog-echo v1.9.1
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.20231211044338-5c7ae6ab8ac9
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
@@ -32,17 +32,17 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-faster/city v1.0.1 // indirect github.com/go-faster/city v1.0.1 // indirect
github.com/go-faster/errors v0.7.0 // indirect github.com/go-faster/errors v0.7.1 // indirect
github.com/go-logr/logr v1.3.0 // indirect github.com/go-logr/logr v1.4.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/protobuf v1.5.3 // indirect github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.4.0 // indirect github.com/google/uuid v1.5.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.17.4 // indirect github.com/klauspost/compress v1.17.4 // indirect
github.com/labstack/gommon v0.4.1 // indirect github.com/labstack/gommon v0.4.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
@@ -67,15 +67,15 @@ require (
go.opentelemetry.io/otel/metric v1.21.0 // indirect go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/sdk v1.21.0 // indirect go.opentelemetry.io/otel/sdk v1.21.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect go.opentelemetry.io/proto/otlp v1.0.0 // indirect
golang.org/x/crypto v0.16.0 // indirect golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb // indirect golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 // indirect
golang.org/x/mod v0.14.0 // indirect golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/grpc v1.59.0 // indirect google.golang.org/grpc v1.60.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect google.golang.org/protobuf v1.31.0 // indirect
) )

32
go.sum
View File

@@ -3,6 +3,8 @@ github.com/ClickHouse/ch-go v0.61.0/go.mod h1:POJBl0MxEMS91Zd0uTgDDt05KfXEjf5KIw
github.com/ClickHouse/clickhouse-go v1.5.4 h1:cKjXeYLNWVJIx2J1K6H2CqyRmfwVJVY1OV1coaaFcI0= github.com/ClickHouse/clickhouse-go v1.5.4 h1:cKjXeYLNWVJIx2J1K6H2CqyRmfwVJVY1OV1coaaFcI0=
github.com/ClickHouse/clickhouse-go/v2 v2.16.0 h1:rhMfnPewXPnY4Q4lQRGdYuTLRBRKJEIEYHtbUMrzmvI= github.com/ClickHouse/clickhouse-go/v2 v2.16.0 h1:rhMfnPewXPnY4Q4lQRGdYuTLRBRKJEIEYHtbUMrzmvI=
github.com/ClickHouse/clickhouse-go/v2 v2.16.0/go.mod h1:J7SPfIxwR+x4mQ+o8MLSe0oY50NNntEqCIjFe/T1VPM= github.com/ClickHouse/clickhouse-go/v2 v2.16.0/go.mod h1:J7SPfIxwR+x4mQ+o8MLSe0oY50NNntEqCIjFe/T1VPM=
github.com/ClickHouse/clickhouse-go/v2 v2.17.0 h1:xvsVYxOWb2obaIwL9NJZSZ3T/umJSh9P1gf1dfMFlI8=
github.com/ClickHouse/clickhouse-go/v2 v2.17.0/go.mod h1:rkGTvFDTLqLIm0ma+13xmcCfr/08Gvs7KmFt1tgiWHQ=
github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI= github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
@@ -21,9 +23,13 @@ github.com/go-faster/city v1.0.1 h1:4WAxSZ3V2Ws4QRDrscLEDcibJY8uf41H6AhXDrNDcGw=
github.com/go-faster/city v1.0.1/go.mod h1:jKcUJId49qdW3L1qKHH/3wPeUstCVpVSXTM6vO3VcTw= github.com/go-faster/city v1.0.1/go.mod h1:jKcUJId49qdW3L1qKHH/3wPeUstCVpVSXTM6vO3VcTw=
github.com/go-faster/errors v0.7.0 h1:UnD/xusnfUgtEYkgRZohqL2AfmPTwv13NAJwwFFaNYc= github.com/go-faster/errors v0.7.0 h1:UnD/xusnfUgtEYkgRZohqL2AfmPTwv13NAJwwFFaNYc=
github.com/go-faster/errors v0.7.0/go.mod h1:5ySTjWFiphBs07IKuiL69nxdfd5+fzh1u7FPGZP2quo= github.com/go-faster/errors v0.7.0/go.mod h1:5ySTjWFiphBs07IKuiL69nxdfd5+fzh1u7FPGZP2quo=
github.com/go-faster/errors v0.7.1 h1:MkJTnDoEdi9pDabt1dpWf7AA8/BaSYZqibYyhZ20AYg=
github.com/go-faster/errors v0.7.1/go.mod h1:5ySTjWFiphBs07IKuiL69nxdfd5+fzh1u7FPGZP2quo=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.4.0 h1:wx+BduGRXjIL6VPeeb7DRX+ii7sR/ch8DlRifHR589o=
github.com/go-logr/logr v1.4.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
@@ -41,6 +47,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 h1:6UKoz5ujsI55KNpsJH3UwCq3T8kKbZwNZBNPuTTje8U= github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 h1:6UKoz5ujsI55KNpsJH3UwCq3T8kKbZwNZBNPuTTje8U=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1/go.mod h1:YvJ2f6MplWDhfxiUC3KpyTy76kYUZA4W3pTv/wdKQ9Y= github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1/go.mod h1:YvJ2f6MplWDhfxiUC3KpyTy76kYUZA4W3pTv/wdKQ9Y=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
@@ -64,8 +72,12 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/labstack/echo/v4 v4.11.3 h1:Upyu3olaqSHkCjs1EJJwQ3WId8b8b1hxbogyommKktM= github.com/labstack/echo/v4 v4.11.3 h1:Upyu3olaqSHkCjs1EJJwQ3WId8b8b1hxbogyommKktM=
github.com/labstack/echo/v4 v4.11.3/go.mod h1:UcGuQ8V6ZNRmSweBIJkPvGfwCMIlFmiqrPqiEBfPYws= github.com/labstack/echo/v4 v4.11.3/go.mod h1:UcGuQ8V6ZNRmSweBIJkPvGfwCMIlFmiqrPqiEBfPYws=
github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8=
github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8=
github.com/labstack/gommon v0.4.1 h1:gqEff0p/hTENGMABzezPoPSRtIh1Cvw0ueMOe0/dfOk= github.com/labstack/gommon v0.4.1 h1:gqEff0p/hTENGMABzezPoPSRtIh1Cvw0ueMOe0/dfOk=
github.com/labstack/gommon v0.4.1/go.mod h1:TyTrpPqxR5KMk8LKVtLmfMjeQ5FEkBYdxLYPw/WfrOM= github.com/labstack/gommon v0.4.1/go.mod h1:TyTrpPqxR5KMk8LKVtLmfMjeQ5FEkBYdxLYPw/WfrOM=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
@@ -103,6 +115,8 @@ github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/samber/slog-echo v1.9.0 h1:1Cvn2/JXAM2ki26uBMbbk0jB9UxZgaE9xM1DVZvLAlo= github.com/samber/slog-echo v1.9.0 h1:1Cvn2/JXAM2ki26uBMbbk0jB9UxZgaE9xM1DVZvLAlo=
github.com/samber/slog-echo v1.9.0/go.mod h1:0ab2AwcciQXNAXEcjkHwD9okOh9vEHEYn8xP97ocuhM= github.com/samber/slog-echo v1.9.0/go.mod h1:0ab2AwcciQXNAXEcjkHwD9okOh9vEHEYn8xP97ocuhM=
github.com/samber/slog-echo v1.9.1 h1:QEzOuZtZiXe0/60bmfcVZdwYzq1T6SCBC4RiMfg9Riw=
github.com/samber/slog-echo v1.9.1/go.mod h1:/f78pHjVxGrIlHlS5fzWiW+BxkWltQ+SWKk8LKMjAMQ=
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys= github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs= github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
@@ -142,6 +156,10 @@ 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.ntppool.org/common v0.2.6-0.20231211044338-5c7ae6ab8ac9 h1:5NHaULU9+qu6hA/teqYSfPag4Mkozt0baIb3DP9Hcng=
go.ntppool.org/common v0.2.6-0.20231211044338-5c7ae6ab8ac9/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=
@@ -170,8 +188,14 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb h1:c0vyKkb6yr3KR7jEfJaOSv4lG7xPkbN6r52aJz1d8a8= golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb h1:c0vyKkb6yr3KR7jEfJaOSv4lG7xPkbN6r52aJz1d8a8=
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 h1:qCEDpW1G+vcj3Y7Fy52pEM1AWm3abj8WimGYejI3SC4=
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 h1:+iq7lrkxmFNBM7xx+Rae2W6uyPfhPeDWD+n+JgppptE=
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
@@ -220,10 +244,18 @@ google.golang.org/genproto v0.0.0-20231120223509-83a465c0220f h1:Vn+VyHU5guc9KjB
google.golang.org/genproto v0.0.0-20231120223509-83a465c0220f/go.mod h1:nWSwAFPb+qfNJXsoeO3Io7zf4tMSfN8EA8RlDA04GhY= google.golang.org/genproto v0.0.0-20231120223509-83a465c0220f/go.mod h1:nWSwAFPb+qfNJXsoeO3Io7zf4tMSfN8EA8RlDA04GhY=
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4 h1:ZcOkrmX74HbKFYnpPY8Qsw93fC29TbJXspYKaBkSXDQ= google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4 h1:ZcOkrmX74HbKFYnpPY8Qsw93fC29TbJXspYKaBkSXDQ=
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4/go.mod h1:k2dtGpRrbsSyKcNPKKI5sstZkrNCZwpU/ns96JoHbGg= google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4/go.mod h1:k2dtGpRrbsSyKcNPKKI5sstZkrNCZwpU/ns96JoHbGg=
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0 h1:s1w3X6gQxwrLEpxnLd/qXTVLgQE2yXwaOaoa6IlY/+o=
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0/go.mod h1:CAny0tYF+0/9rmDB9fahA9YLzX3+AEVl1qXbv5hhj6c=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 h1:DC7wcm+i+P1rN3Ff07vL+OndGg5OhNddHyTA+ocPqYE= google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 h1:DC7wcm+i+P1rN3Ff07vL+OndGg5OhNddHyTA+ocPqYE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4/go.mod h1:eJVxU6o+4G1PSczBr85xmyvSNYAKvAYgkub40YGomFM= google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4/go.mod h1:eJVxU6o+4G1PSczBr85xmyvSNYAKvAYgkub40YGomFM=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 h1:/jFB8jK5R3Sq3i/lmeZO0cATSzFfZaJq1J2Euan3XKU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0/go.mod h1:FUoWkonphQm3RhTS+kOEhF8h0iDpm4tdXolVCeZ9KKA=
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk= google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98= google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
google.golang.org/grpc v1.60.0 h1:6FQAR0kM31P6MRdeluor2w2gPaS4SVNrD/DNTxrQ15k=
google.golang.org/grpc v1.60.0/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM=
google.golang.org/grpc v1.60.1 h1:26+wFr+cNqSGFcOXcabYC0lUVJVRa2Sb2ortSK7VrEU=
google.golang.org/grpc v1.60.1/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=

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"`
@@ -112,3 +317,21 @@ type Server struct {
ScoreRaw float64 `db:"score_raw" json:"score_raw"` ScoreRaw float64 `db:"score_raw" json:"score_raw"`
DeletionOn sql.NullTime `db:"deletion_on" json:"deletion_on"` DeletionOn sql.NullTime `db:"deletion_on" json:"deletion_on"`
} }
type Zone struct {
ID uint32 `db:"id" json:"id"`
Name string `db:"name" json:"name"`
Description sql.NullString `db:"description" json:"description"`
ParentID sql.NullInt32 `db:"parent_id" json:"parent_id"`
Dns bool `db:"dns" json:"dns"`
}
type ZoneServerCount struct {
ID uint32 `db:"id" json:"id"`
ZoneID uint32 `db:"zone_id" json:"zone_id"`
IpVersion ZoneServerCountsIpVersion `db:"ip_version" json:"ip_version"`
Date time.Time `db:"date" json:"date"`
CountActive uint32 `db:"count_active" json:"count_active"`
CountRegistered uint32 `db:"count_registered" json:"count_registered"`
NetspeedActive uint32 `db:"netspeed_active" json:"netspeed_active"`
}

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,75 @@ 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)
}
// GetZoneByName implements QuerierTx
func (_d QuerierTxWithTracing) GetZoneByName(ctx context.Context, name string) (z1 Zone, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetZoneByName")
defer func() {
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
"name": name}, map[string]interface{}{
"z1": z1,
"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.GetZoneByName(ctx, name)
}
// GetZoneCounts implements QuerierTx
func (_d QuerierTxWithTracing) GetZoneCounts(ctx context.Context, zoneID uint32) (za1 []ZoneServerCount, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetZoneCounts")
defer func() {
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
"zoneID": zoneID}, map[string]interface{}{
"za1": za1,
"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.GetZoneCounts(ctx, zoneID)
}
// 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,20 @@ 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)
GetZoneByName(ctx context.Context, name string) (Zone, error)
GetZoneCounts(ctx context.Context, zoneID uint32) ([]ZoneServerCount, 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,98 @@ 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 like ?
order by id
limit 1
`
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 +157,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 +262,133 @@ 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 getZoneByName = `-- name: GetZoneByName :one
select id, name, description, parent_id, dns from zones
where
name = ?
`
func (q *Queries) GetZoneByName(ctx context.Context, name string) (Zone, error) {
row := q.db.QueryRowContext(ctx, getZoneByName, name)
var i Zone
err := row.Scan(
&i.ID,
&i.Name,
&i.Description,
&i.ParentID,
&i.Dns,
)
return i, err
}
const getZoneCounts = `-- name: GetZoneCounts :many
select id, zone_id, ip_version, date, count_active, count_registered, netspeed_active from zone_server_counts
where zone_id = ?
order by date
`
func (q *Queries) GetZoneCounts(ctx context.Context, zoneID uint32) ([]ZoneServerCount, error) {
rows, err := q.db.QueryContext(ctx, getZoneCounts, zoneID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ZoneServerCount
for rows.Next() {
var i ZoneServerCount
if err := rows.Scan(
&i.ID,
&i.ZoneID,
&i.IpVersion,
&i.Date,
&i.CountActive,
&i.CountRegistered,
&i.NetspeedActive,
); 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)

16
ntpdb/server.go Normal file
View File

@@ -0,0 +1,16 @@
package ntpdb
import "time"
func (s *Server) DeletionAge(dur time.Duration) bool {
if !s.DeletionOn.Valid {
return false
}
if dur > 0 {
dur = dur * -1
}
if s.DeletionOn.Time.Before(time.Now().Add(dur)) {
return true
}
return false
}

View File

@@ -42,8 +42,54 @@ 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 like sqlc.arg('tls_name')
order by id
limit 1;
-- 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 ?;
-- name: GetZoneByName :one
select * from zones
where
name = sqlc.arg(name);
-- name: GetZoneCounts :many
select * from zone_server_counts
where zone_id = ?
order by date;

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,15 +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" "time"
"github.com/hashicorp/go-retryablehttp" "github.com/hashicorp/go-retryablehttp"
@@ -22,8 +19,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 {
@@ -39,6 +34,8 @@ func (srv *Server) graphImage(c echo.Context) error {
log = log.With("serverID", serverID).With("type", imageType) log = log.With("serverID", serverID).With("type", imageType)
log.Info("graph parameters") log.Info("graph parameters")
span.SetAttributes(attribute.String("url.server_parameter", serverID))
if imageType != "offset.png" { if imageType != "offset.png" {
return c.String(http.StatusNotFound, "invalid image name") return c.String(http.StatusNotFound, "invalid image name")
} }
@@ -51,24 +48,15 @@ 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 err != nil {
var serverData ntpdb.Server span.RecordError(err)
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.StatusInternalServerError, "server error")
} }
if serverData.ID == 0 {
return c.String(http.StatusNotFound, "not found") return c.String(http.StatusNotFound, "not found")
} }
if serverData.DeletionAge(7 * 24 * time.Hour) {
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")
} }

333
server/history.go Normal file
View File

@@ -0,0 +1,333 @@
package server
import (
"bytes"
"context"
"database/sql"
"encoding/csv"
"errors"
"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 = 100
}
if limit > 10000 {
limit = 10000
}
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 {
monitorID = uint32(mID)
} else {
// only accept the name prefix; no wildcards; trust the database
// to filter out any other crazy
if strings.ContainsAny(monitorParam, "_%. \t\n") {
return nil, echo.NewHTTPError(http.StatusNotFound, "monitor not found")
}
if err != nil {
monitorParam = monitorParam + ".%"
monitor, err := q.GetMonitorByName(ctx, sql.NullString{Valid: true, String: monitorParam})
if err != nil {
log.Warn("could not find monitor", "name", monitorParam, "err", err)
return nil, echo.NewHTTPError(http.StatusNotFound, "monitor not found")
}
monitorID = monitor.ID
}
}
}
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 echo.NewHTTPError(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 echo.NewHTTPError(http.StatusInternalServerError, "internal error")
}
if server.DeletionAge(30 * 24 * time.Hour) {
span.AddEvent("server deleted")
return echo.NewHTTPError(http.StatusNotFound, "server not found")
}
if server.ID == 0 {
span.AddEvent("server not found")
return echo.NewHTTPError(http.StatusNotFound, "server not found")
}
history, err := srv.getHistory(ctx, c, server)
if err != nil {
var httpError *echo.HTTPError
if errors.As(err, &httpError) {
if httpError.Code >= 500 {
log.Error("get history", "err", err)
span.RecordError(err)
}
return httpError
} else {
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 len(history.LogScores) == 0 ||
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.Response().Header().Set("Cache-Control", "s-maxage=150,max-age=120")
c.Response().Header().Set("Content-Disposition", "inline")
// Chrome and Firefox force-download text/csv files, so use text/plain
// https://bugs.chromium.org/p/chromium/issues/detail?id=152911
return c.Blob(http.StatusOK, "text/plain", b.Bytes())
}

View File

@@ -83,12 +83,15 @@ func NewServer(ctx context.Context, configFile string) (*Server, error) {
func (srv *Server) Run() error { func (srv *Server) Run() error {
log := logger.Setup() log := logger.Setup()
ntpconf := config.New()
ctx, cancel := context.WithCancel(srv.ctx) ctx, cancel := context.WithCancel(srv.ctx)
defer cancel() defer cancel()
g, _ := errgroup.WithContext(ctx) g, _ := errgroup.WithContext(ctx)
g.Go(func() error { g.Go(func() error {
version.RegisterMetric("dataapi", srv.metrics.Registry())
return srv.metrics.ListenAndServe(ctx, 9020) return srv.metrics.ListenAndServe(ctx, 9020)
}) })
@@ -137,6 +140,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.Request().RequestURI))
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,9 +191,26 @@ 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)
if len(ntpconf.WebHostname()) > 0 {
e.POST("/api/server/scores/:server/:mode", func(c echo.Context) error {
// POST requests used to work, so make them not error out
mode := c.Param("mode")
server := c.Param("server")
query := c.Request().URL.Query()
return c.Redirect(
http.StatusSeeOther,
ntpconf.WebURL(
fmt.Sprintf("/scores/%s/%s", server, mode),
&query,
),
)
})
}
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/zone/counts/:zone_name", srv.zoneCounts)
g.Go(func() error { g.Go(func() error {
return e.Start(":8030") return e.Start(":8030")

148
server/zones.go Normal file
View File

@@ -0,0 +1,148 @@
package server
import (
"database/sql"
"errors"
"net/http"
"strconv"
"time"
"github.com/labstack/echo/v4"
"go.ntppool.org/common/logger"
"go.ntppool.org/common/tracing"
"go.ntppool.org/data-api/ntpdb"
)
func (srv *Server) zoneCounts(c echo.Context) error {
log := logger.Setup()
ctx, span := tracing.Tracer().Start(c.Request().Context(), "zoneCounts")
defer span.End()
// just cache for a short time by default
c.Response().Header().Set("Cache-Control", "public,max-age=240")
c.Response().Header().Set("Access-Control-Allow-Origin", "*")
c.Response().Header().Del("Vary")
q := ntpdb.NewWrappedQuerier(ntpdb.New(srv.db))
zone, err := q.GetZoneByName(ctx, c.Param("zone_name"))
if err != nil || zone.ID == 0 {
if errors.Is(err, sql.ErrNoRows) {
return c.String(http.StatusNotFound, "Not found")
}
log.ErrorContext(ctx, "could not query for zone", "err", err)
span.RecordError(err)
return echo.NewHTTPError(http.StatusInternalServerError, "internal error")
}
counts, err := q.GetZoneCounts(ctx, zone.ID)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
log.ErrorContext(ctx, "get counts", "err", err)
span.RecordError(err)
return c.String(http.StatusInternalServerError, "internal error")
}
}
type historyEntry struct {
D string `json:"d"` // date
Ts int `json:"ts"` // epoch timestamp
Rc int `json:"rc"` // count registered
Ac int `json:"ac"` // count active
W int `json:"w"` // netspeed active
Iv string `json:"iv"` // ip version
}
rv := struct {
History []historyEntry `json:"history"`
}{}
skipCount := 0.0
limit := 0
if limitParam := c.QueryParam("limit"); len(limitParam) > 0 {
if limitInt, err := strconv.Atoi(limitParam); err == nil && limitInt > 0 {
limit = limitInt
}
}
var mostRecentDate int64 = -1
if limit > 0 {
count := 0
dates := map[int64]bool{}
for _, c := range counts {
ep := c.Date.Unix()
if _, ok := dates[ep]; !ok {
count++
dates[ep] = true
mostRecentDate = ep
}
}
if limit < count {
if limit > 1 {
skipCount = float64(count) / float64(limit-1)
} else {
// skip everything and use the special logic that we always include the most recent date
skipCount = float64(count) + 1
}
}
log.DebugContext(ctx, "mod", "count", count, "limit", limit, "mod", count%limit, "skipCount", skipCount)
// log.Info("limit plan", "date count", count, "limit", limit, "skipCount", skipCount)
}
toSkip := 0.0
if limit == 1 {
toSkip = skipCount // we just want to look for the last entry
}
lastDate := int64(0)
lastSkip := int64(0)
skipThreshold := 0.5
for _, c := range counts {
cDate := c.Date.Unix()
if (toSkip <= skipThreshold && cDate != lastSkip) ||
lastDate == cDate ||
mostRecentDate == cDate {
// log.Info("adding date", "date", c.Date.Format(time.DateOnly))
rv.History = append(rv.History, historyEntry{
D: c.Date.Format(time.DateOnly),
Ts: int(cDate),
Ac: int(c.CountActive),
Rc: int(c.CountRegistered),
W: int(c.NetspeedActive),
Iv: string(c.IpVersion),
})
lastDate = cDate
} else {
// log.Info("skipping date", "date", c.Date.Format(time.DateOnly))
if lastSkip == cDate {
continue
}
toSkip--
lastSkip = cDate
continue
}
if toSkip <= skipThreshold && skipCount > 0 {
toSkip += skipCount
}
}
if limit > 0 {
count := 0
dates := map[int]bool{}
for _, c := range rv.History {
ep := c.Ts
if _, ok := dates[ep]; !ok {
count++
dates[ep] = true
}
}
log.DebugContext(ctx, "result counts", "skipCount", skipCount, "limit", limit, "got", count)
}
c.Response().Header().Set("Cache-Control", "s-maxage=28800, max-age=7200")
return c.JSON(http.StatusOK, rv)
}

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"