def clientlog(request): try: request_object = ast.literal_eval(request.read()) except: return HttpResponse(error_message_helper.invalid_format()) log = ClientLog() log.log_time = request_object['time'] log.deviceid = request_object['deviceid'] log.error_text = request_object['text'] log.tag = request_object['tag'] log.value = request_object['value'] log.save() return HttpResponse("")
def parameterCheck(request): response = {} print "start" try: request_object = ast.literal_eval(request.read()) except: return HttpResponse(error_message_helper.invalid_format()) s_type = request_object['networkType'] s_cellid = request_object['cellId'] s_localtime = request_object['localtime'] s_time = request_object['time'] s_deviceid = request_object['deviceid'] localtime_object = datetime.strptime(s_localtime, '%Y-%m-%d %H:%M:%S') s_timeslice = (int(localtime_object.hour) / 6) * 6 day_of_week = int(localtime_object.strftime('%w')) s_weekday = 0 if day_of_week > 0 and day_of_week < 6: s_weekday = 1 try: killdevice = KillList.objects.filter(deviceid=s_deviceid)[0] response['go_ahead'] = 0 return HttpResponse(str(response)) except: pass try: states = State.objects.filter(cellid=s_cellid, deviceid=s_deviceid, timeslice=s_timeslice, weekday=s_weekday, networktype=s_type)[0] response['go_ahead'] = 0 except: response['go_ahead'] = 1 pass response['go_ahead'] = 0 return HttpResponse(str(response))
def parameterCheck(request): response = {} print "start" try: request_object = ast.literal_eval(request.read()) except: return HttpResponse(error_message_helper.invalid_format()) s_type = request_object['networkType'] s_cellid = request_object['cellId'] s_localtime = request_object['localtime'] s_time = request_object['time'] s_deviceid = request_object['deviceid'] localtime_object = datetime.strptime(s_localtime, '%Y-%m-%d %H:%M:%S') s_timeslice = (int(localtime_object.hour)/6)*6 day_of_week = int(localtime_object.strftime('%w')) s_weekday=0 if day_of_week>0 and day_of_week<6: s_weekday = 1 try: killdevice = KillList.objects.filter(deviceid=s_deviceid)[0] response['go_ahead']=0 return HttpResponse(str(response)) except: pass try: states = State.objects.filter(cellid=s_cellid,deviceid=s_deviceid,timeslice=s_timeslice,weekday=s_weekday,networktype=s_type)[0] response['go_ahead']=0 except: response['go_ahead']=1 pass response['go_ahead']=0 return HttpResponse(str(response))
def measurement(request): response = {} message = [] print "start" try: request_object = ast.literal_eval(request.read()) except: return HttpResponse(error_message_helper.invalid_format()) m_wifi = None m_sim = None m_state = {} lastmiles = {} count = 0 m_loss = None delay_variation = {} m_delay_variation = 0 try: try: m_deviceid = request_object['deviceid'] except: m_deviceid = "" pass m_time = request_object['time'] m_localtime = request_object['localtime'] count += 1 pings = request_object['pings'] count += 1 try: lastmiles = request_object['lastmiles'] except: pass m_device = request_object['device'] count += 1 m_network = request_object['network'] count += 1 try: m_sim = request_object['sim'] except: pass m_throughput = request_object['throughput'] count += 1 m_gps = request_object['gps'] count += 1 m_usage = request_object['usage'] count += 1 m_battery = request_object['battery'] try: m_warmup = request_object['warmup_experiment'] except: pass count += 1 m_screens = request_object['screens'] count += 1 try: m_wifi = request_object['wifi'] except: pass try: m_state = request_object['state'] except: pass try: m_ismanual = request_object['isManual'] except: m_ismanual = 0 try: m_loss = request_object['loss'] except: pass try: delay_variation = request_object['delay_variation'] m_delay_variation = 1 except: m_delay_variation = 0 pass except Exception as inst: message.append( error_message_helper.insert_entry_fail("measurement-extract", inst)) insertJSON.error_log(request_object, m_deviceid, request) return HttpResponse(str(message)) print "measurement insertion started..." measurement = Measurement() measurement.time = m_time if m_ismanual == 1: measurement.ismanual = True else: measurement.ismanual = False measurement.localtime = m_localtime try: details = Device.objects.filter(deviceid=m_deviceid)[0] try: exist = Measurement.objects.filter(deviceid=details, time=m_time)[0] print m_time print exist.measurementid return HttpResponse(error_message_helper.duplicate_entry()) except Exception as inst: pass except: pass print "trying device" try: details = insertJSON.device(m_device, m_deviceid, m_sim) measurement.deviceid = details except Exception as inst: message.append(error_message_helper.insert_entry_fail( "device++", inst)) insertJSON.error_log(message, m_deviceid, request) return HttpResponse(str(message)) try: measurement.save() except Exception as inst: message.append( error_message_helper.insert_entry_fail("measurement", inst)) try: network = insertJSON.network(m_network, measurement) except Exception as inst: print inst message.append(error_message_helper.insert_entry_fail("network", inst)) try: insertJSON.screen(m_screens, m_deviceid) except Exception as inst: message.append(error_message_helper.insert_entry_fail("screens", inst)) try: throughput = insertJSON.throughput(m_throughput, measurement) except Exception as inst: message.append( error_message_helper.insert_entry_fail("throughput", inst)) try: gps = insertJSON.gps(m_gps, measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("gps", inst)) try: if not m_wifi == None: wifi = insertJSON.wifi(m_wifi, measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("wifi", inst)) try: battery = insertJSON.battery(m_battery, measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("battery", inst)) try: battery = insertJSON.warmup(m_warmup, measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("warmup", inst)) try: usage = insertJSON.usage(m_usage, measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("usage", inst)) try: if not m_loss == None: loss = insertJSON.loss(m_loss, measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("loss", inst)) try: if not throughput.measurementid == None: s_cellid = m_state['cellId'] s_localtime = m_state['localtime'] s_time = m_state['time'] s_deviceid = m_state['deviceid'] s_type = m_state['networkType'] localtime_object = datetime.strptime(s_localtime, '%Y-%m-%d %H:%M:%S') s_timeslice = (int(localtime_object.hour) / 6) * 6 day_of_week = int(localtime_object.strftime('%w')) if day_of_week > 0 and day_of_week < 6: s_weekday = 1 else: s_weekday = 0 try: current_states = State.objects.filter(cellid=s_cellid, deviceid=s_deviceid, timeslice=s_timeslice, weekday=s_weekday, networktype=s_type)[0] except: states = State(cellid=s_cellid, deviceid=s_deviceid, timeslice=s_timeslice, weekday=s_weekday, networktype=s_type, measurementid=measurement.measurementid) states.save() except Exception as inst: pass m_id = measurement.measurementid #except Exception as inst: # return HttpResponse(error_message_helper.insert_entry_fail("measurement",inst)) count = 0 try: insertJSON.pings(pings, measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("ping", inst)) try: insertJSON.lastmiles(lastmiles, measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail( "lastmile", inst)) try: if not m_delay_variation == 0: insertJSON.delay_variation(delay_variation, measurement) except Exception as inst: message.append( error_message_helper.insert_entry_fail("delay_variation", inst)) print "measurement insertion ended" print str(message) if len(str(message)) > 5: print message insertJSON.error_log(message, m_deviceid, request) response['message'] = 'measurement inserted: ' + str(message) response['status'] = 'OK' return HttpResponse(str(response))
def measurement(request): response = {} message=[] print "start" try: request_object = ast.literal_eval(request.read()) except: return HttpResponse(error_message_helper.invalid_format()) m_wifi = None m_sim = None m_state = {} lastmiles = {} count = 0 m_loss = None delay_variation = {} m_delay_variation = 0 try: try: m_deviceid = request_object['deviceid'] except: m_deviceid = "" pass m_time = request_object['time'] m_localtime = request_object['localtime'] count+=1 pings = request_object['pings'] count+=1 try: lastmiles = request_object['lastmiles'] except: pass m_device = request_object['device'] count+=1 m_network = request_object['network'] count+=1 try: m_sim = request_object['sim'] except: pass m_throughput = request_object['throughput'] count+=1 m_gps = request_object['gps'] count+=1 m_usage = request_object['usage'] count+=1 m_battery = request_object['battery'] try: m_warmup = request_object['warmup_experiment'] except: pass count+=1 m_screens = request_object['screens'] count+=1 try: m_wifi = request_object['wifi'] except: pass try: m_state = request_object['state'] except: pass try: m_ismanual = request_object['isManual'] except: m_ismanual = 0 try: m_loss = request_object['loss'] except: pass try: delay_variation = request_object['delay_variation'] m_delay_variation = 1 except: m_delay_variation = 0 pass except Exception as inst: message.append(error_message_helper.insert_entry_fail("measurement-extract",inst)) insertJSON.error_log(request_object,m_deviceid,request) return HttpResponse(str(message)) print "measurement insertion started..." measurement = Measurement() measurement.time = m_time if m_ismanual == 1: measurement.ismanual = True else: measurement.ismanual = False measurement.localtime = m_localtime; try: details=Device.objects.filter(deviceid=m_deviceid)[0] try: exist = Measurement.objects.filter(deviceid=details,time=m_time)[0] print m_time print exist.measurementid return HttpResponse(error_message_helper.duplicate_entry()) except Exception as inst: pass except: pass print "trying device" try: details=insertJSON.device(m_device,m_deviceid,m_sim) measurement.deviceid = details except Exception as inst: message.append(error_message_helper.insert_entry_fail("device++",inst)) insertJSON.error_log(message,m_deviceid,request) return HttpResponse(str(message)) try: measurement.save() except Exception as inst: message.append(error_message_helper.insert_entry_fail("measurement",inst)) try: network=insertJSON.network(m_network,measurement) except Exception as inst: print inst message.append(error_message_helper.insert_entry_fail("network",inst)) try: insertJSON.screen(m_screens,m_deviceid) except Exception as inst: message.append(error_message_helper.insert_entry_fail("screens",inst)) try: throughput=insertJSON.throughput(m_throughput,measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("throughput",inst)) try: gps=insertJSON.gps(m_gps,measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("gps",inst)) try: if not m_wifi == None: wifi=insertJSON.wifi(m_wifi,measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("wifi",inst)) try: battery=insertJSON.battery(m_battery,measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("battery",inst)) try: battery=insertJSON.warmup(m_warmup,measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("warmup",inst)) try: usage=insertJSON.usage(m_usage,measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("usage",inst)) try: if not m_loss == None: loss=insertJSON.loss(m_loss,measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("loss",inst)) try: if not throughput.measurementid == None: s_cellid = m_state['cellId'] s_localtime = m_state['localtime'] s_time = m_state['time'] s_deviceid = m_state['deviceid'] s_type = m_state['networkType'] localtime_object = datetime.strptime(s_localtime, '%Y-%m-%d %H:%M:%S') s_timeslice = (int(localtime_object.hour)/6)*6 day_of_week = int(localtime_object.strftime('%w')) if day_of_week>0 and day_of_week<6: s_weekday = 1 else: s_weekday = 0 try: current_states =State.objects.filter(cellid=s_cellid,deviceid=s_deviceid,timeslice=s_timeslice,weekday=s_weekday,networktype=s_type)[0] except: states = State(cellid=s_cellid,deviceid=s_deviceid,timeslice=s_timeslice,weekday=s_weekday,networktype=s_type,measurementid=measurement.measurementid) states.save() except Exception as inst: pass m_id = measurement.measurementid #except Exception as inst: # return HttpResponse(error_message_helper.insert_entry_fail("measurement",inst)) count = 0 try: insertJSON.pings(pings,measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("ping",inst)) try: insertJSON.lastmiles(lastmiles,measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("lastmile",inst)) try: if not m_delay_variation == 0: insertJSON.delay_variation(delay_variation,measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("delay_variation",inst)) print "measurement insertion ended" print str(message) if len(str(message)) > 5: print message insertJSON.error_log(message,m_deviceid,request) response['message'] = 'measurement inserted: ' + str(message) response['status'] = 'OK' return HttpResponse(str(response))
def measurement(request): response = {} message = [] #reqdata = request.META.items() print("REQ " + request.META['REMOTE_ADDR'] + " --- " + request.META['HTTP_USER_AGENT']) #for k, v in values: # print(str(k)+" --- "+str(v)) ''' Parse JSON ''' try: rawcontent = request.read() content = unicode(rawcontent, errors='ignore') request_object = json.loads(content) print "JSON parsed!" except: print "JSON parse FAILED!" log_dataerror(rawcontent) return HttpResponse(error_message_helper.invalid_format()) ''' Extract data from JSON ''' try: m_time = request_object['time'] m_localtime = request_object['localtime'] m_deviceid = request_object['deviceid'] print("DEV " + str(m_deviceid)) try: m_pings = request_object['pings'] except: m_pings = {} try: m_traceroutes = request_object['traceroutes'] except: m_traceroutes = {} try: m_lastmiles = request_object['lastmiles'] except: m_lastmiles = {} try: m_warmup = request_object['warmup_experiment'] except: m_warmup = {} try: m_device = request_object['device'] except: m_device = None try: m_version = m_device['applicationVersion'] except: m_version = None try: m_network = request_object['network'] except: m_network = None try: m_sim = request_object['sim'] except: m_sim = None try: m_throughput = request_object['throughput'] except: m_throughput = None try: m_usage = request_object['usage'] except: m_usage = None try: m_battery = request_object['battery'] except: m_battery = None try: m_wifi = request_object['wifi'] except: m_wifi = None try: m_state = request_object['state'] except: m_state = None try: m_ismanual = request_object['isManual'] except: m_ismanual = 0 print "Data extracted from JSON!" except Exception as inst: '''Exception if one of the required fields is missing''' print traceback.format_exc() message.append( error_message_helper.insert_entry_fail("measurement-extract", inst)) insertJSON.error_log(request_object, m_deviceid, request) return HttpResponse(str(message)) ''' Construct measurement ''' measurement = Measurement() measurement.localtime = m_localtime measurement.time = m_time if m_ismanual == 1: measurement.ismanual = True else: measurement.ismanual = False ''' Check for a duplicate entry ''' try: device = Device.objects.filter( deviceid=m_deviceid)[0] # Check if the device exists in the DB try: duplicate = Measurement.objects.filter( deviceid=device, time=m_time)[0] # Check if the measurement is a duplicate return HttpResponse(error_message_helper.duplicate_entry()) except Exception as inst: pass except: pass ''' Insert device ''' try: device = insertJSON.device(m_device, m_deviceid, m_sim) print "Device inserted!" except Exception as inst: message.append(error_message_helper.insert_entry_fail( "device++", inst)) insertJSON.error_log(message, m_deviceid, request) return HttpResponse(str(message)) measurement.deviceid = device try: measurement.applicationversion = m_version except: measurement.applicationversion = None print measurement.applicationversion ''' Insert measurement ''' try: measurement.save() print "Measurement inserted!" except Exception as inst: message.append( error_message_helper.insert_entry_fail("measurement", inst)) return HttpResponse(str(message)) ''' Insert network ''' try: network = insertJSON.network(m_network, measurement) print "Network inserted!" except Exception as inst: message.append(error_message_helper.insert_entry_fail("network", inst)) ''' Insert throughput ''' if not m_throughput == None: try: throughput = insertJSON.throughput(m_throughput, measurement) print "Throughput inserted!" except Exception as inst: log_dataerror(rawcontent) message.append( error_message_helper.insert_entry_fail("throughput", inst)) ''' Insert wifi ''' if not m_wifi == None: try: wifi = insertJSON.wifi(m_wifi, measurement) print "Wifi inserted!" except Exception as inst: message.append(error_message_helper.insert_entry_fail( "wifi", inst)) ''' Insert battery ''' try: battery = insertJSON.battery(m_battery, measurement) print "Battery inserted!" except Exception as inst: message.append(error_message_helper.insert_entry_fail("battery", inst)) ''' Insert usage ''' if not m_usage == None: try: usage = insertJSON.usage(m_usage, measurement) print "Usage inserted!" except Exception as inst: message.append( error_message_helper.insert_entry_fail("usage", inst)) ''' Insert state ''' try: if not throughput.measurementid == None: s_cellid = m_state['cellId'] s_localtime = m_state['localtime'] s_time = m_state['time'] s_deviceid = m_state['deviceid'] s_type = m_state['networkType'] localtime_object = datetime.strptime(s_localtime, '%Y-%m-%d %H:%M:%S') s_timeslice = (int(localtime_object.hour) / 6) * 6 day_of_week = int(localtime_object.strftime('%w')) if day_of_week > 0 and day_of_week < 6: s_weekday = 1 else: s_weekday = 0 try: current_states = State.objects.filter(cellid=s_cellid, deviceid=s_deviceid, timeslice=s_timeslice, weekday=s_weekday, networktype=s_type)[0] except: states = State(cellid=s_cellid, deviceid=s_deviceid, timeslice=s_timeslice, weekday=s_weekday, networktype=s_type, measurementid=measurement.measurementid) states.save() print "State inserted!" except Exception as inst: pass ''' Insert ping ''' try: insertJSON.pings(m_pings, measurement) print "Pings inserted!" except Exception as inst: log_dataerror(rawcontent) message.append(error_message_helper.insert_entry_fail("ping", inst)) ''' Insert lastmiles ''' try: insertJSON.lastmiles(m_lastmiles, measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail( "lastmile", inst)) ''' Insert warmup ''' try: insertJSON.warmup(m_warmup, measurement) except Exception as inst: message.append(error_message_helper.insert_entry_fail("warmup", inst)) ''' Insert traceroute ''' try: insertJSON.traceroutes(m_traceroutes, measurement) except Exception as inst: log_dataerror(rawcontent) message.append( error_message_helper.insert_entry_fail("traceroute", inst)) response['message'] = 'measurement inserted: ' + str(message) response['status'] = 'OK' return HttpResponse(str(response))