def air_deductions(): beginDate = util.getDateFromParam(request.params.get("begin_span1")) endDate = util.getDateFromParam(request.params.get("end_span2")) if (not beginDate): ids = util.getIdsFromTable("air_deduct") else: ids = util.getIdsFromTable( "air_deduct", extraWhere=" ( begin_datetime >= %s AND begin_datetime <= %s ) ", extraArgs=(beginDate, endDate)) return { 'xlink': ['/resources/data/air_deductions/' + str(id) for id in ids] }
def get_airon_fills(): fill_ids = util.getIdsFromTable( "fill", extraWhere='''fill_type_id = 1 AND ( ((air_begin_datetime < NOW() AND air_end_datetime IS NULL) OR (air_begin_datetime < NOW() AND air_end_datetime > NOW())) )''' ) return fill_ids
def sensor_types(): device_type_id = request.params.get('device_type_id', None) extra_where = "" extra_args = () if device_type_id: try: device_type_id = int(device_type_id) extra_where = "device_type_id=%s" extra_args = (device_type_id, ) except (ValueError, TypeError): abort(400, 'Invalid device_type_id') rows = util.getRowsFromTable("device_type_to_sensor_type", extraWhere=extra_where, extraArgs=extra_args) return { 'xlink': [ '/resources/conf/sensor_types/' + str(row["sensor_type_id"]) for row in rows ] } else: ids = util.getIdsFromTable("sensor_type") return { 'xlink': ['/resources/conf/sensor_types/' + str(id) for id in ids] }
def alarm_contact_types_list(): format = request.params.get("format", "json") ids = util.getIdsFromTable("alarm_contact_type") return { 'xlink': ['/resources/alarms/alarm_contact_types/' + str(id) for id in ids] }
def devices(): bin_id = request.params.get('bin_id') bin_section_id = request.params.get('bin_section_id') year = request.params.get('year') # TODO: What if we want all devices regardless of year, we have no method to get a # TODO: list of valid years. if not year: abort(400, 'Bad parameters') extra_where = "year = %s " extra_args = (year, ) if bin_id: try: bin_id = int(bin_id) extra_where += " AND bin_id = %s " extra_args += (bin_id, ) except ValueError: abort(400, 'Invalid bin_id') if bin_section_id: try: bin_section_id = int(bin_section_id) extra_where += " AND bin_section_id = %s " extra_args += (bin_section_id, ) except ValueError: abort(400, 'Invalid bin_section_id') ids = util.getIdsFromTable('device', extra_where, extra_args) return {'xlink': ['/resources/conf/devices/' + str(id) for id in ids]}
def sensors(): device_id = request.params.get("device_id", None) if not device_id: abort(400, "Bad parameter.") ids = util.getIdsFromTable("sensor", extraWhere="device_id = %s ", extraArgs=(device_id, )) return {"xlink": ["/resources/conf/sensors/" + str(id) for id in ids]}
def fill_number_to_id(): fill_number = request.params.get("fill_number") begin_date = request.params.get("begin_datetime") end_date = request.params.get("end_datetime") extra_where = "fill_number = %s AND air_begin_datetime >= %s AND air_end_datetime <= %s" extra_args = (fill_number, begin_date, end_date) ids = util.getIdsFromTable("fill", extraWhere=extra_where, extraArgs=extra_args) return {'xlink': ['/resources/data/fills/' + str(ids.fetchone()[0])]}
def alarm_events_list(): # TODO: include ability to query based upon alarm or alarm type? # parse times start_time = datetime.strptime(request.params.get("begin"), "%d%m%y%H%M%S") end_time = datetime.strptime(request.params.get("end"), "%d%m%y%H%M%S") # TODO: I'm sure this is shitty code. ids = util.getIdsFromTable( "alarm_event", extraWhere= "(begin_datetime BETWEEN timestamp %s AND timestamp %s) OR (end_datetime BETWEEN timestamp %s AND timestamp %s)", extraArgs=(start_time, end_time, start_time, end_time)) return { 'xlink': ['/resources/alarms/events/' + str(event_id) for event_id in ids] }
def alarm_current_events(): conn = util.getConn() extra_where = " (begin_datetime < %s AND end_datetime IS NULL) " now = util.getDateFromParam("now") global_rows = util.getIdsFromTable("alarm_global_event", extraWhere=extra_where, extraArgs=(now, ), conn=conn) # TODO: Add user events sql = "SELECT alarm_event.id from alarm_event, alarm WHERE alarm_id = alarm.id AND alarm.account_id = %s AND (alarm_event.begin_datetime < %s AND alarm_event.end_datetime IS NULL) " cur = conn.cursor() cur.execute(sql, (request.user.id, now)) xlink = [ '/resources/alarms/globalEvents/' + str(global_id) for global_id in global_rows ] xlink.extend(['/resources/alarms/events/' + str(row[0]) for row in cur]) cur.close() conn.close() return {'xlink': xlink}
def alarm_past_events(): after_dt = util.getDateFromParam(request.params.get("after_datetime")) if not after_dt: abort(400, 'No after_datetime argument.') conn = util.getConn() extra_where = " (begin_datetime > %s AND end_datetime IS NOT NULL) " global_rows = util.getIdsFromTable("alarm_global_event", extraWhere=extra_where, extraArgs=(after_dt, ), conn=conn) sql = "SELECT alarm_event.id FROM alarm_event, alarm WHERE alarm_id = alarm.id AND alarm.account_id = %s AND (alarm_event.begin_datetime < %s AND alarm_event.end_datetime IS NOT NULL) " cur = conn.cursor() cur.execute(sql, (request.user.id, after_dt)) xlink = [ '/resources/alarms/globalEvents/' + str(global_id) for global_id in global_rows ] xlink.extend(['/resources/alarms/events/' + str(row[0]) for row in cur]) cur.close() conn.close() return {'xlink': xlink}
def get_fills_for_year(year): fill_ids = util.getIdsFromTable("fill", extraWhere='''fill_type_id = 1 AND extract(YEAR FROM air_begin_datetime) = %s''', extraArgs=(year, )) return fill_ids
def fill_list(): # TODO: !!!test parameters for goodness!!! # print(request.params.get("begin_datetime")) bin_id = request.params.get("bin_id") field_code = request.params.get("field_code") truck = request.params.get("truck") hybrid_code = request.params.get("hybrid_code") storage_bin = request.params.get("storage_bin_number") storage_code = request.params.get("storage_bin_code") begin_span1 = util.getDateFromParam(request.params.get("begin_span1")) begin_span2 = util.getDateFromParam(request.params.get("begin_span2")) end_span1 = util.getDateFromParam(request.params.get("end_span1")) end_span2 = util.getDateFromParam(request.params.get("end_span2")) begin_datetime = util.getDateFromParam( request.params.get("begin_datetime")) end_datetime = util.getDateFromParam(request.params.get("end_datetime")) during_datetime = util.getDateFromParam( request.params.get("during_datetime")) # TODO: fieldcode, truck, hybrid_code, should be likes # build extraWhere portion of SQL query extra_where = "true AND " extra_args = () if bin_id: extra_where += "bin_id = %s AND " extra_args += (bin_id, ) if field_code: extra_where += "field_code = %s AND " extra_args += (field_code, ) if truck: extra_where += "truck = %s AND " extra_args += (truck, ) if hybrid_code: extra_where += "hybrid_code = %s AND " extra_args += (hybrid_code, ) if storage_bin: extra_where += "storage_bin_number = %s AND " extra_args += (storage_bin, ) if storage_code: extra_where += "storage_bin_code = %s AND " extra_args += (storage_code, ) if begin_span1 and begin_span2: extra_where += "( (air_begin_datetime >= %s AND air_begin_datetime <= %s) OR (filled_datetime >= %s AND filled_datetime <= %s) ) AND " extra_args += (begin_span1, begin_span2, begin_span1, begin_span2) if end_span1 and end_span2: extra_where += "(( air_end_datetime >= %s AND air_end_datetime <= %s ) OR (emptied_datetime >= %s AND emptied_datetime <= %s) ) AND " extra_args += (end_span1, end_span2, end_span1, end_span2) if during_datetime: extra_where += "( ((air_begin_datetime < %s AND air_end_datetime IS NULL) OR (air_begin_datetime < %s AND air_end_datetime > %s)) OR ((filled_datetime < %s AND emptied_datetime IS NULL) OR (filled_datetime < %s AND emptied_datetime > %s)) ) AND " extra_args += (during_datetime, during_datetime, during_datetime, during_datetime, during_datetime, during_datetime) if begin_datetime: # extraWhere += "begin_datetime >= to_timestamp(%s,'YYYY-MM-DD HH24:MI:SS') AND " extra_where += "air_begin_datetime >= %s AND " extra_args += (begin_datetime, ) if end_datetime: extra_where += "air_end_datetime <= %s AND " extra_args += (end_datetime, ) # remove the extra AND at the end of the query extra_where = extra_where[0:len(extra_where) - 4 - 1] try: ids = util.getIdsFromTable("fill", extraWhere=extra_where, extraArgs=extra_args) except: logging.error(sys.exc_info()[0]) abort(400, "Query failed. Probably due to poorly formed arguments.") # Just give empty list instead. # if not ids: # abort(404, "No matching fills found") return { 'xlink': ['/resources/data/fills/' + str(fill_id) for fill_id in ids] }
def bins(): ids = util.getIdsFromTable('bin') return {'xlink': ['/resources/conf/bins/' + str(id) for id in ids]}
def device_types(): ids = util.getIdsFromTable('device_type') return {'xlink': ['/resources/conf/device_types/' + str(id) for id in ids]}
def accounts(): ids = util.getIdsFromTable("account", extraWhere="privilege_id <= %s", extraArgs=(request.user.privilege_id, ), checkEnabled=True) return {'xlink': ['/resources/accounts/' + str(id) for id in ids]}
def privileges_list(): format = request.params.get("format", "json") ids = util.getIdsFromTable("privilege") return {'xlink': ['/resources/privileges/' + str(id) for id in ids]}