API for ratios of DNS answers oer server
This commit is contained in:
@@ -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)
|
||||
|
||||
82
server/dnsanswers.go
Normal file
82
server/dnsanswers.go
Normal file
@@ -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, "")
|
||||
|
||||
}
|
||||
@@ -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")
|
||||
})
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user