Files
Showy/frontend/src/app/features/home/home.component.ts
T
2024-09-10 19:04:40 +01:00

299 lines
8.1 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import ApexCharts from 'apexcharts';
import { LibraryService } from '../../core/services/library.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',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css'],
})
export class HomeComponent implements OnInit {
counts: {
movies: number;
series: number;
favorit: number;
watchlist: number;
} = {
movies: 0,
series: 0,
favorit: 0,
watchlist: 0,
};
yearlyChart: any;
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,
};
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 statisticsService: StatisticsService,
private libraryService: LibraryService
) {}
async ngOnInit() {
// ? get counts (movies, series, favorit, watchlist)
this.getCounts();
// ? get yearly watch stats
let year: number = new Date().getFullYear();
this.watchCount.year = year;
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.getYearlyWatchCount();
}
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];
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 < 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 < 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(this.watchTime);
},
error: (error) => {
console.log(error);
},
complete: () => {
forkObs.unsubscribe();
},
});
// this.entrySeenService.getYearlyStats(year).subscribe({
// next: (stats) => {
// // looping through stats
// for (let i = 0; i < stats.length; i++) {
// this.watchCount.movies[stats[i].month - 1] = stats[i].count;
// }
// console.log(`Movies Stats for the year ${year}`, stats);
// },
// error: (error) => {
// console.log(`Error getting stats for the year ${year}`, error);
// },
// });
// this.episodeSeenService.getYearlyStats(year).subscribe({
// next: (stats) => {
// // looping through stats
// for (let i = 0; i < stats.length; i++) {
// this.watchCount.series[stats[i].month - 1] = stats[i].count;
// }
// this.showYearlyStats();
// console.log(`Series Stats for the year ${year}`, stats);
// },
// error: (error) => {
// console.log(`Error getting series stats for the year ${year}`, error);
// },
// });
}
getCounts() {
// TODO performance: change this to get stats from backend directly
this.libraryService.getAllEntries(true).subscribe((entries) => {
this.counts.favorit = entries.filter((e) => e.favorit).length;
this.counts.watchlist = entries.filter((e) => e.watchlist).length;
this.counts.movies = entries.filter(
(e) => !e.is_tvseries && !e.watchlist
).length;
this.counts.series = entries.filter(
(e) => e.is_tvseries && !e.watchlist
).length;
});
}
showYearlyStats() {
var yearlyChartOptions = {
series: [
{
name: 'Movies',
data: this.watchCount.movies,
},
{
name: 'Tv-Series Episodes',
data: this.watchCount.series,
// data: [17, 10, 45, 15, 25, 15, 40, 10, 24, 5, 6, 3],
},
],
chart: {
//width:150,
foreColor: '#9ba7b2',
height: 235,
type: 'bar',
toolbar: {
show: !1,
},
sparkline: {
enabled: !1,
},
zoom: {
enabled: false,
},
},
dataLabels: {
enabled: false,
},
stroke: {
width: 4,
curve: 'smooth',
colors: ['transparent'],
},
fill: {
type: 'gradient',
gradient: {
shade: 'dark',
gradientToColors: ['#0d6efd', '#6f42c1'],
shadeIntensity: 1,
type: 'vertical',
// opacityFrom: 0.8,
// opacityTo: 0.1,
stops: [0, 100, 100, 100],
},
},
colors: ['#0d6efd', '#6f42c1'],
plotOptions: {
bar: {
horizontal: false,
borderRadius: 0,
borderRadiusApplication: 'around',
borderRadiusWhenStacked: 'last',
columnWidth: '55%',
},
},
grid: {
show: false,
borderColor: 'rgba(0, 0, 0, 0.15)',
strokeDashArray: 4,
},
tooltip: {
theme: 'dark',
fixed: {
enabled: !0,
},
x: {
show: !0,
},
y: {
title: 'dd',
},
marker: {
show: !1,
},
},
xaxis: {
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
],
},
};
if (this.yearlyChart) {
this.yearlyChart.destroy(); // Destroy the existing chart instance if it exists
}
this.yearlyChart = new ApexCharts(
document.querySelector('#yearlyChart'),
yearlyChartOptions
);
this.yearlyChart.render();
}
}