forked from EUDAT-B2FIND/md-ingestion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.py
655 lines (561 loc) · 26.7 KB
/
output.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
import os
import logging
class Output(object):
"""
### Output - class
# Initializes the logger class, saves statistics per every subset and creates HTML output file
#
# Parameters:
# -----------
# 1. (dict) pstat - States of all processes
# 2. (string) now - Current date and time
# 3. (string) jid - Job id of the programm in the system
# 4. (OptionParser object) options - Object with all command line options
#
# Return Values:
# --------------
# 1. OUTPUT object
#
# Public Methods:
# ---------------
# .get_stats()
# .save_stats(request, subset, mode, stats) - Saves the statistics of a process per subset
# .start_logger() - Initializes logger class of the program
#
#
# Usage:
# ------
## EXAMPLE ##
"""
def __init__(self, pstat, now, jid, options):
self.options = options
self.pstat = pstat
self.start_time = now
self.jid = jid
# create jobdir if it is necessary:
if (hasattr(options,'jobdir') and options.jobdir):
self.jobdir = options.jobdir
elif hasattr(options,'mode') and hasattr(options,'list') :
self.jobdir='log/%s/%s/%s_%s_%s' % (
now.split(' ')[0],
now.split(' ')[1].split(':')[0],
jid, options.mode, options.list
)
else:
self.jobdir='log/%s/%s/%s' % (
now.split(' ')[0],
now.split(' ')[1].split(':')[0],
jid
)
if not os.path.exists(self.jobdir):
os.makedirs(self.jobdir)
self.convert_list = None
self.verbose = options.verbose
# Generate special request fields in statistic dictionary:
self.stats = {
'#GetPackages' : {
'time':0,
'count':0,
},
'#Start' : {
'StartTime':0,
'TotalTime':0,
'#critical':False,
'#error':False,
}
}
self.stats_counter = 0
# create the logger and start it:
##HEW-CHG!!! self.start_logger()
self.logger = logging.getLogger('root')
self.table_code = ''
self.details_code = ''
def setup_custom_logger(self,name,verbose):
log_level=logging.CRITICAL
log_format='[%(levelname)s <%(module)s:%(funcName)s> @l %(lineno)4s] %(message)s'
# choose the debug level:
if verbose == 1 : log_level=logging.ERROR
elif verbose == 2 : log_level=logging.WARNING
elif verbose == 3 : log_level=logging.INFO
elif verbose >3 : log_level=logging.DEBUG
### elif verbose > 5 : log_level=logging.NOTSET
###HEW-D?? # choose the debug level:
###HEW-D?? print('VVVVVVVVVV %s' % self.verbose)
###HEW-D?? if self.verbose == 1:
###HEW-D?? self.log_level = {
###HEW-D?? 'log':logging.DEBUG,
###HEW-D?? 'err':logging.ERROR,
###HEW-D?? 'std':logging.INFO,
###HEW-D?? }
###HEW-D?? elif self.verbose == 2:
###HEW-D?? self.log_level = {
###HEW-D?? 'log':logging.DEBUG,
###HEW-D?? 'err':logging.ERROR,
###HEW-D?? 'std':logging.DEBUG,
###HEW-D?? }
###HEW-D?? else:
###HEW-D?? self.log_level = {
###HEW-D?? 'log':logging.INFO,
###HEW-D?? 'err':logging.ERROR,
###HEW-D?? 'err':logging.DEBUG,
###HEW-D?? 'std':logging.INFO,
###HEW-D?? }
###HEW-D??
formatter = logging.Formatter(fmt=log_format)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
self.logger.setLevel(log_level)
###HEW-D??
self.logger.addHandler(handler)
return self.logger
def save_stats(self,request,subset,mode,stats):
## save_stats (OUT object, request, subset, mode, stats) - method
# Saves the statistics of a process (harvesting, converting, oai-converting, mapping or uploading) per subset in <OUTPUT.stats>.
# <OUTPUT.stats> is a big dictionary with all results statistics of the harvesting, converting, oai-converting and uploading routines.
# Requests which start with a '#' are special requests like '#Start' or '#GetPackages' and will be ignored in the most actions.
#
# Special Requests:
# #Start - contents statistics from the start periode and common details of the manager.py
# Subsets:
# TotalTime - total time of all processes (without HTML file generation) since start
# StartTime - start time of the manager
#
# Parameters:
# -----------
# (string) request - normal request named by <community>-<mdprefix> or a special request which begins with a '#'
# (string) subset - ...
# (string) mode - process mode (can be 'h', 'c', 'm','v', 'u' or 'o')
# (dict) stats - a dictionary with results stats
#
# Return Values:
# --------------
# None
# create a statistic dictionary for this request:
if(not request in self.stats):
# create request dictionary:
self.stats[request] = dict()
# special requests have only dictionaries with two-level-depth:
if (request.startswith('#')):
# special request e.g. '#Start':
self.stats[request][mode] += stats
# normal requests have dictionaries with three-level-depth:
else:
# create an empty template dictionary if the <subset> have not exists yet:
if(not subset in self.stats[request]):
self.stats_counter += 1
template = {
'g':{
'count':0,
'dcount':0,
'ecount':0,
'tcount':0,
'time':0,
'avg':0,
},
'h':{
'count':0,
'dcount':0,
'ecount':0,
'tcount':0,
'time':0,
'avg':0,
},
'c':{
'count':0,
'ecount':0,
'tcount':0,
'time':0,
'avg':0
},
'm':{
'count':0,
'ecount':0,
'tcount':0,
'time':0,
'avg':0
},
'v':{
'count':0,
'ecount':0,
'tcount':0,
'time':0,
'avg':0
},
'o':{
'count':0,
'ecount':0,
'tcount':0,
'time':0,
'avg':0
},
'u':{
'count':0,
'ecount':0,
'tcount':0,
'ncount':0,
'time':0,
'avg':0
},
'd':{
'count':0,
'dcount':0,
'ecount':0,
'tcount':0,
'time':0,
'avg':0
},
'#critical':False,
'#error':False,
'#id':self.stats_counter,
}
self.stats[request].update({subset : template})
# update the values in the old dictionary with the newest <stats>
for k in stats:
self.stats[request][subset][mode][k] += stats[k]
# calculate the average time per record:
if (self.stats[request][subset][mode]['count'] != 0):
self.stats[request][subset][mode]['avg'] = \
self.stats[request][subset][mode]['time'] / self.stats[request][subset][mode]['count']
else:
self.stats[request][subset][mode]['avg'] = 0
# If a normal or the #Start request is saved then write the log files separately for each <subset>
if (not request.startswith('#') or request == '#Start'):
# shutdown the logger:
logging.shutdown()
# make the new log dir if it is necessary:
logdir= self.jobdir
if (not os.path.exists(logdir)):
os.makedirs(logdir)
# generate new log and error filename:
logfile, errfile = '',''
if (request == '#Start'):
logfile='%s/start.logging.txt' % (logdir)
errfile='%s/start.err.txt' % (logdir)
else:
logfile='%s/%s_%s.logging.txt' % (logdir,mode,self.get_stats(request,subset,'#id'))
errfile='%s/%s_%s.err.txt' % (logdir,mode,self.get_stats(request,subset,'#id'))
# move log files:
try:
if (os.path.exists(logdir+'/myapp.log')):
os.rename(logdir+'/myapp.log',logfile )
if (os.path.exists(logdir+'/myapp.err')):
os.rename(logdir+'/myapp.err',errfile )
except OSError :
print("[ERROR] Cannot move log and error files to %s and %s: %s\n" % (logfile,errfile))
else:
# set ERROR or CRITICAL flag in stats dictionary if an error log exists:
if (os.path.exists(errfile)):
# open error file:
errors=open(errfile,'r').read() or 'No error occured'
if (request != '#Start'):
# color this line red if an critical or a non-critical error occured:
if (errors.find('CRITICAL:') != -1):
self.stats[request][subset]['#critical'] = True
elif (errors.find('ERROR:') != -1):
self.stats[request][subset]['#error'] = True
def get_stats(self,request,subset='',mode='',key=''):
## get_stats (OUTPUT object, request, subset, mode, key) - method
# Returns the statistic dictionary which are identified by <request>, <subset>, <mode> and <key> and
# saved by .save_stats() before
#
# Parameters:
# -----------
# (string) request - param_des
# (string) subset - param_des
# (string) mode - param_des
# (string) key - param_des
#
# Return Values:
# --------------
# Statistic values
if (not subset) : subset=''
if ('#' in ''.join([request,subset,mode])):
if (request == '#AllRequests'):
# returns all requests except all which start with an '#'
return filter(lambda x: not x.startswith('#'), self.stats.keys())
elif (subset == '#AllSubsets'):
# returns all subsets except all which start with an '#'
return filter(lambda x: x and not x.startswith('#'), self.stats[request].keys())
if(request == '#total'):
# returns the sum of the keys in the modes in all subsets and all requests
total = 0
for r in self.stats:
if (not r.startswith('#')):
for s in self.stats[r]:
total += self.stats[r][s][mode][key]
return total
elif(subset == '#total' and mode != '#total'):
# returns the sum of the keys in the modes of all subsets in the request
total = 0
for s in self.stats[request]:
total += self.stats[request][s][mode][key]
return total
elif(mode == '#total'):
total = 0
for s in self.stats[request]:
for m in self.stats[request][s]:
if (not m.startswith('#')):
total += self.stats[request][s][m][key]
return total
elif('#' in mode and subset ):
return self.stats[request][subset][mode]
elif(subset):
return self.stats[request][subset]
elif(request):
return self.stats[request]
return self.stats[request][subset][mode][key]
def HTML_print_begin(self):
## HTML_print_begin (OUTPUT object) - method
# Writes header and layout stylesheet at the begin of HTML overview file and save it to '<OUTPUT.jobdir>/overview.html'
#
# Parameters:
# -----------
# None
#
# Return Values:
# --------------
# None
pstat = self.pstat
options = self.options
# open results.html
reshtml = open(self.jobdir+'/overview.html', 'w')
# write header with css stylesheets at the begin of the HTML file:
head='''<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"
\"http://www.w3.org/TR/html4/strict.dtd\">
<html>
<head><style type=\"text/css\">
body {}
.table-error { background-color:#FFC0C0; }
.table-critical { background-color:#FF6060; }
.table-disabled { color:#cecece; }
.details-head {}
.details-body-log { background-color:#DFE8FF; padding:4px; margin:0px; }
.details-body-error { background-color:#FFC0C0; padding:4px; margin:0px; }
</style></head>
<body>
'''
reshtml.write(head)
# write head of the body:
reshtml.write("\t\t<h1>Results of B2FIND ingestion workflow</h1>\n")
reshtml.write(
'\t\t<b>Date:</b> %s UTC, <b>Process ID:</b> %s, <b>Epic check:</b> %s<br />\n\t\t<ol>\n'
% (self.start_time, self.jid, options.handle_check)
)
i=1
for proc in pstat['status']:
if (pstat['status'][proc] == 'tbd' and proc != 'a') :
reshtml.write('\t\t\t<li>%s</li>\n' % (pstat['text'][proc]))
self.logger.debug(' %d. %s' % (i,pstat['text'][proc]))
i+=1
reshtml.write('\t\t</ol>\n')
reshtml.close()
def HTML_print_end(self):
pstat = self.pstat
options = self.options
# open HTML file:
reshtml = open(self.jobdir+'/overview.html', 'a+')
# shows critical script errors:
critical_script_error = False
if (os.path.exists(self.jobdir+'/myapp.log') and os.path.exists(self.jobdir+'/myapp.log')):
size_log = os.path.getsize(self.jobdir+'/myapp.log')
size_err = os.path.getsize(self.jobdir+'/myapp.err')
if (size_log != 0):
size_log = int(size_log/1024.) or 1
if (size_err != 0):
size_err = int(size_err/1024.) or 1
reshtml.write('<span style="color:red"><strong>A critical script error occured! Look at <a href="myapp.log">log</a> (%d kB) and <a href="myapp.err">error</a> (%d kB) file. </strong></span><br />'% (size_log, size_err))
critical_script_error = True
elif (os.path.isfile(self.jobdir+'/start.err.txt')):
size_log = os.path.getsize(self.jobdir+'/start.logging.txt')
size_err = os.path.getsize(self.jobdir+'/start.err.txt')
if (size_log != 0):
size_log = int(size_log/1024.) or 1
if (size_err != 0):
size_err = int(size_err/1024.) or 1
reshtml.write('<span style="color:red"><strong>A critical script error occured! Look at main <a href="start.logging.txt">log</a> (%d kB) and <a href="start.err.txt">error</a> (%d kB) file. </strong></span><br />'% (size_log, size_err))
critical_script_error = True
# get all and processed modes:
all_modes = pstat['short']
processed_modes = list()
for mode in all_modes:
if (pstat['status'][mode[0]] == 'tbd'):
processed_modes.append(mode)
## table with total statistics:
reshtml.write('''
<table border=\"1\" rules=\"all\" cellpadding=\"6\"><a name=\"mtable\"
<tr>
<th>Stage</th>
<th>provided</th>
<th>processed</th>
<th>failed</th>
<th>Elapsed time [s]</th>
<th>Average time/record [s]</th>
</tr>
''')
for mode in all_modes:
reshtml.write(
'<tr %s><th>%s</th><td>%d</td><td>%d</td><td>%d</td><td>%7.3f</td><td>%7.3f</td></tr>' % (
'class="table-disabled"' if ('no' in pstat['status'].values()) else '',
## 'class="table-disabled"' if (pstat['status'][mode[0]] == 'no') else '',
pstat['short'][mode],
self.get_stats('#total','#total',mode[0],'tcount'),
self.get_stats('#total','#total',mode[0],'count'),
self.get_stats('#total','#total',mode[0],'ecount'),
self.get_stats('#total','#total',mode[0],'time'),
self.get_stats('#total','#total',mode[0],'time') / \
self.get_stats('#total','#total',mode[0],'count') if (self.get_stats('#total','#total',mode[0],'count') != 0) else 0,
)
)
reshtml.write('''
<tr border=3><td colspan=3 rowspan=2><td><b>Get dataset list:</b></td><td>%7.3f</td><td>%7.3f</td></tr><br />
<tr><td><b>Total:</b></td><td>%7.3f</td><td></td></tr>
\t\t</table>\n\n<br /><br />
''' % (
self.get_stats('#GetPackages','time'),
self.get_stats('#GetPackages','time') / self.get_stats('#GetPackages','count') if (self.get_stats('#GetPackages','count') != 0) else 0,
self.get_stats('#Start','TotalTime'),
))
if len(list(self.get_stats('#AllRequests'))) > 0:
## table with details for every request:
reshtml.write("\t\t<h2>Details per community and mdPrefix</h2>")
reshtml.write('''\n
<table border=\"1\" rules=\"all\" cellpadding=\"6\"><a name=\"table\">
<col width=\"20%\">
<tr>
<th> Community - mdPrefix</th>
<th rowspan=2>Time [s]</th>
''')
reshtml.write(
'\t\t<th colspan=\"%d\">#provided | #processed | #failed | Elapsed time [s] | Avgt./ proc. rec [s]</th>'
% (len(processed_modes)*5)
)
reshtml.write('''
<th> Details</th>
</tr>\n
<tr>
<th> Processes > </th>
''')
for mode in processed_modes:
reshtml.write('\t\t<th colspan=\"5\">%s</th>\n' % pstat['short'][mode])
reshtml.write('</tr>\n')
rcount = 0
for request in self.get_stats('#AllRequests'):
reshtml.write('<td valign=\"top\">%s</td><td>%7.3f</td>'% (
request,self.get_stats(request,'#total','#total','time')
))
for mode in processed_modes:
reshtml.write('<td>%d</td><td>%d</td><td>%d<td>%7.3f</td><td>%7.3f</td>'% (
self.get_stats(request,'#total',mode[0],'tcount'),
self.get_stats(request,'#total',mode[0],'count'),
self.get_stats(request,'#total',mode[0],'ecount'),
self.get_stats(request,'#total',mode[0],'time'),
self.get_stats(request,'#total',mode[0],'avg')
))
reshtml.write('<td><a href="#details-%s">Details</a></td></tr>'%(request))
reshtml.write("\t\t</table>\n<br/><br /> <br />")
## table with details for every subset:
reshtml.write("\t\t<h2>Details per subset</h2>")
for request in self.get_stats('#AllRequests'):
reshtml.write('''\t\t<hr />
<span class=\"details-head\">
<a name="details-%s" /> %s
</span><br />
''' % (request,request))
reshtml.write('''\n
<table border=\"1\" rules=\"all\" cellpadding=\"6\">
<col width=\"20%\">
<tr>
<th> Set</th>
''')
reshtml.write('<th colspan=\"%d\">#provided | #processed | #failed</th>' % (len(processed_modes)*3))
reshtml.write('''
<th colspan=\"2\"> Output</th>
</tr>\n
<tr>
<th> Processes > </th>
''')
for mode in processed_modes:
reshtml.write('\t\t<th colspan=\"3\">%s</th>\n' % pstat['short'][mode])
reshtml.write('\t\t\t<th>Log</th><th>Error</th>\t\t</tr>\n')
for subset in sorted(self.get_stats(request,'#AllSubsets')):
# color this line red if an critical or a non-critical error occured:
if self.get_stats(request,subset,'#critical'):
reshtml.write('<tr class="table-critical">')
elif self.get_stats(request,subset,'#error'):
reshtml.write('<tr class="table-error">')
else:
reshtml.write('<tr>')
reshtml.write('<td valign=\"top\">→ %s</td>'% (subset))
for mode in processed_modes:
reshtml.write('<td>%d</td><td>%d</td><td>%d'% (
self.get_stats(request,subset,mode[0],'tcount'),
self.get_stats(request,subset,mode[0],'count'),
self.get_stats(request,subset,mode[0],'ecount'),
))
# link standard output files:
reshtml.write('<td valign=\"top\">')
for mode in processed_modes:
if (pstat['status'][mode[0]] == 'tbd' and os.path.exists(self.jobdir+'/%s_%d.logging.txt'%(mode[0],self.get_stats(request,subset,'#id')))):
try:
size = os.path.getsize(self.jobdir+'/%s_%d.logging.txt'%(mode[0],self.get_stats(request,subset,'#id')))
if (size != 0):
size = int(size/1024.) or 1
reshtml.write('<a href="%s_%d.logging.txt">%s</a> (%d kB)<br />'% (mode[0],self.get_stats(request,subset,'#id'),pstat['short'][mode],size))
except OSError :
reshtml.write('%s log file not available!<br /><small><small>(<i>%s</i>)</small></small><br />'% (pstat['short'][mode], e))
reshtml.write('</td>')
# link error files:
reshtml.write('<td valign=\"top\">')
for mode in processed_modes:
if (pstat['status'][mode[0]] == 'tbd' and os.path.exists(self.jobdir+'/%s_%d.err.txt'%(mode[0],self.get_stats(request,subset,'#id')))):
try:
size = os.path.getsize(self.jobdir+'/%s_%d.err.txt'%(mode[0],self.get_stats(request,subset,'#id')))
if (size != 0):
size = int(size/1024.) or 1
reshtml.write('<a href="%s_%d.err.txt">%s</a> (%d kB)<br />'% (mode[0],self.get_stats(request,subset,'#id'),pstat['short'][mode],size))
except OSError :
reshtml.write('No %s error file! <br /><small><small>(<i>OSError</i>)</small></small><br />'% (pstat['short'][mode]))
reshtml.write('</td>')
reshtml.write('</tr>')
reshtml.write('</table>')
else:
reshtml.write('\t\t<h2>No data found</h2>\n')
if not critical_script_error:
reshtml.write('This does not have to be an error. Maybe you just have no requests (or only commented requests) in your list file?')
# close the html document and file:
reshtml.write("\t</body>\n</html>\n\n")
reshtml.close()
def print_convert_list(self,community,source,mdprefix,dir,fromdate):
## print_convert_list (OUT object, community, source, mdprefix, dir, fromdate) - method
# Write directories with harvested files in convert_list
#
# Parameters:
# -----------
# ...
#
# Return Values:
# --------------
# None
self.convert_list = 'convert_list_total'
##HEW-D else:
##HEW-D self.convert_list = './convert_list_' + fromdate
new_entry = '%s\t%s\t%s\t%s\t%s\n' % (community,source,os.path.dirname(dir),mdprefix,os.path.basename(dir))
file = new_entry
# don't create duplicated items:
if(os.path.isfile(self.convert_list)):
try:
f = open(self.convert_list, 'r')
file = f.read()
f.close()
if(not new_entry in file):
file += new_entry
except IOError :
logging.critical("Cannot read data from '{0}'".format(self.convert_list))
f.close
try:
f = open(self.convert_list, 'w')
f.write(file)
f.close()
except IOError :
logging.critical("Cannot write data to '{0}'".format(self.convert_list))
f.close