-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcharts.py
194 lines (172 loc) · 6.19 KB
/
charts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-
from svg.charts.plot import Plot
from svg.charts import time_series,bar
from datetime import timedelta,datetime,date,time
from dbentities import *
def myclamp( val, min, max ):
if val < min:
return min
elif val > max:
return max
else:
return val
class Charts:
def __init__(self,slbot,svgdir):
self.slbot = slbot
self.db = slbot.db
self.svgdir = svgdir
self.dateformat = '%Y-%m-%d'
def GetNewUserCount(self,begin,period):
session = self.db.sessionmaker()
ret = dict()
lobbies = session.query( Lobby ).all()
for lobby in lobbies:
ret[lobby.name] = []
end = begin + period
while end < datetime.now():
for lobby in lobbies:
ret[lobby.name].append( end.strftime(self.dateformat) )
ret[lobby.name].append( session.query( User ).filter( User.lobby_id == lobby.id ).filter( User.firstlogin <= end ).filter( User.firstlogin >= begin ).count() )
end += period
begin += period
end = datetime.now()
session.close()
return ret
def GetNewGameUserCount(self,begin,period,game_name):
session = self.db.sessionmaker()
ret = []
end = begin + period
while end < datetime.now():
ret.append( end.strftime(self.dateformat) )
ret.append( session.query( User ).filter( User.primary_game == game_name ).filter( User.firstlogin <= end ).filter( User.firstlogin >= begin ).count() )
end += period
begin += period
end = datetime.now()
session.close()
return ret
def GetGraph(self,title,period):
g = time_series.Plot({
'min_y_value': 0,
'step_include_first_x_label': True,
'area_fill': True,
#'show_x_guidelines': True,
'rotate_x_labels': True,
'x_label_format': self.dateformat,
'show_graph_subtitle': True,
'show_graph_title' : True,
'graph_title' : title,
'graph_subtitle' : period[2],
'timescale_divisions': "%d days"%(period[1].days) #so we get as many x_labels as datapoints
})
return g
def NewUsers(self,periods):
for period in periods:
data = self.GetNewUserCount(period[0],period[1])
g = self.GetGraph( 'New users per lobby', period )
for name in data.keys():
g.add_data({'data': data[name], 'title': name})
res = g.burn()
f = open(self.svgdir + '/newusers-' + period[2].replace(' ','_') + '.svg', 'w')
f.write(res)
f.close()
def GetRevCount(self,begin,period):
session = self.db.sessionmaker()
ret = dict()
base = dict()
valid = []
for i in range(23,45):
valid.append( '0.%d'%i )
revs_q = session.query( LobbyRevision ).filter( LobbyRevision.revision.in_( valid ) )
for rev in revs_q.all():
ret[rev.revision] = []
base[rev.revision] = session.query( User ).filter( User.lobbyrev_id == rev.id ).count()
end = begin + period
while end < datetime.now():
updates = session.query( LobbyUpdate ).filter( LobbyUpdate.date >= end ).all()
current = base.copy()
for update in updates:
#to get the current we have to 'update backwards'
if update.oldrev.revision in valid:
current[update.oldrev.revision] += 1
if update.newrev.revision in valid :
current[update.newrev.revision] -= 1
for rev in revs_q.all():
ret[rev.revision].append( end.strftime(self.dateformat) )
ret[rev.revision].append( myclamp(current[rev.revision],0,current[rev.revision]) )
end += period
begin += period
end = datetime.now()
session.close()
return ret
def LobbyRevs(self,periods):
for period in periods:
data = self.GetRevCount(period[0],period[1])
g = self.GetGraph( 'SpringLobby revisions', period )
g.width = 1024
g.height = 800
g.area_fill = False
for name in data.keys():
g.add_data({'data': data[name], 'title': name})
res = g.burn()
f = open(self.svgdir + '/sl_revisions-' + period[2].replace(' ','_') + '.svg', 'w')
f.write(res)
f.close()
def NewGameUsers(self,periods,game_name):
for period in periods:
data = self.GetNewGameUserCount(period[0],period[1],game_name)
g = self.GetGraph( 'New %s users'%game_name , period )
g.key = False #we only got one data entry anyways
g.add_data({'data': data, 'title': game_name })
res = g.burn()
f = open( "%s/new_%s_users-%s.svg"%( self.svgdir ,game_name,period[2].replace(' ','_') ), 'w')
f.write(res)
f.close()
def CurrentSLrevs(self,threshold):
session = self.db.sessionmaker()
revs_q = session.query( LobbyRevision ).filter( LobbyRevision.revision != 'unknown' )
datalinux = dict()
datawin = dict()
win_id = session.query( OperatingSystem ).filter( OperatingSystem.name == 'MicrosoftWindowsNT' ).first().id
linux_id = session.query( OperatingSystem ).filter( OperatingSystem.name == 'Linux' ).first().id
for rev in revs_q.all():
count = session.query( User ).filter( User.lobbyrev_id == rev.id ).count()
if count > threshold:
datawin[rev.revision] = session.query( User ).filter( User.lobbyrev_id == rev.id ).filter( User.os_id == win_id ).count()
datalinux[rev.revision] = session.query( User ).filter( User.lobbyrev_id == rev.id ).filter( User.os_id == linux_id ).count()
g = bar.VerticalBar(datawin.keys())
g.min_y_value=0
g.width = 1024
g.height = 800
g.stack = 'side'
g.step_include_first_x_label=True
#'area_fill': True,
#'show_x_guidelines': True,
g.rotate_x_labels=True
#g.x_label_format=self.dateformat,
g.show_graph_subtitle=True
g.show_graph_title=True
g.graph_title='SpringLobby revisions in use'
g.graph_subtitle='current'
g.add_data({'data': datawin.values(), 'title': 'Windows'})
g.add_data({'data': datalinux.values(), 'title': 'Linux'})
res = g.burn()
f = open( "%s/current-sl-revs.svg"%( self.svgdir ), 'w')
f.write(res)
f.close()
def All(self):
periods = []
now = datetime.now()
today = datetime.combine(date.today(), time.min ) #datetime( now.year, now.month, now.day )
lastweek = today - timedelta(days=7)
inc = timedelta(days=1)
periods.append( [lastweek, inc, 'Last week'] )
lastmonths = today - timedelta(days=90)
inc = timedelta(days=7)
periods.append ( [lastmonths, inc, 'Last 3 months'] )
lastyear = today - timedelta(days=365)
inc = timedelta(days=30)
periods.append ( [lastyear, inc, 'Last year'] )
self.NewUsers(periods)
self.NewGameUsers(periods,self.slbot.app.config.get('gamebot', 'mod'))
self.LobbyRevs(periods)
self.CurrentSLrevs(20)