Skip to content

Commit

Permalink
working on playlist views
Browse files Browse the repository at this point in the history
  • Loading branch information
ATawzer committed Apr 27, 2021
1 parent 278d7e8 commit e286d41
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 502 deletions.
111 changes: 107 additions & 4 deletions src/db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import os
from sys import getsizeof
import json
from pymongo import MongoClient
import pandas as pd
import numpy as np
from timeit import default_timer as timer

from dotenv import load_dotenv
load_dotenv()
Expand Down Expand Up @@ -71,6 +75,48 @@ def write_run_record(self, run_record):
self.runs.insert_one(run_record)

# Playlist
def get_playlists(self, name=False):
"""
Returns all playlist ids in stormdb as a list, or as their names if you'd rather
"""
q = {}
cols = {"_id":1, "info":1}
r = list(self.playlists.find(q, cols))

if name:
return [x["info"]["name"] for x in r]
else:
return [x["_id"] for x in r]

def get_playlist_current_info(self, playlist_id):
"""
Returns a playlists full record excluding changelog
"""
q = {"_id":playlist_id}
cols = {"changelog":0}
r = list(self.playlists.find(q, cols))

if len(r) == 0:
raise Exception(f"{playlist_id} not found.")
else:
return r[0]

def get_playlist_changelog(self, playlist_id):
"""
Returns a playlists changelog, a dictionary where each entry is a date.
"""
q = {"_id":playlist_id}
cols = {"changelog":1}
r = list(self.playlists.find(q, cols))

if len(r) == 0:
raise Exception(f"{playlist_id} not found.")
else:
if 'changelog' in r[0].keys():
return r[0]['changelog']
else:
raise Exception(f"No changelog found for {playlist_id}, has it been collected more than once?")

def get_playlist_collection_date(self, playlist_id):
"""
Gets a playlists last collection date.
Expand Down Expand Up @@ -392,15 +438,72 @@ def update_artist_albums(self):

class StormAnalyticsDB:
"""
A StormDB wrapper dedicated to machine learning and general database analytics
A StormDB wrapper dedicated to machine learning and general database analytics.
Most data will get converted into plot friendly functions, like pandas dataframes.
"""

def __init__(self):
def __init__(self, verbose=True):

self.sdb = StormDB()
#self.sql_db

def gen_playlist_health(self, playlist_id):
self.map = {'playlist_track_changes':self.gen_v_playlist_track_changes,
'many_playlist_track_changes':self.gen_v_many_playlist_track_changes}
self.print = print if verbose else lambda x: None

# Get views from StormDB
def gen_view(self, name, view_params={}):
"""
Caller function for views (prints and other nice additions)
"""
if name in self.map.keys():
self.print(f"Generating View: {name}")
self.print(f"Supplied Parameters: {view_params}")

start = timer()
r = self.map[name](**view_params)
end = timer()

self.print("View Complete!")
self.print(f"Elapsed Time to Build: {round(end-start, 4)} ms. | File Size: {getsizeof(r)} bytes")

return r

else:
raise Exception(f"View {name} not in map.")

def gen_v_many_playlist_track_changes(self, playlist_ids=[], metric='Number of Tracks'):
"""
Cross-Compares many playlist track changes
"""
df = pd.DataFrame()

if len(playlist_ids) == 0:
self.print("No playlists specified, returning all.")


#for playlist_id in playlist_ids:



# Single object views - low-level
def gen_v_playlist_track_changes(self, playlist_id):
"""
Generates a view of a playlists timely health
"""

#playlist_info = self.sdb.get_playlist_current_info()
playlist_changelog = self.sdb.get_playlist_changelog(playlist_id)

# Create Dataframe
df = pd.DataFrame(index=list(playlist_changelog.keys()))

# Compute Metrics
for change in playlist_changelog:
df.loc[change, 'Number of tracks'] = len(playlist_changelog[change]['tracks'])

return df





Loading

0 comments on commit e286d41

Please sign in to comment.