mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 21:21:02 +00:00
feat: top genres report
This commit is contained in:
parent
62119d44b0
commit
a0c28e2324
21 changed files with 317 additions and 104 deletions
|
|
@ -79,53 +79,9 @@ export class CreateGenreTables0000000000005 implements MigrationInterface {
|
|||
}),
|
||||
true
|
||||
);
|
||||
|
||||
await queryRunner.createTable(
|
||||
new Table({
|
||||
name: "artist_genres",
|
||||
columns: [
|
||||
{
|
||||
name: "artistId",
|
||||
type: "uuid",
|
||||
isPrimary: true,
|
||||
},
|
||||
{
|
||||
name: "genreId",
|
||||
type: "uuid",
|
||||
isPrimary: true,
|
||||
},
|
||||
],
|
||||
indices: [
|
||||
new TableIndex({
|
||||
name: "IDX_ARTIST_GENRES_ARTIST_ID",
|
||||
columnNames: ["artistId"],
|
||||
}),
|
||||
new TableIndex({
|
||||
name: "IDX_ARTIST_GENRES_GENRE_ID",
|
||||
columnNames: ["genreId"],
|
||||
}),
|
||||
],
|
||||
foreignKeys: [
|
||||
new TableForeignKey({
|
||||
name: "FK_ARTIST_ID",
|
||||
columnNames: ["artistId"],
|
||||
referencedColumnNames: ["id"],
|
||||
referencedTableName: "artist",
|
||||
}),
|
||||
new TableForeignKey({
|
||||
name: "FK_GENRE_ID",
|
||||
columnNames: ["genreId"],
|
||||
referencedColumnNames: ["id"],
|
||||
referencedTableName: "genre",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.dropTable("album_genres");
|
||||
await queryRunner.dropTable("artist_genres");
|
||||
await queryRunner.dropTable("genre");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import {
|
|||
} from "typeorm";
|
||||
import { SpotifyLibraryDetails } from "../sources/spotify/spotify-library-details.entity";
|
||||
import { Artist } from "./artist.entity";
|
||||
import { Genre } from "./genre.entity";
|
||||
import { Track } from "./track.entity";
|
||||
|
||||
@Entity()
|
||||
|
|
@ -26,9 +25,6 @@ export class Album {
|
|||
@OneToMany(() => Track, (track) => track.album)
|
||||
tracks?: Track[];
|
||||
|
||||
@ManyToMany(() => Genre)
|
||||
genres?: Genre[];
|
||||
|
||||
@Column(() => SpotifyLibraryDetails)
|
||||
spotify: SpotifyLibraryDetails;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,10 @@
|
|||
import { Column, Entity, ManyToMany, PrimaryGeneratedColumn } from "typeorm";
|
||||
import {
|
||||
Column,
|
||||
Entity,
|
||||
JoinTable,
|
||||
ManyToMany,
|
||||
PrimaryGeneratedColumn,
|
||||
} from "typeorm";
|
||||
import { SpotifyLibraryDetails } from "../sources/spotify/spotify-library-details.entity";
|
||||
import { Album } from "./album.entity";
|
||||
import { Genre } from "./genre.entity";
|
||||
|
|
@ -15,6 +21,7 @@ export class Artist {
|
|||
albums?: Album[];
|
||||
|
||||
@ManyToMany(() => Genre)
|
||||
@JoinTable({ name: "artist_genres" })
|
||||
genres?: Genre[];
|
||||
|
||||
@Column(() => SpotifyLibraryDetails)
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
import { SpotifyLibraryDetails } from "../../sources/spotify/spotify-library-details.entity";
|
||||
import { Artist } from "../artist.entity";
|
||||
import { Genre } from "../genre.entity";
|
||||
|
||||
export class CreateAlbumDto {
|
||||
name: string;
|
||||
artists: Artist[];
|
||||
genres: Genre[];
|
||||
spotify?: SpotifyLibraryDetails;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ export class MusicLibraryService {
|
|||
const artist = this.artistRepository.create();
|
||||
|
||||
artist.name = data.name;
|
||||
artist.genres = data.genres;
|
||||
artist.spotify = data.spotify;
|
||||
|
||||
try {
|
||||
|
|
|
|||
10
src/reports/dto/get-top-genres-report.dto.ts
Normal file
10
src/reports/dto/get-top-genres-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 GetTopGenresReportDto {
|
||||
user: User;
|
||||
|
||||
@ValidateNested()
|
||||
time: ReportTimeDto;
|
||||
}
|
||||
10
src/reports/dto/top-genres-report.dto.ts
Normal file
10
src/reports/dto/top-genres-report.dto.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Genre } from "../../music-library/genre.entity";
|
||||
import { TopArtistsReportDto } from "./top-artists-report.dto";
|
||||
|
||||
export class TopGenresReportDto {
|
||||
items: {
|
||||
genre: Genre;
|
||||
artists: TopArtistsReportDto["items"];
|
||||
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 { TopGenresReportDto } from "./dto/top-genres-report.dto";
|
||||
import { TopTracksReportDto } from "./dto/top-tracks-report.dto";
|
||||
import { ReportsService } from "./reports.service";
|
||||
import { Timeframe } from "./timeframe.enum";
|
||||
|
|
@ -50,4 +51,13 @@ export class ReportsController {
|
|||
): Promise<TopTracksReportDto> {
|
||||
return this.reportsService.getTopTracks({ user, time });
|
||||
}
|
||||
|
||||
@Get("top-genres")
|
||||
@AuthAccessToken()
|
||||
async getTopGenres(
|
||||
@Query() time: ReportTimeDto,
|
||||
@ReqUser() user: User
|
||||
): Promise<TopGenresReportDto> {
|
||||
return this.reportsService.getTopGenres({ user, time });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,13 +15,17 @@ import {
|
|||
sub,
|
||||
} from "date-fns";
|
||||
import { ListensService } from "../listens/listens.service";
|
||||
import { User } from "../users/user.entity";
|
||||
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 { GetTopGenresReportDto } from "./dto/get-top-genres-report.dto";
|
||||
import { GetTopTracksReportDto } from "./dto/get-top-tracks-report.dto";
|
||||
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 { TopGenresReportDto as TopGenresReportDto } from "./dto/top-genres-report.dto";
|
||||
import { TopTracksReportDto } from "./dto/top-tracks-report.dto";
|
||||
import { Interval } from "./interval";
|
||||
import { Timeframe } from "./timeframe.enum";
|
||||
|
|
@ -66,16 +70,11 @@ export class ReportsService {
|
|||
constructor(private readonly listensService: ListensService) {}
|
||||
|
||||
async getListens(options: GetListenReportDto): Promise<ListenReportDto> {
|
||||
const { user, timeFrame, time: timePreset } = options;
|
||||
const { timeFrame, time: timePreset } = options;
|
||||
|
||||
const listens = await this.getListensQueryFromOptions(options).getMany();
|
||||
|
||||
const interval = this.getIntervalFromPreset(timePreset);
|
||||
|
||||
const listens = await this.listensService
|
||||
.getScopedQueryBuilder()
|
||||
.byUser(user)
|
||||
.duringInterval(interval)
|
||||
.getMany();
|
||||
|
||||
const { eachOfInterval, isSame } = timeframeToDateFns[timeFrame];
|
||||
|
||||
const reportItems = eachOfInterval(interval).map((date) => {
|
||||
|
|
@ -91,14 +90,7 @@ export class ReportsService {
|
|||
async getTopArtists(
|
||||
options: GetTopArtistsReportDto
|
||||
): Promise<TopArtistsReportDto> {
|
||||
const { user, time: timePreset } = options;
|
||||
|
||||
const interval = this.getIntervalFromPreset(timePreset);
|
||||
|
||||
const getArtistsWithCountQB = this.listensService
|
||||
.getScopedQueryBuilder()
|
||||
.byUser(user)
|
||||
.duringInterval(interval)
|
||||
const getArtistsWithCountQB = this.getListensQueryFromOptions(options)
|
||||
.leftJoin("listen.track", "track")
|
||||
.leftJoinAndSelect("track.artists", "artists")
|
||||
.groupBy("artists.id")
|
||||
|
|
@ -132,18 +124,8 @@ export class ReportsService {
|
|||
async getTopAlbums(
|
||||
options: GetTopAlbumsReportDto
|
||||
): Promise<TopAlbumsReportDto> {
|
||||
const { user, time: timePreset } = options;
|
||||
|
||||
const interval = this.getIntervalFromPreset(timePreset);
|
||||
|
||||
const getListensQB = () =>
|
||||
this.listensService
|
||||
.getScopedQueryBuilder()
|
||||
.byUser(user)
|
||||
.duringInterval(interval);
|
||||
|
||||
const [rawAlbumsWithCount, rawAlbumDetails] = await Promise.all([
|
||||
getListensQB()
|
||||
this.getListensQueryFromOptions(options)
|
||||
.leftJoin("listen.track", "track")
|
||||
.leftJoinAndSelect("track.album", "album")
|
||||
.groupBy("album.id")
|
||||
|
|
@ -154,7 +136,7 @@ export class ReportsService {
|
|||
|
||||
// Because of the GROUP BY required to calculate the count we can
|
||||
// not properly join the album relations in one query
|
||||
getListensQB()
|
||||
this.getListensQueryFromOptions(options)
|
||||
.leftJoinAndSelect("listen.track", "track")
|
||||
.leftJoinAndSelect("track.album", "album")
|
||||
.leftJoinAndSelect("album.artists", "artists")
|
||||
|
|
@ -181,18 +163,8 @@ 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()
|
||||
this.getListensQueryFromOptions(options)
|
||||
.leftJoin("listen.track", "track")
|
||||
.groupBy("track.id")
|
||||
.select("track.*")
|
||||
|
|
@ -202,7 +174,7 @@ export class ReportsService {
|
|||
|
||||
// Because of the GROUP BY required to calculate the count we can
|
||||
// not properly join the artist relations in one query
|
||||
getListensQB()
|
||||
this.getListensQueryFromOptions(options)
|
||||
.leftJoinAndSelect("listen.track", "track")
|
||||
.leftJoinAndSelect("track.artists", "artists")
|
||||
.distinctOn(["track.id"])
|
||||
|
|
@ -225,6 +197,77 @@ export class ReportsService {
|
|||
};
|
||||
}
|
||||
|
||||
async getTopGenres(
|
||||
options: GetTopGenresReportDto
|
||||
): Promise<TopGenresReportDto> {
|
||||
const [rawGenresWithCount, rawGenresWithArtistsCount] = await Promise.all([
|
||||
this.getListensQueryFromOptions(options)
|
||||
.leftJoin("listen.track", "track")
|
||||
.leftJoin("track.artists", "artists")
|
||||
.leftJoin("artists.genres", "genres")
|
||||
.groupBy("genres.id")
|
||||
.select("genres.*")
|
||||
.addSelect("count(*) as listens")
|
||||
.orderBy("listens", "DESC")
|
||||
.getRawMany(),
|
||||
this.getListensQueryFromOptions(options)
|
||||
.leftJoin("listen.track", "track")
|
||||
.leftJoin("track.artists", "artists")
|
||||
.leftJoin("artists.genres", "genres")
|
||||
.groupBy("genres.id")
|
||||
.addGroupBy("artists.id")
|
||||
.select("genres.id", "genreID")
|
||||
.addSelect("artists.*")
|
||||
.addSelect("count(*) as listens")
|
||||
.orderBy("listens", "DESC")
|
||||
.getRawMany(),
|
||||
]);
|
||||
|
||||
const items: TopGenresReportDto["items"] = rawGenresWithCount
|
||||
.filter((rawGenre) => rawGenre.id) // Make sure that listen has related genre
|
||||
.map((data) => ({
|
||||
count: Number.parseInt(data.listens, 10),
|
||||
genre: {
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
},
|
||||
artists: rawGenresWithArtistsCount
|
||||
.filter(({ genreID }) => genreID === data.id)
|
||||
.map((artistsData) => ({
|
||||
count: Number.parseInt(artistsData.listens, 10),
|
||||
artist: {
|
||||
id: artistsData.id,
|
||||
name: artistsData.name,
|
||||
spotify: {
|
||||
id: artistsData.spotifyId,
|
||||
uri: artistsData.spotifyUri,
|
||||
type: artistsData.spotifyType,
|
||||
href: artistsData.spotifyHref,
|
||||
},
|
||||
},
|
||||
}))
|
||||
.filter((_, i) => i < 5),
|
||||
}));
|
||||
|
||||
return {
|
||||
items,
|
||||
};
|
||||
}
|
||||
|
||||
private getListensQueryFromOptions(options: {
|
||||
user: User;
|
||||
time: ReportTimeDto;
|
||||
}) {
|
||||
const { user, time: timePreset } = options;
|
||||
|
||||
const interval = this.getIntervalFromPreset(timePreset);
|
||||
|
||||
return this.listensService
|
||||
.getScopedQueryBuilder()
|
||||
.byUser(user)
|
||||
.duringInterval(interval);
|
||||
}
|
||||
|
||||
private getIntervalFromPreset(options: {
|
||||
timePreset: TimePreset;
|
||||
customTimeStart?: string;
|
||||
|
|
|
|||
|
|
@ -5,13 +5,6 @@ import { SimplifiedArtistObject } from "./simplified-artist-object";
|
|||
// tslint:disable: variable-name
|
||||
|
||||
export class AlbumObject {
|
||||
/**
|
||||
* A list of the genres used to classify the album.
|
||||
* For example: "Prog Rock" , "Post-Grunge".
|
||||
* (If not yet classified, the array is empty.)
|
||||
*/
|
||||
genres: string[];
|
||||
|
||||
/**
|
||||
* The artists of the album.
|
||||
* Each artist object includes a link in href to more detailed information about the artist.
|
||||
|
|
|
|||
|
|
@ -210,14 +210,9 @@ export class SpotifyService {
|
|||
)
|
||||
);
|
||||
|
||||
const genres = await Promise.all(
|
||||
spotifyAlbum.genres.map((genreName) => this.importGenre(genreName))
|
||||
);
|
||||
|
||||
return this.musicLibraryService.createAlbum({
|
||||
name: spotifyAlbum.name,
|
||||
artists,
|
||||
genres,
|
||||
spotify: {
|
||||
id: spotifyAlbum.id,
|
||||
uri: spotifyAlbum.uri,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue