def process_measure(report_id, data, session): def add_missing_dict_entries(dst, src): # x.update(y) overwrites entries in x with those in y; # we want to only add those not already present for (k, v) in src.items(): if k not in dst: dst[k] = v cell_measures = {} wifi_measures = {} measure_data = dict( report_id=report_id, lat=data['lat'], lon=data['lon'], heading=data.get('heading', -1.0), speed=data.get('speed', -1.0), time=encode_datetime(data['time']), accuracy=data.get('accuracy', 0), altitude=data.get('altitude', 0), altitude_accuracy=data.get('altitude_accuracy', 0), ) measure_radio = RADIO_TYPE.get(data['radio'], -1) if data.get('cell'): # flatten measure / cell data into a single dict for c in data['cell']: add_missing_dict_entries(c, measure_data) c = normalized_cell_measure_dict(c, measure_radio) if c is None: continue key = to_cellkey_psc(c) if key in cell_measures: existing = cell_measures[key] if existing['ta'] > c['ta'] or \ (existing['signal'] != 0 and existing['signal'] < c['signal']) or \ existing['asu'] < c['asu']: cell_measures[key] = c else: cell_measures[key] = c cell_measures = cell_measures.values() # flatten measure / wifi data into a single dict if data.get('wifi'): for w in data['wifi']: add_missing_dict_entries(w, measure_data) w = normalized_wifi_measure_dict(w) if w is None: continue key = w['key'] if key in wifi_measures: existing = wifi_measures[key] if existing['signal'] != 0 and \ existing['signal'] < w['signal']: wifi_measures[key] = w else: wifi_measures[key] = w wifi_measures = wifi_measures.values() return (cell_measures, wifi_measures)
def check_normalized_wifi(self, measure, wifi, expect): d = measure.copy() d.update(wifi) result = normalized_wifi_measure_dict(d) if expect is None: self.assertEqual(result, expect) else: for (k, v) in expect.items(): self.assertEqual(result[k], v) return result
def create_wifi_measure(utcnow, entry): entry = normalized_wifi_measure_dict(entry) if entry is None: return None return WifiMeasure( measure_id=entry.get('measure_id'), created=utcnow, lat=entry['lat'], lon=entry['lon'], time=decode_datetime(entry.get('time', '')), accuracy=entry.get('accuracy', 0), altitude=entry.get('altitude', 0), altitude_accuracy=entry.get('altitude_accuracy', 0), key=entry['key'], channel=entry.get('channel', 0), signal=entry.get('signal', 0), )
def create_wifi_measure(utcnow, entry): entry = normalized_wifi_measure_dict(entry) if entry is None: return None report_id = entry.get('report_id') if report_id: report_id = uuid.UUID(hex=report_id).bytes return WifiMeasure( report_id=report_id, created=utcnow, lat=entry['lat'], lon=entry['lon'], time=decode_datetime(entry.get('time', '')), accuracy=entry.get('accuracy', 0), altitude=entry.get('altitude', 0), altitude_accuracy=entry.get('altitude_accuracy', 0), key=entry['key'], channel=entry.get('channel', 0), signal=entry.get('signal', 0), snr=entry.get('signalToNoiseRatio', 0), heading=entry.get('heading', -1.0), speed=entry.get('speed', -1.0), )