add statistic service & add new stats to home page

This commit is contained in:
X4Lo committed 2024-07-06 21:41:18 +01:00
1 parent 90e3f936fc
commit 1b36bc66fe
7 files changed
+213 -210

No files matched your search

@@ -4,6 +4,9 @@ import { EntrySeenService } from '../../core/services/entry-seen.service';
import { LibraryService } from '../../core/services/library.service';
import { EpisodeSeenService } from '../../core/services/episode-seen.service';
import { forkJoin } from 'rxjs';
import { StatisticsService } from '../../core/services/statistics.service';
import { WatchTime } from '../../shared/models/statistics/watchTime.model';
import { WatchCount } from '../../shared/models/statistics/watchCount.model';
@Component({
selector: 'app-home-page',
@@ -24,23 +27,32 @@ export class HomeComponent implements OnInit {
};
yearlyChart: any;
watchCount: {
movies: number[];
series: number[];
moviesTotal: number;
seriesTotal: number;
year: number;
} = {
watchCount: WatchCount = {
year: 0,
movies: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
series: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
moviesTotal: 0,
seriesTotal: 0,
year: 0,
};
watchTime: WatchTime = {
allTime: {
series: 0,
movies: 0,
},
yearly: {
year: 0,
series: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
movies: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
moviesTotal: 0,
seriesTotal: 0,
},
} as WatchTime;
minutesAge = 0;
constructor(
private entrySeenService: EntrySeenService,
private episodeSeenService: EpisodeSeenService,
private statisticsService: StatisticsService,
private libraryService: LibraryService
) {
console.log('hey im home');
@@ -53,44 +65,83 @@ export class HomeComponent implements OnInit {
// ? get yearly watch stats
let year: number = new Date().getFullYear();
this.watchCount.year = year;
this.getYearlyWatchHistoryStats();
this.getYearlyWatchCount();
this.getWatchTime();
this.minutesAge =
(new Date().getTime() - new Date('2000-08-07').getTime()) / 1000 / 60;
}
changeYear(year: number) {
// TODO implement this in the view
this.watchCount.year = year;
console.log(`Year set to: `, this.watchCount.year);
this.getYearlyWatchHistoryStats();
this.getYearlyWatchCount();
}
async getYearlyWatchHistoryStats() {
let moviesObs = this.entrySeenService.getYearlyStats(this.watchCount.year);
let seriesObs = this.episodeSeenService.getYearlyStats(
this.watchCount.year
);
async getYearlyWatchCount() {
let watchCountObs = this.statisticsService
.fetchYearlyWatchCount(this.watchCount.year)
.subscribe({
next: (wc) => {
// console.log(`results: `, movies, series);
this.watchCount.moviesTotal = 0;
this.watchCount.seriesTotal = 0;
this.watchCount.movies = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
this.watchCount.series = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
const forkObs = forkJoin([moviesObs, seriesObs]).subscribe({
next: ([movies, series]) => {
console.log(`results: `, movies, series);
this.watchCount.moviesTotal = 0;
this.watchCount.seriesTotal = 0;
this.watchCount.movies = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
this.watchCount.series = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for (let i = 0; i < wc.movies.length; i++) {
this.watchCount.movies[wc.movies[i].month - 1] = wc.movies[i].count;
this.watchCount.moviesTotal += wc.movies[i].count;
}
for (let i = 0; i < movies.length; i++) {
this.watchCount.movies[movies[i].month - 1] = movies[i].count;
this.watchCount.moviesTotal += movies[i].count;
for (let i = 0; i < wc.series.length; i++) {
this.watchCount.series[wc.series[i].month - 1] = wc.series[i].count;
this.watchCount.seriesTotal += wc.series[i].count;
}
console.log(
`Watch stats for the year ${this.watchCount.year} fetched successfully.`
);
this.showYearlyStats();
},
error: (error) => {
console.log(error);
},
complete: () => {
watchCountObs.unsubscribe();
},
});
}
async getWatchTime() {
const thisYear = new Date().getFullYear();
let allTimeObs = this.statisticsService.fetchAllTimeTotalWatchTime();
let yearlyObs = this.statisticsService.fetchYearlyTotalWatchTime(thisYear);
const forkObs = forkJoin([allTimeObs, yearlyObs]).subscribe({
next: ([allTime, yearly]) => {
// console.log(`results: `, allTime, yearly);
this.watchTime.allTime.series = allTime.series;
this.watchTime.allTime.movies = allTime.movies;
this.watchTime.allTime.total =
Number.parseInt(allTime.series) + Number.parseInt(allTime.movies);
this.watchTime.yearly.year = thisYear;
for (let i = 0; i < yearly.series.length; i++) {
this.watchTime.yearly.series[yearly.series[i].month - 1] =
yearly.series[i].watchTime;
this.watchTime.yearly.seriesTotal += yearly.series[i].watchTime;
}
for (let i = 0; i < series.length; i++) {
this.watchCount.series[series[i].month - 1] = series[i].count;
this.watchCount.seriesTotal += series[i].count;
for (let i = 0; i < yearly.movies.length; i++) {
this.watchTime.yearly.movies[yearly.movies[i].month - 1] =
yearly.movies[i].watchTime;
this.watchTime.yearly.moviesTotal += yearly.movies[i].watchTime;
}
console.log(
`Watch stats for the year ${this.watchCount.year} fetched successfully.`
);
this.showYearlyStats();
console.log(this.watchTime);
},
error: (error) => {
console.log(error);