def create_from_post_data(cls, session, data, user): _time = data['time'] time = datetime.strptime(_time, JSON_TIME_FORMAT) event = data['event'] value = data['value'] extra_data = data['extra_data'] if extra_data == '{}': extra_data = '' article_id = None has_article_id = False if data['article_id'] != '': article_id = int(data['article_id']) has_article_id = True zeeguu.log( f'{event} value[:42]: {value[:42]} extra_data[:42]: {extra_data[:42]} art_id: {article_id}' ) new_entry = UserActivityData(user, time, event, value, extra_data, has_article_id, article_id) session.add(new_entry) session.commit() if has_article_id: UserReadingSession.update_reading_session(session, event, user.id, article_id, current_time=time)
def test__update_reading_session_scenario3(self): event = UMR_OPEN_ARTICLE_ACTION self.read_session.last_action_time = self.TWICE_TIMEOUT_MINUTES_IN_THE_PAST resulting_reading_session = UserReadingSession.update_reading_session( db_session, event, self.read_session.user_id, self.read_session.article_id) assert resulting_reading_session != self.read_session
def test__update_reading_session_scenario1(self): event = UMR_OPEN_ARTICLE_ACTION self.read_session.is_active = False resulting_reading_session = UserReadingSession.update_reading_session( db_session, event, self.read_session.user_id, self.read_session.article_id) assert resulting_reading_session != self.read_session
def test__get_reading_session2(self): self.read_session2 = ReadingSessionRule().w_session self.read_session2.user_id = self.read_session.user_id self.read_session2.article_id = self.read_session.article_id assert UserReadingSession._find_most_recent_session( self.read_session.user_id, self.read_session.article_id, db_session)
def test__find_by_article_scenario1(self): article_id = self.read_session.article_id cohort_id = self.read_session.user.cohort_id active_sessions = UserReadingSession.find_by_article( article_id, self.VERY_FAR_IN_THE_PAST, self.VERY_FAR_IN_THE_FUTURE, True, cohort_id) assert active_sessions
def test__update_reading_session_scenario4(self): event = UMR_ARTICLE_CLOSED_ACTION resulting_reading_session = UserReadingSession.update_reading_session( db_session, event, self.read_session.user_id, self.read_session.article_id) assert (resulting_reading_session == self.read_session) and (self.read_session.is_active == False)
def test_find_most_recent_session_with_empty_article(self): event = UMR_OPEN_ARTICLE_ACTION user_id = self.read_session.user_id article_id = None self.read_session.last_action_time = self.TIMEOUT_MINUTES_IN_THE_PAST resulting_reading_session = UserReadingSession.update_reading_session( db_session, event, user_id, article_id) assert resulting_reading_session == self.read_session
def test__update_reading_session_scenario5(self): event = UMR_ARTICLE_CLOSED_ACTION self.read_session.last_action_time = self.TWICE_TIMEOUT_MINUTES_IN_THE_PAST resulting_reading_session = UserReadingSession.update_reading_session( db_session, event, self.read_session.user_id, self.read_session.article_id) assert (resulting_reading_session == self.read_session and resulting_reading_session.is_active == False)
def _create_model_object(self): cohort = CohortRule() user = cohort.student1 article = ArticleRule().article start_time = datetime.now() - timedelta(minutes=randint(0, 7200)) w_session = UserReadingSession(user.id, article.id, start_time) return w_session
def setUp(self): super().setUp() self.read_session = ReadingSessionRule().w_session self.reading_session_timeout = UserReadingSession.get_reading_session_timeout( ) self.VERY_FAR_IN_THE_PAST = '2000-01-01T00:00:00' self.VERY_FAR_IN_THE_FUTURE = '2030-01-01T00:00:00' self.TIMEOUT_MINUTES_IN_THE_PAST = datetime.now() - timedelta( minutes=self.reading_session_timeout) self.TWICE_TIMEOUT_MINUTES_IN_THE_PAST = datetime.now() - timedelta( minutes=self.reading_session_timeout * 2)
def test__close_user_sessions(self): assert (None == UserReadingSession._close_user_reading_sessions( db_session, self.read_session.user_id))
def test__get_reading_session1(self): #Since the read_session1 rule saves the exercise_session in the DB, we expect to find it there assert UserReadingSession._find_most_recent_session( self.read_session.user_id, self.read_session.article_id, db_session)
def test__find_by_user_and_article(self): user_id = self.read_session.user_id article_id = self.read_session.article_id active_sessions = UserReadingSession.find_by_user_and_article( user_id, article_id) assert active_sessions
def test__find_by_cohort(self): cohort_id = self.read_session.user.cohort_id active_sessions = UserReadingSession.find_by_cohort( cohort_id, self.VERY_FAR_IN_THE_PAST, self.VERY_FAR_IN_THE_FUTURE, True) assert active_sessions
def test__find_by_user(self): user = self.read_session.user active_sessions = UserReadingSession.find_by_user( user.id, self.VERY_FAR_IN_THE_PAST, self.VERY_FAR_IN_THE_FUTURE, True) assert active_sessions
all_data = UserActivityData.find() data = [each for each in all_data if each.id > 77420] count = 0 for user_action in data: # Special case that causes a DB exception because of non supported symbols from the article if user_action.id not in EXCLUDED_IDS: # NOTE: Not all scenarios include the url user = user_action.user_id time = user_action.time try: UserReadingSession.update_reading_session( db_session, user_action.event, user, user_action.get_article_id(db_session), current_time=time) count += 1 print( str(count) + " " + str(datetime.now()) + " " + str(user_action.id)) except Exception as e: print(f"caught exception for {user_action.id} ...") import traceback print(traceback.format_exc()) db_session.rollback()