Skip to content

Commit

Permalink
more data
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronClaydon committed Nov 7, 2024
1 parent 130cf82 commit 56c63ac
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 17 deletions.
12 changes: 8 additions & 4 deletions pkg/stats/calculator/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions pkg/stats/calculator/recordstats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package calculator

import "time"

type RecordStatsData struct {
Type string `json:"-"`
Stats interface{}
Timestamp time.Time
}
4 changes: 4 additions & 0 deletions pkg/stats/calculator/servicealerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type ServiceAlertsStats struct {
Total int
Active int
Inactive int

Datasources map[string]int
}

func GetServiceAlerts() ServiceAlertsStats {
Expand Down Expand Up @@ -42,5 +44,7 @@ func GetServiceAlerts() ServiceAlertsStats {
Total: numberActiveAlerts + numberInactiveAlerts,
Active: numberActiveAlerts,
Inactive: numberInactiveAlerts,

Datasources: CountAggregate(collection, "$datasource.datasetid"),
}
}
16 changes: 13 additions & 3 deletions pkg/stats/calculator/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 7 additions & 3 deletions pkg/stats/calculator/stops.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
32 changes: 32 additions & 0 deletions pkg/stats/calculator/util.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 1 addition & 7 deletions pkg/stats/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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(),
Expand Down
24 changes: 24 additions & 0 deletions pkg/stats/web_api/routes/calculated.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 2 additions & 0 deletions pkg/stats/web_api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 56c63ac

Please sign in to comment.