frodo-wii/network-broker/stats.py

155 lines
5.5 KiB
Python
Raw Normal View History

2009-04-08 18:33:10 +02:00
import sys, pickle, time
2009-03-31 19:16:10 +02:00
from operator import itemgetter
class Container:
def __init__(self):
2009-03-31 19:27:47 +02:00
self.total_connections = 0
2009-03-31 19:16:10 +02:00
self.country_count = {}
self.last_10 = []
self.messages = []
self.nr_active = 0
self.nr_waiting = 0
def set_nr_active(self, nr_active):
self.nr_active = nr_active
def set_nr_waiting(self, nr_waiting):
self.nr_waiting = nr_waiting
2009-03-31 19:16:10 +02:00
2009-04-08 18:33:10 +02:00
def copy_from_other(self, other):
try:
self.nr_active = 0 # Always reset
self.nr_waiting = 0
2009-04-08 18:33:10 +02:00
self.total_connections = other.total_connections
self.country_count = other.country_count
self.last_10 = other.last_10
self.messages = other.messages
2009-04-08 18:33:10 +02:00
except:
pass
2009-03-31 19:16:10 +02:00
def add_country(self, country):
try:
cur = self.country_count[country]
except KeyError, e:
cur = 0
self.country_count[country] = cur + 1
def set_messages(self, messages):
self.messages = messages
2009-03-31 19:16:10 +02:00
def add_connection(self, who, country):
2009-04-08 18:33:10 +02:00
time_now = time.strftime("%Y-%m-%d %H:%M", time.gmtime())
s = "%s - %s (%s)" % (time_now, who, country)
2009-03-31 19:27:47 +02:00
self.total_connections = self.total_connections + 1
2009-03-31 19:16:10 +02:00
self.last_10 = [s] + self.last_10[:9]
self.add_country(country)
class HtmlGenerator:
def __init__(self, container):
self.container = container
def generate(self, outf):
sorted_countries = sorted(self.container.country_count.items(),
reverse=True, key=itemgetter(1))
outf.write("<html><body>\n")
2009-03-31 19:27:47 +02:00
outf.write("<H2>Frodo-Wii network statistics</H2>\n")
2009-11-01 17:12:22 +01:00
outf.write("The total number of connections is <b>%d</b><br><br>\n" % (self.container.total_connections))
outf.write("There are currently <b>%d</b> players waiting for connections and <b>%d</b> players playing<br><br>\n" % (self.container.nr_waiting, self.container.nr_active))
2009-11-01 12:15:37 +01:00
outf.write("<TABLE border=\"0\" cellpadding=\"0\">\n")
2009-11-02 07:35:31 +01:00
outf.write("<TR><TD><H3>Last %d connections</H3></TD><TD>&nbsp;</TD<TD colspan=4><H3>Random connection screenshots</TD></TR>\n" %
2009-11-01 12:15:37 +01:00
(len(self.container.last_10)) )
2009-03-31 19:16:10 +02:00
2009-11-01 12:15:37 +01:00
cnt = 0
for item in self.container.last_10:
images = ""
if cnt % 4 == 0:
cur = cnt
images = "<TH ROWSPAN=4><IMG SRC=\"images/%d.png\"></TH><TH ROWSPAN=4><IMG SRC=\"images/%d.png\"></TH><TH ROWSPAN=4><IMG SRC=\"images/%d.png\"></TH><TH ROWSPAN=4><IMG SRC=\"images/%d.png\"></TH>" % (cur, cur + 1, cur + 2, cur + 3)
2009-11-01 17:12:22 +01:00
outf.write("<TR><TD>%s</TD><TD>&nbsp;</TD>%s</TR>\n" % (item, images) )
2009-11-01 12:15:37 +01:00
cnt = cnt + 1
2009-11-01 17:12:22 +01:00
outf.write("<TR><TD>&nbsp;</TD></TR>")
2009-11-01 12:15:37 +01:00
outf.write("</TABLE>\n")
2009-11-01 17:12:22 +01:00
outf.write("<br><br><TABLE border=\"0\" cellpadding=\"0\">\n")
2009-11-03 21:36:41 +01:00
outf.write("<TR><TD colspan=3><H3>List of countries</H3></TD></TR>\n")
2009-03-31 19:16:10 +02:00
count = 1
2009-11-03 21:21:52 +01:00
outf.write("<H3>Last server messages</H3>\n")
2010-02-28 09:56:45 +01:00
outf.write("<UL>\n")
for msg in self.container.messages:
2010-02-28 09:56:45 +01:00
outf.write("<LI>%s</LI>\n" % (msg))
outf.write("</UL><br>")
2009-11-03 21:21:52 +01:00
n_countries = len(sorted_countries)
2009-11-03 21:36:41 +01:00
for i in range(0, n_countries / 3):
2009-11-03 21:21:52 +01:00
c1, n1 = sorted_countries[i]
2009-11-03 21:36:41 +01:00
c2, n2 = sorted_countries[i + n_countries / 3]
c3, n3 = sorted_countries[i + (n_countries / 3) * 2]
outf.write("<TR><TD><b>%3d</b></TD><TD>&nbsp;</TD><TD>%s (%d)</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD><b>%3d</b></TD><TD>&nbsp;</TD><TD>%s (%d)</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD><b>%3d</b></TD><TD>&nbsp;</TD><TD>%s (%d)</TD></TR>\n" %
(count, c1, n1, count + n_countries / 3, c2, n2, count + (n_countries / 3) * 2, c3, n3) )
2009-03-31 19:16:10 +02:00
count = count + 1
2009-11-03 21:36:41 +01:00
# Output the last if it's odd
for i in range(0, n_countries % 3):
c1, n1 = sorted_countries[n_countries - i - 1]
outf.write("<TR><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD><b>%3d</b></TD><TD>&nbsp;</TD><TD>%s (%d)</TD></TR>\n" %
(n_countries - n_countries % 3 + i + 1, c1, n1) )
2009-11-01 17:12:22 +01:00
outf.write("</TABLE>\n")
2009-03-31 19:16:10 +02:00
outf.write("</body></html>\n")
g_stat = None
def save(filename):
global g_stat
of = open(filename, "wb")
pickle.dump(g_stat, of)
of.close()
def generate_html(filename):
of = open(filename, "wb")
hg = HtmlGenerator(g_stat)
hg.generate(of)
of.close()
def load(filename):
global g_stat
2009-04-08 18:33:10 +02:00
g_stat = Container()
2009-03-31 19:16:10 +02:00
try:
of = open(filename, "r")
2009-04-08 18:33:10 +02:00
other = pickle.load(of)
g_stat.copy_from_other(other)
2009-03-31 19:16:10 +02:00
of.close()
except:
2009-04-08 18:33:10 +02:00
pass
2009-03-31 19:16:10 +02:00
def update_peer_nr(waiting, active):
g_stat.set_nr_waiting(waiting)
g_stat.set_nr_active(active)
2009-03-31 19:16:10 +02:00
def add_connection(who, country):
g_stat.add_connection(who, country)
def set_messages(messages):
g_stat.set_messages(messages)
2009-03-31 19:16:10 +02:00
if __name__ == "__main__":
load("/tmp/vobb")
for i in range(0, 10):
add_connection("MABOO", "Unknown country")
add_connection("SIMONK", "Sweden")
add_connection("SIMONK", "Sweden")
add_connection("SIMONK", "Sweden")
add_connection("Linda", "Germany")
add_connection("Linda", "Germany")
set_messages(["Hej", "Who framed Roger Rabbit?", "IK+ at 19.00 CET?"])
2009-03-31 19:16:10 +02:00
save("/tmp/vobb")
hg = HtmlGenerator(g_stat)
hg.generate(sys.stdout)