def generate_daily_reports(): records = get_state_change_btw(yesterday_start, today_start) if records == None or len(records) == 0: logging.info('No records available to generate daily report') return else: logging.info('Generating daily report') if records[0].new_state == 0: if records[0].timestamp == yesterday_start: del records[0] else: first_on_record = StateChange(1, yesterday_start) records.insert(0, first_on_record) if records[-1].new_state == 1: if records[-1].timestamp == today_start: del records[-1] else: last_off_record = StateChange(0, today_start) records.append(last_off_record) on_time = 0 for x, y in pairwise(records): single_on_time = y.timestamp - x.timestamp on_time = on_time + single_on_time last_report = get_report(yesterday_start - 86400, ReportType.DAILY_REPORT) message = get_report_message(ReportType.DAILY_REPORT, on_time, yesterday) message += get_percentage_change(on_time, last_report.on_time if last_report else None) new_tweet(Tweet(int(time.time()), message, None, today_start + 43200)) new_report(Report(yesterday_start, on_time, ReportType.DAILY_REPORT))
def generate_report(report_type, report_str, process_report, get_start_date): date = get_start_date() date_start = int(time.mktime(date.timetuple())) reports = get_reports(process_report, date_start, yesterday_start) if reports == None or len(reports) == 0: logging.info('No records available to generate ' + report_str + ' report') return else: logging.info('Generating ' + report_str + ' report') from_date = datetime.datetime.fromtimestamp(reports[0].date) on_time = 0 for report in reports: on_time = on_time + report.on_time avg_on_time = on_time/len(reports) last_report = get_report(date_start, report_type) message = get_report_message(report_type, avg_on_time, from_date, yesterday) message += get_percentage_change(avg_on_time, last_report.on_time if last_report else None) new_tweet(Tweet(int(time.time()), message, None, today_start + 86400)) new_report(Report(today_start, avg_on_time, report_type))