Private
Public Access
1
0

Calculate netspeed points, too
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-07-16 02:40:50 -07:00
parent 1f7973e885
commit 27c8bc4776
11 changed files with 281 additions and 123 deletions

View File

@@ -1,66 +0,0 @@
package server
import (
"context"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
"go.ntppool.org/common/version"
"golang.org/x/exp/slog"
)
func (srv *Server) chConn(ctx context.Context) (driver.Conn, error) {
conn, err := clickhouse.Open(&clickhouse.Options{
Addr: []string{"10.43.207.123:9000"},
Auth: clickhouse.Auth{
Database: "geodns3",
Username: "default",
Password: "",
},
// Debug: true,
// Debugf: func(format string, v ...interface{}) {
// slog.Info("debug format", "format", format)
// fmt.Printf(format+"\n", v)
// },
Settings: clickhouse.Settings{
"max_execution_time": 60,
},
Compression: &clickhouse.Compression{
Method: clickhouse.CompressionLZ4,
},
DialTimeout: time.Second * 5,
MaxOpenConns: 5,
MaxIdleConns: 5,
ConnMaxLifetime: time.Duration(10) * time.Minute,
ConnOpenStrategy: clickhouse.ConnOpenInOrder,
BlockBufferSize: 10,
MaxCompressionBuffer: 10240,
ClientInfo: clickhouse.ClientInfo{
Products: []struct {
Name string
Version string
}{
{Name: "data-api", Version: version.Version()},
},
},
})
if err != nil {
return nil, err
}
v, err := conn.ServerVersion()
if err != nil {
return nil, err
}
slog.Info("clickhouse connection", "version", v)
err = conn.Ping(ctx)
if err != nil {
slog.Error("clickhouse ping", "err", err)
return nil, err
}
return conn, nil
}

View File

@@ -6,7 +6,10 @@ import (
"github.com/labstack/echo/v4"
"go.ntppool.org/common/logger"
chdb "go.ntppool.org/data-api/chdb"
"go.ntppool.org/data-api/ntpdb"
"golang.org/x/exp/slog"
"golang.org/x/sync/errgroup"
)
const pointBasis float64 = 10000
@@ -23,11 +26,11 @@ func (srv *Server) dnsAnswers(c echo.Context) error {
c.Response().Header().Set("Cache-Control", "max-age=20")
conn, err := srv.chConn(ctx)
if err != nil {
slog.Error("could not connect to clickhouse", "err", err)
return c.String(http.StatusInternalServerError, "clickhouse error")
}
// 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 {
@@ -35,37 +38,93 @@ func (srv *Server) dnsAnswers(c echo.Context) error {
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")
// }
if ip.String() != c.Param("server") || len(c.QueryString()) > 0 {
return c.Redirect(http.StatusPermanentRedirect, "https://www.ntppool.org/api/data/server/dns/answers/"+ip.String())
}
days := 4
queryGroup, ctx := errgroup.WithContext(ctx)
serverData, err := srv.ch.ServerAnswerCounts(c.Request().Context(), conn, ip.String(), days)
var zoneStats []ntpdb.GetZoneStatsV2Row
var serverNetspeed uint32
queryGroup.Go(func() error {
var err error
q := ntpdb.New(srv.db)
zoneStats, err = q.GetZoneStatsV2(ctx, ip.String())
if err != nil {
slog.Error("GetZoneStatsV2", "err", err)
return err
}
if zoneStats == nil {
slog.Info("didn't get zoneStats")
}
serverNetspeed, err = q.GetServerNetspeed(ctx, ip.String())
if err != nil {
slog.Error("GetServerNetspeed", "err", err)
return err
}
return nil
})
days := 3
var serverData chdb.ServerQueries
queryGroup.Go(func() error {
var err error
serverData, err = srv.ch.ServerAnswerCounts(c.Request().Context(), ip.String(), days)
if err != nil {
slog.Error("ServerUserCCData", "err", err)
return err
}
return nil
})
var totalData chdb.ServerTotals
queryGroup.Go(func() error {
var err error
qtype := "A"
if ip.Is6() {
qtype = "AAAA"
}
totalData, err = srv.ch.AnswerTotals(c.Request().Context(), qtype, days)
if err != nil {
slog.Error("AnswerTotals", "err", err)
}
return err
})
err = queryGroup.Wait()
if err != nil {
slog.Error("ServerUserCCData", "err", err)
slog.Error("query error", "err", err)
return c.String(http.StatusInternalServerError, err.Error())
}
qtype := "A"
if ip.Is6() {
qtype = "AAAA"
}
zoneTotals := map[string]int32{}
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 _, z := range zoneStats {
zn := z.ZoneName
if zn == "@" {
zn = ""
}
zoneTotals[zn] = z.NetspeedActive // binary.BigEndian.Uint64(...)
// slog.Info("zone netspeed", "cc", z.ZoneName, "speed", z.NetspeedActive)
}
for _, cc := range serverData {
cc.Points = (pointBasis / float64(totalData[cc.CC])) * float64(cc.Count)
totalName := cc.CC
if totalName == "gb" {
totalName = "uk"
}
if zt, ok := zoneTotals[totalName]; ok {
cc.Netspeed = (pointBasis / float64(zt)) * float64(serverNetspeed)
}
// log.Info("points", "cc", cc.CC, "points", cc.Points)
}

View File

@@ -32,7 +32,7 @@ type Server struct {
}
func NewServer(ctx context.Context, configFile string) (*Server, error) {
ch, err := chdb.New(configFile)
ch, err := chdb.New(ctx, configFile)
if err != nil {
return nil, fmt.Errorf("clickhouse open: %w", err)
}
@@ -107,12 +107,6 @@ func (srv *Server) userCountryData(c echo.Context) error {
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")
}
q := ntpdb.New(srv.db)
zoneStats, err := q.GetZoneStats(ctx)
if err != nil {
@@ -123,7 +117,7 @@ func (srv *Server) userCountryData(c echo.Context) error {
slog.Info("didn't get zoneStats")
}
data, err := srv.ch.UserCountryData(c.Request().Context(), conn)
data, err := srv.ch.UserCountryData(c.Request().Context())
if err != nil {
slog.Error("UserCountryData", "err", err)
return c.String(http.StatusInternalServerError, err.Error())