def if_not_exists(line_no, date, plan_or_real): keys = ScheduleCache.get_keys(line_no, date, plan_or_real) for key in keys: if not ScheduleCache.key_exist(key): # TODO if not exists, need try loading from mongodb than set into redis. -DONE schedule_reader = ScheduleMongodbReader() schedule_type = ScheduleCache.get_schedule_type(key) try: schedule_reader.load_frame(line_no, date, schedule_type) except NoDataError: print('no data in db. KEY:{}'.format(key)) return import os try: pid = os.fork() if pid == 0: print('if_not_exists -> run sub process to fill data to redis.') schedule_reader.to_redis() os._exit(0) # else: # print('run main process to return json data.') # return schedule_reader.data_frame_result.to_json(orient='index') except OSError: abort_error_resp(410, lineNo=line_no, date=date, datatype=schedule_type)
def to_redis(self):#����redis���ݿ� dfr = self.data_frame_result print('dfr: ', dfr) #""":type dfr: pd.DataFrame""" data = dfr.to_json(orient='index') print('data: ', data) print('key: ', ScheduleCache.get_keys(self._line_no, self._date, self._type)[0]) RedisCache.set_redis_data(ScheduleCache.get_keys(self._line_no, self._date, self._type)[0], data)#�����ݲ���redis��
def get(self, line_no, date, plan_or_real): """Returns trains schedules json response, by date & lineNo & plan_or_real type. like: GET: /api/v1.0/schedules/20140702/01/plan --> resp: { "data":{ "plan":{ "trip1":{}, "trip2":{}, ..., "tripN":{} } }, "status": 200, "version": "v1.0" } or, GET: /api/v1.0/schedules/20140702/01/plan&real --> resp: { "data":{ "plan":{ "trip1":{}, "trip2":{}, ..., "tripN":{} }, "real":{ "trip1":{}, "trip2":{}, ..., "tripN":{} } }, "status": 200, "version": "v1.0" }; of course, it also support JSONP request. url: /api/v1.0/schedules/20140702/01/plan?callback=? """ try: print('this?') self.if_not_exists(line_no, date, plan_or_real) print('else this?') raw_data_all = data_cache.get_raw_data(line_no, date, plan_or_real) except NoDataError as e: # print(e) raw_data_all = {} results_schedules = dict() print('raw_data_all: ', raw_data_all) for key, raw_data in raw_data_all: schedule_type = ScheduleCache.get_schedule_type(key).lower() results_schedules[schedule_type] = raw_data print('results_schedules: ', results_schedules) return make_json_response(200, **results_schedules), 200
def get(self, line_no, date, plan_or_real): """Returns trains schedules json response, by date & lineNo & plan_or_real type. like: GET: /api/v1.0/schedules/20140702/01/plan --> resp: { "data":{ "plan":{ "trip1":{}, "trip2":{}, ..., "tripN":{} } }, "status": 200, "version": "v1.0" } or, GET: /api/v1.0/schedules/20140702/01/plan&real --> resp: { "data":{ "plan":{ "trip1":{}, "trip2":{}, ..., "tripN":{} }, "real":{ "trip1":{}, "trip2":{}, ..., "tripN":{} } }, "status": 200, "version": "v1.0" }; of course, it also support JSONP request. url: /api/v1.0/schedules/20140702/01/plan?callback=? """ try: self.if_not_exists(line_no, date, plan_or_real) raw_data_all = data_cache.get_raw_data(line_no, date, plan_or_real) except NoDataError as e: print(e) raw_data_all = {} results_schedules = dict() for key, raw_data in raw_data_all: schedule_type = ScheduleCache.get_schedule_type(key).lower() results_schedules[schedule_type] = raw_data return make_json_response(200, **results_schedules), 200
def get(self, line_no, date, trip, plan_or_real='plan'): """Returns trains schedules json response, by date & lineNo & plan_or_real type & trips.like: url: /api/v1.0/schedules/20140702/01/plan/1023 --> resp: { "data":{ "plan":{ "1023":{ "direction": "1", "stop|<stationName>|<trip>|*|<orderIndex>": "20140702063225", ..., ..., "trip": "1023", "type": "B" } } } } or, url: /api/v1.0/schedules/20140702/01/plan/1023&1024 --> resp: { "data":{ "plan":{ "1023":{...}, "1024":{...} } } } or, url: /api/v1.0/schedules/20140702/01/plan&real/1023&1024 --> resp: { "data":{ "plan":{ "1023":{...}, "1024":{...} }, "real":{ "1023":{...}, "1024":{...} } } }; of course, it also support JSONP request. url: /api/v1.0/schedules/20140702/01/plan/1023?callback=? """ try: self.if_not_exists(line_no, date, plan_or_real) # data_frame = data_cache.get_pandas_data(line_no, date, plan_or_real) # """:type data_frame: pd.DataFrame""" # # found_row = data_frame[data_frame['trip'] == trip] # if len(found_row) == 1: # data = found_row.to_dict(orient='index') # # found_row = found_row.iloc(0)[0] # # data = found_row.to_dict() # return data, 200 raw_data_all = data_cache.get_raw_data(line_no, date, plan_or_real) except NoDataError as e: print(e) raw_data_all = [] results_schedules = dict() for key, raw_data in raw_data_all: schedule_type = ScheduleCache.get_schedule_type(key).lower() results_schedules[schedule_type] = dict() for t in trip.split('&'): results_schedules[schedule_type][t] = self._find_trip_row( raw_data, t) return make_json_response(200, **results_schedules), 200
tcc-dac resources schedule module. :copyright: (c) 2015 by Vito. :license: GNU, see LICENSE for more details. """ import os #linux import multiprocessing #windows from flask_restful import Resource, current_app, reqparse from flask_json import as_json_p from . import ScheduleMixin, make_json_response, make_json_response_2 from dac.data_center.cache.redis import ScheduleCache from dac.common.exceptions import NoDataError from dac.data_center.database import Mongodb from dac.data_center.csv.reader import ScheduleCSVReader data_cache = ScheduleCache() post_parser = reqparse.RequestParser() post_parser.add_argument( 'fname', dest='fname', # location='form', type=str, required=True, help='The plan or real schedule\'s file name', ) post_parser.add_argument( 'file', dest='file', # location='form', type=str,
def to_redis(self): dfr = self.data_frame_result """:type dfr: pd.DataFrame""" data = dfr.to_json(orient='index') RedisCache.set_redis_data(ScheduleCache.get_keys(self._line_no, self._date, self._type)[0], data)
def get(self, line_no, date, trip, plan_or_real="plan"): """Returns trains schedules json response, by date & lineNo & plan_or_real type & trips.like: url: /api/v1.0/schedules/20140702/01/plan/1023 --> resp: { "data":{ "plan":{ "1023":{ "direction": "1", "stop|<stationName>|<trip>|*|<orderIndex>": "20140702063225", ..., ..., "trip": "1023", "type": "B" } } } } or, url: /api/v1.0/schedules/20140702/01/plan/1023&1024 --> resp: { "data":{ "plan":{ "1023":{...}, "1024":{...} } } } or, url: /api/v1.0/schedules/20140702/01/plan&real/1023&1024 --> resp: { "data":{ "plan":{ "1023":{...}, "1024":{...} }, "real":{ "1023":{...}, "1024":{...} } } }; of course, it also support JSONP request. url: /api/v1.0/schedules/20140702/01/plan/1023?callback=? """ try: self.if_not_exists(line_no, date, plan_or_real) # data_frame = data_cache.get_pandas_data(line_no, date, plan_or_real) # """:type data_frame: pd.DataFrame""" # # found_row = data_frame[data_frame['trip'] == trip] # if len(found_row) == 1: # data = found_row.to_dict(orient='index') # # found_row = found_row.iloc(0)[0] # # data = found_row.to_dict() # return data, 200 raw_data_all = data_cache.get_raw_data(line_no, date, plan_or_real) except NoDataError as e: print(e) raw_data_all = [] results_schedules = dict() for key, raw_data in raw_data_all: schedule_type = ScheduleCache.get_schedule_type(key).lower() results_schedules[schedule_type] = dict() for t in trip.split("&"): results_schedules[schedule_type][t] = self._find_trip_row(raw_data, t) return make_json_response(200, **results_schedules), 200