-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathget-stats.py
executable file
·193 lines (177 loc) · 9 KB
/
get-stats.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
#!/usr/bin/env python
#
# get-stats.py
#
# Simple wrapper that giving some stats on specific Wekan dashboard
#
# Author: Florent MONTHEL ([email protected])
#
#! /usr/bin/python
import os
import argparse
import datetime
import logging
import ConfigParser
from terminaltables import AsciiTable
from lib.wsmotor import WsMotor
def main() :
# Parameters
file_config = os.path.join(os.path.dirname(__file__), 'conf/config.ini')
Config = ConfigParser.ConfigParser()
Config.read(file_config)
# Logging setup
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(Config.get('GLOBAL','application'))
handler = logging.FileHandler(os.path.join(os.path.dirname(__file__), 'log/'+Config.get('GLOBAL','application')+'.log'))
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
# Options
parser = argparse.ArgumentParser(description='Get some stats on Wekan Dashboard')
parser.add_argument('--board', action='store', dest='board', help='Board name indicated in ini file - Example : my-board', required=True)
parser.add_argument('--action', action='store', dest='action', choices=['list-stats', 'label-stats', 'user-stats', 'event-stats'], required=True)
args = parser.parse_args()
# WsMotor object instance
try :
inst_wsmotor = WsMotor(Config.get('GLOBAL','application'))
# Parse JSON Wekan URLs and populate dict
dic_wekan = dict()
for board, url in Config.items('WEKAN_JSON') :
# Is it board that we want ?
if board != args.board :
continue
# Populate dic
inst_wsmotor.get_data_from_json(board, url)
# Get boad data
dic_wekan = inst_wsmotor.data[ args.board ]
# Board not found
if len(dic_wekan) == 0 :
raise RuntimeError('Board "' + args.board + '" not found in ini file :(')
except Exception as e :
logger.error('RunTimeError during instance creation : %s', str(e))
raise RuntimeError('Exception during instance creation : ' + str(e))
# Stats on lists
if args.action == 'list-stats' :
# Ascii table for List
myAsciiTableList = [['List name','Event(s) generated','NB of live card(s)','NB of archived card(s)','Total card(s)']]
events_total = 0
cards_live_total = 0
cards_arch_total = 0
cards_total = 0
for (k,v) in sorted(dic_wekan['lists_sort'].items()) :
events_total = events_total + len(dic_wekan['lists'][ v ]['events']['all'])
cards_live_total = cards_live_total + len(dic_wekan['lists'][ v ]['cards_live'])
cards_arch_total = cards_arch_total + len(dic_wekan['lists'][ v ]['cards_arch'])
cards_total = cards_total + len(dic_wekan['lists'][ v ]['cards_live']) + len(dic_wekan['lists'][ v ]['cards_arch'])
tmpdata = list()
tmpdata.append(dic_wekan['lists'][ v ]['name']) # ListName
tmpdata.append(str(len(dic_wekan['lists'][ v ]['events']['all']))) # Total events
tmpdata.append(str(len(dic_wekan['lists'][ v ]['cards_live']))) # Live cards
tmpdata.append(str(len(dic_wekan['lists'][ v ]['cards_arch']))) # Archive cards
tmpdata.append(str(len(dic_wekan['lists'][ v ]['cards_arch'])+len(dic_wekan['lists'][ v ]['cards_live']))) # Total cards
myAsciiTableList.append(tmpdata)
# Total for List
tmpdata = list()
tmpdata.append("Total : " + str(len(myAsciiTableList) - 1) + " list(s)")
tmpdata.append(str(events_total))
tmpdata.append(str(cards_live_total))
tmpdata.append(str(cards_arch_total))
tmpdata.append(str(cards_total))
myAsciiTableList.append(tmpdata)
# Create AsciiTable for List
myTable = AsciiTable(myAsciiTableList)
myTable.inner_footing_row_border = True
myTable.justify_columns[1] = myTable.justify_columns[2] = myTable.justify_columns[3] = myTable.justify_columns[4] = 'right'
# Output data
print myTable.table
# Stats on labels
if args.action == 'label-stats' :
# Ascii table for Label
myAsciiTableLabel = [['Label name','NB of live card(s)','NB of archived card(s)','Total card(s)']]
cards_live_total = 0
cards_arch_total = 0
cards_total = 0
for (k,v) in sorted(dic_wekan['labels_sort'].items(),reverse=True) :
cards_live_total = cards_live_total + len(dic_wekan['labels'][ v ]['cards_live'])
cards_arch_total = cards_arch_total + len(dic_wekan['labels'][ v ]['cards_arch'])
cards_total = cards_total + len(dic_wekan['labels'][ v ]['cards_live']) + len(dic_wekan['labels'][ v ]['cards_arch'])
tmpdata = list()
tmpdata.append(dic_wekan['labels'][ v ]['name']) # ListName
tmpdata.append(str(len(dic_wekan['labels'][ v ]['cards_live']))) # Live cards
tmpdata.append(str(len(dic_wekan['labels'][ v ]['cards_arch']))) # Archive cards
tmpdata.append(str(len(dic_wekan['labels'][ v ]['cards_arch'])+len(dic_wekan['labels'][ v ]['cards_live']))) # Total cards
myAsciiTableLabel.append(tmpdata)
# Total for Label
tmpdata = list()
tmpdata.append("Total : " + str(len(myAsciiTableLabel) - 1) + " label(s)")
tmpdata.append(str(cards_live_total))
tmpdata.append(str(cards_arch_total))
tmpdata.append(str(cards_total))
myAsciiTableLabel.append(tmpdata)
# Create AsciiTable for Label
myTable = AsciiTable(myAsciiTableLabel)
myTable.inner_footing_row_border = True
myTable.justify_columns[1] = myTable.justify_columns[2] = myTable.justify_columns[3] = 'right'
# Output data
print myTable.table
# Stats on users
if args.action == 'user-stats' :
# Ascii table for User
myAsciiTableUser = [['Username','Event(s) generated','NB of live card(s)','NB of archived card(s)','Total card(s)']]
events_total = 0
cards_live_total = 0
cards_arch_total = 0
cards_total = 0
for (k,v) in sorted(dic_wekan['users_sort'].items(),reverse=True) :
if len(dic_wekan['users'][ v ]['cards_live']) + len(dic_wekan['users'][ v ]['cards_arch']) == 0 :
continue
events_total = events_total + len(dic_wekan['users'][ v ]['events']['all'])
cards_live_total = cards_live_total + len(dic_wekan['users'][ v ]['cards_live'])
cards_arch_total = cards_arch_total + len(dic_wekan['users'][ v ]['cards_arch'])
cards_total = cards_total + len(dic_wekan['users'][ v ]['cards_live']) + len(dic_wekan['users'][ v ]['cards_arch'])
tmpdata = list()
tmpdata.append(dic_wekan['users'][ v ]['username']) # Username
tmpdata.append(str(len(dic_wekan['users'][ v ]['events']['all']))) # Total events
tmpdata.append(str(len(dic_wekan['users'][ v ]['cards_live']))) # Live cards
tmpdata.append(str(len(dic_wekan['users'][ v ]['cards_arch']))) # Archive cards
tmpdata.append(str(len(dic_wekan['users'][ v ]['cards_arch'])+len(dic_wekan['users'][ v ]['cards_live']))) # Total cards
myAsciiTableUser.append(tmpdata)
# Total for users
tmpdata = list()
tmpdata.append("Total : " + str(len(myAsciiTableUser) - 1) + " User(s)")
tmpdata.append(str(events_total))
tmpdata.append(str(cards_live_total))
tmpdata.append(str(cards_arch_total))
tmpdata.append(str(cards_total))
myAsciiTableUser.append(tmpdata)
# Create AsciiTable for User
myTable = AsciiTable(myAsciiTableUser)
myTable.inner_footing_row_border = True
myTable.justify_columns[1] = myTable.justify_columns[2] = myTable.justify_columns[3] = myTable.justify_columns[4] = 'right'
# Output data
print myTable.table
# Stats on events
if args.action == 'event-stats' :
# Ascii table for Label
myAsciiEventLabel = [['Event name','NB of event(s)']]
events_total = 0
for (k,v) in dic_wekan['events']['type'].items() :
events_total = events_total + len(dic_wekan['events']['type'][ k ])
tmpdata = list()
tmpdata.append(k) # Event name
tmpdata.append(str(len(dic_wekan['events']['type'][ k ]))) # Events count
myAsciiEventLabel.append(tmpdata)
# Total for Events
tmpdata = list()
tmpdata.append("Total : " + str(len(myAsciiEventLabel) - 1) + " Event(s)")
tmpdata.append(str(events_total))
myAsciiEventLabel.append(tmpdata)
# Create AsciiTable for Label
myTable = AsciiTable(myAsciiEventLabel)
myTable.inner_footing_row_border = True
myTable.justify_columns[1] = myTable.justify_columns[2] = 'right'
# Output data
print myTable.table
if __name__ == "__main__" :
main()