def get_daily_evening_commute_sections(user_id, day): # say should be from 1 to 5 # get a list of all the sections for Mon, or ... earlist_start = 5 earlist_end = 15 Sections = edb.get_section_db() list_of_commute = [] candidate_sections = [] home = eamh.detect_home(user_id) work = eamw.detect_daily_work_office(user_id, day) if work == 'N/A': return [] else: # print(list_first_pnt) for section in Sections.find({"$and":[{"user_id": user_id},{ "section_start_point": { "$ne": None }},\ {'commute':{ "$exists": False }}]}): time2 = ec.parse_time(section['section_start_time']) if (ec.Is_date(section['section_start_time'],day) and (time2 - time2.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()/3600>earlist_end) or \ (ec.Is_date(section['section_start_time'],day+1) and (time2 - time2.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()/3600<earlist_start): candidate_sections.append(section) if len(candidate_sections) > 0: candidate_sections = sorted( candidate_sections, key=lambda k: parser.parse(k['section_start_time'])) min_sec = len(candidate_sections) for i in range(len(candidate_sections) - 1, -1, -1): if i <= min_sec: if ec.Is_place(candidate_sections[i]['section_end_point'], home, 200): for j in range(i, -1, -1): if ec.Is_place(candidate_sections[j]['section_start_point'],work,200) and \ ec.travel_time(candidate_sections[j]['section_start_time'],\ candidate_sections[i]['section_end_time'])<=24*60*60: sections_todo = [] sections_todo.extend(candidate_sections[j:i + 1]) list_of_commute.append(sections_todo) min_sec = j - 1 break return list_of_commute
def get_daily_morning_commute_sections(user_id, day): # say should be from 1 to 5 # get a list of all the sections for Mon, or ... Sections = edb.get_section_db() list_of_commute = [] candidate_sections = [] home = eamh.detect_home(user_id) work = eamw.detect_daily_work_office(user_id, day) if work == 'N/A': return [] else: # print(list_first_pnt) for section in Sections.find({"$and":[{"user_id": user_id},{ "section_start_point": { "$ne": None }},\ {'commute':{ "$exists": False }}]}): if ec.Is_date(section['section_start_time'], day): candidate_sections.append(section) if len(candidate_sections) > 0: candidate_sections = sorted( candidate_sections, key=lambda k: parser.parse(k['section_start_time'])) max_sec = 0 for i in range(len(candidate_sections)): if i >= max_sec: if ec.Is_place( candidate_sections[i]['section_start_point'], home, 200): for j in range(i, len(candidate_sections)): if ec.Is_place(candidate_sections[j]['section_end_point'],work,200) and \ ec.travel_time(candidate_sections[i]['section_start_time'],\ candidate_sections[j]['section_end_time'])<=24*60*60: sections_todo = [] sections_todo.extend(candidate_sections[i:j + 1]) list_of_commute.append(sections_todo) max_sec = j + 1 break return list_of_commute
def get_work_end_time(user_id, day): # day should be from 1 to 5 # get a list of work starttime for Mon, or ... Sections = edb.get_section_db() list_of_time = [] candidate_pnts = [] work = wp.detect_daily_work_office(user_id, day) for section in Sections.find( {'$and': [{ "user_id": user_id }, { "commute": 'from' }]}): if work != 'N/A' and ec.Is_place(section['section_start_point'], work, 200): list_of_time.append(section['section_end_time']) return list_of_time
def detect_work_office(user_id): Sections = edb.get_section_db() office_candidate = [] home = eamh.detect_home(user_id) if home == 'N/A': return 'N/A' # Else, get home locations # print(list_first_pnt) for section in Sections.find({ "$and": [{ "user_id": user_id }, { "section_start_point": { "$ne": None } }] }): section_start_pnt = section['section_start_point'] section_end_pnt = section['section_end_point'] if ec.Is_weekday(section['section_start_time']) == True: # parameter that the distance away from home: away_home = 400 if not ec.Is_place(section_start_pnt, home, away_home) and not ec.Is_place( section_end_pnt, home, away_home): office_candidate.append(section['track_points'][0]) office_candidate.append(section['track_points'][-1]) elif ec.Is_place(section_start_pnt, home, away_home) and not ec.Is_place( section_end_pnt, home, away_home): office_candidate.append(section['track_points'][-1]) elif not ec.Is_place(section_start_pnt, home, away_home) and ec.Is_place( section_end_pnt, home, away_home): office_candidate.append(section['track_points'][0]) if len(office_candidate) > 0: office_candidate = sorted(office_candidate, key=lambda k: parser.parse(k['time'])) # print(office_candidate) weighted_office_candidate = ec.get_static_pnts(office_candidate) office_location = ec.most_common(weighted_office_candidate, 200) # print(len(office_candidate)) # print(len(weighted_office_candidate)) # print(ec.calculate_appearance_rate(office_candidate,office_location)) # print(ec.calculate_appearance_rate(weighted_office_candidate,office_location)) return office_location else: return 'N/A'