def calculate_average_daily_measurements_for_last_28_days(): ms = monitoring_site.MonitoringSite() gr = graph.Graph() monitoring_sites = list( app.mongo.db.monitoring_site.find({}, { "DeviceCode": 1, "Parish": 1, "LocationCode": 1, "Division": 1, "_id": 0 })) devices_historical_records = [] for monitoring_site_device in monitoring_sites: print(monitoring_site_device) code = monitoring_site_device['DeviceCode'] historical_results = [] records = [] pm25_daily_values = [] average_pm25 = 0 if code: #check if code is not empty print(code) parish = monitoring_site_device['Parish'] division = monitoring_site_device['Division'] location_code = monitoring_site_device['LocationCode'] created_at = helpers.str_to_date( helpers.date_to_str(datetime.now())) endtime = helpers.date_to_str(datetime.now()) starttime = helpers.date_to_str(datetime.now() - timedelta(days=28)) monitoring_site_measurements_cursor = gr.get_filtered_data( code, starttime, endtime, 'daily', 'PM 2.5') #monitoring_site_measurements_cursor = ms.get_device_past_28_days_measurements(code) for site in monitoring_site_measurements_cursor: record = { 'pm2_5_value': int(site['pollutant_value']), 'time': site["time"] } records.append(record) pm25_daily_values.append(int(site['pollutant_value'])) historical_results.append(site) if len(pm25_daily_values) > 0: average_pm25 = np.mean(pm25_daily_values) historical_record = { 'deviceCode': code, 'average_pm25': average_pm25, 'historical_records': records, 'Parish': parish, 'Division': division, 'LocationCode': location_code, 'created_at': created_at } devices_historical_records.append(historical_record) mongo_helpers.save_device_daily_historical_averages( devices_historical_records) return jsonify({'response': 'all new hourly measurements saved'}), 200
def get_device_past_28_days_measurements(): ms = monitoring_site.MonitoringSite() gr = graph.Graph() if request.method == 'GET': device_code = request.args.get('device_code') if device_code: historical_results = [] records = [] pm25_daily_values = [] endtime = helpers.date_to_str(datetime.now()) starttime = helpers.date_to_str(datetime.now() - timedelta(days=28)) monitoring_site_measurements_cursor = gr.get_filtered_data( device_code, starttime, endtime, 'daily', 'PM 2.5') #monitoring_site_measurements_cursor = ms.get_device_past_28_days_measurements(device_code) for site in monitoring_site_measurements_cursor: record = { 'pm2_5_value': int(site['pollutant_value']), 'time': site["time"] } records.append(record) pm25_daily_values.append(int(site['pollutant_value'])) historical_results.append(site) return jsonify({ "historical_measurements": historical_results, "records": records, "pm25_values": pm25_daily_values }) else: return jsonify({ "error msg": "device code wasn't supplied in the query string parameter." })
def get_exceedances(): gr = graph.Graph() if request.method == 'POST': json_data = request.get_json() if not json_data: return {'message': 'No input data provided'}, 400 pollutant = json_data["pollutant"] standard = json_data["standard"] exceedances_data = gr.get_all_devices_past_28_days_exceedences( pollutant, standard) return jsonify(exceedances_data[0]['exceedences'])
def get_random_location_hourly_customised_chart_data_2(): ms = monitoring_site.MonitoringSite() gr = graph.Graph() device_code = 'A743BPWK' start_date = '2020-04-09T07:00:00.000000Z' end_date = '2020-05-12T07:00:00.000000Z' frequency = 'monthly' pollutant = 'PM 2.5' chart_type = 'pie' organisation_name = 'KCCA' parish = ' Wandegeya' location_code = 'KCCA_KWPE_AQ05' division = 'Kawempe' custom_chat_data = [] datasets = [] colors = ['#7F7F7F', '#E377C2', '#17BECF', '#BCBD22', '#3f51b5'] # blue,cyan, olive, custom_chart_title = 'Mean ' + frequency.capitalize() + ' ' + \ pollutant + ' for ' locations_names = parish custom_chart_title = custom_chart_title + locations_names + ' Between ' + helpers.convert_date_to_formated_str(helpers.str_to_date( start_date), frequency) + ' and ' + helpers.convert_date_to_formated_str(helpers.str_to_date(end_date), frequency) values = [] labels = [] device_results = {} filtered_data = gr.get_filtered_data( device_code, start_date, end_date, frequency, pollutant) if filtered_data: for data in filtered_data: values.append(data['pollutant_value']) labels.append(data['time']) device_results = {'pollutant_values': values, 'labels': labels} color = colors.pop() dataset = {'data': values, 'label': parish + ' ' + pollutant, 'borderColor': color, 'backgroundColor': color, 'fill': False} datasets.append(dataset) custom_chat_data.append({'start_date': start_date, 'end_date': end_date, 'division': division, 'parish': parish, 'frequency': frequency, 'pollutant': pollutant, 'location_code': location_code, 'chart_type': chart_type, 'chart_data': device_results}) return jsonify({'results': custom_chat_data, 'datasets': datasets, 'custom_chart_title': custom_chart_title})
mnode.add_edge('8', 20) mnode.add_edge('2', 15) mnode.add_edge('10', 10) nodes.append(mnode) mnode = node.Node('10', {}) mnode.add_edge('2', 5) mnode.add_edge('3', 15) nodes.append(mnode) return nodes #init nodes = CreateNodes() mygraph1 = graph.Graph('x', nodes) existnode = CreateexistNodewithnewedge() mygraph1.add_node(existnode) nodes2 = CreateNodes2() mygraph2 = graph.Graph('Y', nodes2) # Magic functions: print('\n__str__ - print (mygraph1)') print(mygraph1) print('\n__len__ - nodes number :{}\n'.format(len(mygraph1))) print('\n__add__ - new_graph = Y+X :'.format()) new_graph = mygraph1 + mygraph2
def download_customised_data(): # create an instance of the MonitoringSite class ms = monitoring_site.MonitoringSite() # create an instance of the Graph class gr = graph.Graph() if request.method != 'POST': return {'message': 'Method not allowed. The method is not allowed for the requested URL'}, 400 else: json_data = request.get_json() download_type = request.args.get('type') if not json_data: return {'message': 'No input data provided'}, 400 if not download_type: return {'message': 'Please specify the type of data you wish to download'}, 400 if download_type not in ['csv', 'json']: return {'message': 'Invalid data type. Please specify csv or json'}, 400 locations = json_data["locations"] start_date = json_data["startDate"] end_date = json_data["endDate"] frequency = json_data["frequency"] # select multiple pollutants pollutants = json_data["pollutants"] file_type = json_data["fileType"] degree_of_cleanness = json_data["degreeOfClean"] organisation_name = json_data["organisation_name"] custom_chat_data = [] # datasets = {} # displaying multiple locations datasets = [] locations_devices = [] for location in locations: devices = ms.get_location_devices_code( organisation_name, location['label']) # datasets[location['label']] = {} for device in devices: device_code = device['DeviceCode'] division = device['Division'] parish = device['Parish'] location_code = device['LocationCode'] values = [] labels = [] pollutant_list = [] device_results = {} data_to_download = {} channel_ref = [] # control how some of the data is accessed flag = 0 # loop through pollutants selected by the user for pollutant in pollutants: filtered_data = gr.get_filtered_data( device_code, start_date, end_date, frequency, pollutant['value']) data_to_download[pollutant['value']] = [] if filtered_data: for data in filtered_data: values.append(data['pollutant_value']) if flag == 0: labels.append(data['time']) channel_ref.append(device_code) pollutant_list.append(pollutant['value']) data_to_download[pollutant['value']].append( data['pollutant_value']) flag = 1 data_to_download['channel_ref'] = channel_ref data_to_download['device_code'] = device_code data_to_download['division'] = division data_to_download['parish'] = parish data_to_download['time'] = labels data_to_download['location_code'] = location_code data_to_download['start_date'] = start_date data_to_download['end_date'] = end_date data_to_download['frequency'] = frequency data_to_download['file_type'] = file_type data_to_download['owner'] = organisation_name data_to_download['location'] = location['label'] # datasets[location['label']] = data_to_download datasets.append(data_to_download) # downloading json if download_type == 'json': return jsonify({'results': datasets}) # downloading csv if download_type == 'csv': # print(json.dumps(datasets, indent=1)) # json normalization to pandas datafrome tempp = pd.DataFrame() for location in locations: temp = pd.json_normalize(datasets, 'time', ['owner']) tempp['datetime'] = temp[0] for pollutant in pollutants: temp = pd.json_normalize(datasets, pollutant['label'], [ 'owner', 'location', 'parish', 'division', 'frequency', 'location_code']) tempp['owner'] = temp['owner'] tempp['location'] = temp['location'] tempp['location_code'] = temp['location_code'] tempp['parish'] = temp['parish'] tempp['division'] = temp['division'] tempp['frequency'] = temp['frequency'] tempp[pollutant['label']] = temp[0] # FIELDS = ["location", "PM 2.5", "start_date"] # df = pd.json_normalize(datasets, max_level=1) # print(df) for location in locations: temp = pd.json_normalize(datasets, 'channel_ref', ['owner']) tempp['channel_ref'] = temp[0] print(tempp) final_data = tempp.to_json(orient='records') # buid a dataframe from data_to_download # df_new = pd.DataFrame(columns=['PM 2.5', 'PM 10', 'channel_ref']) # for item in datasets: # df_new[item] = datasets[item] # print(df_new) # print(df_new.to_json(orient='columns')) # return jsonify({'results': datasets}) return final_data
def generate_customised_chart_data(): ms = monitoring_site.MonitoringSite() gr = graph.Graph() if request.method == 'POST': json_data = request.get_json() if not json_data: return {'message': 'No input data provided'}, 400 # input_data, errors = validate_inputs(input_data=json_data) //add server side validation # if not errors: locations = json_data["locations"] start_date = json_data["startDate"] end_date = json_data["endDate"] frequency = json_data["frequency"] pollutant = json_data["pollutant"] chart_type = json_data["chartType"] organisation_name = json_data["organisation_name"] custom_chat_data = [] datasets = [] #displaying multiple locations locations_devices =[] colors =['#7F7F7F','#E377C2', '#17BECF', '#BCBD22','#3f51b5'] custom_chart_title= 'Mean ' + frequency.capitalize() + ' '+ pollutant + ' for ' locations_names = ','.join([str(location['label']) for location in locations]) custom_chart_title = custom_chart_title + locations_names custom_chart_title_second_section = ' Between ' + helpers.convert_date_to_formated_str(helpers.str_to_date(start_date),frequency) + ' and ' + helpers.convert_date_to_formated_str(helpers.str_to_date(end_date),frequency) for location in locations: devices = ms.get_location_devices_code( organisation_name, location['label']) for device in devices: device_code = device['DeviceCode'] division = device['Division'] parish = device['Parish'] location_code= device['LocationCode'] values =[] labels =[] background_colors= [] device_results={} if chart_type == 'pie': filtered_data = gr.get_piechart_data( device_code, start_date, end_date, frequency, pollutant) if filtered_data: for data in filtered_data: values.append(data['category_count']) labels.append(data['category_name']) background_colors.append( helpers.assign_color_to_pollutant_category(data['category_name'])) device_results = { 'pollutant_values': values, 'labels': labels} color = colors.pop() dataset = {'data': values, 'label': parish + ' ' + pollutant, 'backgroundColor': background_colors} datasets.append(dataset) custom_chat_data.append({'start_date':start_date, 'end_date':end_date, 'division':division, 'parish':parish,'frequency':frequency, 'pollutant':pollutant, 'location_code':location_code, 'chart_type':chart_type,'chart_data':device_results, 'datasets':datasets, 'custom_chart_title':custom_chart_title, 'custom_chart_title_second_section':custom_chart_title_second_section}) else: filtered_data = gr.get_filtered_data(device_code, start_date, end_date, frequency, pollutant) if filtered_data: for data in filtered_data: values.append(data['pollutant_value']) labels.append(data['time']) device_results = { 'pollutant_values': values, 'labels': labels} color = colors.pop() dataset = {'data': values, 'label': parish + ' ' + pollutant, 'borderColor': color, 'backgroundColor': color, 'fill': False} datasets.append(dataset) measurement_units = '(µg/m3)' if pollutant == 'NO2': measurement_units = ' Concentration' chart_label = pollutant + measurement_units custom_chat_data.append({'start_date':start_date, 'end_date':end_date, 'division':division, 'parish':parish,'frequency':frequency, 'pollutant':pollutant, 'location_code':location_code, 'chart_type':chart_type,'chart_data':device_results, 'datasets':datasets, 'custom_chart_title':custom_chart_title, 'chart_label':chart_label, 'custom_chart_title_second_section':custom_chart_title_second_section}) locations_devices.append(devices) return jsonify({'results':custom_chat_data, 'datasets':datasets, 'custom_chart_title':custom_chart_title, 'custom_chart_title_second_section':custom_chart_title_second_section})
# Build dict holds avg = time spent on path / number of users go throw this path for k,v in timedict_we.items(): avgdict_we[k] = int(round(v/countdict_we[k])) # Build dict holds avg = time spent on path / number of users go throw this path for kew,vew in timedict_ew.items(): avgdict_ew[kew] = int(round(v/countdict_ew[kew])) # Create first Graph nodes=[] # k exmple - north_east:1233 for k,v in avgdict_we.items(): mnode = node.Node(k.split('_')[0], {}) mnode.add_edge(k.split('_')[1], v) nodes.append(mnode) graph1= graph.Graph('r1', nodes) print(graph1) # Create second Graph nodes=[] for k,v in avgdict_ew.items(): mnode = node.Node(k.split('_')[0], {}) mnode.add_edge(k.split('_')[1], v) nodes.append(mnode) graph2= graph.Graph('r2', nodes) print(graph2) graph3 = graph1+graph2 print(graph3)
def creatgraphs(): nodes = [] mnode = node.Node('1', {}) mnode.add_edge('5', 20) mnode.add_edge('6', 5) mnode.add_edge('7', 15) mnode.add_edge('2', 10) mnode.add_edge('4', 20) nodes.append(mnode) mnode = node.Node('2', {}) mnode.add_edge('4', 10) mnode.add_edge('3', 20) nodes.append(mnode) mnode = node.Node('3', {}) mnode.add_edge('4', 5) mnode.add_edge('2', 15) nodes.append(mnode) mnode = node.Node('4', {}) mnode.add_edge('5', 10) nodes.append(mnode) graph1 = graph.Graph('1', nodes) nodes = [] mnode = node.Node('5', {}) mnode.add_edge('6', 5) nodes.append(mnode) mnode = node.Node('6', {}) nodes.append(mnode) mnode = node.Node('7', {}) mnode.add_edge('6', 10) nodes.append(mnode) graph2 = graph.Graph('2', nodes) nodes = [] mnode = node.Node('8', {}) mnode.add_edge('1', 5) mnode.add_edge('2', 20) mnode.add_edge('7', 5) nodes.append(mnode) mnode = node.Node('9', {}) mnode.add_edge('8', 20) mnode.add_edge('2', 15) mnode.add_edge('10', 10) nodes.append(mnode) mnode = node.Node('10', {}) mnode.add_edge('2', 5) mnode.add_edge('3', 15) nodes.append(mnode) graph3 = graph.Graph('3', nodes) return graph1, graph2, graph3