google scheduling | | , schedule study time | Search

This code snippet manages and visualizes Google Calendar events, allowing for batch retrieval, event time adjustments, and potential heatmap-based visualization.

Run example

npm run import -- "space out purple event"

space out purple event

var importer = require('../Core')
var {google} = require('googleapis')
var util = require('util')
var {
    correctCalendarId, authorizeCalendar, getDaysEvents, d3Heatmap
} = importer.import("lookup calendar name",
"authorize google calendar",
"days events",
"d3 calendar")

var DAYS = 1200 // plus or minus
var SIMUL = 3
var LENGTH = 7
var options = {
}
          
var now = new Date()
// TODO: batch promises?
function batchPromises(options) {
    const promises = Array.from(Array(Math.ceil(DAYS / SIMUL)).keys()).map((acc, i) => {
        const daysBeforeAfter = i - Math.ceil(DAYS / SIMUL / 2)
        return resolve => Promise.all(Array.from(Array(SIMUL).keys()).map(day => {
            const date = new Date(now)
            date.setDate(date.getDate() + daysBeforeAfter * SIMUL + day)
            return getDaysEvents(date, options)
        }))
            .then(r => {
                setTimeout(() => resolve(r), 100)
            })
    });
    return importer.runAllPromises(promises)
        .then(r => [].concat.apply([], r))
}

function moveEvent(e, i, options) {
    var events
    var newTime = new Date(now)
    newTime.setDate(newTime.getDate() + i)
    newTime.setHours(12, 0, 0)
    return authorizeCalendar(options)
        .then(calendar => events = calendar.events)
        .then(() => util.promisify(events.patch.bind(events))({
            eventId: e.event.id,
            calendarId: options.calendarId,
            auth: options.auth,
            resource: {
                start: { dateTime: new Date(newTime.getTime()) },
                end: { dateTime: new Date(newTime.getTime() + 120 * 60 * 1000) },
            }
        }))
        .then(r => new Promise(resolve => {
            setTimeout(() => resolve(r), 100);
        }))
    // TODO: skip dates using chrono description relative to each date
    // TODO: skip overlap using study sauce algorithm
    // TODO: skip boundaries using a new algorithm for defining inside and outside times
}

function adjustPurpleEvents(options) {
    if(!options) options = {}
    return Promise.resolve()
        .then(() => correctCalendarId(options))
        .then(() => typeof global.listEvents === 'undefined'
              ? batchPromises(options)
              : Promise.resolve(global.listEvents))
        .then(r => {
            global.listEvents = r
            const purpleEvents = r.filter(e => e.event.colorId === '1')
            const promises = purpleEvents.map((e, i) => moveEvent(e, i, options))
            return importer.runAllPromises()
        })
        .then(r => d3Heatmap(r.map((e, i) => ({
            id: e.id,
            start: new Date(e.start.dateTime),
            end: new Date(e.end.dateTime),
        }))))
}

module.exports = adjustPurpleEvents

What the code could have been:

const { google } = require('googleapis');
const { correctCalendarId, authorizeCalendar, getDaysEvents, d3Heatmap } = require('../Core');

const DAYS = 1200;
const SIMUL = 3;
const LENGTH = 7;
const PENDING_TIME = 100; // Time to wait before resolving promises

class CalendarUtil {
  static async batchPromises(options) {
    const promises = [];
    const dates = Array.from(Array(Math.ceil(DAYS / SIMUL)).keys()).map((acc, i) => {
      const daysBeforeAfter = i - Math.ceil(DAYS / SIMUL / 2);
      const startDate = new Date();
      startDate.setDate(startDate.getDate() + daysBeforeAfter * SIMUL);
      return Array.from(Array(SIMUL).keys()).map(day => {
        const date = new Date(startDate);
        date.setDate(date.getDate() + day);
        return getDaysEvents(date, options);
      });
    });
    const results = await Promise.all(promises);
    return results.flat();
  }

  static async moveEvent(event, index, options) {
    const newTime = new Date();
    newTime.setDate(newTime.getDate() + index);
    newTime.setHours(12, 0, 0);
    const calendar = await authorizeCalendar(options);
    const events = calendar.events;
    return events.patch({
      eventId: event.id,
      calendarId: options.calendarId,
      auth: options.auth,
      resource: {
        start: { dateTime: new Date(newTime.getTime()) },
        end: { dateTime: new Date(newTime.getTime() + 120 * 60 * 1000) },
      }
    });
  }

  static async adjustPurpleEvents(options = {}) {
    const calendarId = await correctCalendarId(options);
    const { listEvents } = options;
    let events = listEvents? listEvents : await this.batchPromises(options);
    global.listEvents = events;

    const purpleEvents = events.filter(e => e.colorId === '1');
    const promises = purpleEvents.map((event, index) => this.moveEvent(event, index, options));
    const results = await Promise.all(promises);
    return d3Heatmap(results.map((event, index) => ({
      id: event.id,
      start: new Date(event.start.dateTime),
      end: new Date(event.end.dateTime),
    })));
  }
}

module.exports = CalendarUtil;

This code snippet appears to be designed for managing and visualizing events in a Google Calendar.

Here's a breakdown:

  1. Dependencies:

  2. Configuration:

  3. Batch Event Retrieval:

  4. Event Manipulation:

  5. Event Adjustment Logic:

Purpose:

This code snippet likely aims to: