1#!/usr/bin/env python3
 2
 3"""Daily report on numbers of events raised per satellite.
 4For inclusion in the daily digest."""
 5
 6
 7
 8import fnmatch
 9import functools
10import collections
11from collections import OrderedDict
12
13from chart.events.db import find_events
14from chart.common.prettyprint import Table
15from chart.common.scid import get_satellites
16from chart.reports.widget import Widget
17
18
19class Events(Widget):
20    """Display table of events raised in time window."""
21
22    name = 'digest-events'
23
24    document_options = OrderedDict([
25            ('sensing_start', {'type': 'datetime'}),
26            ('sensing_stop', {'type': 'datetime'})])
27
28    def html(self, document):
29        dc = document.config
30        html = document.html
31
32        matchers = [('*', '*')]
33
34        scids = []
35        sat_names = []
36
37        for s in get_satellites(operational=True):
38            scids.append(s[0])
39            sat_names.append(s[1]['name'])
40
41        res = collections.defaultdict(functools.partial(collections.defaultdict, int))
42        for e in find_events(sid=None,
43                             start_time=dc['sensing-start'],
44                             stop_time=dc['sensing-stop']):
45            for matcher in matchers:
46                if fnmatch.fnmatch(e.event_classname, matcher[0]) and \
47                        fnmatch.fnmatch(e.sid.name, matcher[1]):
48
49                    res[e.event_classname][e.sid.name] += 1
50
51        t = Table(headings=['Name'] + sat_names, old_ie_support=False)
52        for classname in sorted(res.keys()):
53            counts = res[classname]
54            t.append([classname] + ['0' if scid not in counts else counts[scid] for scid in scids])
55
56        t.write_html(html)