def sample(self, **kwargs): report = { 'lat': GB_LAT, 'lon': GB_LON, } for (k, v) in kwargs.items(): report[k] = v return Report.validate(report)
def process_report(self, data): 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. # We also only want to copy the top-level base report data # and not any nested values like cell or wifi. for (key, value) in src.items(): if key != 'radio' and key not in dst \ and not isinstance(value, (tuple, list, dict)): dst[key] = value report_data = Report.validate(data) if report_data is None: return ([], []) cell_observations = {} wifi_observations = {} if data.get('cell'): # flatten report / cell data into a single dict for cell in data['cell']: # only validate the additional fields cell = CellReport.validate(cell) if cell is None: continue add_missing_dict_entries(cell, report_data) cell_key = CellObservation.to_hashkey(cell) if cell_key in cell_observations: existing = cell_observations[cell_key] if existing['ta'] > cell['ta'] or \ (existing['signal'] != 0 and existing['signal'] < cell['signal']) or \ existing['asu'] < cell['asu']: cell_observations[cell_key] = cell else: cell_observations[cell_key] = cell cell_observations = cell_observations.values() # flatten report / wifi data into a single dict if data.get('wifi'): for wifi in data['wifi']: # only validate the additional fields wifi = WifiReport.validate(wifi) if wifi is None: continue add_missing_dict_entries(wifi, report_data) wifi_key = WifiObservation.to_hashkey(wifi) if wifi_key in wifi_observations: existing = wifi_observations[wifi_key] if existing['signal'] != 0 and \ existing['signal'] < wifi['signal']: wifi_observations[wifi_key] = wifi else: wifi_observations[wifi_key] = wifi wifi_observations = wifi_observations.values() return (cell_observations, wifi_observations)
def process_report(self, data): report = Report.create(**data) if report is None: return ({}, {}) malformed = {} observations = {} for name, report_cls, obs_cls in (('blue', BlueReport, BlueObservation), ('cell', CellReport, CellObservation), ('wifi', WifiReport, WifiObservation)): malformed[name] = 0 observations[name] = {} if data.get(name): for item in data[name]: # validate the blue/cell/wifi specific fields item_report = report_cls.create(**item) if item_report is None: malformed[name] += 1 continue # combine general and specific report data into one item_obs = obs_cls.combine(report, item_report) item_key = item_obs.unique_key # if we have better data for the same key, ignore existing = observations[name].get(item_key) if existing is not None and existing.better(item_obs): continue observations[name][item_key] = item_obs obs = { 'blue': observations['blue'].values(), 'cell': observations['cell'].values(), 'wifi': observations['wifi'].values(), } return (obs, malformed)
def process_report(self, data): report = Report.create(**data) if report is None: return ({}, {}) malformed = {} observations = {} for name, report_cls, obs_cls in ( ('blue', BlueReport, BlueObservation), ('cell', CellReport, CellObservation), ('wifi', WifiReport, WifiObservation)): malformed[name] = 0 observations[name] = {} if data.get(name): for item in data[name]: # validate the blue/cell/wifi specific fields item_report = report_cls.create(**item) if item_report is None: malformed[name] += 1 continue # combine general and specific report data into one item_obs = obs_cls.combine(report, item_report) item_key = item_obs.unique_key # if we have better data for the same key, ignore existing = observations[name].get(item_key) if existing is not None and existing.better(item_obs): continue observations[name][item_key] = item_obs obs = { 'blue': observations['blue'].values(), 'cell': observations['cell'].values(), 'wifi': observations['wifi'].values(), } return (obs, malformed)
def process_report(self, data): malformed = {'cell': 0, 'wifi': 0} observations = {'cell': {}, 'wifi': {}} report = Report.create(**data) if report is None: return (None, None, malformed) for name, report_cls, obs_cls in ( ('cell', CellReport, CellObservation), ('wifi', WifiReport, WifiObservation)): observations[name] = {} if data.get(name): for item in data[name]: # validate the cell/wifi specific fields item_report = report_cls.create(**item) if item_report is None: malformed[name] += 1 continue # combine general and specific report data into one item_obs = obs_cls.combine(report, item_report) item_key = item_obs.hashkey() # if we have better data for the same key, ignore existing = observations[name].get(item_key) if existing is not None: if existing.better(item_obs): continue observations[name][item_key] = item_obs return ( observations['cell'].values(), observations['wifi'].values(), malformed, )
def process_report(self, data): 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. # We also only want to copy the top-level base report data # and not any nested values like cell or wifi. for (key, value) in src.items(): if key != 'radio' and key not in dst \ and not isinstance(value, (tuple, list, dict)): dst[key] = value def better_cell_obs(new, old): comparators = [ ('ta', operator.lt), ('signal', operator.gt), ('asu', operator.gt), ] for field, better in comparators: if (None not in (old[field], new[field]) and better(new[field], old[field])): return True return False def better_wifi_obs(new, old): if (None not in (old['signal'], new['signal']) and new['signal'] > old['signal']): return True return False report_data = Report.validate(data) if report_data is None: return ([], []) cell_observations = {} wifi_observations = {} if data.get('cell'): # flatten report / cell data into a single dict for cell in data['cell']: # only validate the additional fields cell = CellReport.validate(cell) if cell is None: continue add_missing_dict_entries(cell, report_data) cell_key = CellObservation.to_hashkey(cell) if cell_key in cell_observations: existing = cell_observations[cell_key] if better_cell_obs(cell, existing): cell_observations[cell_key] = cell else: cell_observations[cell_key] = cell cell_observations = cell_observations.values() # flatten report / wifi data into a single dict if data.get('wifi'): for wifi in data['wifi']: # only validate the additional fields wifi = WifiReport.validate(wifi) if wifi is None: continue add_missing_dict_entries(wifi, report_data) wifi_key = WifiObservation.to_hashkey(wifi) if wifi_key in wifi_observations: existing = wifi_observations[wifi_key] if better_wifi_obs(wifi, existing): wifi_observations[wifi_key] = wifi else: wifi_observations[wifi_key] = wifi wifi_observations = wifi_observations.values() return (cell_observations, wifi_observations)
def sample(self, **kwargs): report = {"lat": GB_LAT, "lon": GB_LON} for (k, v) in kwargs.items(): report[k] = v return Report.validate(report)
def test_valid(self): self.assertFalse(Report.create(lat=GB_LAT, lon=GB_LON) is None)
def test_invalid(self): self.assertTrue(Report.create(lat=0.0) is None) self.assertTrue(Report.create(lat=0.0, lon='abc') is None) self.assertTrue(Report.create(lat=0.0, lon=250.0) is None)