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