def create_graph(filepaths): """ Prepare the `index.html` template. """ graphs = [] if isinstance(filepaths, str): filepaths = [filepaths] for path in filepaths: contributions = parser.parse_file(path) graph = { "data": gridify_contributions(contributions), "cell_class": _cell_class(list(contributions.values())), "longest_streak": statistics.longest_streak( [key for key, val in contributions.items() if val > 0]), "current_streak": statistics.current_streak( [key for key, val in contributions.items() if val > 0]), "sum": sum(contributions.values()), "repo_name": ntpath.basename(path) } graph["last_date"] = ( [""] + sorted([key for key, v in contributions.items() if v]))[-1] graphs.append(graph) env = Environment(loader=PackageLoader('render_html', 'templates')) env.filters['tooltip'] = tooltip_text env.filters['display_date'] = dateutils.display_date env.filters['elapsed_time'] = dateutils.elapsed_time template = env.get_template("index.html") weekdays = dateutils.weekday_initials() for idx in [0, 2, 4, 6]: weekdays[idx] = "" months = [ cell.date.strftime("%b") for cell in gridify_contributions(contributions)[0] ] months = filter_months(months) return template.render(graphs=graphs, today=dateutils.today(), start=dateutils.start(), weekdays=weekdays, months=months)
def insert_overwrite_history(): engine = create_engine( f"postgresql://{config['db_username']}:{config['db_password']}@{config['db_host']}:{config['db_port']}/{config['db_name']}" ) df = get_p2p_overall_dataframe(all_dates_since_last_month(today())) df.to_sql("p2p_migration", engine, if_exists="replace", index=False, method="multi")
def class_label(cell): if cell.date > dateutils.today() or cell.date < dateutils.start(): return "empty" elif cell.contributions == 0: return "grad0" elif cell.contributions <= quartiles[1]: return "grad1" elif cell.contributions <= quartiles[2]: return "grad2" elif cell.contributions <= quartiles[3]: return "grad3" else: return "grad4"
def current_streak(dates): """ Given a list of datetime.date objects, return today's date (if present) and all/any preceding consecutive dates. """ streak = [] current_date = dateutils.today() while current_date in dates: streak.append(current_date) current_date = dateutils.previous_day(current_date) return sorted(streak)
def gridify_contributions(contributions): """ The contributions graph has seven rows (one for each day of the week). It spans a year. Given a dict of date/value pairs, rearrange these results into seven rows of "cells", where each cell records a date and a value. """ start = dateutils.start() today = dateutils.today() graph_entries = [] # The first row is a Sunday, so go back to the last Sunday before the start if start.weekday() == 6: first_date = start else: first_date = start - datetime.timedelta(start.weekday() + 1 % 7) next_date = first_date first_row_dates = [first_date] while (next_date <= today) and (next_date + datetime.timedelta(7) <= today): next_date += datetime.timedelta(7) first_row_dates.append(next_date) # Now get contribution counts for each of these dates, and save the row first_row = [ GridCell(date, contributions[date]) for date in first_row_dates ] graph_entries.append(first_row) # For each subsequent day of the week, use the first row as a model: add # the appropriate number of days and count the contributions for i in range(1, 7): row_dates = [day + datetime.timedelta(i) for day in first_row_dates] next_row = [GridCell(date, contributions[date]) for date in row_dates] graph_entries.append(next_row) return graph_entries