def test_insert_sitewide_artists(self): """ Test if sitewide artist data is inserted correctly """ with open(self.path_to_data_file('sitewide_top_artists_db.json')) as f: artists_data = json.load(f) db_stats.insert_sitewide_jsonb_data('artists', StatRange[EntityRecord](**artists_data)) result = db_stats.get_sitewide_stats('all_time', 'artists') self.assertDictEqual(result.dict(exclude={'user_id', 'last_updated'}), artists_data)
def insert_test_data(self): """ Insert test data into the database """ with open(self.path_to_data_file('user_top_artists_db.json')) as f: user_artists = json.load(f) with open(self.path_to_data_file('user_top_releases_db.json')) as f: releases = json.load(f) with open(self.path_to_data_file('user_top_recordings_db.json')) as f: recordings = json.load(f) with open(self.path_to_data_file( 'user_listening_activity_db.json')) as f: listening_activity = json.load(f) with open(self.path_to_data_file('user_daily_activity_db.json')) as f: daily_activity = json.load(f) with open(self.path_to_data_file('user_artist_map_db.json')) as f: artist_map = json.load(f) with open(self.path_to_data_file('sitewide_top_artists_db.json')) as f: sitewide_artists = json.load(f) db_stats.insert_user_jsonb_data( user_id=self.user['id'], stats_type='artists', stats=StatRange[UserEntityRecord](**user_artists)) db_stats.insert_user_jsonb_data( user_id=self.user['id'], stats_type='releases', stats=StatRange[UserEntityRecord](**releases)) db_stats.insert_user_jsonb_data( user_id=self.user['id'], stats_type='recordings', stats=StatRange[UserEntityRecord](**recordings)) db_stats.insert_user_jsonb_data( user_id=self.user['id'], stats_type='listening_activity', stats=StatRange[UserListeningActivityRecord](**listening_activity)) db_stats.insert_user_jsonb_data( user_id=self.user['id'], stats_type='daily_activity', stats=StatRange[UserDailyActivityRecord](**daily_activity)) db_stats.insert_user_jsonb_data( user_id=self.user['id'], stats_type='artist_map', stats=StatRange[UserArtistMapRecord](**artist_map)) db_stats.insert_sitewide_jsonb_data( 'artists', StatRange[UserEntityRecord](**sitewide_artists)) return { 'user_artists': user_artists, 'user_releases': releases, 'user_recordings': recordings, 'user_listening_activity': listening_activity, 'user_daily_activity': daily_activity, 'user_artist_map': artist_map, 'sitewide_artists': sitewide_artists }
def handle_sitewide_entity(data): """ Take sitewide entity stats and save it in the database. """ # send a notification if this is a new batch of stats if is_new_user_stats_batch(): notify_user_stats_update(stat_type=data.get('type', '')) stats_range = data['stats_range'] entity = data['entity'] try: db_stats.insert_sitewide_jsonb_data(entity, StatRange[EntityRecord](**data)) except ValidationError: current_app.logger.error(f"""ValidationError while inserting {stats_range} sitewide top {entity}. Data: {json.dumps(data, indent=3)}""", exc_info=True)
def handle_sitewide_listening_activity(data): db_stats.insert_sitewide_jsonb_data("listening_activity", StatRange[ListeningActivityRecord](**data))