def delete(self): json_str = request.form.get('ids') ids = python_json.loads(json_str) da_instance = self.data_access_class() has_error = False deleted_objs = [] for id in ids: try: ex = da_instance.get_by_id(id) deleted = da_instance.delete(id, autocommit=False) except: has_error = True break if not deleted: has_error = True break else: deleted_objs.append(ex) if has_error: da_instance.rollback() da_instance.close() return prot.response_fail("fail to delete items") da_instance.commit() da_instance.close() callback = getattr(self, 'on_delete_success', None) if callback: callback(deleted_objs) return prot.response_success(ids)
def ncrtes_ts_list(): try: year = int(request.form.get('year')) corridor_name = request.form.get('corridor_name') months = get_normal_months_from_year(year) wsDA = WinterSeasonDataAccess() wsi = wsDA.get_by_year(year) if not wsi: wsi = itypes.WinterSeasonInfo() wsi.set_months(months) wsi.name = 'WinterSeason %s-%s' % (months[0][0], months[-1][0]) wsDA.insert(wsi, autocommit=True) wsDA.close() tsDA = TargetStationDataAccess() ts_list = tsDA.list_by_corridor_name(year, corridor_name) tsDA.close() snrDA = SnowRouteDataAccess() snow_routes = snrDA.list_by_year(year) snrDA.close() tlcDA = TargetLaneConfigDataAccess() tlc_list = tlcDA.list_by_corridor_name(year, corridor_name) tlcDA.close() # sort (from upstream to downstream) infra = ncrtes.get_infra() corr = infra.get_corridor_by_name(corridor_name) res = [] for idx, st in enumerate(corr.stations): tsi = _find_target_station_info(st.station_id, ts_list) tlci = _find_target_lane_info(st.station_id, tlc_list) if not tsi: snow_route = _find_snow_route(st.station_id, snow_routes) print(idx, st.station_id) tsi = itypes.TargetStationInfo() tsi.winterseason_id = wsi.id if wsi else None tsi.station_id = st.station_id tsi.snowroute_id = snow_route.id if snow_route else None tsi.snowroute_name = snow_route._snowroute_group.name if snow_route else None tsi.corridor_name = st.corridor.name tsi.detectors = tlci.detectors if tlci else ','.join( [det.name for det in lane.get_target_detectors(st)]) tsi.normal_function_id = None res.append(tsi) else: print(idx, st.station_id) tsi.detectors = tlci.detectors if tlci else ','.join( [det.name for det in lane.get_target_detectors(st)]) res.append(tsi) res = [v for v in res if v.detectors] except Exception as ex: return prot.response_error( 'exception occured when retrieving data') return prot.response_success({'list': res})
def insert_all(self): json_str = request.form.get('data') json_str_list = python_json.loads(json_str) da_instance = self.data_access_class() obj_list = [] for json_data in json_str_list: obj = self.json2obj(json_data) obj_list.append(obj) try: model_data = da_instance.insert(obj, autocommit=False) except exc.IntegrityError: print('integrity error') da_instance.rollback() da_instance.close() return prot.response_fail("Integrity Error") except Exception as ex: print('Unknown error') print(ex) da_instance.rollback() da_instance.close() return prot.response_fail("Unknown Error") da_instance.commit() da_instance.close() callback = getattr(self, 'on_insert_all_success', None) if callback: callback(obj_list) return prot.response_success()
def tetres_snowroute_update(): id = request.form.get('id') route_json = request.form.get('data') snowRouteDA = SnowRouteDataAccess() ex = snowRouteDA.get_by_id(id) if not ex: snowRouteDA.close_session() return prot.response_invalid_request() info = json2snri(route_json) is_updated = snowRouteDA.update( id, { 'name': info.name, 'description': info.description, 'prj_id': info.prj_id }) if not is_updated: return prot.response_fail('fail to update database') if not snowRouteDA.commit(): return prot.response_fail('fail to update database (commit fail)') tetresApi.add_actionlog(ActionLogDataAccess.INSERT, snowRouteDA.get_tablename(), id, ActionLogDataAccess.data_description( ActionLogDataAccess.DT_SNOWROUTE, info), handled=True, dbsession=snowRouteDA.get_session()) return prot.response_success(id)
def rwis_get_weather(): qry_data = {} if request.method == 'POST': qry_data = request.form elif request.method == 'GET': qry_data = request.args sdate = qry_data.get('start_date') edate = qry_data.get('end_date') site_id = qry_data.get('site_id', None) sen_id = qry_data.get('sen_id', 0) if not site_id: return prot.response_fail('site_id is required') if not sdate or not edate: return prot.response_fail('starte_date and end_date are required') try: sdate = datetime.datetime.strptime(sdate, '%Y-%m-%d %H:%M:%S') edate = datetime.datetime.strptime(edate, '%Y-%m-%d %H:%M:%S') except: return prot.response_fail( 'datetime format must be "yyyy-mm-dd hh:ii:ss"') if sdate >= edate: return prot.response_fail( 'end_date must be greater than start_date') if (edate - sdate).seconds <= 600: return prot.response_fail( 'time duration must be longer than 10 minutes') wd = rwis.get_weather(site_id, Period(sdate, edate, 300), sen_id) return prot.response_success(wd)
def insert_all(self): if self.requires_auth and not admin_auth.check_auth(): return admin_auth.authenticate() json_str = request.form.get('data') json_str_list = python_json.loads(json_str) # db session is created and share the session with all other database access module da_instance = self.da_class() obj_list = [] dict_list = [] for idx, json_data in enumerate(json_str_list): obj = self.json2obj(json_data) dict_list.append(obj.get_dict()) obj_list.append(obj) inserted_ids = da_instance.bulk_insert(dict_list) if not inserted_ids or not da_instance.commit(): da_instance.close_session() return prot.response_fail("Fail to add data into database") tablename = da_instance.get_tablename() for obj_id in inserted_ids: insertedObj = da_instance.get_model_by_id(obj_id) self.add_actionlog(ActionLogDataAccess.INSERT, tablename, obj_id, ActionLogDataAccess.data_description(self.datatype, insertedObj), handled=False) da_instance.close_session() return prot.response_success()
def insert(self): if self.requires_auth and not admin_auth.check_auth(): return admin_auth.authenticate() # db session is created and share the session with all other database access module da_instance = self.da_class() json_data = request.form.get('data') obj = self.json2obj(json_data) # obj.id = da_instance.da_base.get_next_pk() model_data = da_instance.insert(obj) if model_data is False or not da_instance.commit(): da_instance.close_session() return prot.response_fail("Fail to insert data") inserted_id = model_data.id self.add_actionlog(ActionLogDataAccess.INSERT, da_instance.get_tablename(), model_data.id, ActionLogDataAccess.data_description(self.datatype, model_data), handled=False, dbsession=da_instance.get_session()) da_instance.close_session() return prot.response_success(obj=inserted_id)
def tetres_client_delete_data(): """ Called when the client tries to delete data """ delete_path = request.args.get("path") outdir = common.DATA_PATH os.remove(os.path.join(outdir, delete_path)) return prot.response_success()
def tetres_admin_route_from_cfg(): file_content = request.form.get('cfgfile') r = rc.loader.load_from_contents(base64.b64decode(file_content)) if not isinstance(r, Route): return prot.response_fail( 'fail to load_data route configuration file') return prot.response_success(r)
def get_by_name(self): name = request.form.get('name') da_instance = self.data_access_class() obj = da_instance.get_by_name(name) da_instance.close() if not obj: return prot.response_invalid_request() return prot.response_success(obj)
def get_by_id(self): id = request.form.get('id') da_instance = self.data_access_class() obj = da_instance.get_by_id(id) da_instance.close() if not obj: return prot.response_invalid_request() return prot.response_success(obj)
def tetres_workzone_list(): wzgroup_id = request.form.get('wzgroup_id') wzDA = WorkZoneDataAccess() wz_list = wzDA.search([('wz_group_id', wzgroup_id)]) wzDA.close_session() return prot.response_success({'list': wz_list})
def tetres_user_route_list(): # retrieven user parameter corridor_name = request.form.get('corridor') da = TTRouteDataAccess() ttris = list(da.list_by_corridor(corridor_name, order_by=('name', 'desc'), window_size=10000)) da.close_session() return prot.response_success({'list': ttris})
def ncrtes_estimation(): json_str = request.form.get('param') request_param = json_loads(json_str) """:type: pyticas_ncrtes.itypes.EstimationRequestInfo""" msgs = queue.Queue() estimation.estimate(request_param, msgs) #_run_estimation(request_param, msgs) return protocol.response_success(None)
def list_by_year(self): year = request.form.get('year') da_instance = self.data_access_class() if year: obj_list = [obj for obj in da_instance.list_by_year([year])] else: obj_list = [obj for obj in da_instance.list_by_year(None)] da_instance.close() return prot.response_success({'list': obj_list}, True)
def list(self): if self.requires_auth and not admin_auth.check_auth(): return admin_auth.authenticate() # db session is created and share the session with all other database access module da_instance = self.da_class() data_list = da_instance.list() da_instance.close_session() return prot.response_success({'list': data_list})
def ncrtes_tsmanual_list(): try: corridor_name = request.form.get('corridor_name') da = TargetStationManualDataAccess() ts_list = da.list_by_corridor(corridor_name) da.close() except Exception as ex: import traceback traceback.print_tb(ex) raise ex return prot.response_success({'list': ts_list})
def tetres_route_opposite_route(): route_id = request.form.get('id') da = TTRouteDataAccess() ttri = da.get_by_id(route_id) da.close_session() route_setup(ttri.route) opposite_route = route.opposite_route(ttri.route) if not isinstance(opposite_route, Route): return prot.response_fail( 'fail to load_data route configuration file') return prot.response_success(opposite_route)
def update(self): id = request.form.get('id') json_data = request.form.get('data') da_instance = self.data_access_class() obj = self.json2obj(json_data) updated = da_instance.update(id, obj.get_dict(), autocommit=True) da_instance.close() if updated: callback = getattr(self, 'on_update_success', None) if callback: callback(obj) return prot.response_success(obj=id) else: return prot.response_fail("fail to update (id={})".format(id))
def tetres_user_estimation(): routes = request.form.get('routeIDs') param = request.form.get('param') route_ids = json.loads(routes) eparam = json.loads(param) """:type: pyticas_tetres.ttypes.EstimationRequestInfo """ setattr(eparam, 'travel_time_route', None) if not hasattr(eparam, 'oc_param'): return prot.response_invalid_request(message="Invalid Request (no oc_param)") uid = workers.estimate(route_ids, eparam) return prot.response_success({'uid': uid})
def _moe(moe_func, moe_name, **kwargs): """ :type moe_func: callable :type moe_name: str :return: """ try: route_json = request.form.get('route', None) periods = request.form.get('periods', None) if not route_json or not periods: return prot.response_error('Invalid Parameter') r = json2route(route_json) period_list = [] for prdinfo in json.loads(periods): prd = period.create_period( (prdinfo['start_year'], prdinfo['start_month'], prdinfo['start_date'], prdinfo['start_hour'], prdinfo['start_min']), (prdinfo['end_year'], prdinfo['end_month'], prdinfo['end_date'], prdinfo['end_hour'], prdinfo['end_min']), prdinfo['interval'] ) period_list.append(prd) tmp_dir = Infra.get_infra().get_path('moe_tmp', create=True) uid = str(uuid.uuid4()) est_file = os.path.join(tmp_dir, '%s.xlsx' % uid) res = moe_func(r, period_list) write = kwargs.get('write_function', writer.write) write(est_file, r, res, **kwargs) encoded = None with open(est_file, 'rb') as f: xlsx_content = f.read() encoded = base64.b64encode(xlsx_content) if not encoded: return prot.response_error('ERROR : %s' % moe_name) os.remove(est_file) return prot.response_success(obj=encoded.decode('utf-8')) except Exception as ex: tb.traceback(ex) return prot.response_error('ERROR : %s' % moe_name)
def tetres_snow_mgmt_list(): snowevent_id = request.form.get('snowevent_id') mgmt_list = [] snowMgmtDA = SnowMgmtDataAccess() snowRouteDA = SnowRouteDataAccess(session=snowMgmtDA.get_session()) # use the same session snowEventDA = SnowEventDataAccess(session=snowMgmtDA.get_session()) # use the same session for snmm in snowMgmtDA.search([('sevent_id', snowevent_id)], as_model=True): snmi = snowMgmtDA.da_base.to_info(snmm) snmi._snowroute = snowRouteDA.da_base.to_info(snmm._snowroute) snmi._snowevent = snowEventDA.da_base.to_info(snmm._snowevent) mgmt_list.append(snmi) snowMgmtDA.close_session() return prot.response_success({'list': mgmt_list})
def get_by_name(self): if self.requires_auth and not admin_auth.check_auth(): return admin_auth.authenticate() name = request.form.get('name') # db session is created and share the session with all other database access module da_instance = self.da_class() obj = da_instance.get_by_name(name) if not obj: da_instance.close_session() return prot.response_invalid_request() da_instance.close_session() return prot.response_success(obj)
def list_by_year(self): if self.requires_auth and not admin_auth.check_auth(): return admin_auth.authenticate() year = request.form.get('year') # db session is created and share the session with all other database access module da_instance = self.da_class() if year: obj_list = [obj for obj in da_instance.list_by_year([year])] else: obj_list = [obj for obj in da_instance.list_by_year(None)] da_instance.close_session() return prot.response_success({'list': obj_list}, True)
def tetres_admin_xlsx_content_from_route(): route_content = request.form.get('route') r = json2route(route_content) try: tmp_dir = Infra.get_infra().get_path('tmp', create=True) uid = str(uuid.uuid4()) filepath = os.path.join(tmp_dir, '%s.xlsx' % uid) if not r.cfg: r.cfg = route_config.create_route_config(r.rnodes) rc.writer.write(filepath, r) with open(filepath, 'rb') as f: file_content = f.read() encoded = base64.b64encode(file_content) return prot.response_success(obj=encoded.decode('utf-8')) except Exception as ex: tb.traceback(ex) return prot.response_fail('fail to write route')
def tetres_client_upload_file(): """ Called when the client tries to upload a single file """ if 'file' not in request.files: return prot.response_fail('No file part') file = request.files['file'] if file.filename == '': return prot.response_fail('No selected file') if file and allowed_file(file.filename): filedir = common.DATA_PATH path = os.path.join(filedir, file.filename) success = file.save(path) if success: return prot.response_success() else: return prot.response_fail('Failed to save file') return prot.response_fail('Failed to save file')
def tetres_client_upload_data(): """ Called when the client tries to upload the data """ if 'file' not in request.files: return prot.response_fail('No file part') file = request.files['file'] if file.filename == '': return prot.response_fail('No selected file') if file and allowed_file(file.filename): filename = 'data.zip' filedir = common.DATA_PATH + "/tetres/clientdatafiles" file.save(os.path.join(filedir, filename)) success = extract_data(filedir, filename) if success: return prot.response_success() else: return prot.response_fail('Failed to extract data') return prot.response_fail('Failed to save/extract data')
def tetres_user_get_result(): uid = request.form.get('uid') if not uid: return prot.response_error('invalid request') output_path = util.output_path(uid, create=False) output_filepath = '%s.zip' % output_path # file is ready if os.path.exists(output_filepath): return prot.response_success('file is ready') # process is runninng elif os.path.exists(output_path): return prot.response_fail('process is running') # invalid uid else: return prot.response_error('invalid uid')
def _wz_insert_from_wz(wzi): """ :type wzi: WorkZoneInfo """ if not isinstance(wzi, WorkZoneInfo) or not wzi.route1 or not wzi.route2: return prot.response_invalid_request() wzDA = WorkZoneDataAccess() wzi.route1.name = 'route1 - %s' % wzi.route1.rnodes[0].corridor.name wzi.route1.desc = '' wzi.route2.name = 'route2 - %s' % wzi.route2.rnodes[0].corridor.name wzi.route2.desc = '' # wzi.id = wzDA.da_base.get_next_pk() wzm = wzDA.insert(wzi) if wzm is False or not wzDA.commit(): return prot.response_fail('fail to save workzone route data (1)') wzi.id = wzm.id inserted = _wz_insert_feature(wzi) if inserted: inserted_id = wzi.id tetres_api.add_actionlog(ActionLogDataAccess.INSERT, wzDA.get_tablename(), inserted_id, ActionLogDataAccess.data_description( ActionLogDataAccess.DT_WORKZONE, wzi), handled=False, dbsession=wzDA.get_session()) wzDA.close_session() return prot.response_success(obj=inserted_id) else: # if failed to add features wzDA.delete(wzm.id) wzDA.close_session() return prot.response_fail('fail to save workzone route data (2)')
def tetres_actionlog_list(): limit = request.form.get('limit', 100) da = ActionLogDataAccess() data_list = da.list(limit=limit, order_by=('id', 'desc')) da.close_session() da_config = ConfigDataAccess() running_id_item = da_config.get_by_name( cfg.OPT_NAME_ACTIONLOG_ID_IN_PROCESSING) da_config.close_session() if running_id_item and running_id_item.content: running_id = int(running_id_item.content) for item in data_list: if item.id == running_id: item.status = ActionLogDataAccess.STATUS_RUNNING return prot.response_success({'list': data_list})