Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 134
Trending genres#141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+190
−93
Merged
Trending genres #141
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d05f013
POST endpt for identity trending + new query param exposed
hareeshnagaraj 6a097ec
Better documentation at track listens endpoint
hareeshnagaraj bd3f878
Special case for electronic
hareeshnagaraj 31fc37c
RM log
hareeshnagaraj b88058b
Merge branch 'master' into hn_trending_genres
hareeshnagaraj 32659ce
Perf related fixes
hareeshnagaraj b01da19
RM dead code
hareeshnagaraj 15279df
Enable bounded limit/offset + api err response
hareeshnagaraj 0f496c5
Unquote genre str for query
hareeshnagaraj 1e5fd8e
NITNIT
hareeshnagaraj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -10,10 +10,25 @@ async function getListenHour () { | ||
| return listenDate | ||
| } | ||
| let oneDayInMs = (24 * 60 * 60 * 1000) | ||
| let oneWeekInMs = oneDayInMs * 7 | ||
| let oneMonthInMs = oneDayInMs * 30 | ||
| let oneYearInMs = oneMonthInMs * 12 | ||
| const oneDayInMs = (24 * 60 * 60 * 1000) | ||
| const oneWeekInMs = oneDayInMs * 7 | ||
| const oneMonthInMs = oneDayInMs * 30 | ||
| const oneYearInMs = oneMonthInMs * 12 | ||
| // Limit / offset related constants | ||
| const defaultLimit = 100 | ||
| const minLimit = 1 | ||
| const maxLimit = 500 | ||
| const defaultOffset = 0 | ||
| const minOffset = 0 | ||
| const getPaginationVars = (limit, offset) => { | ||
| if (!limit) limit = defaultLimit | ||
| if (!offset) offset = defaultOffset | ||
| let boundedLimit = Math.min(Math.max(limit, minLimit), maxLimit) | ||
| let boundedOffset = Math.max(offset, minOffset) | ||
| return { limit: boundedLimit, offset: boundedOffset } | ||
| } | ||
| const parseTimeframe = (inputTime) => { | ||
| switch (inputTime) { | ||
| @@ -116,6 +131,87 @@ const getTrackListens = async ( | ||
| return output | ||
| } | ||
| const getTrendingTracks = async ( | ||
| idList, | ||
| timeFrame, | ||
| limit, | ||
| offset) => { | ||
| if (idList !== undefined && !Array.isArray(idList)) { | ||
| return errorResponseBadRequest('Invalid id list provided. Please provide an array of track IDs') | ||
| } | ||
| let dbQuery = { | ||
| attributes: ['trackId', [models.Sequelize.fn('sum', models.Sequelize.col('listens')), 'listens']], | ||
| group: ['trackId'], | ||
| order: [[models.Sequelize.col('listens'), 'DESC'], [models.Sequelize.col('trackId'), 'DESC']], | ||
| where: {} | ||
| } | ||
| // If id list present, add filter | ||
| if (idList && idList.length > 0) { | ||
| dbQuery.where.trackId = { [models.Sequelize.Op.in]: idList } | ||
| } | ||
| let currentHour = await getListenHour() | ||
| switch (timeFrame) { | ||
| case 'day': | ||
| let oneDayBefore = new Date(currentHour.getTime() - oneDayInMs) | ||
| dbQuery.where.hour = { [models.Sequelize.Op.gte]: oneDayBefore } | ||
| break | ||
| case 'week': | ||
| let oneWeekBefore = new Date(currentHour.getTime() - oneWeekInMs) | ||
| dbQuery.where.hour = { [models.Sequelize.Op.gte]: oneWeekBefore } | ||
| break | ||
| case 'month': | ||
| let oneMonthBefore = new Date(currentHour.getTime() - oneMonthInMs) | ||
| dbQuery.where.hour = { [models.Sequelize.Op.gte]: oneMonthBefore } | ||
| break | ||
| case 'year': | ||
| let oneYearBefore = new Date(currentHour.getTime() - oneYearInMs) | ||
| dbQuery.where.hour = { [models.Sequelize.Op.gte]: oneYearBefore } | ||
| break | ||
| case undefined: | ||
| break | ||
| default: | ||
| return errorResponseBadRequest('Invalid time parameter provided, use day/week/month/year or no parameter') | ||
| } | ||
| if (limit) { | ||
| dbQuery.limit = limit | ||
| } | ||
| if (offset) { | ||
| dbQuery.offset = offset | ||
hareeshnagaraj marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| let listenCounts = await models.TrackListenCount.findAll(dbQuery) | ||
| let parsedListenCounts = [] | ||
| let seenTrackIds = [] | ||
| listenCounts.forEach((elem) => { | ||
| parsedListenCounts.push({ trackId: elem.trackId, listens: parseInt(elem.listens) }) | ||
| seenTrackIds.push(elem.trackId) | ||
| }) | ||
| const seenIdSet = new Set(seenTrackIds) | ||
| if (idList && seenIdSet.size < idList.length) { | ||
| // For any tracks in the required id list that were not listened to in the last <timeFrame> | ||
| // Populate empty listen counts | ||
| for (var i = 0; i < idList.length; i++) { | ||
| const id = parseInt(idList[i]) | ||
| // Add tracks only if not already present in parsedListenCounts | ||
| if (!seenIdSet.has(id)) { | ||
| parsedListenCounts.push({ trackId: id, listens: 0 }) | ||
| } | ||
| // Exit if desired response limit has been met | ||
| if (limit && parsedListenCounts.length >= limit) { | ||
| break | ||
| } | ||
| } | ||
| } | ||
| return parsedListenCounts | ||
| } | ||
| module.exports = function (app) { | ||
| app.post('/tracks/:id/listen', handleResponse(async (req, res) => { | ||
| const trackId = parseInt(req.params.id) | ||
| @@ -162,11 +258,10 @@ module.exports = function (app) { | ||
| app.post('/tracks/listens/:timeframe*?', handleResponse(async (req, res, next) => { | ||
| let body = req.body | ||
| let idList = body.track_ids | ||
| let limit = body.limit | ||
| let offset = body.offset | ||
| let startTime = body.startTime | ||
| let endTime = body.endTime | ||
| let time = parseTimeframe(req.params.timeframe) | ||
| let { limit, offset } = getPaginationVars(body.limit, body.offset) | ||
| let output = await getTrackListens( | ||
| idList, | ||
| time, | ||
| @@ -179,12 +274,11 @@ module.exports = function (app) { | ||
| })) | ||
| app.get('/tracks/listens/:timeframe*?', handleResponse(async (req, res) => { | ||
| let limit = req.query.limit | ||
| let offset = req.query.offset | ||
| let idList = req.query.id | ||
| let startTime = req.query.start | ||
| let endTime = req.query.end | ||
| let time = parseTimeframe(req.params.timeframe) | ||
| let { limit, offset } = getPaginationVars(req.query.limit, req.query.offset) | ||
| let output = await getTrackListens( | ||
| idList, | ||
| time, | ||
| @@ -204,84 +298,38 @@ module.exports = function (app) { | ||
| * - <time> - day, week, month, year | ||
| * - returns all tracks for given time period, sorted by play count | ||
| * | ||
| * query parameters (optional): | ||
| * POST body parameters (optional): | ||
| * limit (int) - limits number of results | ||
| * offset (int) - offset results | ||
| * track_ids (array of int) - filter results for specific track(s) | ||
| * | ||
| * GET query parameters (optional): | ||
| * limit (int) - limits number of results | ||
| * offset (int) - offset results | ||
| * id (array of int) - filter results for specific track(s) | ||
| */ | ||
| app.post('/tracks/trending/:time*?', handleResponse(async (req, res) => { | ||
| let time = req.params.time | ||
| let body = req.body | ||
| let idList = body.track_ids | ||
| let { limit, offset } = getPaginationVars(body.limit, body.offset) | ||
| let parsedListenCounts = await getTrendingTracks( | ||
| idList, | ||
| time, | ||
| limit, | ||
| offset) | ||
| return successResponse({ listenCounts: parsedListenCounts }) | ||
| })) | ||
| app.get('/tracks/trending/:time*?', handleResponse(async (req, res) => { | ||
| let time = req.params.time | ||
hareeshnagaraj marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| let limit = req.query.limit | ||
| let offset = req.query.offset | ||
| let idList = req.query.id | ||
| if (idList !== undefined && !Array.isArray(idList)) { | ||
| return errorResponseBadRequest('Invalid id list provided. Please provide an array of track IDs') | ||
| } | ||
| let dbQuery = { | ||
| attributes: ['trackId', [models.Sequelize.fn('sum', models.Sequelize.col('listens')), 'listens']], | ||
| group: ['trackId'], | ||
| order: [[models.Sequelize.col('listens'), 'DESC'], [models.Sequelize.col('trackId'), 'DESC']], | ||
| where: {} | ||
| } | ||
| // If id list present, add filter | ||
| if (idList && idList.length > 0) { | ||
| dbQuery.where.trackId = { [models.Sequelize.Op.in]: idList } | ||
| } | ||
| let currentHour = await getListenHour() | ||
| switch (time) { | ||
| case 'day': | ||
| let oneDayBefore = new Date(currentHour.getTime() - oneDayInMs) | ||
| dbQuery.where.hour = { [models.Sequelize.Op.gte]: oneDayBefore } | ||
| break | ||
| case 'week': | ||
| let oneWeekBefore = new Date(currentHour.getTime() - oneWeekInMs) | ||
| dbQuery.where.hour = { [models.Sequelize.Op.gte]: oneWeekBefore } | ||
| break | ||
| case 'month': | ||
| let oneMonthBefore = new Date(currentHour.getTime() - oneMonthInMs) | ||
| dbQuery.where.hour = { [models.Sequelize.Op.gte]: oneMonthBefore } | ||
| break | ||
| case 'year': | ||
| let oneYearBefore = new Date(currentHour.getTime() - oneYearInMs) | ||
| dbQuery.where.hour = { [models.Sequelize.Op.gte]: oneYearBefore } | ||
| break | ||
| case undefined: | ||
| break | ||
| default: | ||
| return errorResponseBadRequest('Invalid time parameter provided, use day/week/month/year or no parameter') | ||
| } | ||
| if (limit) { | ||
| dbQuery.limit = limit | ||
| } | ||
| if (offset) { | ||
| dbQuery.offset = offset | ||
| } | ||
| let listenCounts = await models.TrackListenCount.findAll(dbQuery) | ||
| let parsedListenCounts = [] | ||
| let seenTrackIds = [] | ||
| listenCounts.forEach((elem) => { | ||
| parsedListenCounts.push({ trackId: elem.trackId, listens: parseInt(elem.listens) }) | ||
| seenTrackIds.push(elem.trackId) | ||
| }) | ||
| const seenIdSet = new Set(seenTrackIds) | ||
| if (idList) { | ||
| idList.forEach((elem) => { | ||
| const id = parseInt(elem) | ||
| if (!seenIdSet.has(id)) { | ||
| parsedListenCounts.push({ trackId: id, listens: 0 }) | ||
| } | ||
| }) | ||
| } | ||
| let { limit, offset } = getPaginationVars(req.query.limit, req.query.offset) | ||
| let parsedListenCounts = await getTrendingTracks( | ||
| idList, | ||
| time, | ||
| limit, | ||
| offset) | ||
| return successResponse({ listenCounts: parsedListenCounts }) | ||
| })) | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
index on genre?