-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add optimise-services-and-exceptions β
π
part of #10
- Loading branch information
Showing
6 changed files
with
327 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict' | ||
|
||
const {join: pathJoin} = require('path') | ||
const readCsv = require('../read-csv') | ||
const optimiseServicesAndExceptions = require('../optimise-services-and-exceptions') | ||
|
||
const fixtureDir = pathJoin(__dirname, '..', 'test', 'fixtures', 'optimise-services-and-exceptions') | ||
const readFile = (file) => { | ||
return readCsv(pathJoin(fixtureDir, file + '.csv')) | ||
} | ||
|
||
;(async () => { | ||
const optimisedSvcs = optimiseServicesAndExceptions(readFile, 'Europe/Berlin') | ||
for await (const [id, changed, service, exceptions] of optimisedSvcs) { | ||
if (changed) { | ||
console.log(id, 'changed!') | ||
console.log('service:', service) | ||
console.log('exceptions:', exceptions) | ||
} else { | ||
console.log(id, 'unchanged!', id) | ||
} | ||
} | ||
})() | ||
.catch((err) => { | ||
console.error(err) | ||
process.exit(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
'use strict' | ||
|
||
const readServicesAndExceptions = require('./read-services-and-exceptions') | ||
const datesBetween = require('./lib/dates-between') | ||
|
||
const WEEKDAYS = [ | ||
// JS Date ordering | ||
'sunday', | ||
'monday', | ||
'tuesday', | ||
'wednesday', | ||
'thursday', | ||
'friday', | ||
'saturday', | ||
] | ||
|
||
const noWeekday = { | ||
monday: false, | ||
tuesday: false, | ||
wednesday: false, | ||
thursday: false, | ||
friday: false, | ||
saturday: false, | ||
sunday: false, | ||
} | ||
|
||
const formatDate = isoDate => isoDate.split('-').join('') | ||
|
||
const optimiseServicesAndExceptions = async function* (readFile, timezone, filters = {}, opt = {}) { | ||
const weekdaysMap = new Map() | ||
const svcsAndExceptions = readServicesAndExceptions(readFile, timezone, filters, { | ||
...opt, | ||
exposeStats: true, | ||
weekdaysMap, | ||
}) | ||
|
||
for await (let [serviceId, dates, svc, nrOfDates, removedDates] of svcsAndExceptions) { | ||
const nrOfDefaultDates = [] | ||
for (let wd = 0; wd < WEEKDAYS.length; wd++) { | ||
const defaultDates = datesBetween( | ||
svc.start_date, svc.end_date, | ||
{...noWeekday, [WEEKDAYS[wd]]: true}, | ||
timezone, | ||
weekdaysMap, | ||
) | ||
nrOfDefaultDates[wd] = defaultDates.length | ||
} | ||
|
||
let changed = false | ||
svc = {...svc} | ||
for (let wd = 0; wd < 7; wd++) { | ||
// todo: make this customisable | ||
const flag = nrOfDates[wd] > nrOfDefaultDates[wd] / 2 | 0 ? '1' : '0' | ||
changed = changed || (flag !== svc[WEEKDAYS[wd]]) | ||
svc[WEEKDAYS[wd]] = flag | ||
} | ||
|
||
const exceptions = [] | ||
for (const date of dates) { | ||
const wd = weekdaysMap.get(date) | ||
if (svc[WEEKDAYS[wd]] === '1') continue | ||
exceptions.push({ | ||
service_id: serviceId, | ||
date: formatDate(date), | ||
exception_type: '1', // added | ||
}) | ||
} | ||
|
||
for (const date of removedDates) { | ||
const wd = weekdaysMap.get(date) | ||
if (svc[WEEKDAYS[wd]] === '0') continue | ||
exceptions.push({ | ||
service_id: serviceId, | ||
date: formatDate(date), | ||
exception_type: '2', // removed | ||
}) | ||
} | ||
|
||
// todo [breaking]: remove serviceId (idx 0), move svc first, | ||
// follow read-services-and-exceptions here | ||
yield [serviceId, changed, svc, exceptions] | ||
} | ||
} | ||
|
||
module.exports = optimiseServicesAndExceptions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.