This code generates a heatmap visualization of Google Calendar events based on specified keywords, calculating the total time spent on those events. It uses the Google Calendar API to fetch events, calculates event durations, and visualizes the results using a heatmap.
npm run import -- "calendar search heatmap"
var importer = require('../Core');
var {
getOauthClient,
d3Heatmap,
listEvents,
correctCalendarId,
sumEvents
} = importer.import("import google calendar api",
"d3 calendar",
"list events",
"lookup calendar name",
"sum a list of events");
var options = {
}
function calendarHeatmap(search, calendar) {
if(calendar) {
options.calendarId = calendar;
}
var total = 0;
return getOauthClient(options)
.then(() => correctCalendarId(options))
.then(() => importer.runAllPromises(search.split('|').map(term => resolve => {
return listEvents({
auth: options.auth,
calendarId: options.calendarId,
q: term
}).then(r => {
console.log(term);
console.log(r.map(e => e.event.summary).slice(0, 10));
const hours = sumEvents(r);
console.log(hours + ' hrs');
total += hours;
return resolve(r);
})
})))
.then(r => [].concat(...r.map((e, i) => ({
id: e.id,
start: new Date(e.event.start.dateTime),
end: new Date(e.event.end.dateTime),
}))))
.then(r => {
console.log(total + ' hrs total');
console.log((total / 41) + ' hrs / week') // subtract from 48 weeks, 2 weeks of mental health, 2 weeks of holidays
return [d3Heatmap(r)]
})
}
module.exports = calendarHeatmap;
if(typeof $ !== 'undefined') {
$.async();
// calendarHeatmap('work')
calendarHeatmap('portal|eloqua|zuora|angular|webpack|renewals|unit test|unit tests|unit testing|selenium|angular2|pricing|jupytangular|notebook|work|sos|study sauce|notebooks|jupyter|cloud|docker|build')
/*
5776.5 hrs total
120.34375 hrs / week
*/
// calendarHeatmap('portal|eloqua|zuora|angular|webpack|renewals|unit test|unit tests|unit testing|selenium|angular2|pricing')
// calendarHeatmap('portal')
/*
1353.75 hrs total
28.203125 hrs / week
*/
.then(r => $.html(r[0]))
.catch(e => $.sendError(e))
}
const { google } = require('googleapis');
const { default: d3Heatmap } = require('d3-heatmap');
const { resolve } = require('path');
// Import required modules and functions
const importer = require('../Core');
const {
getOauthClient,
listEvents,
correctCalendarId,
sumEvents,
d3Heatmap as d3HeatmapFunc,
} = importer.import([
'import google calendar api',
'list events',
'lookup calendar name',
'sum a list of events',
'd3 calendar',
]);
// Define options and default values
const options = {
calendarId: null,
auth: null,
};
/**
* Function to generate a calendar heatmap based on search terms.
*
* @param {string} search - Search terms separated by pipe characters.
* @param {string} calendar - Google calendar ID.
* @returns {Promise} - A promise that resolves with a d3 heatmap object.
*/
async function calendarHeatmap(search, calendar) {
// Set calendar ID if provided
if (calendar) {
options.calendarId = calendar;
}
// Initialize total hours
let total = 0;
// Get OAuth client and correct calendar ID
try {
const oauthClient = await getOauthClient(options);
options.auth = oauthClient;
const correctCalendarIdResult = await correctCalendarId(options);
options.calendarId = correctCalendarIdResult;
// List events for each search term
const eventLists = await Promise.all(
search.split('|').map((term) => listEvents({
auth: options.auth,
calendarId: options.calendarId,
q: term,
})).then((results) => results.map((result) => result.data.items))
);
// Calculate total hours and format event data
const eventData = eventLists.flat().map((event, index) => ({
id: event.id,
start: new Date(event.start.dateTime),
end: new Date(event.end.dateTime),
}));
// Sum hours and calculate hours per week
const hours = sumEvents(eventData);
total += hours;
const hoursPerWeek = (total / 41).toFixed(4); // subtract from 48 weeks, 2 weeks of mental health, 2 weeks of holidays
// Generate d3 heatmap
const d3HeatmapResult = await d3HeatmapFunc(eventData);
// Log total hours and hours per week
console.log(`Total hours: ${total} hrs`);
console.log(`Hours per week: ${hoursPerWeek} hrs/week`);
// Return d3 heatmap result
return d3HeatmapResult;
} catch (error) {
// Log error and return error message
console.error('Error generating calendar heatmap:', error.message);
return error.message;
}
}
// Export calendarHeatmap function
module.exports = calendarHeatmap;
// Usage
if (typeof $!== 'undefined') {
$.async();
calendarHeatmap('portal|eloqua|zuora|angular|webpack|renewals|unit test|unit tests|unit testing|selenium|angular2|pricing|jupytangular|notebook|work|sos|study sauce|notebooks|jupyter|cloud|docker|build')
.then((result) => $.html(result))
.catch((error) => $.sendError(error));
}
This code snippet is designed to generate a heatmap visualization of events from a Google Calendar based on keywords.
Here's a breakdown:
Dependencies: It imports functions for interacting with the Google Calendar API, creating a heatmap, listing events, and handling calendar IDs.
Configuration: It sets up options for interacting with the Google Calendar, including the calendar ID.
calendarHeatmap
Function:
d3Heatmap
function, likely displaying event occurrences over time.Execution:
calendarHeatmap
function with a list of keywords.