From 56c63acdc60dba0233b5395781b60bd50e5644d7 Mon Sep 17 00:00:00 2001 From: Aaron Claydon Date: Thu, 7 Nov 2024 23:28:55 +0000 Subject: [PATCH] more data --- pkg/stats/calculator/operators.go | 12 ++++++---- pkg/stats/calculator/recordstats.go | 9 ++++++++ pkg/stats/calculator/servicealerts.go | 4 ++++ pkg/stats/calculator/services.go | 16 ++++++++++--- pkg/stats/calculator/stops.go | 10 +++++--- pkg/stats/calculator/util.go | 32 ++++++++++++++++++++++++++ pkg/stats/cli/cli.go | 8 +------ pkg/stats/web_api/routes/calculated.go | 24 +++++++++++++++++++ pkg/stats/web_api/server.go | 2 ++ 9 files changed, 100 insertions(+), 17 deletions(-) create mode 100644 pkg/stats/calculator/recordstats.go create mode 100644 pkg/stats/calculator/util.go create mode 100644 pkg/stats/web_api/routes/calculated.go diff --git a/pkg/stats/calculator/operators.go b/pkg/stats/calculator/operators.go index a71d7dc4..b12277c9 100644 --- a/pkg/stats/calculator/operators.go +++ b/pkg/stats/calculator/operators.go @@ -9,13 +9,17 @@ import ( type OperatorsStats struct { Total int + + Datasources map[string]int } func GetOperators() OperatorsStats { + stats := OperatorsStats{} operatorsCollection := database.GetCollection("operators") - numberOperators, _ := operatorsCollection.CountDocuments(context.Background(), bson.D{}) + numberoperators, _ := operatorsCollection.CountDocuments(context.Background(), bson.D{}) + stats.Total = int(numberoperators) + + stats.Datasources = CountAggregate(operatorsCollection, "$datasource.datasetid") - return OperatorsStats{ - Total: int(numberOperators), - } + return stats } diff --git a/pkg/stats/calculator/recordstats.go b/pkg/stats/calculator/recordstats.go new file mode 100644 index 00000000..02e71599 --- /dev/null +++ b/pkg/stats/calculator/recordstats.go @@ -0,0 +1,9 @@ +package calculator + +import "time" + +type RecordStatsData struct { + Type string `json:"-"` + Stats interface{} + Timestamp time.Time +} diff --git a/pkg/stats/calculator/servicealerts.go b/pkg/stats/calculator/servicealerts.go index e512c3be..594d1cb7 100644 --- a/pkg/stats/calculator/servicealerts.go +++ b/pkg/stats/calculator/servicealerts.go @@ -14,6 +14,8 @@ type ServiceAlertsStats struct { Total int Active int Inactive int + + Datasources map[string]int } func GetServiceAlerts() ServiceAlertsStats { @@ -42,5 +44,7 @@ func GetServiceAlerts() ServiceAlertsStats { Total: numberActiveAlerts + numberInactiveAlerts, Active: numberActiveAlerts, Inactive: numberInactiveAlerts, + + Datasources: CountAggregate(collection, "$datasource.datasetid"), } } diff --git a/pkg/stats/calculator/services.go b/pkg/stats/calculator/services.go index 30a64cae..6eb8453c 100644 --- a/pkg/stats/calculator/services.go +++ b/pkg/stats/calculator/services.go @@ -9,13 +9,23 @@ import ( type ServicesStats struct { Total int + + TransportTypes map[string]int + Datasources map[string]int } func GetServices() ServicesStats { + stats := ServicesStats{ + TransportTypes: map[string]int{}, + Datasources: map[string]int{}, + } servicesCollection := database.GetCollection("services") + numberServices, _ := servicesCollection.CountDocuments(context.Background(), bson.D{}) + stats.Total = int(numberServices) - return ServicesStats{ - Total: int(numberServices), - } + stats.TransportTypes = CountAggregate(servicesCollection, "$transporttype") + stats.Datasources = CountAggregate(servicesCollection, "$datasource.datasetid") + + return stats } diff --git a/pkg/stats/calculator/stops.go b/pkg/stats/calculator/stops.go index 487c8b5f..80ff9d03 100644 --- a/pkg/stats/calculator/stops.go +++ b/pkg/stats/calculator/stops.go @@ -9,13 +9,17 @@ import ( type StopsStats struct { Total int + + Datasources map[string]int } func GetStops() StopsStats { + stats := StopsStats{} stopsCollection := database.GetCollection("stops") numberStops, _ := stopsCollection.CountDocuments(context.Background(), bson.D{}) + stats.Total = int(numberStops) + + stats.Datasources = CountAggregate(stopsCollection, "$datasource.datasetid") - return StopsStats{ - Total: int(numberStops), - } + return stats } diff --git a/pkg/stats/calculator/util.go b/pkg/stats/calculator/util.go new file mode 100644 index 00000000..9659ae7a --- /dev/null +++ b/pkg/stats/calculator/util.go @@ -0,0 +1,32 @@ +package calculator + +import ( + "context" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" +) + +func CountAggregate(collection *mongo.Collection, aggregateKey string) map[string]int { + countMap := map[string]int{} + + aggregation := mongo.Pipeline{ + bson.D{ + {Key: "$group", + Value: bson.D{ + {Key: "_id", Value: aggregateKey}, + {Key: "count", Value: bson.D{{Key: "$sum", Value: 1}}}, + }, + }, + }, + } + var result []bson.M + cursor, _ := collection.Aggregate(context.Background(), aggregation) + cursor.All(context.Background(), &result) + + for _, record := range result { + countMap[record["_id"].(string)] = int(record["count"].(int32)) + } + + return countMap +} diff --git a/pkg/stats/cli/cli.go b/pkg/stats/cli/cli.go index 46a8a3fc..66c0cf0c 100644 --- a/pkg/stats/cli/cli.go +++ b/pkg/stats/cli/cli.go @@ -17,12 +17,6 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) -type RecordStatsData struct { - Type string - Stats interface{} - Timestamp time.Time -} - func RegisterCLI() *cli.Command { return &cli.Command{ Name: "stats", @@ -94,7 +88,7 @@ func RegisterCLI() *cli.Command { log.Info().Str("type", objectName).Interface("stats", statsData).Msg("Calculated") - recordStatsData := RecordStatsData{ + recordStatsData := calculator.RecordStatsData{ Type: objectName, Stats: statsData, Timestamp: time.Now(), diff --git a/pkg/stats/web_api/routes/calculated.go b/pkg/stats/web_api/routes/calculated.go new file mode 100644 index 00000000..438c0f5d --- /dev/null +++ b/pkg/stats/web_api/routes/calculated.go @@ -0,0 +1,24 @@ +package routes + +import ( + "context" + + "github.com/gofiber/fiber/v2" + "github.com/travigo/travigo/pkg/database" + "go.mongodb.org/mongo-driver/bson" +) + +func CalculatedRoute(c *fiber.Ctx) error { + collection := database.GetCollection("stats") + + var statsRecords []bson.M + cursor, _ := collection.Find(context.Background(), bson.M{}) + cursor.All(context.Background(), &statsRecords) + + statsRecordsMap := map[string]bson.M{} + for _, statsRecord := range statsRecords { + statsRecordsMap[statsRecord["type"].(string)] = statsRecord + } + + return c.JSON(statsRecordsMap) +} diff --git a/pkg/stats/web_api/server.go b/pkg/stats/web_api/server.go index 0e4d34f8..7a7c5289 100644 --- a/pkg/stats/web_api/server.go +++ b/pkg/stats/web_api/server.go @@ -15,5 +15,7 @@ func SetupServer(listen string) { group.Get("version", routes.APIVersion) routes.IdentificationRateRouter(group.Group("/identification_rate")) + group.Get("calculated", routes.CalculatedRoute) + webApp.Listen(listen) }