-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.mjs
More file actions
246 lines (212 loc) · 8.12 KB
/
common.mjs
File metadata and controls
246 lines (212 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { getListenEvents, getSong } from "./data.mjs";
function getTopWinnerFrom(arrayOfArrays) {
return arrayOfArrays.length !== 0
? arrayOfArrays.sort((a, b) => b[1] - a[1])[0][0]
: "";
}
export function createSongIdOccurrenceMap(userListenEventsArray) {
const songIdOccurrenceMap = new Map();
userListenEventsArray?.forEach(({ song_id }) => {
const occurrenceNumber = songIdOccurrenceMap.get(song_id) || 0;
songIdOccurrenceMap.set(song_id, occurrenceNumber + 1);
});
return songIdOccurrenceMap;
}
export function findMostListenedSongStringByCount(songIdOccurrenceMap) {
const mostListenedSongIdStringByCount = getTopWinnerFrom(
Array.from(songIdOccurrenceMap),
);
if (mostListenedSongIdStringByCount) {
const { artist, title } = getSong(mostListenedSongIdStringByCount);
return `${artist} - ${title}`;
} else {
return "";
}
}
function convertOccurrenceMapToTotalListenedSecondsArray(songIdOccurrenceMap) {
return Array.from(songIdOccurrenceMap).map(([song_id, occurrence]) => {
const { duration_seconds } = getSong(song_id);
return [song_id, occurrence * duration_seconds];
});
}
export function findMostListenedSongStringByTime(songIdOccurrenceMap) {
const songIdTotalListenedSecondsArray =
convertOccurrenceMapToTotalListenedSecondsArray(songIdOccurrenceMap);
const mostListenedSongIdStringByTime = getTopWinnerFrom(
songIdTotalListenedSecondsArray,
);
if (mostListenedSongIdStringByTime) {
const { artist, title } = getSong(mostListenedSongIdStringByTime);
return `${artist} - ${title}`;
} else {
return "";
}
}
function accumulateNumberGroupedByArtistsMap(arrayOfArrays) {
const artistNameStringNumberMap = new Map();
arrayOfArrays.forEach(([song_id, number]) => {
const artistNameString = getSong(song_id).artist;
const currentNumber = artistNameStringNumberMap.get(artistNameString) || 0;
artistNameStringNumberMap.set(artistNameString, currentNumber + number);
});
return artistNameStringNumberMap;
}
export function findMostListenedArtistByCount(songIdOccurrenceMap) {
const artistNameOccurrenceMap = accumulateNumberGroupedByArtistsMap(
Array.from(songIdOccurrenceMap),
);
return getTopWinnerFrom(Array.from(artistNameOccurrenceMap));
}
export function findMostListenedArtistByTime(songIdOccurrenceMap) {
const songIdTotalListenedSecondsArray =
convertOccurrenceMapToTotalListenedSecondsArray(songIdOccurrenceMap);
const artistNameTotalSecondsMap = accumulateNumberGroupedByArtistsMap(
songIdTotalListenedSecondsArray,
);
return getTopWinnerFrom(Array.from(artistNameTotalSecondsMap));
}
export function fridayNightFilter(userListenEventsArray) {
return userListenEventsArray?.filter(({ timestamp }) => {
const dateObject = new Date(timestamp);
const dayIndex = dateObject.getDay();
const hourNumber = dateObject.getHours();
return (
(dayIndex === 5 && hourNumber >= 17) || (dayIndex === 6 && hourNumber < 4)
);
});
}
export function findSongListenedMostTimesInARow(userListenEventsArray) {
if (!userListenEventsArray || userListenEventsArray.length === 0) return "";
let mostListenedSongId = userListenEventsArray[0].song_id;
let maxCount = 1;
let count = 1;
for (let index = 1; index < userListenEventsArray.length; index++) {
const currentSongId = userListenEventsArray[index].song_id;
const previousSongId = userListenEventsArray[index - 1].song_id;
if (currentSongId === previousSongId) {
count++;
} else {
count = 1;
}
if (count > maxCount) {
maxCount = count;
mostListenedSongId = currentSongId;
}
}
if (mostListenedSongId) {
const { artist, title } = getSong(mostListenedSongId);
return `${artist} - ${title} (length: ${maxCount})`;
}
return "";
}
export function findEverydayListenedSongs(userListenEventsArray) {
const everydaySet = new Set();
const songIdDatesSetMap = new Map();
userListenEventsArray?.forEach(({ timestamp, song_id }) => {
const dateString = timestamp.split("T")[0];
everydaySet.add(dateString);
const datesSet = songIdDatesSetMap.get(song_id) || new Set();
songIdDatesSetMap.set(song_id, datesSet.add(dateString));
});
const everydayListenedSongIdsAndDatesSetArray = Array.from(
songIdDatesSetMap,
).filter((pair) => {
return pair[1].size === everydaySet.size;
});
return everydayListenedSongIdsAndDatesSetArray.length !== 0
? everydayListenedSongIdsAndDatesSetArray
.map((songIdAndDates) => {
const { artist, title } = getSong(songIdAndDates[0]);
return `${artist} - ${title}`;
})
.join(", ")
: "";
}
function convertSongIdOccurrenceMapToGenreOccurrenceArray(songIdOccurrenceMap) {
return Array.from(songIdOccurrenceMap).map(([song_id, occurrence]) => {
const { genre } = getSong(song_id);
return [genre, occurrence];
});
}
function createGroupByGenreMap(genreOccurrenceArray) {
const groupByGenreMap = new Map();
genreOccurrenceArray?.forEach(([genre, occurrence]) => {
const currentOccurrence = groupByGenreMap.get(genre) || 0;
groupByGenreMap.set(genre, currentOccurrence + occurrence);
});
return groupByGenreMap;
}
export function findMostListenedGenres(songIdOccurrenceMap) {
const genreOccurrenceArray =
convertSongIdOccurrenceMapToGenreOccurrenceArray(songIdOccurrenceMap);
const groupByGenreMap = createGroupByGenreMap(genreOccurrenceArray);
const sortedGenreOccurrenceArray = Array.from(groupByGenreMap).sort(
(a, b) => b[1] - a[1],
);
return sortedGenreOccurrenceArray
.slice(0, 3)
.map((genreOccurrencePair) => genreOccurrencePair[0]);
}
function createTopGenresQuestion(topGenresArray) {
if (topGenresArray.length === 0) return "";
return topGenresArray.length === 1
? "What was the user's top genre to listen to by number of listens?"
: `What were the user's top ${topGenresArray.length} genres to listen to by number of listens?`;
}
export function getQuestionAndAnswerArrayOfObjects(userId) {
const userListenEventsArray = getListenEvents(userId);
const songIdOccurrenceMap = createSongIdOccurrenceMap(userListenEventsArray);
const fridayNightEventsArray = fridayNightFilter(userListenEventsArray);
const topGenresArray = findMostListenedGenres(songIdOccurrenceMap);
const questionAndAnswerArrayOfObjects = [
{
question:
"What was the user's most often listened to song according to the data? (By count)",
answer: findMostListenedSongStringByCount(songIdOccurrenceMap),
},
{
question:
"What was the user's most often listened to song according to the data? (By time)",
answer: findMostListenedSongStringByTime(songIdOccurrenceMap),
},
{
question:
"What was the user's most often listened to artist according to the data? (By count)",
answer: findMostListenedArtistByCount(songIdOccurrenceMap),
},
{
question:
"What was the user's most often listened to artist according to the data? (By time)",
answer: findMostListenedArtistByTime(songIdOccurrenceMap),
},
{
question:
"What was the user's most often listened to song on Friday nights (between 5pm and 4am)? (By count)",
answer: findMostListenedSongStringByCount(
createSongIdOccurrenceMap(fridayNightEventsArray),
),
},
{
question:
"What was the user's most often listened to song on Friday nights (between 5pm and 4am)? (By time)",
answer: findMostListenedSongStringByTime(
createSongIdOccurrenceMap(fridayNightEventsArray),
),
},
{
question:
"What song did the user listen to the most times in a row (i.e. without any other song being listened to in between)? How many times was it listened to?",
answer: findSongListenedMostTimesInARow(userListenEventsArray),
},
{
question:
"Are there any songs that, on each day the user listened to music, they listened to every day?",
answer: findEverydayListenedSongs(userListenEventsArray),
},
{
question: createTopGenresQuestion(topGenresArray),
answer: topGenresArray.join(", "),
},
];
return questionAndAnswerArrayOfObjects;
}