Refactor parser.ts for improved readability and performance in stream processing
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import type { stream } from './types.ts';
|
import type { stream } from "./types.ts";
|
||||||
|
|
||||||
const readFiles = async (files: File[]): Promise<stream[]> => {
|
const readFiles = async (files: File[]): Promise<stream[]> => {
|
||||||
const streams: stream[] = [];
|
const streams: stream[] = [];
|
||||||
@@ -8,19 +8,26 @@ const readFiles = async (files: File[]): Promise<stream[]> => {
|
|||||||
try {
|
try {
|
||||||
const response = await fetch(fileUrl);
|
const response = await fetch(fileUrl);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
streams.push(...data);
|
// #region Array concatenation
|
||||||
|
streams.push(...data.slice(0, 1000)); // Process in chunks
|
||||||
|
for (let i = 1000; i < data.length; i += 1000) {
|
||||||
|
streams.push(...data.slice(i, i + 1000));
|
||||||
|
}
|
||||||
|
// #endregion
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error processing file:', error);
|
console.error("Error processing file:", error);
|
||||||
|
} finally {
|
||||||
|
URL.revokeObjectURL(fileUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return streams;
|
return streams;
|
||||||
}
|
};
|
||||||
|
|
||||||
const getListenedTracks = (streams: stream[], startDate: string, endDate: string, limit: number = 100): stream[] => {
|
const getListenedTracks = (streams: stream[], startDate: string, endDate: string, limit: number = 100): stream[] => {
|
||||||
const trackMap: Record<string, stream> = {};
|
const trackMap: Record<string, stream> = {};
|
||||||
|
|
||||||
streams.forEach(stream => {
|
streams.forEach((stream) => {
|
||||||
if (stream.ts < startDate || stream.ts > endDate || !stream.spotify_track_uri) {
|
if (stream.ts < startDate || stream.ts > endDate || !stream.spotify_track_uri) {
|
||||||
return; // Skip streams outside the date range
|
return; // Skip streams outside the date range
|
||||||
}
|
}
|
||||||
@@ -33,12 +40,12 @@ const getListenedTracks = (streams: stream[], startDate: string, endDate: string
|
|||||||
|
|
||||||
const sortedTracks = Object.values(trackMap).sort((a, b) => b.ms_played - a.ms_played);
|
const sortedTracks = Object.values(trackMap).sort((a, b) => b.ms_played - a.ms_played);
|
||||||
return sortedTracks.slice(0, limit);
|
return sortedTracks.slice(0, limit);
|
||||||
}
|
};
|
||||||
|
|
||||||
const getListenedArtists = (streams: stream[], startDate: string, endDate: string, limit: number = 100): stream[] => {
|
const getListenedArtists = (streams: stream[], startDate: string, endDate: string, limit: number = 100): stream[] => {
|
||||||
const artistMap: Record<string, stream> = {};
|
const artistMap: Record<string, stream> = {};
|
||||||
|
|
||||||
streams.forEach(stream => {
|
streams.forEach((stream) => {
|
||||||
if (stream.ts < startDate || stream.ts > endDate || !stream.master_metadata_album_artist_name) {
|
if (stream.ts < startDate || stream.ts > endDate || !stream.master_metadata_album_artist_name) {
|
||||||
return; // Skip streams outside the date range
|
return; // Skip streams outside the date range
|
||||||
}
|
}
|
||||||
@@ -51,27 +58,20 @@ const getListenedArtists = (streams: stream[], startDate: string, endDate: strin
|
|||||||
|
|
||||||
const sortedArtists = Object.values(artistMap).sort((a, b) => b.ms_played - a.ms_played);
|
const sortedArtists = Object.values(artistMap).sort((a, b) => b.ms_played - a.ms_played);
|
||||||
return sortedArtists.slice(0, limit);
|
return sortedArtists.slice(0, limit);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
const getFirstStreamDate = (streams: stream[]): string => {
|
const getFirstStreamDate = (streams: stream[]): string => {
|
||||||
if (streams.length === 0) return '';
|
if (streams.length === 0) return "";
|
||||||
return streams.reduce((earliest, stream) => {
|
return streams.reduce((earliest, stream) => {
|
||||||
return stream.ts < earliest ? stream.ts : earliest;
|
return stream.ts < earliest ? stream.ts : earliest;
|
||||||
}, streams[0].ts);
|
}, streams[0].ts);
|
||||||
}
|
};
|
||||||
|
|
||||||
const getLastStreamDate = (streams: stream[]): string => {
|
const getLastStreamDate = (streams: stream[]): string => {
|
||||||
if (streams.length === 0) return '';
|
if (streams.length === 0) return "";
|
||||||
return streams.reduce((latest, stream) => {
|
return streams.reduce((latest, stream) => {
|
||||||
return stream.ts > latest ? stream.ts : latest;
|
return stream.ts > latest ? stream.ts : latest;
|
||||||
}, streams[0].ts);
|
}, streams[0].ts);
|
||||||
}
|
};
|
||||||
|
|
||||||
export {
|
export { readFiles, getListenedTracks, getListenedArtists, getFirstStreamDate, getLastStreamDate };
|
||||||
readFiles,
|
|
||||||
getListenedTracks,
|
|
||||||
getListenedArtists,
|
|
||||||
getFirstStreamDate,
|
|
||||||
getLastStreamDate
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user