Skip to content

Instantly share code, notes, and snippets.

@mmodrow
Last active March 25, 2022 09:57
Show Gist options
  • Save mmodrow/fab01196cec0cfa49c988374ed1691b5 to your computer and use it in GitHub Desktop.
Save mmodrow/fab01196cec0cfa49c988374ed1691b5 to your computer and use it in GitHub Desktop.
Get days in certain location from a time camp location report
/* *************************************************************************
* Filter TimeCamp Location Report for specific Locations (e.g. to check
* home office days vs. on premise days).
* *************************************************************************
* Goto https://app.timecamp.com/app#/location
* Filter the desired time span etc.
* Inspect result of https://app.timecamp.com/location/index in your browser
* dev tools network analysis.
* ************************************************************************* */
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
function getDates(startDate, stopDate) {
var dateArray = new Array();
var currentDate = startDate;
while (currentDate <= stopDate) {
dateArray.push(new Date (currentDate));
currentDate = currentDate.addDays(1);
}
return dateArray;
}
var officeLocation = "GoogleMaps-tracking-accurate string to identify your office address";
var homeLocation = "GoogleMaps-tracking-accurate string to identify your home address";
var response = response ?? {}; // insert response from the ajax call in the header - can be several thousands of lines long.
var locationHistory = response.locationHistory;
var datesInOffice = locationHistory.filter(x => x.end_location_name.contains(officeLocation))
.map(x => x.date)
.filter(onlyUnique);
var datesInHomeOffice = locationHistory.filter(x => x.end_location_name.contains(homeLocation))
.map(x => x.date)
.filter(onlyUnique)
.filter(x=>datesInOffice.indexOf(x) === -1);
var datesWithMigration = locationHistory.filter(x => x.end_location_name.contains(homeLocation))
.map(x => x.date)
.filter(onlyUnique)
.filter(x=>datesInOffice.indexOf(x) != -1);
var allDates = locationHistory.map(x => x.date).filter(onlyUnique);
var allDateUnixTimestamps = allDates.map(x => Date.parse(x));
var latestDate = new Date(allDateUnixTimestamps[0]);
var earliestDate = new Date(allDateUnixTimestamps[allDateUnixTimestamps.length - 1]);
var allDatesInRange = getDates(earliestDate, latestDate);
var daysWithoutTrackedWorkLocation = allDatesInRange.map(x=>x.toISOString().split('T')[0])
.filter(x=>datesInOffice.indexOf(x) === -1)
.filter(x=>datesInHomeOffice.indexOf(x) === -1)
// filtering out weekends
.filter(x => [...new Array(5).keys()].contains(new Date(x).getDay() - 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment