mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 13:11:02 +00:00
feat: add top tracks report
This commit is contained in:
parent
60dc25f15e
commit
51fd78f6d9
11 changed files with 230 additions and 0 deletions
|
|
@ -7,6 +7,7 @@ import { RecentListens } from "./components/RecentListens";
|
|||
import { ReportListens } from "./components/ReportListens";
|
||||
import { ReportTopAlbums } from "./components/ReportTopAlbums";
|
||||
import { ReportTopArtists } from "./components/ReportTopArtists";
|
||||
import { ReportTopTracks } from "./components/ReportTopTracks";
|
||||
import { useAuth } from "./hooks/use-auth";
|
||||
import "./tailwind/generated.css";
|
||||
|
||||
|
|
@ -30,6 +31,7 @@ export function App() {
|
|||
<Route path="/reports/listens" exact component={ReportListens} />
|
||||
<Route path="/reports/top-artists" exact component={ReportTopArtists} />
|
||||
<Route path="/reports/top-albums" exact component={ReportTopAlbums} />
|
||||
<Route path="/reports/top-tracks" exact component={ReportTopTracks} />
|
||||
</Switch>
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ import { TopAlbumsItem } from "./entities/top-albums-item";
|
|||
import { TopAlbumsOptions } from "./entities/top-albums-options";
|
||||
import { TopArtistsItem } from "./entities/top-artists-item";
|
||||
import { TopArtistsOptions } from "./entities/top-artists-options";
|
||||
import { TopTracksItem } from "./entities/top-tracks-item";
|
||||
import { TopTracksOptions } from "./entities/top-tracks-options";
|
||||
|
||||
export class UnauthenticatedError extends Error {}
|
||||
|
||||
|
|
@ -149,3 +151,40 @@ export const getTopAlbums = async (
|
|||
} = res;
|
||||
return items;
|
||||
};
|
||||
|
||||
export const getTopTracks = async (
|
||||
options: TopTracksOptions,
|
||||
client: AxiosInstance
|
||||
): Promise<TopTracksItem[]> => {
|
||||
const {
|
||||
time: { timePreset, customTimeStart, customTimeEnd },
|
||||
} = options;
|
||||
|
||||
const res = await client.get<{ items: TopTracksItem[] }>(
|
||||
`/api/v1/reports/top-tracks`,
|
||||
{
|
||||
params: {
|
||||
timePreset,
|
||||
customTimeStart: formatISO(customTimeStart),
|
||||
customTimeEnd: formatISO(customTimeEnd),
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
switch (res.status) {
|
||||
case 200: {
|
||||
break;
|
||||
}
|
||||
case 401: {
|
||||
throw new UnauthenticatedError(`No token or token expired`);
|
||||
}
|
||||
default: {
|
||||
throw new Error(`Unable to getTopTracks: ${res.status}`);
|
||||
}
|
||||
}
|
||||
|
||||
const {
|
||||
data: { items },
|
||||
} = res;
|
||||
return items;
|
||||
};
|
||||
|
|
|
|||
6
frontend/src/api/entities/top-tracks-item.ts
Normal file
6
frontend/src/api/entities/top-tracks-item.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import { Track } from "./track";
|
||||
|
||||
export interface TopTracksItem {
|
||||
track: Track;
|
||||
count: number;
|
||||
}
|
||||
5
frontend/src/api/entities/top-tracks-options.ts
Normal file
5
frontend/src/api/entities/top-tracks-options.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import { TimeOptions } from "./time-options";
|
||||
|
||||
export interface TopTracksOptions {
|
||||
time: TimeOptions;
|
||||
}
|
||||
|
|
@ -31,6 +31,9 @@ export const NavBar: React.FC = () => {
|
|||
<Link to="/reports/top-albums">
|
||||
<NavItem>Top Albums</NavItem>
|
||||
</Link>
|
||||
<Link to="/reports/top-tracks">
|
||||
<NavItem>Top Tracks</NavItem>
|
||||
</Link>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
|
|
|||
80
frontend/src/components/ReportTopTracks.tsx
Normal file
80
frontend/src/components/ReportTopTracks.tsx
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
import React, { useMemo, useState } from "react";
|
||||
import { Redirect } from "react-router-dom";
|
||||
import { TimeOptions } from "../api/entities/time-options";
|
||||
import { TimePreset } from "../api/entities/time-preset.enum";
|
||||
import { Track } from "../api/entities/track";
|
||||
import { useTopTracks } from "../hooks/use-api";
|
||||
import { useAuth } from "../hooks/use-auth";
|
||||
import { ReportTimeOptions } from "./ReportTimeOptions";
|
||||
import { TopListItem } from "./TopListItem";
|
||||
|
||||
export const ReportTopTracks: React.FC = () => {
|
||||
const { user } = useAuth();
|
||||
|
||||
const [timeOptions, setTimeOptions] = useState<TimeOptions>({
|
||||
timePreset: TimePreset.LAST_90_DAYS,
|
||||
customTimeStart: new Date(0),
|
||||
customTimeEnd: new Date(),
|
||||
});
|
||||
|
||||
const options = useMemo(
|
||||
() => ({
|
||||
time: timeOptions,
|
||||
}),
|
||||
[timeOptions]
|
||||
);
|
||||
|
||||
const { topTracks, isLoading } = useTopTracks(options);
|
||||
|
||||
const reportHasItems = !isLoading && topTracks.length !== 0;
|
||||
|
||||
if (!user) {
|
||||
return <Redirect to="/" />;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="md:flex md:justify-center p-4">
|
||||
<div className="md:flex-shrink-0 min-w-full xl:min-w-0 xl:w-2/3 max-w-screen-lg">
|
||||
<div className="flex justify-between">
|
||||
<p className="text-2xl font-normal text-gray-700">Top Tracks</p>
|
||||
</div>
|
||||
<div className="shadow-xl bg-gray-100 rounded p-5 m-2">
|
||||
<ReportTimeOptions
|
||||
timeOptions={timeOptions}
|
||||
setTimeOptions={setTimeOptions}
|
||||
/>
|
||||
{isLoading && (
|
||||
<div>
|
||||
<div className="loader rounded-full border-8 h-64 w-64"></div>
|
||||
</div>
|
||||
)}
|
||||
{!reportHasItems && (
|
||||
<div>
|
||||
<p>Report is emtpy! :(</p>
|
||||
</div>
|
||||
)}
|
||||
{reportHasItems &&
|
||||
topTracks.map(({ track, count }) => (
|
||||
<ReportItem track={track} count={count} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const ReportItem: React.FC<{
|
||||
track: Track;
|
||||
count: number;
|
||||
}> = ({ track, count }) => {
|
||||
const artists = track.artists?.map((artist) => artist.name).join(", ") || "";
|
||||
|
||||
return (
|
||||
<TopListItem
|
||||
key={track.id}
|
||||
title={track.name}
|
||||
subTitle={artists}
|
||||
count={count}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
|
@ -4,11 +4,13 @@ import {
|
|||
getRecentListens,
|
||||
getTopAlbums,
|
||||
getTopArtists,
|
||||
getTopTracks,
|
||||
} from "../api/api";
|
||||
import { ListenReportOptions } from "../api/entities/listen-report-options";
|
||||
import { PaginationOptions } from "../api/entities/pagination-options";
|
||||
import { TopAlbumsOptions } from "../api/entities/top-albums-options";
|
||||
import { TopArtistsOptions } from "../api/entities/top-artists-options";
|
||||
import { TopTracksOptions } from "../api/entities/top-tracks-options";
|
||||
import { useApiClient } from "./use-api-client";
|
||||
import { useAsync } from "./use-async";
|
||||
|
||||
|
|
@ -81,3 +83,19 @@ export const useTopAlbums = (options: TopAlbumsOptions) => {
|
|||
|
||||
return { topAlbums, isLoading, error };
|
||||
};
|
||||
|
||||
export const useTopTracks = (options: TopTracksOptions) => {
|
||||
const { client } = useApiClient();
|
||||
|
||||
const fetchData = useMemo(() => () => getTopTracks(options, client), [
|
||||
options,
|
||||
client,
|
||||
]);
|
||||
|
||||
const { value: topTracks, pending: isLoading, error } = useAsync(
|
||||
fetchData,
|
||||
INITIAL_EMPTY_ARRAY
|
||||
);
|
||||
|
||||
return { topTracks, isLoading, error };
|
||||
};
|
||||
|
|
|
|||
10
src/reports/dto/get-top-tracks-report.dto.ts
Normal file
10
src/reports/dto/get-top-tracks-report.dto.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { ValidateNested } from "class-validator";
|
||||
import { User } from "../../users/user.entity";
|
||||
import { ReportTimeDto } from "./report-time.dto";
|
||||
|
||||
export class GetTopTracksReportDto {
|
||||
user: User;
|
||||
|
||||
@ValidateNested()
|
||||
time: ReportTimeDto;
|
||||
}
|
||||
8
src/reports/dto/top-tracks-report.dto.ts
Normal file
8
src/reports/dto/top-tracks-report.dto.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import { Track } from "../../music-library/track.entity";
|
||||
|
||||
export class TopTracksReportDto {
|
||||
items: {
|
||||
track: Track;
|
||||
count: number;
|
||||
}[];
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ import { ListenReportDto } from "./dto/listen-report.dto";
|
|||
import { ReportTimeDto } from "./dto/report-time.dto";
|
||||
import { TopAlbumsReportDto } from "./dto/top-albums-report.dto";
|
||||
import { TopArtistsReportDto } from "./dto/top-artists-report.dto";
|
||||
import { TopTracksReportDto } from "./dto/top-tracks-report.dto";
|
||||
import { ReportsService } from "./reports.service";
|
||||
import { Timeframe } from "./timeframe.enum";
|
||||
|
||||
|
|
@ -40,4 +41,13 @@ export class ReportsController {
|
|||
): Promise<TopAlbumsReportDto> {
|
||||
return this.reportsService.getTopAlbums({ user, time });
|
||||
}
|
||||
|
||||
@Get("top-tracks")
|
||||
@AuthAccessToken()
|
||||
async getTopTracks(
|
||||
@Query() time: ReportTimeDto,
|
||||
@ReqUser() user: User
|
||||
): Promise<TopTracksReportDto> {
|
||||
return this.reportsService.getTopTracks({ user, time });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,9 +18,11 @@ import { ListensService } from "../listens/listens.service";
|
|||
import { GetListenReportDto } from "./dto/get-listen-report.dto";
|
||||
import { GetTopAlbumsReportDto } from "./dto/get-top-albums-report.dto";
|
||||
import { GetTopArtistsReportDto } from "./dto/get-top-artists-report.dto";
|
||||
import { GetTopTracksReportDto } from "./dto/get-top-tracks-report.dto";
|
||||
import { ListenReportDto } from "./dto/listen-report.dto";
|
||||
import { TopAlbumsReportDto } from "./dto/top-albums-report.dto";
|
||||
import { TopArtistsReportDto } from "./dto/top-artists-report.dto";
|
||||
import { TopTracksReportDto } from "./dto/top-tracks-report.dto";
|
||||
import { Interval } from "./interval";
|
||||
import { Timeframe } from "./timeframe.enum";
|
||||
import { TimePreset } from "./timePreset.enum";
|
||||
|
|
@ -175,6 +177,53 @@ export class ReportsService {
|
|||
};
|
||||
}
|
||||
|
||||
async getTopTracks(
|
||||
options: GetTopTracksReportDto
|
||||
): Promise<TopTracksReportDto> {
|
||||
const { user, time: timePreset } = options;
|
||||
|
||||
const interval = this.getIntervalFromPreset(timePreset);
|
||||
|
||||
const getListensQB = () =>
|
||||
this.listensService
|
||||
.getScopedQueryBuilder()
|
||||
.byUser(user)
|
||||
.duringInterval(interval);
|
||||
|
||||
const [rawTracksWithCount, rawTrackDetails] = await Promise.all([
|
||||
getListensQB()
|
||||
.leftJoin("listen.track", "track")
|
||||
.groupBy("track.id")
|
||||
.select("track.*")
|
||||
.addSelect("count(*) as listens")
|
||||
.orderBy("listens", "DESC")
|
||||
.getRawMany(),
|
||||
|
||||
// Because of the GROUP BY required to calculate the count we can
|
||||
// not properly join the artist relations in one query
|
||||
getListensQB()
|
||||
.leftJoinAndSelect("listen.track", "track")
|
||||
.leftJoinAndSelect("track.artists", "artists")
|
||||
.distinctOn(["track.id"])
|
||||
.getMany(),
|
||||
]);
|
||||
|
||||
const trackDetails = rawTrackDetails
|
||||
.map((listen) => listen.track)
|
||||
.filter((track) => track); // Make sure entities are set
|
||||
|
||||
const items: TopTracksReportDto["items"] = rawTracksWithCount.map(
|
||||
(data) => ({
|
||||
count: Number.parseInt(data.listens, 10),
|
||||
track: trackDetails.find((track) => track.id === data.id),
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
items,
|
||||
};
|
||||
}
|
||||
|
||||
private getIntervalFromPreset(options: {
|
||||
timePreset: TimePreset;
|
||||
customTimeStart?: string;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue