diff --git a/frontend/src/app/app.module.ts b/frontend/src/app/app.module.ts index 33e8486..b3939a1 100644 --- a/frontend/src/app/app.module.ts +++ b/frontend/src/app/app.module.ts @@ -18,6 +18,7 @@ import { SearchPageComponent } from './features/discover/search-page/search-page import { SharedModule } from './shared/shared.module'; import { ToastrModule } from 'ngx-toastr'; +import { MinutesFormatPipe } from './shared/pipes/minutes-format/minutes-format.pipe'; @NgModule({ imports: [ @@ -49,7 +50,7 @@ import { ToastrModule } from 'ngx-toastr'; SearchPageComponent, ], - providers: [DatePipe], + providers: [DatePipe, MinutesFormatPipe], bootstrap: [AppComponent], }) export class AppModule {} diff --git a/frontend/src/app/core/services/entry-seen.service.ts b/frontend/src/app/core/services/entry-seen.service.ts index 412e3a3..4851e9f 100644 --- a/frontend/src/app/core/services/entry-seen.service.ts +++ b/frontend/src/app/core/services/entry-seen.service.ts @@ -46,9 +46,4 @@ export class EntrySeenService { ); } - getYearlyStats(year: number) { - return this.http.get( - `${this.backend_apiUrl}/${this.apiAction}/stats-year/${year}` - ); - } } diff --git a/frontend/src/app/core/services/episode-seen.service.ts b/frontend/src/app/core/services/episode-seen.service.ts index e0d72ff..6b64fc0 100644 --- a/frontend/src/app/core/services/episode-seen.service.ts +++ b/frontend/src/app/core/services/episode-seen.service.ts @@ -60,9 +60,4 @@ export class EpisodeSeenService { ); } - getYearlyStats(year: number) { - return this.http.get( - `${this.backend_apiUrl}/${this.apiAction}/stats-year/${year}` - ); - } } diff --git a/frontend/src/app/core/services/statistics.service.ts b/frontend/src/app/core/services/statistics.service.ts new file mode 100644 index 0000000..a8c7335 --- /dev/null +++ b/frontend/src/app/core/services/statistics.service.ts @@ -0,0 +1,32 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { environment } from '../../../environments/environment.prod'; + +@Injectable({ + providedIn: 'root', +}) +export class StatisticsService { + private backend_apiUrl = environment.API_URL; + private apiAction = 'statistics'; + + constructor(private http: HttpClient) {} + + fetchYearlyWatchCount(year: number) { + return this.http.get( + `${this.backend_apiUrl}/${this.apiAction}/watch-count/year/${year}` + ); + } + + fetchAllTimeTotalWatchTime() { + return this.http.get( + `${this.backend_apiUrl}/${this.apiAction}/watch-time` + ); + } + + fetchYearlyTotalWatchTime(year: number) { + return this.http.get( + `${this.backend_apiUrl}/${this.apiAction}/watch-time/year/${year}` + ); + } + +} diff --git a/frontend/src/app/features/home/home.component.css b/frontend/src/app/features/home/home.component.css index e69de29..21221bf 100644 --- a/frontend/src/app/features/home/home.component.css +++ b/frontend/src/app/features/home/home.component.css @@ -0,0 +1,3 @@ +.time-color { + color: var(--shz-blue-default); +} diff --git a/frontend/src/app/features/home/home.component.html b/frontend/src/app/features/home/home.component.html index 35cda1b..352ca2e 100644 --- a/frontend/src/app/features/home/home.component.html +++ b/frontend/src/app/features/home/home.component.html @@ -83,184 +83,106 @@
-
-
-
-
-
-
-
-
-
-

97.4K

-

Total Users

-
- -
-
-
-
-
-

12.5% from last month

-
-
-
-
-
-
-
-
-
-

42.5K

-

Active Users

-
- -
-
-
-
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 78% - - - - - - -
-
-
-
-

24K users increased from last month

-
-
-
-
-
-
-
-
-
-

$65,129

-
-
-

- arrow_upward8.6% -

-
-
-

Sales This Year

-
-

285 left to Goal78%

-
-
-
-
+ +
+ +
+
+
+
+ Total Time Spent Watching +
+
+
+
+
+
+ movie +
+
+ Movies +
+
+
+
+
{{ watchTime.allTime.movies | number }} minutes
+

and it is

+
{{ watchTime.allTime.movies | minutesFormat:'hours' | number }} hours
+

which is about {{watchTime.allTime.movies | minutesFormat:'days'}} days.

-
+
+
+
+ tv +
+
+ Tv-Series +
+
+
+
+
{{ watchTime.allTime.series | number }} minutes
+

and it is

+
{{ watchTime.allTime.series | minutesFormat:'hours' | number }} hours
+

which is about {{watchTime.allTime.series | minutesFormat:'days'}} days.

+
+
+
+
+ +
+ +

You've spent {{ watchTime.allTime.total | minutesFormat:'days' | number }} days of your life + watching this crap. +
+ {{ (watchTime.allTime.total * 100) / minutesAge | floatFormat:2 }}% of your life. +

+ +
+ +
+ + + +
-
{{watchCount.year}} Watch Stats
+
{{watchCount.year}} Watch History
+
+ +
+ \ No newline at end of file diff --git a/frontend/src/app/features/home/home.component.ts b/frontend/src/app/features/home/home.component.ts index 9c6d051..01c410e 100644 --- a/frontend/src/app/features/home/home.component.ts +++ b/frontend/src/app/features/home/home.component.ts @@ -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);