-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-js.py
executable file
·264 lines (243 loc) · 8.39 KB
/
generate-js.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 16 17:45:45 2018
@author: sebastian
"""
import argparse
import collections
import configparser
import datetime
import os.path
#import pprint
import re
import sys
import psycopg2
try:
import plotly
except ImportError:
plotly = None
VERSION = '0.1'
TRACE = """
var trace_{section_name}_{trace_name} = {{
x: {x},
y: {y},
name: '{trace_title}',
type: '{trace_type}',
}};
"""
GRAPH_JS = """<div id="{section_name}" style="width:{width};height:{height};"></div>"""
GRAPH_PNG = """
<h2>{title}</h2>
<img src="{path}/{section_name}.png" width="{width}px" height="{height}px"></img>
"""
PLOT_JS = """
{traces}
var data_{section_name} = [{traces_list}];
var layout_{section_name} = {{
barmode: '{barmode}',
legend: {{
x: 0,
y: 1.0,
bgcolor: 'rgba(255, 255, 255, 0)',
bordercolor: 'rgba(255, 255, 255, 0)'
}},
title: '{title}',
width: {width},
height: {height},
xaxis: {{
type: '{xaxis_type}',
}},
yaxis: {{
type: '{yaxis_type}',
}}
}};
Plotly.newPlot('{section_name}', data_{section_name}, layout_{section_name});
"""
TEMPLATE_JS = """
<html>
<head>
<script src="./plotly-latest.min.js"></script>
</head>
<body>
{graphs}
<script>
{plots}
</script>
</body>
</html>
"""
TEMPLATE_PNG = """
<html>
<head>
</head>
<body>
{graphs}
</body>
</html>
"""
class ConnectionCache(object):
def __getitem__(self, key):
if key not in self.__dict__:
self.__dict__[key] = psycopg2.connect(dsn=key).cursor()
return self.__dict__[key]
CONNECTIONS = ConnectionCache()
def to_js_name(value):
if isinstance(value, (list, tuple)):
value = '_'.join(value)
return re.sub('[^a-zA-Z0-9]+', '_', value)
def main():
parser = argparse.ArgumentParser(
prog='eventdb stats',
description="Generates a HTML file with JS that displays stats on the eventdb.",
epilog="Use either a configuration file or give the DSN and Query as parameters.",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument('-V', '--version',
action='version', version=VERSION)
parser.add_argument('-c', '--config',
help='configuration file to use',
default=None)
parser.add_argument('-o', '--output',
help='output file, default: - for stdout',
default='-')
parser.add_argument('--dsn',
help='DSN connection string',
default=None)
parser.add_argument('-Q', '--query',
help='SQL Query',
default=None)
parser.add_argument('-J', '--js',
help='Use Javascript',
action='store_true',
)
parser.add_argument('-P', '--png',
help='Use PNG',
action='store_true',
)
parser.add_argument('-q', '--quiet',
help='Quiet mode, do not print status information to stderr.',
action='store_true',
)
args = parser.parse_args()
if args.png:
args.js = False
if plotly is None:
print('Needed plotly library is not installed.', file=sys.stderr)
return 2
if args.js:
args.png = False
if args.config:
config = configparser.ConfigParser()
config.read(args.config)
else:
if not args.dsn and args.query:
sys.exit('For cli mode you must give both DSN and query string.')
config = {
'manual': {
'dsn': args.dsn,
'query': args.query,
}
}
graphs = []
plots = []
successes = 0
if '__query_variables' in config:
variables = dict(config['__query_variables'].items())
config.pop('__query_variables', None)
else:
variables = {}
for section_name, section in config.items():
if section_name == 'DEFAULT':
successes += 1
continue
title = section.get('title', '')
width = section.get('width', 1000)
height = section.get('height', 500)
dsn = args.dsn if args.dsn else section['dsn']
if not dsn:
print('Missing DSN!', file=sys.stderr)
continue
db = CONNECTIONS[dsn]
query = section['query'].format(**variables)
if not args.quiet:
print('Starting query for %s: %r' % (section_name, query), file=sys.stderr)
db.execute(query)
data = collections.defaultdict(lambda: ([], []))
for row in db:
row_x = row[0]
row_y = row[-1]
row_names = tuple(str(x) for x in row[1:-1]) # For None/NULL values
if isinstance(row_x, (datetime.datetime, datetime.date, datetime.time)):
row_x = row_x.isoformat()
data[row_names][0].append(row_x)
data[row_names][1].append(row_y)
traces = []
# pprint.pprint(data)
for trace_names, trace_data in data.items():
if args.js:
traces.append(TRACE.format(section_name=section_name, trace_name=to_js_name(trace_names),
trace_title=' '.join(trace_names),
x=trace_data[0], y=trace_data[1],
trace_type=section.get('trace_type', 'line')))
else:
traces.append(plotly.graph_objs.Scatter(x = trace_data[0],
y = trace_data[1],
mode = 'lines',
name = ' '.join(trace_names),
))
if args.js:
graphs.append(GRAPH_JS.format(section_name=section_name, width=width, height=height))
plots.append(PLOT_JS.format(section_name=section_name,
width=width, height=height,
title=title,
traces_list=', '.join(['trace_%s_%s' % (section_name, to_js_name(trace_name)) for trace_name in data.keys()]),
traces='\n'.join(traces),
barmode=section.get('barmode', 'group'),
xaxis_type=section.get('xaxis_type', 'linear'),
yaxis_type=section.get('yaxis_type', 'linear'),
))
else:
if args.output == '-':
path = ''
else:
path = os.path.splitext(args.output)[0]
if not os.path.exists(path) and not os.path.isdir(path):
os.mkdir(path)
elif not os.path.isdir(path):
print('Output path %r already exists and is not a directory!', file=sys.stderr)
break
filename = '{path}/{section_name}.svg'.format(path=path, section_name=section_name)
print(filename)
plotly.offline.plot(traces, filename=filename, image_width=width, image_height=height,
image='svg')
graphs.append(GRAPH_PNG.format(title=title,
section_name=section_name,
path=path,
width=width, height=height))
successes += 1
if successes:
if not args.quiet:
print('Rendering...', file=sys.stderr)
if args.js:
template = TEMPLATE_JS
else:
template = TEMPLATE_PNG
plot = template.format(graphs='\n'.join(graphs),
plots='\n'.join(plots),
)
if args.output == '-':
print(plot)
else:
with open(args.output, 'w') as output_handle:
output_handle.write(plot)
elif config: # something is configured and nothing could be completed
print('Errors happend, could not render.', file=sys.stdout)
return 1
else:
print('Nothing to do.')
if __name__ == '__main__':
try:
sys.exit(main())
except KeyboardInterrupt:
pass