mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 21:21:02 +00:00
fix: improve top-artists response time
By calculating the necessary counts in the database, we avoid unncessary joins and data transmissions.
This commit is contained in:
parent
2ea7366a58
commit
aecc82576a
4 changed files with 44 additions and 39 deletions
|
|
@ -1,13 +1,13 @@
|
||||||
import { Module } from "@nestjs/common";
|
import { Module } from "@nestjs/common";
|
||||||
import { ListensService } from "./listens.service";
|
|
||||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||||
import { ListenRepository } from "./listen.repository";
|
import { ListenRepository } from "./listen.repository";
|
||||||
import { ListensController } from "./listens.controller";
|
import { ListensController } from "./listens.controller";
|
||||||
|
import { ListensService } from "./listens.service";
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [TypeOrmModule.forFeature([ListenRepository])],
|
imports: [TypeOrmModule.forFeature([ListenRepository])],
|
||||||
providers: [ListensService],
|
providers: [ListensService],
|
||||||
exports: [ListensService],
|
exports: [ListensService, TypeOrmModule],
|
||||||
controllers: [ListensController],
|
controllers: [ListensController],
|
||||||
})
|
})
|
||||||
export class ListensModule {}
|
export class ListensModule {}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToMany } from "typeorm";
|
|
||||||
import { SpotifyLibraryDetails } from "src/sources/spotify/spotify-library-details.entity";
|
import { SpotifyLibraryDetails } from "src/sources/spotify/spotify-library-details.entity";
|
||||||
|
import { Column, Entity, ManyToMany, PrimaryGeneratedColumn } from "typeorm";
|
||||||
import { Album } from "./album.entity";
|
import { Album } from "./album.entity";
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
|
|
@ -11,7 +11,7 @@ export class Artist {
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
@ManyToMany((type) => Album, (album) => album.artists)
|
@ManyToMany((type) => Album, (album) => album.artists)
|
||||||
albums: Album[];
|
albums?: Album[];
|
||||||
|
|
||||||
@Column((type) => SpotifyLibraryDetails)
|
@Column((type) => SpotifyLibraryDetails)
|
||||||
spotify: SpotifyLibraryDetails;
|
spotify: SpotifyLibraryDetails;
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { Module } from "@nestjs/common";
|
import { Module } from "@nestjs/common";
|
||||||
import { ReportsService } from "./reports.service";
|
|
||||||
import { ReportsController } from "./reports.controller";
|
|
||||||
import { ListensModule } from "src/listens/listens.module";
|
import { ListensModule } from "src/listens/listens.module";
|
||||||
|
import { ReportsController } from "./reports.controller";
|
||||||
|
import { ReportsService } from "./reports.service";
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ListensModule],
|
imports: [ListensModule],
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ import {
|
||||||
startOfDay,
|
startOfDay,
|
||||||
sub,
|
sub,
|
||||||
} from "date-fns";
|
} from "date-fns";
|
||||||
|
import { ListenRepository } from "../listens/listen.repository";
|
||||||
import { ListensService } from "../listens/listens.service";
|
import { ListensService } from "../listens/listens.service";
|
||||||
import { GetListenReportDto } from "./dto/get-listen-report.dto";
|
import { GetListenReportDto } from "./dto/get-listen-report.dto";
|
||||||
import { GetTopArtistsReportDto } from "./dto/get-top-artists-report.dto";
|
import { GetTopArtistsReportDto } from "./dto/get-top-artists-report.dto";
|
||||||
|
|
@ -62,7 +63,10 @@ const PAGINATION_LIMIT_UNLIMITED = 10000000;
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ReportsService {
|
export class ReportsService {
|
||||||
constructor(private readonly listensService: ListensService) {}
|
constructor(
|
||||||
|
private readonly listensService: ListensService,
|
||||||
|
private readonly listenRepository: ListenRepository
|
||||||
|
) {}
|
||||||
|
|
||||||
async getListens(options: GetListenReportDto): Promise<ListenReportDto> {
|
async getListens(options: GetListenReportDto): Promise<ListenReportDto> {
|
||||||
const { user, timeFrame, timeStart, timeEnd } = options;
|
const { user, timeFrame, timeStart, timeEnd } = options;
|
||||||
|
|
@ -108,40 +112,41 @@ export class ReportsService {
|
||||||
customTimeEnd,
|
customTimeEnd,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { items: listens } = await this.listensService.getListens({
|
const getArtistsWithCountQB = this.listenRepository
|
||||||
user,
|
.createQueryBuilder("l")
|
||||||
filter: { time: interval },
|
.andWhere('l."userId" = :userID', { userID: user.id })
|
||||||
page: 1,
|
.andWhere("l.playedAt BETWEEN :timeStart AND :timeEnd", {
|
||||||
limit: PAGINATION_LIMIT_UNLIMITED,
|
timeStart: interval.start,
|
||||||
});
|
timeEnd: interval.end,
|
||||||
|
})
|
||||||
|
.leftJoin("l.track", "track")
|
||||||
|
.leftJoinAndSelect("track.artists", "artists")
|
||||||
|
.groupBy("artists.id")
|
||||||
|
.select("artists.*")
|
||||||
|
.addSelect("count(*) as listens")
|
||||||
|
.orderBy("listens", "DESC");
|
||||||
|
|
||||||
// Declare types for metrics calculation
|
const rawArtistsWithCount = await getArtistsWithCountQB.getRawMany();
|
||||||
type Item = TopArtistsReportDto["items"][0];
|
|
||||||
type Accumulator = {
|
const items: TopArtistsReportDto["items"] = rawArtistsWithCount.map(
|
||||||
[x: string]: Item;
|
(data) => ({
|
||||||
|
count: Number.parseInt(data.listens, 10),
|
||||||
|
artist: {
|
||||||
|
id: data.id,
|
||||||
|
name: data.name,
|
||||||
|
spotify: {
|
||||||
|
id: data.spotifyId,
|
||||||
|
uri: data.spotifyUri,
|
||||||
|
type: data.spotifyType,
|
||||||
|
href: data.spotifyHref,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
items,
|
||||||
};
|
};
|
||||||
|
|
||||||
const items: TopArtistsReportDto["items"] = Object.values<Item>(
|
|
||||||
listens
|
|
||||||
.flatMap((listen) => listen.track.artists)
|
|
||||||
.reduce<Accumulator>((counters, artist) => {
|
|
||||||
if (!counters[artist.id]) {
|
|
||||||
counters[artist.id] = {
|
|
||||||
artist,
|
|
||||||
count: 0,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
counters[artist.id].count += 1;
|
|
||||||
|
|
||||||
return counters;
|
|
||||||
}, {})
|
|
||||||
)
|
|
||||||
.sort((a, b) => a.count - b.count)
|
|
||||||
.reverse() // sort descending
|
|
||||||
.slice(0, 20); // TODO: Make configurable
|
|
||||||
|
|
||||||
return { items };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private getIntervalFromPreset(options: {
|
private getIntervalFromPreset(options: {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue