def choropleth_data(dcid): """ Get stats var data needed for choropleth charts for a given place API Returns: Dictionary with key as stat var value as object with date,dictionary of place: value, number of data points, exploreUrl, list of sources """ all_stat_vars, choropleth_configs = get_choropleth_sv() geos, _ = get_choropleth_places(dcid) if not all_stat_vars or not geos: return Response(json.dumps({}), 200, mimetype='application/json') # Get data for all the stat vars for every place we will need and process the data all_sv_data = dc_service.get_stats_all(geos, list(all_stat_vars)) if not 'placeData' in all_sv_data: return Response(json.dumps({}), 200, mimetype='application/json') processed_data = process_choropleth_data(all_sv_data['placeData']) result = {} for cc in choropleth_configs: # we should only be making choropleths for configs with a single stat var sv = cc['statsVars'][0] cc_data, statvar_denom = landing_page_api.get_snapshot_across_places( cc, processed_data, geos) data_values = cc_data.get('data', []) data_dict = dict() scaling = cc.get('scaling', 1) if 'relatedChart' in cc: scaling = cc['relatedChart'].get('scaling', scaling) for value in data_values: if 'dcid' not in value or 'data' not in value: continue val = value['data'].get(sv, None) if val: val = val * scaling data_dict[value['dcid']] = val is_scaled = (('relatedChart' in cc and cc['relatedChart'].get('scale', False)) or ('denominator' in cc)) exploreUrl = landing_page_api.build_url([dcid], statvar_denom, is_scaled) cc_result = { 'date': cc_data.get('date', None), 'data': data_dict, 'numDataPoints': len(data_values), # TODO (chejennifer): exploreUrl should link to choropleth tool once the tool is ready 'exploreUrl': exploreUrl, 'sources': cc_data.get('sources', []) } result[sv] = cc_result return Response(json.dumps(result), 200, mimetype='application/json')
def get_stats_all(): dcids = request.args.getlist("places") stat_vars = request.args.getlist("statVars") return Response(json.dumps(dc.get_stats_all(dcids, stat_vars)), 200, mimetype="application/json")