2020-06-01 20:34:59 +02:00
|
|
|
import { BadRequestException, Injectable } from "@nestjs/common";
|
2020-05-09 19:22:17 +02:00
|
|
|
import {
|
|
|
|
|
eachDayOfInterval,
|
|
|
|
|
eachMonthOfInterval,
|
|
|
|
|
eachWeekOfInterval,
|
|
|
|
|
eachYearOfInterval,
|
2020-06-01 20:34:59 +02:00
|
|
|
endOfDay,
|
2020-05-09 19:22:17 +02:00
|
|
|
formatISO,
|
|
|
|
|
isSameDay,
|
|
|
|
|
isSameMonth,
|
|
|
|
|
isSameWeek,
|
|
|
|
|
isSameYear,
|
|
|
|
|
parseISO,
|
2020-06-01 20:34:59 +02:00
|
|
|
parseJSON,
|
|
|
|
|
startOfDay,
|
|
|
|
|
sub,
|
2020-05-09 19:22:17 +02:00
|
|
|
} from "date-fns";
|
2020-07-11 19:35:31 +02:00
|
|
|
import { ListenRepository } from "../listens/listen.repository";
|
2020-05-09 19:22:17 +02:00
|
|
|
import { ListensService } from "../listens/listens.service";
|
|
|
|
|
import { GetListenReportDto } from "./dto/get-listen-report.dto";
|
2020-05-31 23:26:06 +02:00
|
|
|
import { GetTopArtistsReportDto } from "./dto/get-top-artists-report.dto";
|
2020-05-09 19:22:17 +02:00
|
|
|
import { ListenReportDto } from "./dto/listen-report.dto";
|
2020-05-31 23:26:06 +02:00
|
|
|
import { TopArtistsReportDto } from "./dto/top-artists-report.dto";
|
|
|
|
|
import { Interval } from "./interval";
|
2020-05-09 19:22:17 +02:00
|
|
|
import { Timeframe } from "./timeframe.enum";
|
2020-06-01 20:34:59 +02:00
|
|
|
import { TimePreset } from "./timePreset.enum";
|
2020-05-09 19:22:17 +02:00
|
|
|
|
|
|
|
|
const timeframeToDateFns: {
|
|
|
|
|
[x in Timeframe]: {
|
|
|
|
|
eachOfInterval: (interval: Interval) => Date[];
|
|
|
|
|
isSame: (dateLeft: Date, dateRight: Date) => boolean;
|
|
|
|
|
};
|
|
|
|
|
} = {
|
|
|
|
|
[Timeframe.Day]: {
|
|
|
|
|
eachOfInterval: eachDayOfInterval,
|
|
|
|
|
isSame: isSameDay,
|
|
|
|
|
},
|
|
|
|
|
[Timeframe.Week]: {
|
|
|
|
|
eachOfInterval: eachWeekOfInterval,
|
|
|
|
|
isSame: isSameWeek,
|
|
|
|
|
},
|
|
|
|
|
[Timeframe.Month]: {
|
|
|
|
|
eachOfInterval: eachMonthOfInterval,
|
|
|
|
|
isSame: isSameMonth,
|
|
|
|
|
},
|
|
|
|
|
[Timeframe.Year]: {
|
|
|
|
|
eachOfInterval: eachYearOfInterval,
|
|
|
|
|
isSame: isSameYear,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2020-06-01 20:34:59 +02:00
|
|
|
const timePresetToDays: { [x in TimePreset]: number } = {
|
|
|
|
|
[TimePreset.LAST_7_DAYS]: 7,
|
|
|
|
|
[TimePreset.LAST_30_DAYS]: 30,
|
|
|
|
|
[TimePreset.LAST_90_DAYS]: 90,
|
|
|
|
|
[TimePreset.LAST_180_DAYS]: 180,
|
|
|
|
|
[TimePreset.LAST_365_DAYS]: 365,
|
|
|
|
|
[TimePreset.ALL_TIME]: 0, // Not used for this
|
|
|
|
|
[TimePreset.CUSTOM]: 0, // Not used for this
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-31 23:26:06 +02:00
|
|
|
const PAGINATION_LIMIT_UNLIMITED = 10000000;
|
|
|
|
|
|
2020-05-09 19:22:17 +02:00
|
|
|
@Injectable()
|
|
|
|
|
export class ReportsService {
|
2020-07-11 19:35:31 +02:00
|
|
|
constructor(
|
|
|
|
|
private readonly listensService: ListensService,
|
|
|
|
|
private readonly listenRepository: ListenRepository
|
|
|
|
|
) {}
|
2020-05-09 19:22:17 +02:00
|
|
|
|
|
|
|
|
async getListens(options: GetListenReportDto): Promise<ListenReportDto> {
|
|
|
|
|
const { user, timeFrame, timeStart, timeEnd } = options;
|
|
|
|
|
|
2020-06-01 20:34:59 +02:00
|
|
|
// Function should eventually be rewritten to accept a timepreset
|
|
|
|
|
const interval = this.getIntervalFromPreset({
|
|
|
|
|
timePreset: TimePreset.CUSTOM,
|
|
|
|
|
customTimeStart: timeStart,
|
|
|
|
|
customTimeEnd: timeEnd,
|
|
|
|
|
});
|
|
|
|
|
|
2020-05-09 19:22:17 +02:00
|
|
|
const { items: listens } = await this.listensService.getListens({
|
|
|
|
|
user,
|
2020-06-01 20:34:59 +02:00
|
|
|
filter: { time: interval },
|
2020-05-09 19:22:17 +02:00
|
|
|
page: 1,
|
2020-05-31 23:26:06 +02:00
|
|
|
limit: PAGINATION_LIMIT_UNLIMITED,
|
2020-05-09 19:22:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const reportInterval: Interval = {
|
|
|
|
|
start: parseISO(timeStart),
|
|
|
|
|
end: parseISO(timeEnd),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const { eachOfInterval, isSame } = timeframeToDateFns[timeFrame];
|
|
|
|
|
|
|
|
|
|
const reportItems = eachOfInterval(reportInterval).map((date) => {
|
|
|
|
|
const count = listens.filter((listen) => isSame(date, listen.playedAt))
|
|
|
|
|
.length;
|
|
|
|
|
return { date: formatISO(date), count };
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return { items: reportItems, timeStart, timeEnd, timeFrame };
|
|
|
|
|
}
|
2020-05-31 23:26:06 +02:00
|
|
|
|
|
|
|
|
async getTopArtists(
|
|
|
|
|
options: GetTopArtistsReportDto
|
|
|
|
|
): Promise<TopArtistsReportDto> {
|
|
|
|
|
const { user, timePreset, customTimeStart, customTimeEnd } = options;
|
|
|
|
|
|
|
|
|
|
const interval = this.getIntervalFromPreset({
|
|
|
|
|
timePreset,
|
|
|
|
|
customTimeStart,
|
|
|
|
|
customTimeEnd,
|
|
|
|
|
});
|
|
|
|
|
|
2020-07-11 19:35:31 +02:00
|
|
|
const getArtistsWithCountQB = this.listenRepository
|
|
|
|
|
.createQueryBuilder("l")
|
|
|
|
|
.andWhere('l."userId" = :userID', { userID: user.id })
|
|
|
|
|
.andWhere("l.playedAt BETWEEN :timeStart AND :timeEnd", {
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
const rawArtistsWithCount = await getArtistsWithCountQB.getRawMany();
|
|
|
|
|
|
|
|
|
|
const items: TopArtistsReportDto["items"] = rawArtistsWithCount.map(
|
|
|
|
|
(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,
|
2020-05-31 23:26:06 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-01 20:34:59 +02:00
|
|
|
private getIntervalFromPreset(options: {
|
|
|
|
|
timePreset: TimePreset;
|
|
|
|
|
customTimeStart?: string;
|
|
|
|
|
customTimeEnd?: string;
|
|
|
|
|
}): Interval {
|
|
|
|
|
let interval = {
|
|
|
|
|
start: startOfDay(new Date()),
|
|
|
|
|
end: endOfDay(new Date()), // NOW
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
switch (options.timePreset) {
|
|
|
|
|
case TimePreset.LAST_7_DAYS:
|
|
|
|
|
case TimePreset.LAST_30_DAYS:
|
|
|
|
|
case TimePreset.LAST_90_DAYS:
|
|
|
|
|
case TimePreset.LAST_180_DAYS:
|
|
|
|
|
case TimePreset.LAST_365_DAYS: {
|
|
|
|
|
interval.start = startOfDay(
|
|
|
|
|
sub(interval.start, { days: timePresetToDays[options.timePreset] })
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case TimePreset.ALL_TIME: {
|
|
|
|
|
interval.start = new Date(0); // Start of epoch
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case TimePreset.CUSTOM: {
|
|
|
|
|
if (!options.customTimeStart && !options.customTimeEnd) {
|
|
|
|
|
throw new BadRequestException("MissingCustomTime");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interval = {
|
|
|
|
|
start: parseJSON(options.customTimeStart),
|
|
|
|
|
end: parseJSON(options.customTimeEnd),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return interval;
|
|
|
|
|
}
|
2020-05-09 19:22:17 +02:00
|
|
|
}
|