1#!/usr/bin/env python3
 2
 3"""Generate a test event once per day at midday."""
 4
 5import logging
 6
 7from chart.alg import init_algorithm
 8from chart.events.event import Event
 9from chart.alg import JobStatus
10from chart.common.env import get_env
11
12logger = logging.getLogger()
13
14EVENTCLASS = 'TEST-DAILY-EVENT'
15
16def raise_events(eventraiser, sid, start, stop):
17    event_time = start.replace(hour=12, minute=0, second=0, microsecond=0)
18    event = Event(event_classname=EVENTCLASS,
19                  sid=sid,
20                  start_time=event_time)
21    eventraiser.add(event)
22
23
24def dispatch(wo, resultfile, eventraiser):
25    """We are being run by the dispatcher tool."""
26    for job in wo.read_jobs():
27        sid = job.sid
28        start = job.sensing_start
29        stop = job.sensing_stop
30        results = raise_events(eventraiser, sid, start, stop)
31        if get_env('CHART_FAIL', bool, False) is True:
32            resultfile.add_job(job, JobStatus.FAILED)
33
34        else:
35            resultfile.add_job(job, JobStatus.COMPLETED)
36
37    eventraiser.log_report()
38
39
40def main():
41    """This program can either be called via `dispatcher` or standalone."""
42    dispatch(*init_algorithm())
43
44
45if __name__ == '__main__':
46    main()