Skip to content

Commit

Permalink
add kill activity
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGruber committed Sep 23, 2024
1 parent 571999f commit 4908427
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 25 deletions.
38 changes: 27 additions & 11 deletions charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (
)

// generate random data for bar chart
func generateLineItems(data []ConnectionsPerMonth) []opts.LineData {
func generateLineItems(data []CountPerTime) []opts.LineData {
items := make([]opts.LineData, 0)
for i := 0; i < len(data); i++ {
items = append(items, opts.LineData{Value: data[i].Count})
}
return items
}

func getXaxis(ConnectionDatas []ConnectionsPerMonth) []string {
func getXaxis(ConnectionDatas []CountPerTime) []string {
xais := make([]string, 0)
for _, v := range ConnectionDatas {
xais = append(xais, v.Id)
Expand All @@ -30,8 +30,8 @@ func getXaxis(ConnectionDatas []ConnectionsPerMonth) []string {
}

// Since servers were not all created a the same time
func populateMissingConnectionsData(all []ConnectionsPerMonth, current []ConnectionsPerMonth) []ConnectionsPerMonth {
result := make([]ConnectionsPerMonth, len(all))
func populateMissingConnectionsData(all []CountPerTime, current []CountPerTime) []CountPerTime {
result := make([]CountPerTime, len(all))
for i := 0; i < len(result); i++ {
for ry := 0; ry < len(current); ry++ {
if current[ry].Id == all[i].Id {
Expand All @@ -43,7 +43,7 @@ func populateMissingConnectionsData(all []ConnectionsPerMonth, current []Connect
return result
}

func createConnectionsChart(name string, allConnections []ConnectionsPerMonth, connectionDatas []ConnectionData) {
func createLineCountChart(name string, allConnections []CountPerTime, connectionDatas []CountData) {
// create a new line instance
line := charts.NewLine()
line.SetGlobalOptions(charts.WithTitleOpts(opts.Title{
Expand Down Expand Up @@ -171,24 +171,40 @@ func genCharts(db *mongo.Database, mongoCtx context.Context) {
createTop10KillerChart("Top Killer Main US 1", top)
top = getTopKiller(db, mongoCtx, 61, "zombie")
createTop10KillerChart("Top Zombie Killer Help", top)
connectionsDatas := make([]ConnectionData, 0)
allKills := getAllKills(db, mongoCtx)

killsDatas := make([]CountData, 0)
for _, v := range officialServers {
// TODO: shitty it's to avoid help server but it should ignore pve servers in general
if v.ServerId == 61 {
continue
}
data := getKillsToServer(db, mongoCtx, v.ServerId)
if len(data) == 0 {
continue
}
killsDatas = append(killsDatas, CountData{name: v.Name + " " + v.Region, data: data})
}
createLineCountChart("Kill activity", allKills, killsDatas)

connectionsDatas := make([]CountData, 0)
allConnections := getAllConnections(db, mongoCtx)
for _, v := range officialServers {
data := getConnectionsToServer(db, mongoCtx, v.ServerId)
if len(data) == 0 {
continue
}
connectionsDatas = append(connectionsDatas, ConnectionData{name: v.Name + " " + v.Region, data: data})
connectionsDatas = append(connectionsDatas, CountData{name: v.Name + " " + v.Region, data: data})
}
createConnectionsChart("connections", allConnections, connectionsDatas)
lastMonthConnectionsDatas := make([]ConnectionData, 0)
createLineCountChart("connections", allConnections, connectionsDatas)
lastMonthConnectionsDatas := make([]CountData, 0)
allConnectionsLastMonth := getAllConnectionsLastMonth(db, mongoCtx)
for _, v := range officialServers {
data := getConnectionsLastMonthToServer(db, mongoCtx, v.ServerId)
if len(data) == 0 {
continue
}
lastMonthConnectionsDatas = append(lastMonthConnectionsDatas, ConnectionData{name: v.Name + " " + v.Region, data: data})
lastMonthConnectionsDatas = append(lastMonthConnectionsDatas, CountData{name: v.Name + " " + v.Region, data: data})
}
createConnectionsChart("Last month connections", allConnectionsLastMonth, lastMonthConnectionsDatas)
createLineCountChart("Last month connections", allConnectionsLastMonth, lastMonthConnectionsDatas)
}
5 changes: 0 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import (
"time"
)

type ConnectionData struct {
name string
data []ConnectionsPerMonth
}

const REFRESH_TIME = 600

func main() {
Expand Down
41 changes: 33 additions & 8 deletions mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,50 +91,75 @@ func getCharacters(db *mongo.Database, mongoCtx context.Context, serverId uint32
return results
}

func getConnectionsToServer(db *mongo.Database, mongoCtx context.Context, serverId uint32) []ConnectionsPerMonth {
func getConnectionsToServer(db *mongo.Database, mongoCtx context.Context, serverId uint32) []CountPerTime {
ConnectionsCollection := db.Collection(CONNECTIONS_COLLECTION_NAME)
pipeline := getConnectionsPerServerPipeline(serverId)
cursor, error := ConnectionsCollection.Aggregate(mongoCtx, pipeline)
if error != nil {
panic(error)
}
var results []ConnectionsPerMonth
var results []CountPerTime
cursor.All(mongoCtx, &results)
return results
}

func getConnectionsLastMonthToServer(db *mongo.Database, mongoCtx context.Context, serverId uint32) []ConnectionsPerMonth {
func getConnectionsLastMonthToServer(db *mongo.Database, mongoCtx context.Context, serverId uint32) []CountPerTime {
ConnectionsCollection := db.Collection(CONNECTIONS_COLLECTION_NAME)
pipeline := getConnectionsLastMonthPerServerPipeline(serverId)
cursor, error := ConnectionsCollection.Aggregate(mongoCtx, pipeline)
if error != nil {
panic(error)
}
var results []ConnectionsPerMonth
var results []CountPerTime
cursor.All(mongoCtx, &results)
return results
}

func getAllConnections(db *mongo.Database, mongoCtx context.Context) []ConnectionsPerMonth {
func getKillsToServer(db *mongo.Database, mongoCtx context.Context, serverId uint32) []CountPerTime {
ConnectionsCollection := db.Collection(KILLS_COLLECTION_NAME)
pipeline := getKillsPerServerPipeline(serverId)
cursor, error := ConnectionsCollection.Aggregate(mongoCtx, pipeline)
if error != nil {
panic(error)
}
var results []CountPerTime
cursor.All(mongoCtx, &results)
return results
}

func getAllKills(db *mongo.Database, mongoCtx context.Context) []CountPerTime {
ConnectionsCollection := db.Collection(KILLS_COLLECTION_NAME)
// TODO: change this to only get players kills
pipeline := getAllKillsPipeline()
cursor, error := ConnectionsCollection.Aggregate(mongoCtx, pipeline)
if error != nil {
panic(error)
}
var results []CountPerTime
cursor.All(mongoCtx, &results)
return results
}

func getAllConnections(db *mongo.Database, mongoCtx context.Context) []CountPerTime {
ConnectionsCollection := db.Collection(CONNECTIONS_COLLECTION_NAME)
pipeline := getAllConnectionsPipeline()
cursor, error := ConnectionsCollection.Aggregate(mongoCtx, pipeline)
if error != nil {
panic(error)
}
var results []ConnectionsPerMonth
var results []CountPerTime
cursor.All(mongoCtx, &results)
return results
}

func getAllConnectionsLastMonth(db *mongo.Database, mongoCtx context.Context) []ConnectionsPerMonth {
func getAllConnectionsLastMonth(db *mongo.Database, mongoCtx context.Context) []CountPerTime {
ConnectionsCollection := db.Collection(CONNECTIONS_COLLECTION_NAME)
pipeline := getAllConnectionsLastMonthPipeline()
cursor, error := ConnectionsCollection.Aggregate(mongoCtx, pipeline)
if error != nil {
panic(error)
}
var results []ConnectionsPerMonth
var results []CountPerTime
cursor.All(mongoCtx, &results)
return results
}
38 changes: 38 additions & 0 deletions mongo_pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,44 @@ func getAllConnectionsLastMonthPipeline() mongo.Pipeline {
return pipeline
}

func getKillsPerServerPipeline(serverId uint32) mongo.Pipeline {
// TODO: could be great if we could merge getAllKillsPipeline in it
pipeline := mongo.Pipeline{
{{"$match", bson.D{{"serverId", serverId}}}},
{{"$match", bson.D{{"type", "player"}}}},
{{"$addFields", bson.D{{"creationDate", bson.D{{"$toDate", "$_id"}}}}}},
{{"$addFields", bson.D{{"day", bson.D{{"$dateToString", bson.D{
{"format", "%Y-%m-%d"},
{"date", "$creationDate"},
}}}}}}},
{{"$group", bson.D{
{"_id", "$day"},
{"count", bson.D{{"$sum", 1}}},
}}},
{{"$sort", bson.D{{"_id", 1}}}},
}

return pipeline
}

func getAllKillsPipeline() mongo.Pipeline {
pipeline := mongo.Pipeline{
{{"$match", bson.D{{"type", "player"}}}},
{{"$addFields", bson.D{{"creationDate", bson.D{{"$toDate", "$_id"}}}}}},
{{"$addFields", bson.D{{"day", bson.D{{"$dateToString", bson.D{
{"format", "%Y-%m-%d"},
{"date", "$creationDate"},
}}}}}}},
{{"$group", bson.D{
{"_id", "$day"},
{"count", bson.D{{"$sum", 1}}},
}}},
{{"$sort", bson.D{{"_id", 1}}}},
}

return pipeline
}

func getAllConnectionsPipeline() mongo.Pipeline {
pipeline := mongo.Pipeline{
// {{"$match", bson.D{
Expand Down
6 changes: 5 additions & 1 deletion mongo_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

type CountData struct {
name string
data []CountPerTime
}
type CountPerServerResult struct {
Count uint32 `bson:"count"`
}

type Kills struct {
ServerId uint32 `bson:"serverId" json:"serverId"`
}
type ConnectionsPerMonth struct {
type CountPerTime struct {
Id string `bson:"_id" json:"_id"`
Count uint32 `bson:"count" json:"count"`
}
Expand Down
3 changes: 3 additions & 0 deletions template.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ <h1>H1emu Charts</h1>
<li>
<a href="./Top Zombie Killer Help.html">Top Zombie Killer Help</a>
</li>
<li>
<a href="./Kill activity.html">Kill activity</a>
</li>
</ul>
</nav>
</div>
Expand Down

0 comments on commit 4908427

Please sign in to comment.