feat: expand vehicle data platform capabilities
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
|
||||
"lingniu/vehicle-data-platform/apps/api/internal/config"
|
||||
"lingniu/vehicle-data-platform/apps/api/internal/platform"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
cfg := config.Load()
|
||||
if strings.TrimSpace(cfg.MySQLDSN) == "" {
|
||||
return fmt.Errorf("MYSQL_DSN is required")
|
||||
}
|
||||
gateways := map[string]platform.AlertNotificationGateway{}
|
||||
for _, candidate := range []struct {
|
||||
channel string
|
||||
endpoint string
|
||||
secret string
|
||||
}{
|
||||
{channel: "sms", endpoint: cfg.AlertNotificationSMSURL, secret: cfg.AlertNotificationSMSSecret},
|
||||
{channel: "email", endpoint: cfg.AlertNotificationEmailURL, secret: cfg.AlertNotificationEmailSecret},
|
||||
{channel: "wecom", endpoint: cfg.AlertNotificationWeComURL, secret: cfg.AlertNotificationWeComSecret},
|
||||
} {
|
||||
if strings.TrimSpace(candidate.endpoint) == "" && strings.TrimSpace(candidate.secret) == "" {
|
||||
continue
|
||||
}
|
||||
gateway, err := platform.NewHTTPAlertNotificationGateway(candidate.endpoint, candidate.secret, cfg.AlertNotificationTimeout)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s gateway: %w", candidate.channel, err)
|
||||
}
|
||||
gateways[candidate.channel] = gateway
|
||||
}
|
||||
if len(gateways) == 0 {
|
||||
return fmt.Errorf("at least one ALERT_NOTIFICATION_*_URL and matching signing secret are required")
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
db, err := platform.OpenSQL(ctx, "mysql", cfg.MySQLDSN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
host, _ := os.Hostname()
|
||||
dispatcher := platform.NewAlertNotificationDispatcher(
|
||||
platform.NewProductionStore(db, nil, ""),
|
||||
gateways,
|
||||
cfg.AlertNotificationBatchSize,
|
||||
cfg.AlertNotificationLease,
|
||||
"notification-dispatcher-"+host,
|
||||
)
|
||||
poll := cfg.AlertNotificationPollInterval
|
||||
if poll < 250*time.Millisecond {
|
||||
poll = time.Second
|
||||
}
|
||||
if poll > time.Minute {
|
||||
poll = time.Minute
|
||||
}
|
||||
ticker := time.NewTicker(poll)
|
||||
defer ticker.Stop()
|
||||
log.Printf("alert notification dispatcher started channels=%d batch=%d poll=%s lease=%s", len(gateways), cfg.AlertNotificationBatchSize, poll, cfg.AlertNotificationLease)
|
||||
for {
|
||||
started := time.Now()
|
||||
result, dispatchErr := dispatcher.RunOnce(ctx)
|
||||
if dispatchErr != nil {
|
||||
log.Printf("alert notification dispatch failed claimed=%d sent=%d failed=%d duration=%s error=%v", result.Claimed, result.Sent, result.Failed, time.Since(started), dispatchErr)
|
||||
} else if result.Claimed > 0 {
|
||||
log.Printf("alert notification dispatch completed claimed=%d sent=%d failed=%d duration=%s", result.Claimed, result.Sent, result.Failed, time.Since(started))
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Printf("alert notification dispatcher stopped")
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
82
vehicle-data-platform/apps/api/cmd/open-platform-api/main.go
Normal file
82
vehicle-data-platform/apps/api/cmd/open-platform-api/main.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/taosdata/driver-go/v3/taosWS"
|
||||
|
||||
"lingniu/vehicle-data-platform/apps/api/internal/openplatform"
|
||||
)
|
||||
|
||||
func main() {
|
||||
mysqlDSN := os.Getenv("MYSQL_DSN")
|
||||
if mysqlDSN == "" {
|
||||
log.Fatal("MYSQL_DSN is required")
|
||||
}
|
||||
db, err := sql.Open("mysql", mysqlDSN)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := db.PingContext(ctx); err != nil {
|
||||
log.Fatalf("connect MySQL: %v", err)
|
||||
}
|
||||
tdengineDSN := os.Getenv("TDENGINE_DSN")
|
||||
if tdengineDSN == "" {
|
||||
log.Fatal("TDENGINE_DSN is required")
|
||||
}
|
||||
tdengineDriver := env("TDENGINE_DRIVER", "taosWS")
|
||||
tdengineDB, err := sql.Open(tdengineDriver, tdengineDSN)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer tdengineDB.Close()
|
||||
tdCtx, tdCancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer tdCancel()
|
||||
if err := tdengineDB.PingContext(tdCtx); err != nil {
|
||||
log.Fatalf("connect TDengine: %v", err)
|
||||
}
|
||||
addr := env("OPEN_PLATFORM_HTTP_ADDR", ":20310")
|
||||
handler := openplatform.NewStandaloneServer(db, openplatform.StandaloneConfig{
|
||||
StaticDir: os.Getenv("OPEN_PLATFORM_STATIC_DIR"),
|
||||
SessionTTL: time.Duration(envInt("OPEN_PLATFORM_SESSION_TTL_HOURS", 12)) * time.Hour,
|
||||
RequestTimeout: time.Duration(envInt("OPEN_PLATFORM_REQUEST_TIMEOUT_MS", 10000)) * time.Millisecond,
|
||||
Release: os.Getenv("OPEN_PLATFORM_RELEASE"),
|
||||
TDengine: tdengineDB,
|
||||
TDengineDatabase: env("TDENGINE_DATABASE", "lingniu_vehicle_ts"),
|
||||
})
|
||||
server := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: handler,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
log.Printf("vehicle open platform listening on %s", addr)
|
||||
if err := server.ListenAndServe(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func env(name, fallback string) string {
|
||||
if value := os.Getenv(name); value != "" {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func envInt(name string, fallback int) int {
|
||||
value, err := strconv.Atoi(os.Getenv(name))
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/taosdata/driver-go/v3/taosWS"
|
||||
|
||||
"lingniu/vehicle-data-platform/apps/api/internal/config"
|
||||
"lingniu/vehicle-data-platform/apps/api/internal/openplatform"
|
||||
)
|
||||
|
||||
func main() {
|
||||
date := flag.String("date", "", "last date to calculate in yyyy-MM-dd; defaults to yesterday in Asia/Shanghai")
|
||||
lookback := flag.Int("lookback-days", envInt("OPEN_STAT_LOOKBACK_DAYS", 2), "number of dates ending at -date")
|
||||
noise := flag.Float64("hydrogen-noise-kg", envFloat("OPEN_STAT_HYDROGEN_NOISE_KG", 0.05), "ignored mass jitter")
|
||||
maxDrop := flag.Float64("hydrogen-max-drop-kg", envFloat("OPEN_STAT_HYDROGEN_MAX_DROP_KG", 20), "maximum accepted drop between samples")
|
||||
flag.Parse()
|
||||
|
||||
cfg := config.Load()
|
||||
if cfg.MySQLDSN == "" || cfg.TDengineDSN == "" {
|
||||
log.Fatal("MYSQL_DSN and TDENGINE_DSN are required")
|
||||
}
|
||||
mysqlDB, err := sql.Open("mysql", cfg.MySQLDSN)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer mysqlDB.Close()
|
||||
tdengineDB, err := sql.Open(cfg.TDengineDriver, cfg.TDengineDSN)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer tdengineDB.Close()
|
||||
|
||||
location := time.FixedZone("Asia/Shanghai", 8*60*60)
|
||||
endDate := time.Now().In(location).AddDate(0, 0, -1)
|
||||
if *date != "" {
|
||||
endDate, err = time.ParseInLocation("2006-01-02", *date, location)
|
||||
if err != nil {
|
||||
log.Fatal("-date must use yyyy-MM-dd")
|
||||
}
|
||||
}
|
||||
if *lookback < 1 || *lookback > 31 {
|
||||
log.Fatal("-lookback-days must be between 1 and 31")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
|
||||
defer cancel()
|
||||
capacities, err := openplatform.LoadHydrogenCapacities(ctx, mysqlDB)
|
||||
if err != nil {
|
||||
log.Fatalf("load hydrogen tank capacities: %v", err)
|
||||
}
|
||||
for offset := *lookback - 1; offset >= 0; offset-- {
|
||||
start := time.Date(endDate.Year(), endDate.Month(), endDate.Day(), 0, 0, 0, 0, location).AddDate(0, 0, -offset)
|
||||
observations, err := openplatform.LoadHydrogenObservations(ctx, tdengineDB, cfg.TDengineDatabase, start, start.AddDate(0, 0, 1), capacities)
|
||||
if err != nil {
|
||||
log.Fatalf("load hydrogen observations for %s: %v", start.Format("2006-01-02"), err)
|
||||
}
|
||||
stats := openplatform.BuildHydrogenDailyStats(observations, start.Format("2006-01-02"), *noise, *maxDrop)
|
||||
if err := openplatform.ReplaceHydrogenDailyStats(ctx, mysqlDB, start.Format("2006-01-02"), stats); err != nil {
|
||||
log.Fatalf("persist hydrogen statistics for %s: %v", start.Format("2006-01-02"), err)
|
||||
}
|
||||
fmt.Printf("date=%s observations=%d vehicles=%d\n", start.Format("2006-01-02"), len(observations), len(stats))
|
||||
}
|
||||
}
|
||||
|
||||
func envInt(name string, fallback int) int {
|
||||
value, err := strconv.Atoi(os.Getenv(name))
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func envFloat(name string, fallback float64) float64 {
|
||||
value, err := strconv.ParseFloat(os.Getenv(name), 64)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
Reference in New Issue
Block a user