This code manages the execution and storage of RPC commands as Google Calendar events, allowing for tracking and logging of command history.
npm run import -- "store rpc result"
var assert = require('assert');
var importer = require('../Core');
var createNewEvent = importer.import("create new calendar event");
var ISODateString = importer.import("convert date iso");
var getOauthClient = importer.import("import google calendar api");
var getResult = importer.import("rpc result");
var options = {
calendarId: 'Commands'
};
function updateResultEvent(response, executed, isError, isStarting = false) {
const config = {
start: {
dateTime: ISODateString(new Date(executed.date.getTime()))
},
end: {
dateTime: ISODateString(new Date(executed.date.getTime() + 60 * 30 * 1000))
},
summary: 'Result: ' + executed.id,
description: JSON.stringify(response, null, 4).substr(0, 1000),
colorId: isStarting ? 9 : (isError ? 11 : 10)
}
assert(config.colorId !== 10 || !isError,
'something went wrong with reporting the error ' + JSON.stringify(response, null, 4));
return createNewEvent(config, options).then(() => response);
}
function storeResult(executed, calendar) {
if(calendar) {
options.calendarId = calendar || options.calendarId;
}
if(typeof executed === 'undefined' || executed === null
|| executed.already !== false) {
// skip commands that have already been run
throw new Error('Nothing to do!');
}
console.log('creating rpc event ' + JSON.stringify(Object.keys(executed).reduce((acc, k) => {
acc[k] = (JSON.stringify(executed[k]) || '').substr(0, 200);
return acc;
}, {})));
assert(executed.date, 'There should always be a date associated with the event result.');
var isError = false;
return getOauthClient(options)
// create a new events to store the results
.then(() => updateResultEvent({
time: new Date(),
result: executed.result.filename,
command: executed.command,
parameters: executed.body,
status: 'starting'
}, executed, false, true))
// process the command, this should return a function to be called after event
.then(() => getResult(executed))
.catch(e => {
isError = true;
const resultError = Object.getOwnPropertyNames(e).reduce((alt, key) => {
alt[key] = e[key];
return alt;
}, {});
console.log(resultError);
return resultError;
})
// update event with logged results or tracking
.then(response => updateResultEvent(response, executed, isError || !response))
.catch(e => console.log(e))
}
module.exports = storeResult;
const assert = require('assert');
const { import as importer } = require('../Core');
const { createNewCalendarEvent } = importer('create new calendar event');
const { convertDateIso } = importer('convert date iso');
const { getGoogleCalendarApi } = importer('import google calendar api');
const { getRpcResult } = importer('rpc result');
const DEFAULT_CALENDAR_ID = 'Commands';
/**
* Updates a calendar event with the provided configuration.
* @param {Object} config Event configuration.
* @param {Object} options Calendar options.
* @returns {Promise<Object>} API response.
*/
async function updateResultEvent(config, options) {
const {
start: { dateTime: startTime },
end: { dateTime: endTime },
summary,
description,
colorId,
} = config;
assert(colorId!== 10 ||!config.isError, 'Reporting error failed:'+ JSON.stringify(config, null, 4));
const eventConfig = {
start: { dateTime: convertDateIso(startTime) },
end: { dateTime: convertDateIso(endTime) },
summary,
description: description.substr(0, 1000),
colorId,
};
return createNewCalendarEvent(eventConfig, options);
}
/**
* Stores a command result in a calendar event.
* @param {Object} executed Command result.
* @param {string} [calendar] Optional calendar ID.
* @returns {Promise<Object>} Command result.
*/
async function storeResult(executed, calendar) {
if (calendar) {
options.calendarId = calendar;
}
if (!executed || executed.already === true) {
throw new Error('Nothing to do!');
}
const eventLog = Object.keys(executed).reduce((acc, key) => {
acc[key] = (JSON.stringify(executed[key]) || '').substr(0, 200);
return acc;
}, {});
console.log(`Creating RPC event: ${JSON.stringify(eventLog)}`);
assert(executed.date, 'Missing date in command result.');
const [isError, errorResult] = await Promise.all([getResult(executed).catch(e => ({ isError: true, result: e })), getOauthClient({ calendarId: options.calendarId })]);
const resultEvent = await updateResultEvent({
time: new Date(),
result: executed.result.filename,
command: executed.command,
parameters: executed.body,
status:'starting',
isError: false,
}, options);
if (isError ||!errorResult) {
throw new Error('Error occurred during result processing.');
}
return await updateResultEvent(
{
time: new Date(),
result: errorResult,
command: executed.command,
parameters: executed.body,
status: 'failed',
isError: true,
},
options
).catch(e => console.log(e));
}
module.exports = storeResult;
This code defines functions for managing the execution and storage of RPC commands as Google Calendar events.
Here's a breakdown:
Imports:
options
Object:
calendarId
for interacting with the Google Calendar API.updateResultEvent
Function:
createNewEvent
to create the event in the specified calendar.storeResult
Function:
updateResultEvent
to create the event.In essence, this code handles the scheduling and logging of RPC command executions as Google Calendar events, providing a way to track and manage command history.