Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Time Spent Reading By Year and Hover Effect on User Stats #3106

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions API/Controllers/StatsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ public async Task<ActionResult<IEnumerable<StatCount<int>>>> GetPagesReadPerYear
return Ok(_statService.GetPagesReadCountByYear(userId));
}

/// <summary>
/// Returns a count of time spent reading per year for a given userId.
/// </summary>
/// <param name="userId">If userId is 0 and user is not an admin, API will default to userId</param>
/// <returns></returns>
[HttpGet("time-spent-reading-per-year")]
[ResponseCache(CacheProfileName = "Statistics")]
public async Task<ActionResult<IEnumerable<StatCount<int>>>> TimeSpentReadingPerYear(int userId = 0)
{
var isAdmin = User.IsInRole(PolicyConstants.AdminRole);
if (!isAdmin) userId = User.GetUserId();
return Ok(_statService.GetTimeSpentReadingByYear(userId));
}

/// <summary>
/// Returns a count of words read per year for a given userId.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions API/Services/StatisticService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public interface IStatisticService
IEnumerable<StatCount<DayOfWeek>> GetDayBreakdown(int userId = 0);
IEnumerable<StatCount<int>> GetPagesReadCountByYear(int userId = 0);
IEnumerable<StatCount<int>> GetWordsReadCountByYear(int userId = 0);
IEnumerable<StatCount<int>> GetTimeSpentReadingByYear(int userId = 0);
Task UpdateServerStatistics();
Task<long> TimeSpentReadingForUsersAsync(IList<int> userIds, IList<int> libraryIds);
Task<KavitaPlusMetadataBreakdownDto> GetKavitaPlusMetadataBreakdown();
Expand Down Expand Up @@ -539,6 +540,27 @@ public async Task<long> TimeSpentReadingForUsersAsync(IList<int> userIds, IList<
p.chapter.AvgHoursToRead * (p.progress.PagesRead / (1.0f * p.chapter.Pages))));
}

public IEnumerable<StatCount<int>> GetTimeSpentReadingByYear(int userId = 0)
{
var query = _context.AppUserProgresses
.Join(_context.Chapter,
progress => progress.ChapterId,
chapter => chapter.Id,
(progress, chapter) => new { Progress = progress, Chapter = chapter })
.AsSplitQuery()
.AsNoTracking();

if (userId > 0)
{
query = query.Where(p => p.Progress.AppUserId == userId);
}

return query.GroupBy(p => p.Progress.LastModified.Year)
.OrderBy(g => g.Key)
.Select(g => new StatCount<int> { Value = g.Key, Count = g.Sum(x => x.Chapter.AvgHoursToRead) })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be a float. In #3209 I made the AvgHoursToRead a float to fix an issue where anything that was less than 1 hour wasn't counted in stats.

.AsEnumerable();
}

public async Task<KavitaPlusMetadataBreakdownDto> GetKavitaPlusMetadataBreakdown()
{
// We need to count number of Series that have an external series record
Expand Down
7 changes: 7 additions & 0 deletions UI/Web/src/app/_services/statistics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ export class StatisticsService {
})));
}

getTimeSpentReadingPerYear(userId = 0) {
return this.httpClient.get<StatCount<number>[]>(this.baseUrl + 'stats/time-spent-reading-per-year?userId=' + userId).pipe(
map(spreads => spreads.map(spread => {
return {name: spread.value + '', value: spread.count};
})));
}

getTopUsers(days: number = 0) {
return this.httpClient.get<TopUserRead[]>(this.baseUrl + 'stats/server/top/users?days=' + days);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ng-container>

<ng-container>
<div class="col-auto mb-2">
<div class="col-auto mb-2 icon">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this class

<app-icon-and-title [label]="t('total-words-read-label')" [clickable]="true" fontClasses="fa-regular fa-file-lines"
[title]="t('total-words-read-tooltip', {value: totalWordsRead | number})" (click)="openWordByYearList();$event.stopPropagation();">
{{totalWordsRead | compactNumber}}
Expand All @@ -20,10 +20,10 @@
<div class="vr d-none d-lg-block m-2"></div>
</ng-container>

<ng-container >
<ng-container>
<div class="col-auto mb-2">
<app-icon-and-title [label]="t('time-spent-reading-label')" [clickable]="false" fontClasses="fas fa-eye"
[title]="t('time-spent-reading-tooltip', {value: timeSpentReading | number})">
<app-icon-and-title [label]="t('time-spent-reading-label')" [clickable]="false" fontClasses="fas fa-eye icon"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove icon class

[title]="t('time-spent-reading-tooltip', {value: timeSpentReading | number})" (click)="openTimeSpentReadingByYearList();$event.stopPropagation();">
{{timeSpentReading | timeDuration}}
</app-icon-and-title>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ export class UserStatsInfoCardsComponent {
ref.componentInstance.title = 'Words Read By Year';
});
}
openTimeSpentReadingByYearList() {
const numberPipe = new CompactNumberPipe();
this.statsService.getTimeSpentReadingPerYear().subscribe(yearCounts => {
const ref = this.modalService.open(GenericListModalComponent, { scrollable: true });
ref.componentInstance.items = yearCounts.map(t => `${t.name}: ${numberPipe.transform(t.value)} hours`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to use localization everywhere, so find or create the appropriate key.

Same for the title. Usually each component has it's own.

ref.componentInstance.title = 'Time Spent Reading By Year';
});
}
}