Add more stats

This commit is contained in:
sqozz 2018-05-11 03:24:35 +02:00
parent 95b564a6e3
commit 0627f2564e

View file

@ -4,7 +4,7 @@ import json
import logging import logging
from pprint import pprint from pprint import pprint
from websocket import create_connection from websocket import create_connection
from prometheus_client import Counter, Summary, start_http_server from prometheus_client import Counter, Gauge, Summary, start_http_server
from prometheus_client.core import CounterMetricFamily, REGISTRY from prometheus_client.core import CounterMetricFamily, REGISTRY
import time import time
@ -19,19 +19,19 @@ MESSAGES = { "GameInfo" : 0x00,
class BotDeathCollector(object): class BotDeathCollector(object):
def collect(self): def collect(self):
c = CounterMetricFamily("deaths", "Deaths of each player", labels=["name", "bot_rev"]) c = CounterMetricFamily("deaths", "Deaths of each player", labels=["name", "rev"])
for user in game.users.users: for user in game.users.users:
user = game.users.users.get(user) user = game.users.users.get(user)
c.add_metric([user.name, user.current_rev], user.deaths) c.add_metric([user.name, str(user.current_rev)], user.deaths)
yield c yield c
class BotKillCollector(object): class BotKillCollector(object):
def collect(self): def collect(self):
c = CounterMetricFamily("kills", "Kills of each player", labels=["name", "bot_rev"]) c = CounterMetricFamily("kills", "Kills of each player", labels=["name", "rev"])
for user in game.users.users: for user in game.users.users:
user = game.users.users.get(user) user = game.users.users.get(user)
c.add_metric([user.name, user.current_rev], user.kills) c.add_metric([user.name, str(user.current_rev)], user.kills)
yield c yield c
@ -102,9 +102,8 @@ class Game():
user = self.users.get_user(bot.get("db_id")) user = self.users.get_user(bot.get("db_id"))
#print("{}({}): respawn".format(user.name, user.db_id)) #print("{}({}): respawn".format(user.name, user.db_id))
elif typ == MESSAGES["GameInfo"]: elif typ == MESSAGES["GameInfo"]:
pdb.set_trace() game.x = js.get("world_size_c")
game.x = 0 game.y = js.get("world_size_y")
game.y = 0
elif typ == MESSAGES["BotKill"]: elif typ == MESSAGES["BotKill"]:
try: try:
victim = self.users.get_user_by_rev(js.get("victim_id")) victim = self.users.get_user_by_rev(js.get("victim_id"))
@ -124,8 +123,6 @@ class Game():
msg_data = js.get("data") msg_data = js.get("data")
for rev in msg_data: for rev in msg_data:
user = self.users.get_user_by_rev(rev) user = self.users.get_user_by_rev(rev)
print(user)
pdb.set_trace()
pass pass
elif typ == MESSAGES["WorldUpdate"]: elif typ == MESSAGES["WorldUpdate"]:
bots = js.get("bots") bots = js.get("bots")
@ -163,8 +160,6 @@ class BotUser():
@deaths.setter @deaths.setter
def deaths(self, deaths): def deaths(self, deaths):
self.__deaths = deaths self.__deaths = deaths
if self.__deaths % 2 == 0:
print("{} was already killed {} times".format(self.name, self.__deaths))
self.revs.get(self.current_rev).deaths += 1 self.revs.get(self.current_rev).deaths += 1
def parse(self, data): def parse(self, data):
@ -184,7 +179,8 @@ class BotUser():
if self.current_rev != data.get("id"): if self.current_rev != data.get("id"):
self.current_rev = data.get("id") self.current_rev = data.get("id")
else: else:
print("{} new code-version".format(self.name)) #print("{} new code-version".format(self.name))
pass
self.revs.update({ self.current_rev : BotRevision(data) }) self.revs.update({ self.current_rev : BotRevision(data) })
@ -205,8 +201,10 @@ class BotRevision():
class UserList(): class UserList():
def __init__(self): def __init__(self):
self.users = {} self.users = {}
self.__usercount = Gauge('Users', 'Total count of active players')
def update_by_db(self, db_id, data): def update_by_db(self, db_id, data):
self.__usercount.set(len(self.users))
if data.get("db_id") in self.users.keys(): if data.get("db_id") in self.users.keys():
self.users.get(data.get("db_id")).update(data) self.users.get(data.get("db_id")).update(data)
else: else: