def get(self, alert_id): result = db.session.query(model.Data).filter( model.Data.variables["alert_id"].astext == alert_id).first() if result: if not is_allowed_location(result.clinic, g.allowed_location): return {} if result.variables.get("alert_type", None) == "threshold": other_data = rows_to_dicts( db.session.query(model.Data).filter( model.Data.variables["master_alert"].astext == result.uuid).all()) else: other_data = {} return jsonify({ "alert": row_to_dict(result), "linked_alerts": other_data }) else: result = db.session.query(model.DisregardedData).filter( model.DisregardedData.variables["alert_id"].astext == alert_id).first() if result: return jsonify({ "alert": row_to_dict(result), "linked_alerts": {} }) return {}
def get(self, location_id): if not is_allowed_location(location_id, g.allowed_location): return {"records": []} results = db.session.query(Data).filter(or_( loc == location_id for loc in (Data.country, Data.region, Data.district, Data.clinic)), Data.submission_date >= datetime.now() - timedelta(days=1)).order_by(Data.submission_date.desc()).all() return jsonify({"records": rows_to_dicts(results)})
def test_rows_to_dicts(self): rows1 = [] for i in range(50): rows1.append(model.AggregationVariables( id="tot_" + str(i), name="Test " + str(i) )) result_dicts = util.rows_to_dicts(rows1) for i in range(50): self.assertEqual(result_dicts[i]["id"], "tot_" + str(i)) self.assertEqual(result_dicts[i]["name"], "Test " + str(i)) # With id result_dicts = util.rows_to_dicts(rows1, dict_id="id") for i in range(50): self.assertEqual(result_dicts["tot_" + str(i)]["id"], "tot_" + str(i)) # With tuple of tables combined_rows = [] for i in range(50): combined_rows.append( ( rows1[i], model.Locations(name="Location " + str(i), id=str(i)) ) ) result_dicts = util.rows_to_dicts(combined_rows) for i in range(50): self.assertIn("locations", result_dicts[i]) self.assertIn("aggregation_variables", result_dicts[i]) self.assertEqual(result_dicts[i]["aggregation_variables"]["id"], "tot_" + str(i)) self.assertEqual(result_dicts[i]["locations"]["name"], "Location " + str(i)) with self.assertRaises(TypeError): result_dicts = util.rows_to_dicts(combined_rows, dict_id="id")
def get(self): if current_app.config['PRODUCTION']: logging.warning("api/devices call not implemented for production") abort(501, message="Api method not implemented.") devices = db.session.query(model.Devices) devices_dict = rows_to_dicts(devices) if devices_dict: device_ids = [] for item in devices_dict: device_ids.append(item["device_id"]) return_data = c.device_api(url='/device', data=json.dumps(device_ids)) return return_data else: return {"message": "No devices registered"}
def get(self): parser = reqparse.RequestParser() parser.add_argument('deviceId') args = parser.parse_args() device_id = args.get('deviceId') location_query = db.session.query(model.Locations) filter_conditions = [] if device_id: filter_conditions.append(_get_by_device_id_filter(device_id)) return jsonify( rows_to_dicts(location_query.filter(*filter_conditions).all(), dict_id="id"))
def get(self, category): if category == "locations" or "locations:" in category: l = locations.Locations() return l.get() elif category == "alert": if "include_group_b" in request.args: results = db.session.query(model.AggregationVariables).filter( or_(model.AggregationVariables.alert == 1, model.AggregationVariables.alert_desc == "Group B")) else: results = db.session.query(model.AggregationVariables).filter( model.AggregationVariables.alert == 1) elif category != "all": results = db.session.query(model.AggregationVariables).filter( model.AggregationVariables.category.has_key(category)) else: results = db.session.query(model.AggregationVariables) return rows_to_dicts(results.all(), dict_id="id")
def get(self, variable, location_id): if not is_allowed_location(location_id, g.allowed_location): return {"records": []} only_last_week = request.args.get('only_last_week') unique_clinic = request.args.get('unique_clinic') conditions = [ Data.variables.has_key(str(variable)) ] if only_last_week: current_week = epi_week_util.epi_week_for_date(datetime.today())[1] # Get last full week conditions.append(Data.epi_week == current_week - 1) results = db.session.query(Data).filter( *conditions, or_( loc == location_id for loc in (Data.country, Data.region, Data.district, Data.clinic))).all() if unique_clinic: assert unique_clinic == "last" clinic_records = {} for r in results: clinic_records.setdefault(r.clinic, []) clinic_records[r.clinic].append(r) results = [] for clinic, records in clinic_records.items(): if len(records) == 1: results.append(records[0]) else: results.append( sorted(records, key=lambda x: x.date)[-1] ) return jsonify({"records": rows_to_dicts(results)})
def get_alerts(args, allowed_location=1): """ Gets all alerts where if reason is a key in args we only get alerts with a matching reason. If "location" is in the key we get all alerts from the location or any child clinics. Returns a list of alerts where each element is a dict with {"alerts": alert_info, "links": link_info} The link info is the alert investigation Args:\n args: request args that can include "only_latest", "reason" and "location" as keys to restrict the returned alerts. \n Returns:\n alerts(list): a list of alerts. \n """ conditions = [model.Data.variables.has_key("alert")] disregarded_conditions = [model.DisregardedData.variables.has_key("alert")] only_latest = int(args.get("only_latest", 0)) if "reason" in args.keys(): conditions.append( model.Data.variables["alert_reason"].astext == args["reason"]) disregarded_conditions.append( model.DisregardedData.variables["alert_reason"].astext == args["reason"]) if "location" in args.keys(): if not is_allowed_location(args["location"], allowed_location): return {} cond = or_(loc == args["location"] for loc in (model.Data.country, model.Data.zone, model.Data.region, model.Data.district, model.Data.clinic)) disregarded_cond = or_(loc == args["location"] for loc in (model.DisregardedData.country, model.DisregardedData.zone, model.DisregardedData.region, model.DisregardedData.district, model.DisregardedData.clinic)) conditions.append(cond) disregarded_conditions.append(disregarded_cond) else: cond = or_(loc == allowed_location for loc in (model.Data.country, model.Data.zone, model.Data.region, model.Data.district, model.Data.clinic)) disregarded_cond = or_(loc == allowed_location for loc in (model.DisregardedData.country, model.DisregardedData.zone, model.DisregardedData.region, model.DisregardedData.district, model.DisregardedData.clinic)) conditions.append(cond) disregarded_conditions.append(disregarded_cond) if "start_date" in args.keys(): conditions.append(model.Data.date >= args["start_date"]) disregarded_conditions.append( model.DisregardedData.date >= args["start_date"]) if "end_date" in args.keys(): conditions.append(model.Data.date < args["end_date"]) disregarded_conditions.append( model.DisregardedData.date < args["end_date"]) data_query = db.session.query(model.Data).filter(*conditions).order_by( model.Data.date.desc()) disregarded_query = db.session.query( model.DisregardedData).filter(*disregarded_conditions) if only_latest: results = data_query.limit(only_latest).all() results += disregarded_query.limit(only_latest).all() results = sorted(results, key=lambda r: r.date, reverse=True)[:only_latest] else: results = data_query.all() results += disregarded_query.all() return rows_to_dicts(results)