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

Improving performance of all time balance calculation #336

Merged
merged 4 commits into from
Aug 7, 2020
Merged
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
84 changes: 42 additions & 42 deletions __tests__/__main__/time-balance.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-undef */
const Store = require('electron-store');
const {
computeAllTimeBalancelUntil,
computeAllTimeBalanceUntil,
getFirstInputInDb
} = require('../../js/time-balance');

Expand Down Expand Up @@ -94,15 +94,15 @@ describe('Time Balance', () => {
expect(getFirstInputInDb()).toBe('2020-6-6-day-begin');
});

test('computeAllTimeBalancelUntil: no input', () => {
test('computeAllTimeBalanceUntil: no input', () => {
const store = new Store();
const waivedWorkdays = new Store({ name: 'waived-workdays' });
store.clear();
waivedWorkdays.clear();
expect(computeAllTimeBalancelUntil(new Date())).resolves.toBe('00:00');
expect(computeAllTimeBalanceUntil(new Date())).resolves.toBe('00:00');
});

test('computeAllTimeBalancelUntil: only regular days', () => {
test('computeAllTimeBalanceUntil: only regular days', () => {
const store = new Store();
const waivedWorkdays = new Store({ name: 'waived-workdays' });
store.clear();
Expand All @@ -112,20 +112,20 @@ describe('Time Balance', () => {
};
store.set(entryEx);
// time balance until thu (excluding thu)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 2))).resolves.toBe('00:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 2))).resolves.toBe('00:00');
// time balance until fri (excluding fri)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 3))).resolves.toBe('-08:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 3))).resolves.toBe('-08:00');
// time balance until sat (excluding sat)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 4))).resolves.toBe('-16:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 4))).resolves.toBe('-16:00');
// time balance until sun (excluding sun)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 5))).resolves.toBe('-16:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 5))).resolves.toBe('-16:00');
// time balance until mon (excluding mon)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 6))).resolves.toBe('-16:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 6))).resolves.toBe('-16:00');
// time balance until tue (excluding tue)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 7))).resolves.toBe('-24:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 7))).resolves.toBe('-24:00');
});

test('computeAllTimeBalancelUntil: only regular days (with overtime)', () => {
test('computeAllTimeBalanceUntil: only regular days (with overtime)', () => {
const store = new Store();
const waivedWorkdays = new Store({ name: 'waived-workdays' });
store.clear();
Expand All @@ -135,14 +135,14 @@ describe('Time Balance', () => {
};
store.set(entryEx);
// time balance until thu (excluding thu)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 2))).resolves.toBe('01:30');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 2))).resolves.toBe('01:30');
// time balance until fri (excluding fri)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 3))).resolves.toBe('-06:30');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 3))).resolves.toBe('-06:30');
// time balance until sat (excluding sat)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 4))).resolves.toBe('-14:30');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 4))).resolves.toBe('-14:30');
});

test('computeAllTimeBalancelUntil: only regular days (with undertime)', () => {
test('computeAllTimeBalanceUntil: only regular days (with undertime)', () => {
const store = new Store();
const waivedWorkdays = new Store({ name: 'waived-workdays' });
store.clear();
Expand All @@ -152,14 +152,14 @@ describe('Time Balance', () => {
};
store.set(entryEx);
// time balance until thu (excluding thu)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 2))).resolves.toBe('-01:45');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 2))).resolves.toBe('-01:45');
// time balance until fri (excluding fri)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 3))).resolves.toBe('-09:45');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 3))).resolves.toBe('-09:45');
// time balance until sat (excluding sat)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 4))).resolves.toBe('-17:45');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 4))).resolves.toBe('-17:45');
});

test('computeAllTimeBalancelUntil: only regular days (with mixed time)', () => {
test('computeAllTimeBalanceUntil: only regular days (with mixed time)', () => {
const store = new Store();
const waivedWorkdays = new Store({ name: 'waived-workdays' });
store.clear();
Expand All @@ -171,14 +171,14 @@ describe('Time Balance', () => {
};
store.set(entryEx);
// time balance until thu (excluding thu)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 2))).resolves.toBe('-01:45');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 2))).resolves.toBe('-01:45');
// time balance until fri (excluding fri)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 3))).resolves.toBe('-00:30');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 3))).resolves.toBe('-00:30');
// time balance until sat (excluding sat)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 4))).resolves.toBe('-02:15');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 4))).resolves.toBe('-02:15');
});

test('computeAllTimeBalancelUntil: missing entries', () => {
test('computeAllTimeBalanceUntil: missing entries', () => {
const store = new Store();
const waivedWorkdays = new Store({ name: 'waived-workdays' });
store.clear();
Expand All @@ -189,16 +189,16 @@ describe('Time Balance', () => {
};
store.set(entryEx);
// time balance until thu (excluding thu)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 2))).resolves.toBe('00:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 2))).resolves.toBe('00:00');
// time balance until fri (excluding fri)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 3))).resolves.toBe('-08:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 3))).resolves.toBe('-08:00');
// time balance until sat (excluding sat)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 4))).resolves.toBe('-08:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 4))).resolves.toBe('-08:00');
// time balance until sun (excluding sun)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 5))).resolves.toBe('-08:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 5))).resolves.toBe('-08:00');
});

test('computeAllTimeBalancelUntil: with waived days', () => {
test('computeAllTimeBalanceUntil: with waived days', () => {
const store = new Store();
const waivedWorkdays = new Store({ name: 'waived-workdays' });
store.clear();
Expand All @@ -213,14 +213,14 @@ describe('Time Balance', () => {
};
waivedWorkdays.set(waivedEntries);
// time balance until thu (excluding thu)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 2))).resolves.toBe('00:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 2))).resolves.toBe('00:00');
// time balance until fri (excluding fri)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 3))).resolves.toBe('00:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 3))).resolves.toBe('00:00');
// time balance until sat (excluding sat)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 4))).resolves.toBe('00:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 4))).resolves.toBe('00:00');
});

test('computeAllTimeBalancelUntil: with waived days 2', () => {
test('computeAllTimeBalanceUntil: with waived days 2', () => {
const store = new Store();
const waivedWorkdays = new Store({ name: 'waived-workdays' });
store.clear();
Expand All @@ -235,16 +235,16 @@ describe('Time Balance', () => {
};
waivedWorkdays.set(waivedEntries);
// time balance until wed (excluding wed)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 8))).resolves.toBe('00:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 8))).resolves.toBe('00:00');
// time balance until tue (excluding tue)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 9))).resolves.toBe('00:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 9))).resolves.toBe('00:00');
// time balance until fri (excluding fri)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 10))).resolves.toBe('00:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 10))).resolves.toBe('00:00');
// time balance until sat (excluding sat)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 11))).resolves.toBe('00:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 11))).resolves.toBe('00:00');
});

test('computeAllTimeBalancelUntil: with waived days (not full)', () => {
test('computeAllTimeBalanceUntil: with waived days (not full)', () => {
const store = new Store();
const waivedWorkdays = new Store({ name: 'waived-workdays' });
store.clear();
Expand All @@ -259,14 +259,14 @@ describe('Time Balance', () => {
};
waivedWorkdays.set(waivedEntries);
// time balance until tue (excluding tue)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 2))).resolves.toBe('00:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 2))).resolves.toBe('00:00');
// time balance until fri (excluding fri)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 3))).resolves.toBe('-06:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 3))).resolves.toBe('-06:00');
// time balance until sat (excluding sat)
expect(computeAllTimeBalancelUntil(new Date(2020, 6, 4))).resolves.toBe('-06:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 6, 4))).resolves.toBe('-06:00');
});

test('computeAllTimeBalancelUntil: target date in the past of entries', () => {
test('computeAllTimeBalanceUntil: target date in the past of entries', () => {
const store = new Store();
const waivedWorkdays = new Store({ name: 'waived-workdays' });
store.clear();
Expand All @@ -280,6 +280,6 @@ describe('Time Balance', () => {
'2020-07-02': { reason: 'Waiver', hours: '02:00' }, // tue
};
waivedWorkdays.set(waivedEntries);
expect(computeAllTimeBalancelUntil(new Date(2020, 5, 1))).resolves.toBe('00:00');
expect(computeAllTimeBalanceUntil(new Date(2020, 5, 1))).resolves.toBe('00:00');
});
});
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1.5.6 (in development)
------------------------------------------------------------------------
- Fix: Fixed behavior of calendar when moving to next/previous month when current day is in the range of 29-31.
- Fix: [#334] Improving performance of overall balance calculation and fixing balance target date after month change
- Enhancement: [#328] Swap position for overall and month balance on day view

Who built 1.5.6:
Expand Down
34 changes: 29 additions & 5 deletions js/classes/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const {
sendWaiverDay,
displayWaiverWindow
} = require('../workday-waiver-aux.js');
const { computeAllTimeBalancelUntilAsync } = require('../time-balance.js');
const { computeAllTimeBalanceUntilAsync } = require('../time-balance.js');

// Global values for calendar
const store = new Store();
Expand Down Expand Up @@ -85,19 +85,29 @@ class Calendar {
this._draw();
}

_updateAllTimeBalance() {
var targetYear = this._getCalendarYear(),
/**
* Returns a date object for which the all time balance will be calculated.
* If current month, returns the actual day. If not, first day of following month.
* @return {Date}
*/
_getTargetDayForAllTimeBalance() {
let targetYear = this._getCalendarYear(),
targetMonth = this._getCalendarMonth(),
// If we are not displaying the current month we need to compute the balance including the
// last day of the month. To do so we move to the first day of the following month
isCurrentMonth = targetYear === this._getTodayYear() && targetMonth === this._getTodayMonth(),
targetDate = isCurrentMonth ?
new Date(targetYear, targetMonth, this._getCalendarDate()) :
new Date(targetYear, targetMonth, this._getTodayDate()) :
araujoarthur0 marked this conversation as resolved.
Show resolved Hide resolved
new Date(targetYear, targetMonth + 1, 1);
if (isCurrentMonth && this._getCountToday()) {
targetDate.setDate(targetDate.getDate() + 1);
}
computeAllTimeBalancelUntilAsync(targetDate).then(balance => {
return targetDate;
}

_updateAllTimeBalance() {
const targetDate = this._getTargetDayForAllTimeBalance();
computeAllTimeBalanceUntilAsync(targetDate).then(balance => {
var balanceElement = $('#overall-balance');
if (balanceElement) {
balanceElement.val(balance);
Expand Down Expand Up @@ -1255,6 +1265,20 @@ class DayCalendar extends Calendar {

this._updateLeaveBy();
}

/**
* Returns a date object for which the all time balance will be calculated.
* For DayCalendar, it's the day of CalendarDate => the day being displayed.
* If "count_today" is active, the following day.
* @return {Date}
*/
_getTargetDayForAllTimeBalance() {
let targetDate = new Date(this._getCalendarYear(), this._getCalendarMonth(), this._getCalendarDate());
if (this._getCountToday()) {
targetDate.setDate(targetDate.getDate() + 1);
}
return targetDate;
}
}

module.exports = {
Expand Down
Loading