def test_empty_dashboard_chart(self): if frappe.db.exists('Dashboard Chart', 'Test Empty Dashboard Chart'): frappe.delete_doc('Dashboard Chart', 'Test Empty Dashboard Chart') frappe.db.sql('delete from `tabError Log`') frappe.get_doc( dict(doctype='Dashboard Chart', chart_name='Test Empty Dashboard Chart', chart_type='Count', document_type='Error Log', based_on='creation', timespan='Last Year', time_interval='Monthly', filters_json='[]', timeseries=1)).insert() cur_date = datetime.now() - relativedelta(years=1) result = get(chart_name='Test Empty Dashboard Chart', refresh=1) for idx in range(13): month = get_last_day(cur_date) month = formatdate(month.strftime('%Y-%m-%d')) self.assertEqual(result.get('labels')[idx], get_period(month)) cur_date += relativedelta(months=1) frappe.db.rollback()
def test_empty_dashboard_chart(self): if frappe.db.exists("Dashboard Chart", "Test Empty Dashboard Chart"): frappe.delete_doc("Dashboard Chart", "Test Empty Dashboard Chart") frappe.db.sql("delete from `tabError Log`") frappe.get_doc( dict( doctype="Dashboard Chart", chart_name="Test Empty Dashboard Chart", chart_type="Count", document_type="Error Log", based_on="creation", timespan="Last Year", time_interval="Monthly", filters_json="[]", timeseries=1, )).insert() cur_date = datetime.now() - relativedelta(years=1) result = get(chart_name="Test Empty Dashboard Chart", refresh=1) for idx in range(13): month = get_last_day(cur_date) month = formatdate(month.strftime("%Y-%m-%d")) self.assertEqual(result.get("labels")[idx], get_period(month)) cur_date += relativedelta(months=1) frappe.db.rollback()
def get_chart_config(chart, filters, timespan, timegrain, from_date, to_date): if not from_date: from_date = get_from_date_from_timespan(to_date, timespan) from_date = get_period_beginning(from_date, timegrain) if not to_date: to_date = datetime.datetime.now() # get conditions from filters conditions, values = frappe.db.build_conditions(filters) # query will return year, unit and aggregate value data = frappe.db.sql(''' select {unit} as _unit, SUM({value_field}), COUNT(*) from `tab{doctype}` where {conditions} and {datefield} BETWEEN '{from_date}' and '{to_date}' group by _unit order by _unit asc '''.format( unit = chart.based_on, datefield = chart.based_on, value_field = chart.value_based_on or '1', doctype = chart.document_type, conditions = conditions, from_date = from_date.strftime('%Y-%m-%d'), to_date = to_date ), values) # add missing data points for periods where there was no result result = get_result(data, timegrain, from_date, to_date, chart.chart_type) chart_config = { "labels": [get_period(r[0], timegrain) for r in result], "datasets": [{ "name": chart.name, "values": [r[1] for r in result] }] } return chart_config
def get_chart_config(chart, filters, timespan, timegrain, from_date, to_date): if not from_date: from_date = get_from_date_from_timespan(to_date, timespan) from_date = get_period_beginning(from_date, timegrain) if not to_date: to_date = now_datetime() doctype = chart.document_type datefield = chart.based_on aggregate_function = get_aggregate_function(chart.chart_type) value_field = chart.value_based_on or '1' from_date = from_date.strftime('%Y-%m-%d') to_date = to_date filters.append([doctype, datefield, '>=', from_date, False]) filters.append([doctype, datefield, '<=', to_date, False]) data = frappe.db.get_list(doctype, fields=[ '{} as _unit'.format(datefield), '{aggregate_function}({value_field})'.format( aggregate_function=aggregate_function, value_field=value_field), ], filters=filters, group_by='_unit', order_by='_unit asc', as_list=True, ignore_ifnull=True) result = get_result(data, timegrain, from_date, to_date) chart_config = { "labels": [get_period(r[0], timegrain) for r in result], "datasets": [{ "name": chart.name, "values": [r[1] for r in result] }] } return chart_config
def get_chart_config(chart, filters, timespan, timegrain, from_date, to_date): if not from_date: from_date = get_from_date_from_timespan(to_date, timespan) from_date = get_period_beginning(from_date, timegrain) if not to_date: to_date = now_datetime() doctype = chart.document_type datefield = chart.based_on value_field = chart.value_based_on or "1" from_date = from_date.strftime("%Y-%m-%d") to_date = to_date filters.append([doctype, datefield, ">=", from_date, False]) filters.append([doctype, datefield, "<=", to_date, False]) data = frappe.db.get_list( doctype, fields=[ "{} as _unit".format(datefield), "SUM({})".format(value_field), "COUNT(*)" ], filters=filters, group_by="_unit", order_by="_unit asc", as_list=True, ignore_ifnull=True, ) result = get_result(data, timegrain, from_date, to_date, chart.chart_type) chart_config = { "labels": [get_period(r[0], timegrain) for r in result], "datasets": [{ "name": chart.name, "values": [r[1] for r in result] }], } return chart_config
def test_chart_wih_one_value(self): if frappe.db.exists('Dashboard Chart', 'Test Empty Dashboard Chart 2'): frappe.delete_doc('Dashboard Chart', 'Test Empty Dashboard Chart 2') frappe.db.delete("Error Log") # create one data point frappe.get_doc( dict(doctype='Error Log', creation='2018-06-01 00:00:00')).insert() frappe.get_doc( dict(doctype='Dashboard Chart', chart_name='Test Empty Dashboard Chart 2', chart_type='Count', document_type='Error Log', based_on='creation', timespan='Last Year', time_interval='Monthly', filters_json='[]', timeseries=1)).insert() cur_date = datetime.now() - relativedelta(years=1) result = get(chart_name='Test Empty Dashboard Chart 2', refresh=1) for idx in range(13): month = get_last_day(cur_date) month = formatdate(month.strftime('%Y-%m-%d')) self.assertEqual(result.get('labels')[idx], get_period(month)) cur_date += relativedelta(months=1) # only 1 data point with value self.assertEqual(result.get('datasets')[0].get('values')[2], 0) frappe.db.rollback()