Calculate netspeed points, too
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -5,14 +5,14 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/ClickHouse/clickhouse-go/v2"
|
||||
"go.ntppool.org/common/logger"
|
||||
)
|
||||
|
||||
type ccCount struct {
|
||||
CC string
|
||||
Count uint64
|
||||
Points float64
|
||||
CC string
|
||||
Count uint64
|
||||
Points float64
|
||||
Netspeed float64
|
||||
}
|
||||
|
||||
type ServerQueries []*ccCount
|
||||
@@ -29,7 +29,9 @@ 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) {
|
||||
func (d *ClickHouse) ServerAnswerCounts(ctx context.Context, serverIP string, days int) (ServerQueries, error) {
|
||||
|
||||
conn := d.conn
|
||||
|
||||
log := logger.Setup().With("server", serverIP)
|
||||
|
||||
@@ -85,12 +87,12 @@ func (d *ClickHouse) ServerAnswerCounts(ctx context.Context, conn clickhouse.Con
|
||||
return rv, nil
|
||||
}
|
||||
|
||||
func (d *ClickHouse) AnswerTotals(ctx context.Context, conn clickhouse.Conn, qtype string, days int) (ServerTotals, error) {
|
||||
func (d *ClickHouse) AnswerTotals(ctx context.Context, qtype string, days int) (ServerTotals, error) {
|
||||
|
||||
log := logger.Setup()
|
||||
|
||||
// queries by UserCC / Qtype for the ServerIP
|
||||
rows, err := conn.Query(ctx, `
|
||||
rows, err := d.conn.Query(ctx, `
|
||||
select UserCC,Qtype,sum(queries) as queries
|
||||
from by_server_ip_1d
|
||||
where
|
||||
|
||||
74
chdb/db.go
74
chdb/db.go
@@ -1,8 +1,78 @@
|
||||
package chdb
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
type ClickHouse struct {
|
||||
conn clickhouse.Conn
|
||||
}
|
||||
|
||||
func New(dbConfigPath string) (*ClickHouse, error) {
|
||||
return &ClickHouse{}, nil
|
||||
func New(ctx context.Context, dbConfigPath string) (*ClickHouse, error) {
|
||||
conn, err := setupClickhouse(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ClickHouse{conn: conn}, nil
|
||||
}
|
||||
|
||||
func setupClickhouse(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
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/ClickHouse/clickhouse-go/v2"
|
||||
"golang.org/x/exp/slog"
|
||||
)
|
||||
|
||||
@@ -31,10 +30,10 @@ func (s UserCountry) Less(i, j int) bool {
|
||||
return s[i].IPv4 > s[j].IPv4
|
||||
}
|
||||
|
||||
func (d *ClickHouse) UserCountryData(ctx context.Context, conn clickhouse.Conn) (*UserCountry, error) {
|
||||
func (d *ClickHouse) UserCountryData(ctx context.Context) (*UserCountry, error) {
|
||||
|
||||
// rows, err := conn.Query(ctx, "select dt,UserCC,Qtype,sum(queries) as queries from by_usercc_1d group by rollup(dt,Qtype,UserCC) order by dt,UserCC,Qtype;")
|
||||
rows, err := conn.Query(ctx, "select max(dt) as d,UserCC,Qtype,sum(queries) as queries from by_usercc_1d where dt > now() - INTERVAL 4 DAY group by rollup(Qtype,UserCC) order by UserCC,Qtype;")
|
||||
rows, err := d.conn.Query(ctx, "select max(dt) as d,UserCC,Qtype,sum(queries) as queries from by_usercc_1d where dt > now() - INTERVAL 4 DAY group by rollup(Qtype,UserCC) order by UserCC,Qtype;")
|
||||
if err != nil {
|
||||
slog.Error("query error", "err", err)
|
||||
return nil, fmt.Errorf("database error")
|
||||
|
||||
Reference in New Issue
Block a user