From 0258f92306c9cc17d737057079bb717f11df5eb5 Mon Sep 17 00:00:00 2001 From: quentingruber Date: Sat, 23 Nov 2024 15:02:35 +0100 Subject: [PATCH] only display connections on a year scope --- charts.go | 4 ++-- mongo.go | 12 ++++++++++++ mongo_pipelines.go | 30 +++++++++++++++++++++++++++--- template.html | 4 +++- 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/charts.go b/charts.go index 79ffb2f..cb71fd0 100644 --- a/charts.go +++ b/charts.go @@ -208,7 +208,7 @@ func genCharts(db *mongo.Database, mongoCtx context.Context) { createLineCountChart("Zombie kill activity", allZombieKills, zombieKillsDatas) connectionsDatas := make([]CountData, 0) - allConnections := getAllConnections(db, mongoCtx) + allConnectionsLastYear := getAllConnectionsLastYear(db, mongoCtx) for _, v := range officialServers { data := getConnectionsToServer(db, mongoCtx, v.ServerId) if len(data) == 0 { @@ -216,7 +216,7 @@ func genCharts(db *mongo.Database, mongoCtx context.Context) { } connectionsDatas = append(connectionsDatas, CountData{name: v.Name + " " + v.Region, data: data}) } - createLineCountChart("connections", allConnections, connectionsDatas) + createLineCountChart("Last year connections", allConnectionsLastYear, connectionsDatas) lastMonthConnectionsDatas := make([]CountData, 0) allConnectionsLastMonth := getAllConnectionsLastMonth(db, mongoCtx) for _, v := range officialServers { diff --git a/mongo.go b/mongo.go index 7759bc2..a000dbb 100644 --- a/mongo.go +++ b/mongo.go @@ -165,6 +165,18 @@ func getAllConnections(db *mongo.Database, mongoCtx context.Context) []CountPerT return results } +func getAllConnectionsLastYear(db *mongo.Database, mongoCtx context.Context) []CountPerTime { + ConnectionsCollection := db.Collection(CONNECTIONS_COLLECTION_NAME) + pipeline := getAllConnectionsLastYearPipeline() + cursor, error := ConnectionsCollection.Aggregate(mongoCtx, pipeline) + if error != nil { + panic(error) + } + var results []CountPerTime + cursor.All(mongoCtx, &results) + return results +} + func getAllConnectionsLastMonth(db *mongo.Database, mongoCtx context.Context) []CountPerTime { ConnectionsCollection := db.Collection(CONNECTIONS_COLLECTION_NAME) pipeline := getAllConnectionsLastMonthPipeline() diff --git a/mongo_pipelines.go b/mongo_pipelines.go index 710e0a2..7f2e63a 100644 --- a/mongo_pipelines.go +++ b/mongo_pipelines.go @@ -126,6 +126,33 @@ func getAllConnectionsLastMonthPipeline() mongo.Pipeline { return pipeline } +func getAllConnectionsLastYearPipeline() mongo.Pipeline { + now := time.Now() + gte := time.Date(now.Year(), 0, 0, 0, 0, 0, 0, time.UTC) + + pipeline := mongo.Pipeline{ + {{"$addFields", bson.D{{"creationDate", bson.D{{"$toDate", "$_id"}}}}}}, + {{"$match", bson.D{ + {"creationDate", bson.D{ + {"$gte", gte}, + {"$lt", now}, + }}, + }}}, + + {{"$addFields", bson.D{{"yearMonth", bson.D{{"$dateToString", bson.D{ + {"format", "%Y-%m"}, + {"date", "$creationDate"}, + }}}}}}}, + {{"$group", bson.D{ + {"_id", "$yearMonth"}, + {"count", bson.D{{"$sum", 1}}}, + }}}, + {{"$sort", bson.D{{"_id", 1}}}}, + } + + return pipeline +} + func getKillsPerServerPipeline(serverId uint32, entityType string) mongo.Pipeline { pipeline := mongo.Pipeline{ {{"$match", bson.D{{"serverId", serverId}}}}, @@ -165,9 +192,6 @@ func getAllKillsPipeline(entityType string) mongo.Pipeline { func getAllConnectionsPipeline() mongo.Pipeline { pipeline := mongo.Pipeline{ - // {{"$match", bson.D{ - // {"_id", bson.D{{"$gte", primitive.NewObjectIDFromTimestamp(time.Date(2023, 2, 01, 0, 0, 0, 0, time.UTC))}}}, - // }}}, {{"$addFields", bson.D{{"creationDate", bson.D{{"$toDate", "$_id"}}}}}}, {{"$addFields", bson.D{{"yearMonth", bson.D{{"$dateToString", bson.D{ {"format", "%Y-%m"}, diff --git a/template.html b/template.html index 56a8f68..a88a227 100644 --- a/template.html +++ b/template.html @@ -25,7 +25,9 @@

H1emu Charts

  • Last Month Connections
  • -
  • All Connections
  • +
  • + Last Year Connections +
  • Playtime
  • Playtime Top EU :)