class MonthlyReport: def __init__(self): self.rows = [] self.trackings = Trackings() self.bad_trackings = [] def group_by_month(self, trackings): report = defaultdict(int) for t in trackings: if t['action'].lower() != 'exibicao': return f'invalid:{t["action"]}' storageDate = datetime.strptime(t['storageDate'][:10], '%Y-%m-%d') report[(storageDate.year, storageDate.month)] += t['count'] return dict(report) def get_tracking_count(self, tracking, from_date, to_date): return self.trackings.get_value(tracking, from_date, to_date) def generate(self): trackings_names = self.trackings.get_all_view_trackings() for tracking in trackings_names: #print(f'\n{tracking}') begin_date, end_date = get_date_range() monthly_values = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] tracking_totals = self.get_tracking_count(tracking, begin_date, end_date) grouped_by_months = self.group_by_month(tracking_totals) if type(grouped_by_months) is str and grouped_by_months.startswith( 'invalid'): self.bad_trackings.append( f'{tracking} -> action: {grouped_by_months.split(":")[1]}') continue for (year, month) in grouped_by_months: monthly_values[month - 1] = grouped_by_months[(year, month)] self.rows.append([tracking] + monthly_values) print('\n\nTrackings que não são de exibição:\n') print(self.bad_trackings) months = [''] + get_month_list() self.rows.insert(0, months) updated_at = f'Atualizado em: {datetime.now().strftime("%d/%m/%Y %H:%M:%S")}' self.rows.insert(0, [updated_at]) return self.rows
class MAUDailyReport: def __init__(self): self.trackings = Trackings() self.rows = [] def add_header(self, rows): updated_at = f'Atualizado em: {datetime.now().strftime("%d/%m/%Y %H:%M:%S")}' empty_line = [] rows.insert(0, empty_line) rows.insert(0, [updated_at]) return rows def generate(self): print('Running the "MAU" report from {begin_date} to {end_date}\n') begin_date, end_date = get_date_range() oneDay = relativedelta(days = +1) while begin_date <= end_date and begin_date <= datetime.now().date(): total_MAU = self.trackings.getMAU(begin_date, begin_date) self.rows.append([begin_date.strftime("%d/%m/%Y")] + [total_MAU]) print(f'{begin_date}: {total_MAU}') begin_date += oneDay self.rows = self.add_header(self.rows) return self.rows
class MAUReport: def __init__(self): self.trackings = Trackings() self.rows = [] def add_header(self, rows): updated_at = f'Atualizado em: {datetime.now().strftime("%d/%m/%Y %H:%M:%S")}' empty_line = [] rows.insert(0, empty_line) rows.insert(0, [updated_at]) return rows def generate(self): print('Running the "MAU" report...') begin_date, end_date = get_date_range() months = get_months(begin_date.month, end_date.month) oneMonth = relativedelta(months=+1) oneDay = relativedelta(days=+1) for month_name, month_number in months: total_MAU = self.trackings.getMAU(begin_date, (begin_date + oneMonth) - oneDay) begin_date += oneMonth self.rows.append([month_name] + [total_MAU]) print(f'{month_name}: {total_MAU}') self.rows = self.add_header(self.rows) return self.rows
class ListAllTrackings: def __init__(self): self.rows = [] self.trackings = Trackings() def generate(self): print('Running the "Tracking list" report...\n') all_trackings = self.trackings.get_all_trackings() return all_trackings
class NPSReport: def __init__(self): self.rows = [] self.trackings = Trackings() self.bad_trackings = [] def group_by_rate_and_date(self, trackings): ratings = {} for t in trackings: date = t['storageDate'][:10] nota, feedback = t['action'].split(' ', 1) nota = int(nota) # year, month, day = if date not in ratings: ratings[date] = [[], [], [], [], [], [], [], [], [], [], []] ratings[date][nota].append(feedback.replace('\n', ' ')) return ratings def make_feedback_matrix(self, feedbacks): rows = [] for date in sorted(feedbacks): rows.append(['', '', '', '', '', '', '', '', '', '', '', '']) rows.append([date, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']) feedback_list = feedbacks[date] while any(feedback_list): notas = [''] for i in range(11): if feedback_list[i]: notas.append(feedback_list[i].pop(0)) else: notas.append('') rows.append(notas) return rows def generate(self): print('Running the "NPS" report...') tracking = 'Nps pesquisa motivo' begin_date, end_date = get_date_range() nps_tracking = self.trackings.get_value(tracking, begin_date, end_date) ratings = self.group_by_rate_and_date(nps_tracking) feedback_messages = self.make_feedback_matrix(ratings) updated_at = f'Atualizado em: {datetime.now().strftime("%d/%m/%Y %H:%M:%S")}' ratings_headers = [updated_at, '', '', '', '', '', '', '', '', '', '', ''] self.rows.append(ratings_headers) self.rows.extend(feedback_messages) return self.rows
def __init__(self): self.rows = [] self.trackings = Trackings() self.bad_trackings = []
class DailyReport: def __init__(self): self.rows = [] self.trackings = Trackings() self.bad_trackings = [] def group_by_days(self, trackings): report = defaultdict(int) for t in trackings: if t['action'].lower() != 'exibicao': continue storageDate = datetime.strptime(t['storageDate'][:10], '%Y-%m-%d') report[(storageDate.year, storageDate.month, storageDate.day)] += t['count'] return dict(report) def get_daily_count(self, grouped_by_days, current_month): daily_values = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] for (year, _, day) in grouped_by_days: if (year, current_month, day) in grouped_by_days: daily_values[day - 1] = grouped_by_days[(year, current_month, day)] else: daily_values[day - 1] = 0 return daily_values def is_valid_tracking(self, tracking, grouped_by_days): if type(grouped_by_days) is str and grouped_by_days.startswith( 'invalid'): self.bad_trackings.append( f'{tracking} -> action: {grouped_by_days.split(":")[1]}') return False return True def add_header(self, rows): updated_at = f'Atualizado em: {datetime.now().strftime("%d/%m/%Y %H:%M:%S")}' days = ['Dias:'] + get_days_list() rows.insert(0, days) rows.insert(0, [updated_at]) return rows def print_bad_trackings(self): print('\n\nTrackings que não são de exibição:\n') print(self.bad_trackings) def generate(self): print('Running the "Daily" report...') trackings_names = self.trackings.get_all_exhibition_trackings() begin_date, end_date = get_date_range() months = get_months(begin_date.month, end_date.month) for month_name, month_number in months: self.rows.append([month_name] + [ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' ]) print(f'{month_name}') for tracking in trackings_names: print(f'\t{tracking}') tracking_totals = self.trackings.get_value( tracking, begin_date, end_date) grouped_by_days = self.group_by_days(tracking_totals) if not self.is_valid_tracking(tracking, grouped_by_days): continue daily_values = self.get_daily_count(grouped_by_days, month_number) self.rows.append([tracking] + daily_values) self.rows.append([]) self.rows = self.add_header(self.rows) self.print_bad_trackings() return self.rows
class MonthlyReport: def __init__(self): self.rows = [] self.trackings = Trackings() self.bad_trackings = [] def group_by_month(self, trackings): report = defaultdict(int) for t in trackings: if t['action'].lower() != 'exibicao': continue storageDate = datetime.strptime(t['storageDate'][:10], '%Y-%m-%d') report[(storageDate.year, storageDate.month)] += t['count'] return dict(report) def add_header(self, rows): updated_at = f'Atualizado em: {datetime.now().strftime("%d/%m/%Y %H:%M:%S")}' months = [''] + [month for month, _ in get_months()] rows.insert(0, months) rows.insert(0, [updated_at]) return rows def print_bad_trackings(self): print('\n\nTrackings que não são de exibição:\n') print(self.bad_trackings) def get_monthly_count(self, grouped_by_months): monthly_values = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for (year, month) in grouped_by_months: monthly_values[month-1] = grouped_by_months[(year, month)] return monthly_values def is_valid_tracking(self, tracking, grouped_by_months): if type(grouped_by_months) is str and grouped_by_months.startswith('invalid'): self.bad_trackings.append(f'{tracking} -> action: {grouped_by_months.split(":")[1]}') return False return True def generate(self): print('Running the "Monthy" report...') trackings_names = self.trackings.get_all_exhibition_trackings() start_date, final_date = get_date_range() for tracking in trackings_names: print(tracking) begin_date, end_date = start_date, final_date tracking_totals = self.trackings.get_value(tracking, begin_date, end_date) grouped_by_months = self.group_by_month(tracking_totals) if not self.is_valid_tracking(tracking, grouped_by_months): continue monthly_values = self.get_monthly_count(grouped_by_months) self.rows.append([tracking] + monthly_values) self.print_bad_trackings() self.rows = self.add_header(self.rows) return self.rows