Private
Public Access
1
0
This commit is contained in:
2023-05-07 00:01:00 -07:00
parent dfaaf397fa
commit 18185090d1
23 changed files with 3305 additions and 0 deletions

31
ntpdb/db.go Normal file
View File

@@ -0,0 +1,31 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.18.0
package ntpdb
import (
"context"
"database/sql"
)
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
return &Queries{
db: tx,
}
}

75
ntpdb/dbconn.go Normal file
View File

@@ -0,0 +1,75 @@
package ntpdb
import (
"database/sql"
"database/sql/driver"
"fmt"
"log"
"os"
"time"
"github.com/go-sql-driver/mysql"
"gopkg.in/yaml.v3"
)
type DBConfig struct {
DSN string `default:"" flag:"dsn" usage:"Database DSN"`
User string `default:"" flag:"user"`
Pass string `default:"" flag:"pass"`
}
func OpenDB(configFile string) (*sql.DB, error) {
dbconn := sql.OpenDB(Driver{CreateConnectorFunc: createConnector(configFile)})
dbconn.SetConnMaxLifetime(time.Minute * 3)
dbconn.SetMaxOpenConns(10)
dbconn.SetMaxIdleConns(5)
err := dbconn.Ping()
if err != nil {
log.Printf("Could not connect to database: %s", err)
return nil, err
}
return dbconn, nil
}
func createConnector(configFile string) CreateConnectorFunc {
return func() (driver.Connector, error) {
dbFile, err := os.Open(configFile)
if err != nil {
return nil, err
}
dec := yaml.NewDecoder(dbFile)
cfg := DBConfig{}
err = dec.Decode(&cfg)
if err != nil {
return nil, err
}
dsn := cfg.DSN
if len(dsn) == 0 {
return nil, fmt.Errorf("--database.dsn flag or DATABASE_DSN environment variable required")
}
dbcfg, err := mysql.ParseDSN(dsn)
if err != nil {
return nil, err
}
if user := cfg.User; len(user) > 0 && err == nil {
dbcfg.User = user
}
if pass := cfg.Pass; len(pass) > 0 && err == nil {
dbcfg.Passwd = pass
}
return mysql.NewConnector(dbcfg)
}
}

34
ntpdb/dynamic_connect.go Normal file
View File

@@ -0,0 +1,34 @@
package ntpdb
import (
"context"
"database/sql/driver"
"errors"
"fmt"
)
// from https://github.com/Boostport/dynamic-database-config
type CreateConnectorFunc func() (driver.Connector, error)
type Driver struct {
CreateConnectorFunc CreateConnectorFunc
}
func (d Driver) Driver() driver.Driver {
return d
}
func (d Driver) Connect(ctx context.Context) (driver.Conn, error) {
connector, err := d.CreateConnectorFunc()
if err != nil {
return nil, fmt.Errorf("error creating connector from function: %w", err)
}
return connector.Connect(ctx)
}
func (d Driver) Open(name string) (driver.Conn, error) {
return nil, errors.New("open is not supported")
}

793
ntpdb/models.go Normal file
View File

@@ -0,0 +1,793 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.18.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 (
ZoneServerCountsIpVersionV4 ZoneServerCountsIpVersion = "v4"
ZoneServerCountsIpVersionV6 ZoneServerCountsIpVersion = "v6"
)
func (e *ZoneServerCountsIpVersion) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = ZoneServerCountsIpVersion(s)
case string:
*e = ZoneServerCountsIpVersion(s)
default:
return fmt.Errorf("unsupported scan type for ZoneServerCountsIpVersion: %T", src)
}
return nil
}
type NullZoneServerCountsIpVersion struct {
ZoneServerCountsIpVersion ZoneServerCountsIpVersion
Valid bool // Valid is true if ZoneServerCountsIpVersion is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullZoneServerCountsIpVersion) Scan(value interface{}) error {
if value == nil {
ns.ZoneServerCountsIpVersion, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.ZoneServerCountsIpVersion.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullZoneServerCountsIpVersion) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
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"`
}

59
ntpdb/query.sql.go Normal file
View File

@@ -0,0 +1,59 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.18.0
// source: query.sql
package ntpdb
import (
"context"
"time"
)
const getZoneStatsData = `-- name: GetZoneStatsData :many
SELECT zc.date, z.name, zc.ip_version, count_active, count_registered, netspeed_active
FROM zone_server_counts zc USE INDEX (date_idx)
INNER JOIN zones z
ON(zc.zone_id=z.id)
WHERE date IN (SELECT max(date) from zone_server_counts)
ORDER BY name
`
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"`
}
func (q *Queries) GetZoneStatsData(ctx context.Context) ([]GetZoneStatsDataRow, error) {
rows, err := q.db.QueryContext(ctx, getZoneStatsData)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetZoneStatsDataRow
for rows.Next() {
var i GetZoneStatsDataRow
if err := rows.Scan(
&i.Date,
&i.Name,
&i.IpVersion,
&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
}

85
ntpdb/zone_stats.go Normal file
View File

@@ -0,0 +1,85 @@
package ntpdb
import (
"context"
"golang.org/x/exp/slog"
)
type ZoneStats []ZoneStat
type ZoneStat struct {
CC string
V4 float64
V6 float64
}
func (q *Queries) GetZoneStats(ctx context.Context) (*ZoneStats, error) {
zoneStatsRows, err := q.GetZoneStatsData(ctx)
if err != nil {
return nil, err
}
var (
total4 float64
total6 float64
)
// todo: consider how many servers and if they are managed
// by the same account, in the same ASN, etc.
type counts struct {
Netspeed4 uint64
Netspeed6 uint64
PercentTotal struct {
V4 float64
V6 float64
}
}
ccs := map[string]*counts{}
for _, r := range zoneStatsRows {
if r.Name == "." {
switch r.IpVersion {
case "v4":
total4 = float64(r.NetspeedActive)
case "v6":
total6 = float64(r.NetspeedActive)
}
continue
}
if _, ok := ccs[r.Name]; !ok {
ccs[r.Name] = &counts{}
}
c := ccs[r.Name]
switch r.IpVersion {
case "v4":
c.Netspeed4 = uint64(r.NetspeedActive)
case "v6":
c.Netspeed6 = uint64(r.NetspeedActive)
}
}
data := ZoneStats{}
for name, cc := range ccs {
slog.Info("zone stats cc", "name", name)
cc.PercentTotal.V4 = (100 / total4) * float64(cc.Netspeed4)
cc.PercentTotal.V6 = (100 / total6) * float64(cc.Netspeed6)
data = append(data, ZoneStat{
CC: name,
V4: cc.PercentTotal.V4,
V6: cc.PercentTotal.V6,
})
}
return &data, nil
}