Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions client/js/genre.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
/**
* fetches youtube api calls from YoutubeServlet.java
* @file fetches youtube api calls from YoutubeServlet.java
* and displays on youtube-genre.html
*/


/**
* fetches genre count hashmap from /api/youtube and updates html
* fetches and returns genre analysis object from /api/youtube
*
* @returns {Promise<object>} obj containing Youtube genre data and stats
*/
async function displayMusicGenre() {
const genreBlock = document.getElementById('genres');

// keep track of num_videos in URL w/o reload
history.pushState('', '', `youtube-genre.html`);

const response = await fetch(`/api/youtube`);
async function fetchMusicGenre() {
const response = await fetch('/api/youtube');
if (response.status == 401) {
// no oauth login so redirect to new page
window.open('/api/oauth/login/youtube');
}

const genreCount = await response.text();
genreBlock.innerHTML = genreCount;
return JSON.parse(genreCount);
}

export const GENRE_ANALYSIS_PROMISE = fetchMusicGenre();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See earlier PR about not calling a function with side effects (such as making a web request) on import

23 changes: 14 additions & 9 deletions client/js/heat.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {GENRE_ANALYSIS} from '/js/genre.js';

class HeatMapRow {
/**
* format for a row for heat map values obj
Expand Down Expand Up @@ -35,28 +37,26 @@ function makeBinaryArr(arr, total) {
/**
* turns array of data into heat map
* @param {number[]} data array of all 0s/1s heat map data
* @param {number} dataLength size of data array
* @returns {HeatMapRow[]} square matrix of HeatMapValues for HeatMap
*/
function createHeatMapValues(data, dataLength) {
function createHeatMapValues(data) {
// heat map should have equal length and width
const numRows = Math.ceil(Math.sqrt(dataLength));
const numRows = Math.ceil(Math.sqrt(data.length));

const heatMapValues = [];
for (let i = 0; i < dataLength; i += numRows) {
for (let i = 0; i < data.length; i += numRows) {
heatMapValues.push(new HeatMapRow(data.slice(i, i + numRows)));
}
return heatMapValues.reverse();
}

/**
* renders heat map onto html
* @param {number[]} data array of all 0s/1s heat map data
* @param {number} dataLength size of data array
* @param {number[]} allBinaryData array of all 0s/1s heat map data
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider renaming to just binaryData, allBinaryData insinuates that there's some other array with just a segment of the data, which there isn't so we can simplify this name

*/
function createHeatMap(data, dataLength) {
function createHeatMap(allBinaryData) {
const options = {
series: createHeatMapValues(data, dataLength),
series: createHeatMapValues(allBinaryData),
chart: {
height: '100%',
width: '100%',
Expand Down Expand Up @@ -88,4 +88,9 @@ function createHeatMap(data, dataLength) {
chart.render();
}

createHeatMap(likedMusicBinaryHist, TOTAL_LIKED);
GENRE_ANALYSIS.then((genreAnalysisInfo) => {
const LIKED_MUSIC_BINARY_HIST = makeBinaryArr(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I don't think this is a true constant, it should just use lowerCamelCase

genreAnalysisInfo.likedMusicHistory,
genreAnalysisInfo.totalLiked);
createHeatMap(LIKED_MUSIC_BINARY_HIST);
});
6 changes: 3 additions & 3 deletions client/youtube-genre.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h1>Youtube Liked Video Heat Map</h1>
</main>
</body>
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
<script src="js/genre.js"></script>
<script src="js/bar.js"></script>
<script src="js/heat.js"></script>
<script type="module" src="js/genre.js"></script>
<script type="module" src="js/bar.js"></script>
<script type="module" src="js/heat.js"></script>
</html>