Skip to content

Commit

Permalink
Improved aggregation computation
Browse files Browse the repository at this point in the history
  • Loading branch information
valb3r committed Apr 13, 2021
1 parent 0a72942 commit e7f2b7f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<h4>Your aggregate time statistics for {{isoViewDate()}} (click on calendar to change)</h4>
<p>Hours worked this week: {{weeklyH}}</p>
<p>Hours worked this and previous week: {{biWeeklyH}}</p>
<p>Hours worked this month: {{monthlyH}}</p>
<h4>Your aggregate time statistics for {{isoDate(viewDate)}} (click on calendar to change)</h4>
<p>Hours worked in 7 days: {{sevenDaysH}} ({{isoDate(sevenDaysDates[0])}} to {{isoDate(sevenDaysDates[1])}})</p>
<p>Hours worked in 14 days: {{fourteenDaysH}} ({{isoDate(fourteenDaysDates[0])}} to {{isoDate(fourteenDaysDates[1])}})</p>
<p>Hours worked in 30 days: {{thirtyDaysH}} ({{isoDate(thirtyDaysDates[0])}} to {{isoDate(thirtyDaysDates[1])}})</p>
<br/>
<p>Hours worked this week: {{weeklyH}} ({{isoDate(weeklyDates[0])}} to {{isoDate(weeklyDates[1])}})</p>
<p>Hours worked this and previous week: {{biWeeklyH}} ({{isoDate(biWeeklyDates[0])}} to {{isoDate(biWeeklyDates[1])}})</p>
<p>Hours worked this month: {{monthlyH}} ({{isoDate(monthlyDates[0])}} to {{isoDate(monthlyDates[1])}})</p>
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import {Component, Input, OnInit} from '@angular/core';
import {TimeLogUpload} from "../service/timecard-api/time-card-api.service";
import {endOfISOWeek, endOfMonth, formatISO, parseISO, startOfISOWeek, startOfMonth, subDays} from "date-fns";
import {
endOfISOWeek,
endOfMonth,
formatISO,
parseISO,
startOfDay,
startOfISOWeek,
startOfMonth,
subDays
} from "date-fns";
import {ManagedTimeLog} from "../service/admin-api/admin-api-service";

@Component({
Expand All @@ -24,17 +33,31 @@ export class TimeCardAggregateStatsComponent implements OnInit {
this.computeAggregateStats(this.logs, this.viewDate);
}

sevenDaysH = 0;
fourteenDaysH = 0;
thirtyDaysH = 0;
weeklyH = 0;
biWeeklyH = 0;
monthlyH = 0;

sevenDaysDates = [];
fourteenDaysDates = [];
thirtyDaysDates = [];
weeklyDates = [];
biWeeklyDates = [];
monthlyDates = [];

constructor() { }

ngOnInit(): void {
}

isoViewDate() {
return formatISO(this.viewDate, {representation: 'date'});
isoDate(date: Date) {
if (!date) {
return '';
}

return formatISO(date, {representation: 'date'});
}

private computeAggregateStats(updates: TimeLogUpload[], date: Date) {
Expand All @@ -48,9 +71,27 @@ export class TimeCardAggregateStatsComponent implements OnInit {
const monthStart = startOfMonth(date);
const monthEnd = endOfMonth(date);
const cardData = updates.map(it => new CardDuration(parseISO(it.timestamp), it.durationminutes));
this.weeklyH = +this.round(cardData.filter(it => it.at >= weekStart && it.at <= weekEnd).map(it => it.durationminutes).reduce((a, b) => a + b, 0) / 60.0);
this.biWeeklyH = +this.round(cardData.filter(it => it.at >= biWeekStart && it.at <= weekEnd).map(it => it.durationminutes).reduce((a, b) => a + b, 0) / 60.0);
this.monthlyH = +this.round(cardData.filter(it => it.at >= monthStart && it.at <= monthEnd).map(it => it.durationminutes).reduce((a, b) => a + b, 0) / 60.0);
const sevenDaysStart = subDays(date, 6);
const fourteenDaysStart = subDays(date, 13);
const thirtyDaysStart = subDays(date, 29);

this.sevenDaysDates = [sevenDaysStart, date];
this.fourteenDaysDates = [fourteenDaysStart, date];
this.thirtyDaysDates = [thirtyDaysStart, date];
this.weeklyDates = [weekStart, weekEnd];
this.biWeeklyDates = [biWeekStart, weekEnd];
this.monthlyDates = [monthStart, monthEnd];

this.sevenDaysH = this.hoursBetweenDates(cardData, this.sevenDaysDates[0], this.sevenDaysDates[1]);
this.fourteenDaysH = this.hoursBetweenDates(cardData, this.fourteenDaysDates[0], this.fourteenDaysDates[1]);
this.thirtyDaysH = this.hoursBetweenDates(cardData, this.thirtyDaysDates[0], this.thirtyDaysDates[1]);
this.weeklyH = this.hoursBetweenDates(cardData, this.weeklyDates[0], this.weeklyDates[1]);
this.biWeeklyH = this.hoursBetweenDates(cardData, this.biWeeklyDates[0], this.biWeeklyDates[1]);
this.monthlyH = this.hoursBetweenDates(cardData, this.monthlyDates[0], this.monthlyDates[1]);
}

private hoursBetweenDates(cardData: CardDuration[], start: Date, end: Date) {
return +this.round(cardData.filter(it => startOfDay(it.at) >= startOfDay(start) && startOfDay(it.at) <= startOfDay(end)).map(it => it.durationminutes).reduce((a, b) => a + b, 0) / 60.0);
}

private twoWeekStart(weekStart: Date) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class TimeCardCalendarComponent implements OnInit {

private doLoadTimecards() {
this.loading = true;
return this.api.listTimeCards(subDays(startOfMonth(this.viewDate), 14), endOfMonth(this.viewDate)).pipe(
return this.api.listTimeCards(subDays(startOfMonth(this.viewDate), 30) /* Required for aggregate - time-card-aggregate-stats*/, endOfMonth(this.viewDate)).pipe(
map(it => {
this.loading = false;
return it;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ChangeDetectionStrategy, Component, Input, OnInit, ViewEncapsulation} fr
import {CalendarEvent, CalendarEventAction, CalendarMonthViewDay, CalendarView} from 'angular-calendar';
import {Subject} from 'rxjs';

import {endOfMonth, isSameDay, isSameMonth, parseISO, startOfMonth} from 'date-fns';
import {endOfMonth, isSameDay, isSameMonth, parseISO, startOfMonth, subDays} from 'date-fns';
import {MatDialog} from '@angular/material/dialog';
import {TimeCardEditComponent} from '../time-card-edit/time-card-edit.component';
import {TimeLogUpload} from '../service/timecard-api/time-card-api.service';
Expand Down Expand Up @@ -82,8 +82,8 @@ export class TimeCardReportComponent implements OnInit {
private loadTimeLogs() {
this.loading = true;
const resp = this.ALL === this.selectedUserId.value ?
this.api.getManagedTimelogs([this.project.id], startOfMonth(this.viewDate), endOfMonth(this.viewDate))
: this.api.getManagedTimelogsOfUsers([this.project.id], [+this.selectedUserId.value], startOfMonth(this.viewDate), endOfMonth(this.viewDate));
this.api.getManagedTimelogs([this.project.id], subDays(startOfMonth(this.viewDate), 30) /* Required for aggregate - time-card-aggregate-stats*/, endOfMonth(this.viewDate))
: this.api.getManagedTimelogsOfUsers([this.project.id], [+this.selectedUserId.value], subDays(startOfMonth(this.viewDate), 30) /* Required for aggregate - time-card-aggregate-stats*/, endOfMonth(this.viewDate));

resp
.subscribe(res => {
Expand Down

0 comments on commit e7f2b7f

Please sign in to comment.