diff --git a/chdb/chdnsanswers.go b/chdb/chdnsanswers.go new file mode 100644 index 0000000..030b5a9 --- /dev/null +++ b/chdb/chdnsanswers.go @@ -0,0 +1,141 @@ +package chdb + +import ( + "context" + "fmt" + "sort" + + "github.com/ClickHouse/clickhouse-go/v2" + "go.ntppool.org/common/logger" +) + +type ccCount struct { + CC string + Count uint64 + Points float64 +} + +type ServerQueries []*ccCount + +type ServerTotals map[string]uint64 + +func (s ServerQueries) Len() int { + return len(s) +} +func (s ServerQueries) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} +func (s ServerQueries) Less(i, j int) bool { + return s[i].Count > s[j].Count +} + +func (d *ClickHouse) ServerAnswerCounts(ctx context.Context, conn clickhouse.Conn, serverIP string, days int) (ServerQueries, error) { + + log := logger.Setup().With("server", serverIP) + + // queries by UserCC / Qtype for the ServerIP + rows, err := conn.Query(ctx, ` + select UserCC,Qtype,sum(queries) as queries + from by_server_ip_1d + where + ServerIP = ? AND dt > now() - INTERVAL ? DAY + group by rollup(Qtype,UserCC) + order by UserCC,Qtype`, + serverIP, days, + ) + if err != nil { + log.Error("query error", "err", err) + return nil, fmt.Errorf("database error") + } + + rv := ServerQueries{} + + for rows.Next() { + var ( + UserCC, Qtype string + queries uint64 + ) + if err := rows.Scan( + &UserCC, + &Qtype, + &queries, + ); err != nil { + log.Error("could not parse row", "err", err) + continue + } + + if UserCC == "" && Qtype != "" { + // we get the total from the complete rollup + continue + } + + // log.Info("usercc counts", "cc", UserCC, "counts", c) + + rv = append(rv, &ccCount{ + CC: UserCC, + Count: queries, + }) + + // slog.Info("set c", "c", c) + // slog.Info("totals", "totals", totals) + } + + sort.Sort(rv) + + return rv, nil +} + +func (d *ClickHouse) AnswerTotals(ctx context.Context, conn clickhouse.Conn, qtype string, days int) (ServerTotals, error) { + + log := logger.Setup() + + // queries by UserCC / Qtype for the ServerIP + rows, err := conn.Query(ctx, ` + select UserCC,Qtype,sum(queries) as queries + from by_server_ip_1d + where + Qtype = ? AND dt > now() - INTERVAL ? DAY + group by rollup(Qtype,UserCC) + order by UserCC,Qtype`, + qtype, days, + ) + if err != nil { + log.Error("query error", "err", err) + return nil, fmt.Errorf("database error") + } + + rv := ServerTotals{} + + for rows.Next() { + var ( + UserCC, Qtype string + queries uint64 + ) + if err := rows.Scan( + &UserCC, + &Qtype, + &queries, + ); err != nil { + log.Error("could not parse row", "err", err) + continue + } + + if UserCC == "" && Qtype != "" { + // we get the total from the complete rollup + continue + } + + // log.Info("usercc counts", "cc", UserCC, "counts", c) + + if rv[UserCC] > 0 { + log.Warn("duplicate UserCC row", "usercc", UserCC) + } + + rv[UserCC] = queries + + // slog.Info("set c", "c", c) + // slog.Info("totals", "totals", totals) + } + + return rv, nil +} diff --git a/cmd/root.go b/cmd/root.go index be4234e..753942b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,9 +10,8 @@ import ( "os" "github.com/spf13/cobra" - "go.ntppool.org/data-api/version" + "go.ntppool.org/common/version" - homedir "github.com/mitchellh/go-homedir" "github.com/spf13/viper" ) @@ -47,7 +46,7 @@ to quickly create a Cobra application.`, } cmd.AddCommand(cli.serverCmd()) - cmd.AddCommand(version.VersionCmd()) + cmd.AddCommand(version.VersionCmd("data-api")) return cmd } @@ -85,7 +84,7 @@ func initConfig() { viper.SetConfigFile(cfgFile) } else { // Find home directory. - home, err := homedir.Dir() + home, err := os.UserHomeDir() if err != nil { fmt.Println(err) os.Exit(1) diff --git a/go.mod b/go.mod index c4b64fa..88f19bd 100644 --- a/go.mod +++ b/go.mod @@ -6,10 +6,9 @@ require ( github.com/ClickHouse/clickhouse-go/v2 v2.9.1 github.com/go-sql-driver/mysql v1.7.1 github.com/labstack/echo/v4 v4.10.2 - github.com/mitchellh/go-homedir v1.1.0 - github.com/prometheus/client_golang v1.15.1 github.com/spf13/cobra v1.7.0 github.com/spf13/viper v1.15.0 + go.ntppool.org/common v0.1.1 go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.41.1 go.opentelemetry.io/otel v1.15.1 go.opentelemetry.io/otel/exporters/jaeger v1.15.1 @@ -17,7 +16,7 @@ require ( go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.15.1 go.opentelemetry.io/otel/sdk v1.15.1 go.opentelemetry.io/otel/trace v1.15.1 - golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 + golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df golang.org/x/sync v0.2.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -39,7 +38,7 @@ require ( github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/klauspost/compress v1.16.5 // indirect + github.com/klauspost/compress v1.16.7 // indirect github.com/labstack/gommon v0.4.0 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect @@ -48,11 +47,12 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/paulmach/orb v0.9.2 // indirect github.com/pelletier/go-toml/v2 v2.0.7 // indirect - github.com/pierrec/lz4/v4 v4.1.17 // indirect + github.com/pierrec/lz4/v4 v4.1.18 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/prometheus/client_golang v1.16.0 // indirect github.com/prometheus/client_model v0.4.0 // indirect - github.com/prometheus/common v0.43.0 // indirect - github.com/prometheus/procfs v0.9.0 // indirect + github.com/prometheus/common v0.44.0 // indirect + github.com/prometheus/procfs v0.11.0 // indirect github.com/segmentio/asm v1.2.0 // indirect github.com/shopspring/decimal v1.3.1 // indirect github.com/spf13/afero v1.9.5 // indirect @@ -64,13 +64,14 @@ require ( github.com/valyala/fasttemplate v1.2.2 // indirect go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.15.1 // indirect go.opentelemetry.io/proto/otlp v0.19.0 // indirect - golang.org/x/crypto v0.8.0 // indirect - golang.org/x/net v0.9.0 // indirect - golang.org/x/sys v0.8.0 // indirect - golang.org/x/text v0.9.0 // indirect + golang.org/x/crypto v0.10.0 // indirect + golang.org/x/mod v0.11.0 // indirect + golang.org/x/net v0.11.0 // indirect + golang.org/x/sys v0.10.0 // indirect + golang.org/x/text v0.10.0 // indirect golang.org/x/time v0.3.0 // indirect google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect google.golang.org/grpc v1.55.0 // indirect - google.golang.org/protobuf v1.30.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect ) diff --git a/go.sum b/go.sum index 18c1e2c..1df5842 100644 --- a/go.sum +++ b/go.sum @@ -183,8 +183,8 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= -github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI= -github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= +github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -206,8 +206,6 @@ github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp9 github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= -github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= -github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= @@ -216,22 +214,22 @@ github.com/paulmach/orb v0.9.2/go.mod h1:5mULz1xQfs3bmQm63QEJA6lNGujuRafwA5S/Enu github.com/paulmach/protoscan v0.2.1/go.mod h1:SpcSwydNLrxUGSDvXvO0P7g7AuhJ7lcKfDlhJCDw2gY= github.com/pelletier/go-toml/v2 v2.0.7 h1:muncTPStnKRos5dpVKULv2FVd4bMOhNePj9CjgDb8Us= github.com/pelletier/go-toml/v2 v2.0.7/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= -github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc= -github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= +github.com/pierrec/lz4/v4 v4.1.18 h1:xaKrnTkyoqfh1YItXl56+6KJNVYWlEEPuAQW9xsplYQ= +github.com/pierrec/lz4/v4 v4.1.18/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v1.15.1 h1:8tXpTmJbyH5lydzFPoxSIJ0J46jdh3tylbvM1xCv0LI= -github.com/prometheus/client_golang v1.15.1/go.mod h1:e9yaBhRPU2pPNsZwE+JdQl0KEt1N9XgF6zxWmaC0xOk= +github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8= +github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lFEugiJ9HzIqaAx4LKc= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY= github.com/prometheus/client_model v0.4.0/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU= -github.com/prometheus/common v0.43.0 h1:iq+BVjvYLei5f27wiuNiB1DN6DYQkp1c8Bx0Vykh5us= -github.com/prometheus/common v0.43.0/go.mod h1:NCvr5cQIh3Y/gy73/RdVtC9r8xxrxwJnB+2lB3BxrFc= -github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI= -github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY= +github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdOOfY= +github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY= +github.com/prometheus/procfs v0.11.0 h1:5EAgkfkMl659uZPbe9AS2N68a7Cc1TJbPEuGzFuRbyk= +github.com/prometheus/procfs v0.11.0/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= @@ -265,7 +263,7 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8= github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= @@ -283,6 +281,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.mongodb.org/mongo-driver v1.11.4/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g= +go.ntppool.org/common v0.1.1 h1:KuBhGFyhWYN9CtbIPcH9zWXFkoZZCPpiPcqqxQ1UU3g= +go.ntppool.org/common v0.1.1/go.mod h1:kDUK1mJgmMq481TY6xIh+UgWfmdmG7BgLeP/G16nVgY= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= @@ -317,8 +317,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ= -golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE= +golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM= +golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -329,8 +329,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o= -golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= +golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME= +golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -354,6 +354,8 @@ 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.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU= +golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -387,8 +389,8 @@ golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= -golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= +golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU= +golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -455,8 +457,8 @@ golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= +golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -467,8 +469,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58= +golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -628,8 +630,8 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba 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.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= -google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/ntpdb/db.go b/ntpdb/db.go index bb02501..4f2f177 100644 --- a/ntpdb/db.go +++ b/ntpdb/db.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.18.0 +// sqlc v1.19.0 package ntpdb diff --git a/ntpdb/models.go b/ntpdb/models.go index cf9a470..8cd22cf 100644 --- a/ntpdb/models.go +++ b/ntpdb/models.go @@ -1,453 +1,14 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.18.0 +// sqlc v1.19.0 package ntpdb import ( - "database/sql" "database/sql/driver" "fmt" - "time" ) -type AccountInvitesStatus string - -const ( - AccountInvitesStatusPending AccountInvitesStatus = "pending" - AccountInvitesStatusAccepted AccountInvitesStatus = "accepted" - AccountInvitesStatusExpired AccountInvitesStatus = "expired" -) - -func (e *AccountInvitesStatus) Scan(src interface{}) error { - switch s := src.(type) { - case []byte: - *e = AccountInvitesStatus(s) - case string: - *e = AccountInvitesStatus(s) - default: - return fmt.Errorf("unsupported scan type for AccountInvitesStatus: %T", src) - } - return nil -} - -type NullAccountInvitesStatus struct { - AccountInvitesStatus AccountInvitesStatus - Valid bool // Valid is true if AccountInvitesStatus is not NULL -} - -// Scan implements the Scanner interface. -func (ns *NullAccountInvitesStatus) Scan(value interface{}) error { - if value == nil { - ns.AccountInvitesStatus, ns.Valid = "", false - return nil - } - ns.Valid = true - return ns.AccountInvitesStatus.Scan(value) -} - -// Value implements the driver Valuer interface. -func (ns NullAccountInvitesStatus) Value() (driver.Value, error) { - if !ns.Valid { - return nil, nil - } - return string(ns.AccountInvitesStatus), nil -} - -type AccountSubscriptionsStatus string - -const ( - AccountSubscriptionsStatusIncomplete AccountSubscriptionsStatus = "incomplete" - AccountSubscriptionsStatusIncompleteExpired AccountSubscriptionsStatus = "incomplete_expired" - AccountSubscriptionsStatusTrialing AccountSubscriptionsStatus = "trialing" - AccountSubscriptionsStatusActive AccountSubscriptionsStatus = "active" - AccountSubscriptionsStatusPastDue AccountSubscriptionsStatus = "past_due" - AccountSubscriptionsStatusCanceled AccountSubscriptionsStatus = "canceled" - AccountSubscriptionsStatusUnpaid AccountSubscriptionsStatus = "unpaid" - AccountSubscriptionsStatusEnded AccountSubscriptionsStatus = "ended" -) - -func (e *AccountSubscriptionsStatus) Scan(src interface{}) error { - switch s := src.(type) { - case []byte: - *e = AccountSubscriptionsStatus(s) - case string: - *e = AccountSubscriptionsStatus(s) - default: - return fmt.Errorf("unsupported scan type for AccountSubscriptionsStatus: %T", src) - } - return nil -} - -type NullAccountSubscriptionsStatus struct { - AccountSubscriptionsStatus AccountSubscriptionsStatus - Valid bool // Valid is true if AccountSubscriptionsStatus is not NULL -} - -// Scan implements the Scanner interface. -func (ns *NullAccountSubscriptionsStatus) Scan(value interface{}) error { - if value == nil { - ns.AccountSubscriptionsStatus, ns.Valid = "", false - return nil - } - ns.Valid = true - return ns.AccountSubscriptionsStatus.Scan(value) -} - -// Value implements the driver Valuer interface. -func (ns NullAccountSubscriptionsStatus) Value() (driver.Value, error) { - if !ns.Valid { - return nil, nil - } - return string(ns.AccountSubscriptionsStatus), nil -} - -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 - Valid bool // 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 - Valid bool // 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 - Valid bool // 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 - Valid bool // 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 - -const ( - ServersIpVersionV4 ServersIpVersion = "v4" - ServersIpVersionV6 ServersIpVersion = "v6" -) - -func (e *ServersIpVersion) Scan(src interface{}) error { - switch s := src.(type) { - case []byte: - *e = ServersIpVersion(s) - case string: - *e = ServersIpVersion(s) - default: - return fmt.Errorf("unsupported scan type for ServersIpVersion: %T", src) - } - return nil -} - -type NullServersIpVersion struct { - ServersIpVersion ServersIpVersion - Valid bool // Valid is true if ServersIpVersion is not NULL -} - -// Scan implements the Scanner interface. -func (ns *NullServersIpVersion) Scan(value interface{}) error { - if value == nil { - ns.ServersIpVersion, ns.Valid = "", false - return nil - } - ns.Valid = true - return ns.ServersIpVersion.Scan(value) -} - -// Value implements the driver Valuer interface. -func (ns NullServersIpVersion) Value() (driver.Value, error) { - if !ns.Valid { - return nil, nil - } - return string(ns.ServersIpVersion), nil -} - -type UserEquipmentApplicationsStatus string - -const ( - UserEquipmentApplicationsStatusNew UserEquipmentApplicationsStatus = "New" - UserEquipmentApplicationsStatusPending UserEquipmentApplicationsStatus = "Pending" - UserEquipmentApplicationsStatusMaybe UserEquipmentApplicationsStatus = "Maybe" - UserEquipmentApplicationsStatusNo UserEquipmentApplicationsStatus = "No" - UserEquipmentApplicationsStatusApproved UserEquipmentApplicationsStatus = "Approved" -) - -func (e *UserEquipmentApplicationsStatus) Scan(src interface{}) error { - switch s := src.(type) { - case []byte: - *e = UserEquipmentApplicationsStatus(s) - case string: - *e = UserEquipmentApplicationsStatus(s) - default: - return fmt.Errorf("unsupported scan type for UserEquipmentApplicationsStatus: %T", src) - } - return nil -} - -type NullUserEquipmentApplicationsStatus struct { - UserEquipmentApplicationsStatus UserEquipmentApplicationsStatus - Valid bool // Valid is true if UserEquipmentApplicationsStatus is not NULL -} - -// Scan implements the Scanner interface. -func (ns *NullUserEquipmentApplicationsStatus) Scan(value interface{}) error { - if value == nil { - ns.UserEquipmentApplicationsStatus, ns.Valid = "", false - return nil - } - ns.Valid = true - return ns.UserEquipmentApplicationsStatus.Scan(value) -} - -// Value implements the driver Valuer interface. -func (ns NullUserEquipmentApplicationsStatus) Value() (driver.Value, error) { - if !ns.Valid { - return nil, nil - } - return string(ns.UserEquipmentApplicationsStatus), nil -} - -type VendorZonesClientType string - -const ( - VendorZonesClientTypeNtp VendorZonesClientType = "ntp" - VendorZonesClientTypeSntp VendorZonesClientType = "sntp" - VendorZonesClientTypeLegacy VendorZonesClientType = "legacy" -) - -func (e *VendorZonesClientType) Scan(src interface{}) error { - switch s := src.(type) { - case []byte: - *e = VendorZonesClientType(s) - case string: - *e = VendorZonesClientType(s) - default: - return fmt.Errorf("unsupported scan type for VendorZonesClientType: %T", src) - } - return nil -} - -type NullVendorZonesClientType struct { - VendorZonesClientType VendorZonesClientType - Valid bool // Valid is true if VendorZonesClientType is not NULL -} - -// Scan implements the Scanner interface. -func (ns *NullVendorZonesClientType) Scan(value interface{}) error { - if value == nil { - ns.VendorZonesClientType, ns.Valid = "", false - return nil - } - ns.Valid = true - return ns.VendorZonesClientType.Scan(value) -} - -// Value implements the driver Valuer interface. -func (ns NullVendorZonesClientType) Value() (driver.Value, error) { - if !ns.Valid { - return nil, nil - } - return string(ns.VendorZonesClientType), nil -} - -type VendorZonesStatus string - -const ( - VendorZonesStatusNew VendorZonesStatus = "New" - VendorZonesStatusPending VendorZonesStatus = "Pending" - VendorZonesStatusApproved VendorZonesStatus = "Approved" - VendorZonesStatusRejected VendorZonesStatus = "Rejected" -) - -func (e *VendorZonesStatus) Scan(src interface{}) error { - switch s := src.(type) { - case []byte: - *e = VendorZonesStatus(s) - case string: - *e = VendorZonesStatus(s) - default: - return fmt.Errorf("unsupported scan type for VendorZonesStatus: %T", src) - } - return nil -} - -type NullVendorZonesStatus struct { - VendorZonesStatus VendorZonesStatus - Valid bool // Valid is true if VendorZonesStatus is not NULL -} - -// Scan implements the Scanner interface. -func (ns *NullVendorZonesStatus) Scan(value interface{}) error { - if value == nil { - ns.VendorZonesStatus, ns.Valid = "", false - return nil - } - ns.Valid = true - return ns.VendorZonesStatus.Scan(value) -} - -// Value implements the driver Valuer interface. -func (ns NullVendorZonesStatus) Value() (driver.Value, error) { - if !ns.Valid { - return nil, nil - } - return string(ns.VendorZonesStatus), nil -} - type ZoneServerCountsIpVersion string const ( @@ -468,8 +29,8 @@ func (e *ZoneServerCountsIpVersion) Scan(src interface{}) error { } type NullZoneServerCountsIpVersion struct { - ZoneServerCountsIpVersion ZoneServerCountsIpVersion - Valid bool // Valid is true if ZoneServerCountsIpVersion is not NULL + ZoneServerCountsIpVersion ZoneServerCountsIpVersion `json:"zone_server_counts_ip_version"` + Valid bool `json:"valid"` // Valid is true if ZoneServerCountsIpVersion is not NULL } // Scan implements the Scanner interface. @@ -489,305 +50,3 @@ func (ns NullZoneServerCountsIpVersion) Value() (driver.Value, error) { } return string(ns.ZoneServerCountsIpVersion), nil } - -type Account struct { - ID int32 `json:"id"` - Name sql.NullString `json:"name"` - OrganizationName sql.NullString `json:"organization_name"` - OrganizationUrl sql.NullString `json:"organization_url"` - PublicProfile bool `json:"public_profile"` - UrlSlug sql.NullString `json:"url_slug"` - CreatedOn time.Time `json:"created_on"` - ModifiedOn time.Time `json:"modified_on"` - StripeCustomerID sql.NullString `json:"stripe_customer_id"` -} - -type AccountInvite struct { - ID int32 `json:"id"` - AccountID int32 `json:"account_id"` - Email string `json:"email"` - Status NullAccountInvitesStatus `json:"status"` - UserID sql.NullInt32 `json:"user_id"` - SentByID int32 `json:"sent_by_id"` - Code string `json:"code"` - ExpiresOn time.Time `json:"expires_on"` - CreatedOn time.Time `json:"created_on"` - ModifiedOn time.Time `json:"modified_on"` -} - -type AccountSubscription struct { - ID int32 `json:"id"` - AccountID int32 `json:"account_id"` - StripeSubscriptionID sql.NullString `json:"stripe_subscription_id"` - Status NullAccountSubscriptionsStatus `json:"status"` - Name string `json:"name"` - MaxZones int32 `json:"max_zones"` - MaxDevices int32 `json:"max_devices"` - CreatedOn time.Time `json:"created_on"` - EndedOn sql.NullTime `json:"ended_on"` - ModifiedOn time.Time `json:"modified_on"` -} - -type AccountUser struct { - AccountID int32 `json:"account_id"` - UserID int32 `json:"user_id"` -} - -type ApiKey struct { - ID int32 `json:"id"` - ApiKey sql.NullString `json:"api_key"` - Grants sql.NullString `json:"grants"` - CreatedOn time.Time `json:"created_on"` - ModifiedOn time.Time `json:"modified_on"` -} - -type CombustCache struct { - ID string `json:"id"` - Type string `json:"type"` - Created time.Time `json:"created"` - PurgeKey sql.NullString `json:"purge_key"` - Data []byte `json:"data"` - Metadata sql.NullString `json:"metadata"` - Serialized bool `json:"serialized"` - Expire time.Time `json:"expire"` -} - -type CombustSecret struct { - SecretTs int32 `json:"secret_ts"` - ExpiresTs int32 `json:"expires_ts"` - Type string `json:"type"` - Secret sql.NullString `json:"secret"` -} - -type DnsRoot struct { - ID int32 `json:"id"` - Origin string `json:"origin"` - VendorAvailable int32 `json:"vendor_available"` - GeneralUse int32 `json:"general_use"` - NsList string `json:"ns_list"` -} - -type Log struct { - ID int32 `json:"id"` - AccountID sql.NullInt32 `json:"account_id"` - ServerID sql.NullInt32 `json:"server_id"` - UserID sql.NullInt32 `json:"user_id"` - VendorZoneID sql.NullInt32 `json:"vendor_zone_id"` - Type sql.NullString `json:"type"` - Message sql.NullString `json:"message"` - Changes sql.NullString `json:"changes"` - CreatedOn time.Time `json:"created_on"` -} - -type LogScore struct { - ID int64 `json:"id"` - MonitorID sql.NullInt32 `json:"monitor_id"` - ServerID int32 `json:"server_id"` - Ts time.Time `json:"ts"` - Score float64 `json:"score"` - Step float64 `json:"step"` - Offset sql.NullFloat64 `json:"offset"` - Rtt sql.NullInt32 `json:"rtt"` - Attributes sql.NullString `json:"attributes"` -} - -type LogScoresArchiveStatus struct { - ID int32 `json:"id"` - Archiver string `json:"archiver"` - LogScoreID sql.NullInt64 `json:"log_score_id"` - ModifiedOn time.Time `json:"modified_on"` -} - -type LogStatus struct { - ServerID int32 `json:"server_id"` - LastCheck time.Time `json:"last_check"` - TsArchived time.Time `json:"ts_archived"` -} - -type Monitor struct { - ID int32 `json:"id"` - Type MonitorsType `json:"type"` - UserID sql.NullInt32 `json:"user_id"` - AccountID sql.NullInt32 `json:"account_id"` - Name string `json:"name"` - Location string `json:"location"` - Ip sql.NullString `json:"ip"` - IpVersion NullMonitorsIpVersion `json:"ip_version"` - TlsName sql.NullString `json:"tls_name"` - ApiKey sql.NullString `json:"api_key"` - Status MonitorsStatus `json:"status"` - Config string `json:"config"` - ClientVersion string `json:"client_version"` - LastSeen sql.NullTime `json:"last_seen"` - LastSubmit sql.NullTime `json:"last_submit"` - CreatedOn time.Time `json:"created_on"` -} - -type MonitorsDatum struct { - ID int32 `json:"id"` - AccountID sql.NullInt32 `json:"account_id"` - Type MonitorsType `json:"type"` - Name interface{} `json:"name"` - Ip sql.NullString `json:"ip"` - IpVersion NullMonitorsIpVersion `json:"ip_version"` - Status MonitorsStatus `json:"status"` - ClientVersion string `json:"client_version"` - LastSeen sql.NullTime `json:"last_seen"` - LastSubmit sql.NullTime `json:"last_submit"` -} - -type SchemaRevision struct { - Revision int32 `json:"revision"` - SchemaName string `json:"schema_name"` -} - -type ScorerStatus struct { - ID int32 `json:"id"` - ScorerID int32 `json:"scorer_id"` - LogScoreID sql.NullInt64 `json:"log_score_id"` - ModifiedOn time.Time `json:"modified_on"` -} - -type Server struct { - ID int32 `json:"id"` - Ip string `json:"ip"` - IpVersion ServersIpVersion `json:"ip_version"` - UserID int32 `json:"user_id"` - AccountID sql.NullInt32 `json:"account_id"` - Hostname sql.NullString `json:"hostname"` - Stratum sql.NullInt32 `json:"stratum"` - InPool int32 `json:"in_pool"` - InServerList int32 `json:"in_server_list"` - Netspeed int32 `json:"netspeed"` - CreatedOn time.Time `json:"created_on"` - UpdatedOn time.Time `json:"updated_on"` - ScoreTs sql.NullTime `json:"score_ts"` - ScoreRaw float64 `json:"score_raw"` - DeletionOn sql.NullTime `json:"deletion_on"` -} - -type ServerAlert struct { - ServerID int32 `json:"server_id"` - LastScore float64 `json:"last_score"` - FirstEmailTime time.Time `json:"first_email_time"` - LastEmailTime sql.NullTime `json:"last_email_time"` -} - -type ServerNote struct { - ID int32 `json:"id"` - ServerID int32 `json:"server_id"` - Name string `json:"name"` - Note string `json:"note"` - CreatedOn time.Time `json:"created_on"` - ModifiedOn time.Time `json:"modified_on"` -} - -type ServerScore struct { - ID int64 `json:"id"` - MonitorID int32 `json:"monitor_id"` - ServerID int32 `json:"server_id"` - ScoreTs sql.NullTime `json:"score_ts"` - ScoreRaw float64 `json:"score_raw"` - Stratum sql.NullInt32 `json:"stratum"` - Status ServerScoresStatus `json:"status"` - CreatedOn time.Time `json:"created_on"` - ModifiedOn time.Time `json:"modified_on"` -} - -type ServerUrl struct { - ID int32 `json:"id"` - ServerID int32 `json:"server_id"` - Url string `json:"url"` -} - -type ServerZone struct { - ServerID int32 `json:"server_id"` - ZoneID int32 `json:"zone_id"` -} - -type ServersMonitorReview struct { - ServerID int32 `json:"server_id"` - LastReview sql.NullTime `json:"last_review"` - NextReview sql.NullTime `json:"next_review"` - LastChange sql.NullTime `json:"last_change"` - Config string `json:"config"` -} - -type SystemSetting struct { - ID int32 `json:"id"` - Key sql.NullString `json:"key"` - Value sql.NullString `json:"value"` - CreatedOn time.Time `json:"created_on"` - ModifiedOn time.Time `json:"modified_on"` -} - -type User struct { - ID int32 `json:"id"` - Email string `json:"email"` - Name sql.NullString `json:"name"` - Username sql.NullString `json:"username"` - PublicProfile bool `json:"public_profile"` -} - -type UserEquipmentApplication struct { - ID int32 `json:"id"` - UserID int32 `json:"user_id"` - Application sql.NullString `json:"application"` - ContactInformation sql.NullString `json:"contact_information"` - Status UserEquipmentApplicationsStatus `json:"status"` -} - -type UserIdentity struct { - ID int32 `json:"id"` - ProfileID string `json:"profile_id"` - UserID int32 `json:"user_id"` - Provider string `json:"provider"` - Data sql.NullString `json:"data"` - Email sql.NullString `json:"email"` -} - -type UserPrivilege struct { - UserID int32 `json:"user_id"` - SeeAllServers bool `json:"see_all_servers"` - VendorAdmin int32 `json:"vendor_admin"` - EquipmentAdmin int32 `json:"equipment_admin"` - SupportStaff int32 `json:"support_staff"` -} - -type VendorZone struct { - ID int32 `json:"id"` - ZoneName string `json:"zone_name"` - Status VendorZonesStatus `json:"status"` - UserID sql.NullInt32 `json:"user_id"` - OrganizationName sql.NullString `json:"organization_name"` - ClientType VendorZonesClientType `json:"client_type"` - ContactInformation sql.NullString `json:"contact_information"` - RequestInformation sql.NullString `json:"request_information"` - DeviceCount sql.NullInt32 `json:"device_count"` - Opensource bool `json:"opensource"` - OpensourceInfo sql.NullString `json:"opensource_info"` - RtTicket sql.NullInt32 `json:"rt_ticket"` - ApprovedOn sql.NullTime `json:"approved_on"` - CreatedOn time.Time `json:"created_on"` - ModifiedOn time.Time `json:"modified_on"` - DnsRootID int32 `json:"dns_root_id"` - AccountID sql.NullInt32 `json:"account_id"` -} - -type Zone struct { - ID int32 `json:"id"` - Name string `json:"name"` - Description sql.NullString `json:"description"` - ParentID sql.NullInt32 `json:"parent_id"` - Dns bool `json:"dns"` -} - -type ZoneServerCount struct { - ID int32 `json:"id"` - ZoneID int32 `json:"zone_id"` - IpVersion ZoneServerCountsIpVersion `json:"ip_version"` - Date time.Time `json:"date"` - CountActive int32 `json:"count_active"` - CountRegistered int32 `json:"count_registered"` - NetspeedActive int32 `json:"netspeed_active"` -} diff --git a/ntpdb/query.sql.go b/ntpdb/query.sql.go index 39752d1..4f1d0a0 100644 --- a/ntpdb/query.sql.go +++ b/ntpdb/query.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.18.0 +// sqlc v1.19.0 // source: query.sql package ntpdb @@ -23,9 +23,9 @@ type GetZoneStatsDataRow struct { Date time.Time `json:"date"` Name string `json:"name"` IpVersion ZoneServerCountsIpVersion `json:"ip_version"` - CountActive int32 `json:"count_active"` - CountRegistered int32 `json:"count_registered"` - NetspeedActive int32 `json:"netspeed_active"` + CountActive uint32 `json:"count_active"` + CountRegistered uint32 `json:"count_registered"` + NetspeedActive uint32 `json:"netspeed_active"` } func (q *Queries) GetZoneStatsData(ctx context.Context) ([]GetZoneStatsDataRow, error) { diff --git a/server/clickhouse.go b/server/clickhouse.go index a4388cb..9a37f70 100644 --- a/server/clickhouse.go +++ b/server/clickhouse.go @@ -6,7 +6,7 @@ import ( "github.com/ClickHouse/clickhouse-go/v2" "github.com/ClickHouse/clickhouse-go/v2/lib/driver" - "go.ntppool.org/data-api/version" + "go.ntppool.org/common/version" "golang.org/x/exp/slog" ) @@ -19,7 +19,7 @@ func (srv *Server) chConn(ctx context.Context) (driver.Conn, error) { Username: "default", Password: "", }, - // Debug: true, + Debug: true, // Debugf: func(format string, v ...interface{}) { // slog.Info("debug format", "format", format) // fmt.Printf(format+"\n", v) diff --git a/server/dnsanswers.go b/server/dnsanswers.go new file mode 100644 index 0000000..48a488d --- /dev/null +++ b/server/dnsanswers.go @@ -0,0 +1,82 @@ +package server + +import ( + "net/http" + "net/netip" + + "github.com/labstack/echo/v4" + "go.ntppool.org/common/logger" + "golang.org/x/exp/slog" +) + +const pointBasis float64 = 10000 +const pointSymbol = "‱" + +// const pointBasis = 1000 +// const pointSymbol = "‰" + +func (srv *Server) dnsAnswers(c echo.Context) error { + + log := logger.Setup() + + ctx := c.Request().Context() + + conn, err := srv.chConn(ctx) + if err != nil { + slog.Error("could not connect to clickhouse", "err", err) + return c.String(http.StatusInternalServerError, "clickhouse error") + } + + ip, err := netip.ParseAddr(c.Param("server")) + if err != nil { + log.Warn("could not parse server parameter", "server", c.Param("server"), "err", err) + return c.NoContent(http.StatusNotFound) + } + + // q := ntpdb.New(srv.db) + // zoneStats, err := q.GetZoneStats(ctx) + // if err != nil { + // slog.Error("GetZoneStats", "err", err) + // return c.String(http.StatusInternalServerError, err.Error()) + // } + // if zoneStats == nil { + // slog.Info("didn't get zoneStats") + // } + + days := 4 + + serverData, err := srv.ch.ServerAnswerCounts(c.Request().Context(), conn, ip.String(), days) + if err != nil { + slog.Error("ServerUserCCData", "err", err) + return c.String(http.StatusInternalServerError, err.Error()) + } + + qtype := "A" + if ip.Is6() { + qtype = "AAAA" + } + + totalData, err := srv.ch.AnswerTotals(c.Request().Context(), conn, qtype, days) + if err != nil { + slog.Error("AnswerTotals", "err", err) + return c.String(http.StatusInternalServerError, err.Error()) + } + + for _, cc := range serverData { + cc.Points = (pointBasis / float64(totalData[cc.CC])) * float64(cc.Count) + // log.Info("points", "cc", cc.CC, "points", cc.Points) + } + + r := struct { + Server interface{} + // Totals interface{} + PointSymbol string + }{ + Server: serverData, + PointSymbol: pointSymbol, + // Totals: totalData, + } + + return c.JSONPretty(http.StatusOK, r, "") + +} diff --git a/server/server.go b/server/server.go index 4c18383..f3790a2 100644 --- a/server/server.go +++ b/server/server.go @@ -4,34 +4,32 @@ import ( "context" "database/sql" "fmt" - "log" "net/http" - chdb "go.ntppool.org/data-api/chdb" - "go.ntppool.org/data-api/ntpdb" - "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promhttp" "go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho" otrace "go.opentelemetry.io/otel/trace" "golang.org/x/exp/slog" "golang.org/x/sync/errgroup" + + "go.ntppool.org/common/metricsserver" + + chdb "go.ntppool.org/data-api/chdb" + "go.ntppool.org/data-api/ntpdb" ) type Server struct { db *sql.DB ch *chdb.ClickHouse - ctx context.Context - mr *prometheus.Registry - tracer otrace.Tracer + ctx context.Context + + metrics *metricsserver.Metrics + tracer otrace.Tracer } func NewServer(ctx context.Context) (*Server, error) { - mr := prometheus.NewRegistry() - ch, err := chdb.New("database.yaml") if err != nil { return nil, fmt.Errorf("clickhouse open: %w", err) @@ -42,10 +40,10 @@ func NewServer(ctx context.Context) (*Server, error) { } srv := &Server{ - ch: ch, - db: db, - ctx: ctx, - mr: mr, + ch: ch, + db: db, + ctx: ctx, + metrics: metricsserver.New(), } err = srv.initTracer() @@ -57,14 +55,6 @@ func NewServer(ctx context.Context) (*Server, error) { return srv, nil } -func (srv *Server) metricsHandler() http.Handler { - return promhttp.HandlerFor(srv.mr, promhttp.HandlerOpts{ - ErrorLog: log.Default(), - Registry: srv.mr, - EnableOpenMetrics: true, - }) -} - func (srv *Server) Run() error { slog.Info("Run()") @@ -73,17 +63,8 @@ func (srv *Server) Run() error { g, _ := errgroup.WithContext(ctx) - metricsServer := &http.Server{ - Addr: ":9000", - Handler: srv.metricsHandler(), - } - g.Go(func() error { - err := metricsServer.ListenAndServe() - if err != nil { - return fmt.Errorf("metrics server: %w", err) - } - return nil + return srv.metrics.ListenAndServe(ctx, 9000) }) e := echo.New() @@ -100,6 +81,8 @@ func (srv *Server) Run() error { e.GET("/api/usercc", srv.userCountryData) + e.GET("/api/server/dns/answers/:server", srv.dnsAnswers) + g.Go(func() error { return e.Start(":8000") }) diff --git a/server/tracing.go b/server/tracing.go index 4335b4e..fa08629 100644 --- a/server/tracing.go +++ b/server/tracing.go @@ -5,7 +5,7 @@ import ( "log" "os" - "go.ntppool.org/data-api/version" + "go.ntppool.org/common/version" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/exporters/jaeger" diff --git a/sqlc.yaml b/sqlc.yaml index dfb55ab..b2f73c4 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -8,6 +8,7 @@ sql: package: "ntpdb" out: "ntpdb" emit_json_tags: true + omit_unused_structs: true # emit_all_enum_values: true # overrides: # - column: "x.avg_rtt"