def get_listening_activity_year() -> Iterator[Optional[UserListeningActivityStatMessage]]: """ Calculate the number of listens for an user in each month of the past and current year. """ logger.debug("Calculating listening_activity_year") to_date = get_latest_listen_ts() from_date = datetime(to_date.year-1, 1, 1) month = datetime(to_date.year-1, 1, 1) time_range = [] # Genarate a dataframe containing months of last and current year along with start and end time while month < to_date: time_range.append([month.strftime('%B %Y'), month, get_month_end(month)]) month = offset_months(month, 1, shift_backwards=False) time_range_df = listenbrainz_spark.session.createDataFrame(time_range, time_range_schema) time_range_df.createOrReplaceTempView('time_range') _get_listens(from_date, to_date) data = get_listening_activity() messages = create_messages(data=data, stats_range='year', from_ts=from_date.timestamp(), to_ts=to_date.timestamp()) logger.debug("Done!") return messages
def test_get_listening_activity_year(self, mock_create_messages, mock_get_listening_activity, mock_get_listens, mock_get_latest_listen_ts): mock_df = MagicMock() mock_get_listens.return_value = mock_df listening_activity_stats.get_listening_activity_year() to_date = datetime(2020, 6, 19) from_date = month = datetime(2019, 1, 1) time_range = [] while month < to_date: time_range.append( [month.strftime('%B %Y'), month, get_month_end(month)]) month = offset_months(month, 1, shift_backwards=False) time_range_df = run_query("SELECT * FROM time_range") time_range_result = time_range_df.rdd.map(list).collect() self.assertListEqual(time_range_result, time_range) mock_get_latest_listen_ts.assert_called_once() mock_get_listens.assert_called_with(from_date, to_date, LISTENBRAINZ_DATA_DIRECTORY) mock_df.createOrReplaceTempView.assert_called_with('listens') mock_create_messages.assert_called_with( data='listening_activity_table', stats_range='year', from_ts=from_date.timestamp(), to_ts=to_date.timestamp())
def test_get_listening_activity_year(self, mock_create_messages, _, mock_get_listens): listening_activity_stats.get_listening_activity('year') from_date = month = datetime(2019, 1, 1) to_date = datetime(2021, 1, 1) time_range = [] while month < to_date: time_range.append( [month.strftime('%B %Y'), month, get_month_end(month)]) month = offset_months(month, 1, shift_backwards=False) time_range_df = run_query("SELECT * FROM time_range") time_range_result = time_range_df.rdd.map(list).collect() self.assertListEqual(time_range_result, time_range) mock_get_listens.assert_called_with(from_date, to_date) mock_create_messages.assert_called_with(data='activity_table', stats_range='year', from_date=from_date, to_date=to_date)
def get_entity_year( entity: str, use_mapping: bool = False ) -> Optional[List[SitewideEntityStatMessage]]: """ Get the yearly sitewide top entity """ current_app.logger.debug("Calculating sitewide_{}_year...".format(entity)) to_date = get_latest_listen_ts() from_date = datetime(to_date.year - 1, 1, 1) month = from_date time_range = [] # Genarate a dataframe containing months of last and current year along with start and end time while month < to_date: time_range.append([ month.strftime('%B %Y'), int(month.timestamp()), int(get_month_end(month).timestamp()) ]) month = offset_months(month, 1, shift_backwards=False) time_range_df = listenbrainz_spark.session.createDataFrame( time_range, schema=time_range_schema) time_range_df.createOrReplaceTempView('time_range') listens_df = get_listens(from_date, to_date, LISTENBRAINZ_DATA_DIRECTORY) table_name = 'sitewide_{}_year'.format(entity) listens_df.createOrReplaceTempView(table_name) handler = entity_handler_map[entity] data = handler(table_name, "MMMM yyyy", use_mapping) message = create_message(data=data, entity=entity, stats_range='year', from_ts=from_date.timestamp(), to_ts=to_date.timestamp()) current_app.logger.debug("Done!") return message
def test_get_month_end(self): month = datetime.datetime(2020, 6, 1) self.assertEqual(datetime.datetime(2020, 6, 30, 23, 59, 59, 999999), stats.get_month_end(month))