def __plot(plot_basis, start_date, end_date, plot_type=None, df=None): # check if we've plotted this same plot in the past before kwargs = {} now = datetime.datetime.now().strftime(BankSchema.DATE_FORMAT2) title = ImageRegistry._make_plot_key_title(plot_basis, plot_type) plot_cache_result = PlotCache.hit(title, start_date, end_date, now) if plot_cache_result is None: logging.info(f'Plot cache miss for plot: {title}, replotting.') if plot_type and plot_type not in ImageRegistry.get_supported_plots(plot_basis): raise KeyError(f"Provided plot type {plot_type} is not supported.") kwargs['plot_type'] = plot_type kwargs['title'] = title kwargs['figsize'] = ImageRegistry.get_plot_figsize(plot_basis) with ImageRegistry.__global_lock: if df is not None: fig_or_html = ImageRegistry.get_plot_func(plot_basis)(df, **kwargs) else: # heatmap fig_or_html = ImageRegistry.get_plot_func(plot_basis)(DateRange(start_date, end_date), **kwargs) # is figure if fig_or_html is not None and not isinstance(fig_or_html, str): stream_reader = StringIO() fig_or_html.savefig(stream_reader, format='svg', bbox_inches='tight') stream_reader.seek(0) html = stream_reader.getvalue() PlotCache.add_cache_miss(title, start_date, end_date, now, html) elif fig_or_html is None: logging.warning(f'Ignoring plot for {plot_basis}.') else: # heat-map plot PlotCache.add_cache_miss(title, start_date, end_date, now, fig_or_html) else: logging.info(f'Plot cache hit for plot: {title}, ignoring replotting.')
def update_html_df(): DataState.html_df = HTMLHelper.as_html_form_from_sql( BankSchema.BANK_TB_NAME, DateRange(State.date_range_start, State.date_range_end), DataState.order_by_column_name, DataState.order_by, table_id=ConstData.BANK_DATA_TABLE_ID)
class InsightState(State): html_bank_summary = HTMLHelper.as_html_form_from_df( RawInsights.Dynammic.table_summary_statistics( BankSchema.BANK_TB_NAME, DateRange(State.date_range_start, State.date_range_end)), Insights.BANK_SUMMARY_TABLE_ID, replace_default_data_frame_class_with=Insights.INSIGHT_TABLE_CLASS) html_top_inc_cat = HTMLHelper.as_html_form_from_df( RawInsights.Dynammic.get_top_n_income_categories( DateRange(State.date_range_start, State.date_range_end), BankSchema.BANK_TB_NAME), Insights.TOP_INC_TABLE_ID, replace_default_data_frame_class_with=Insights.INSIGHT_TABLE_CLASS) html_top_exp_cat = HTMLHelper.as_html_form_from_df( RawInsights.Dynammic.get_top_n_expense_categories( DateRange(State.date_range_start, State.date_range_end), BankSchema.BANK_TB_NAME), Insights.TOP_EXP_TABLE_ID, replace_default_data_frame_class_with=Insights.INSIGHT_TABLE_CLASS) _static_clock = time.time() html_inc_and_exp_this_month_vs_last_month_summary = HTMLHelper.as_html_form_from_df( RawInsights.Static.get_money_gain_and_spent_this_month_vs_last_month( DateRange(State.date_range_start, State.date_range_end), BankSchema.BANK_TB_NAME, as_dataframe=True), Insights.SPENDING_VS_LAST_MONTH_TABLE_ID, replace_default_data_frame_class_with=Insights.INSIGHT_TABLE_CLASS) html_inc_and_exp_this_month_vs_last_month_summary_as_str = RawInsights.Static. \ summarize_this_and_last_months_spending( DateRange(State.date_range_start, State.date_range_end), BankSchema.BANK_TB_NAME) spending_vs_last_month_pid = 'spending_vs_last_month_p' @staticmethod def as_dict(): return State.as_dict_helper(InsightState, Insights) @staticmethod def get_clock(): return InsightState._static_clock @staticmethod def set_clock(clock): InsightState._static_clock = clock
def valid_dr(start, end): try: DateRange(start, end) return True except KeyError: flash(Flash.DATE_RANGE_FILTER_KEY_ERROR.msg, Flash.DATE_RANGE_FILTER_KEY_ERROR.type) return False
def refresh_static_insights(ignore_clock=False): now = time.time() passed_minutes = now - InsightState.get_clock() # refresh only if a significant time has passed if ignore_clock or passed_minutes <= Insights.STATIC_TIME_CACHE_REFRESH_MINUTES: InsightState.html_inc_and_exp_this_month_vs_last_month_summary = HTMLHelper.as_html_form_from_df( RawInsights.Static. get_money_gain_and_spent_this_month_vs_last_month( DateRange(State.date_range_start, State.date_range_end), BankSchema.BANK_TB_NAME, as_dataframe=True), Insights.SPENDING_VS_LAST_MONTH_TABLE_ID, replace_default_data_frame_class_with=Insights.INSIGHT_TABLE_CLASS) InsightState.html_inc_and_exp_this_month_vs_last_month_summary_as_str = RawInsights.Static. \ summarize_this_and_last_months_spending( DateRange(State.date_range_start, State.date_range_end), BankSchema.BANK_TB_NAME) # refresh the clock for next look up period InsightState.set_clock(time.time())
def refresh_dynamic_insights(): InsightState.html_bank_summary = HTMLHelper.as_html_form_from_df( RawInsights.Dynammic.table_summary_statistics( BankSchema.BANK_TB_NAME, DateRange(State.date_range_start, State.date_range_end)), Insights.BANK_SUMMARY_TABLE_ID, replace_default_data_frame_class_with=Insights.INSIGHT_TABLE_CLASS) InsightState.html_top_inc_cat = HTMLHelper.as_html_form_from_df( RawInsights.Dynammic.get_top_n_income_categories( DateRange(State.date_range_start, State.date_range_end), BankSchema.BANK_TB_NAME), Insights.TOP_INC_TABLE_ID, replace_default_data_frame_class_with=Insights.INSIGHT_TABLE_CLASS) InsightState.html_top_exp_cat = HTMLHelper.as_html_form_from_df( RawInsights.Dynammic.get_top_n_expense_categories( DateRange(State.date_range_start, State.date_range_end), BankSchema.BANK_TB_NAME), Insights.TOP_EXP_TABLE_ID, replace_default_data_frame_class_with=Insights.INSIGHT_TABLE_CLASS)
class DataState(State): order_by_column_name = BankSchema.SCHEMA_BANK_DATE.name order_by = Defaults.ORDER_BY_DEFAULT html_df = HTMLHelper.as_html_form_from_sql( BankSchema.BANK_TB_NAME, DateRange(State.date_range_start, State.date_range_end), order_by_column_name, order_by, table_id=ConstData.BANK_DATA_TABLE_ID) @staticmethod def as_dict(): return State.as_dict_helper(DataState, ConstData)
def visuals_redraw(): if request.method == 'POST': form = request.form drs, dre = form['start_query_name'], form['end_query_name'] if not valid_dr(drs, dre): return _standard_render() State.date_range_start, State.date_range_end = drs, dre # update which visuals to draw and redraw them requested_plots_to_draw = set(ImageRegistry.get_all_plot_ids()) & set(form.keys()) _update_requested_plots_to_draw(set(requested_plots_to_draw)) df = Data.get_table_as_df(DateRange(State.date_range_start, State.date_range_end), table_name=BankSchema.BANK_TB_NAME) if df is None or df.shape[0] == 0: flash(f'No data int table {BankSchema.BANK_TB_NAME} produce diagrams.', 'warning') else: ImageRegistry.plot_all(list(requested_plots_to_draw), df, drs, dre) return _standard_render()
def data_download(): if request.method == 'POST': logger.info('Downloading bank data as csv file.') with tempfile.TemporaryDirectory() as tmpdir: dr = DateRange(State.date_range_start, State.date_range_end) bank_df = Data.get_table_as_df(dr, BankSchema.BANK_TB_NAME, DataState.order_by_column_name, DataState.order_by) if bank_df is None or bank_df.shape[0] == 0: flash( f'{BankSchema.BANK_TB_NAME} table is empty. Nothing to download.', 'warning') return _standard_render() bank_df.to_csv(os.path.join(tmpdir, ConstData.FILE_NAME_DOWNLOAD), index=False) return send_from_directory(directory=tmpdir, filename=ConstData.FILE_NAME_DOWNLOAD, as_attachment=True) return _standard_render()