google calendar graphs | test google calendar search swimlane | Cell 4 | Search

This code analyzes Google Calendar events based on provided search queries, calculates the frequency of events for each query, and visualizes the results as a pie chart.

Run example

npm run import -- "test google calendar search pie chart"

test google calendar search pie chart

var importer = require('../Core');

var options = {};
var {
    listEvents, sumEvents, d3PieChart
} = importer.import("list events",
"import google calendar api",
"sum a list of events",
"d3 pie chart");

function calendarSearchToPieChart(searches) {
    return importer.runAllPromises(searches
        .map(s => (resolve) => listEvents({
            auth: options.auth,
            q: s
        })
            .then(r => {
                //console.log(s);
                //console.log(r.map(e => e.event.summary));
                resolve({label: s, value: sumEvents(r)})
            })
            .catch(e => console.log(e))))
        .then(r => d3PieChart(r))
        .catch(e => console.log(e))
}

$.async();
calendarSearchToPieChart([
    'study sauce',
    'portal',
    'renewal',
    'work on sos',
    'jupyter',
    'jupytangular',
    'unit tests',
    'selenium',
    'angular',
    'mind spree',
    '"c#"',
    'docker'
])
    .then(r => $.svg(r))
    .catch(e => $.sendError(e))

What the code could have been:

// Import required modules
import importer from '../Core';
import $ from 'jquery-async';

// Define default options
const defaultOptions = {
  auth: null,
};

// Define the main function
function calendarSearchToPieChart(searches, options = {}) {
  // Merge default options with passed options
  const mergedOptions = {...defaultOptions,...options };

  // Check if required functions are imported
  const {
    listEvents, sumEvents, d3PieChart,
  } = importer.import([
    'list events',
    'import google calendar api',
   'sum a list of events',
    'd3 pie chart',
  ]);

  // Map searches to promises
  const promises = searches.map((search) => (
    new Promise((resolve) => {
      listEvents({
        auth: mergedOptions.auth,
        q: search,
      })
       .then((response) => {
          resolve({ label: search, value: sumEvents(response) });
        })
       .catch((error) => {
          console.error(`Error processing search: ${search}`, error);
          resolve({ label: search, value: 0 });
        });
    })
  ));

  // Run all promises and then create pie chart
  return Promise.all(promises)
   .then((results) => d3PieChart(results))
   .catch((error) => {
      console.error('Error creating pie chart', error);
      return [];
    });
}

// Run calendar search to pie chart with searches
calendarSearchToPieChart([
 'study sauce',
  'portal',
 'renewal',
  'work on sos',
  'jupyter',
  'jupytangular',
  'unit tests',
 'selenium',
  'angular',
 'mind spree',
  '"c#"',
  'docker',
])
 .then((pieChart) => $.svg(pieChart))
 .catch((error) => $.sendError(error));

This code snippet fetches data from a Google Calendar API, summarizes events based on search queries, and generates a pie chart visualizing the results.

Here's a breakdown:

  1. Imports:

  2. calendarSearchToPieChart Function:

  3. Execution:

Let me know if you have any other code snippets you'd like me to explain!