Skip to content

Instantly share code, notes, and snippets.

@pengelbrecht
Last active August 29, 2015 14:07
Show Gist options
  • Save pengelbrecht/253d4966e74b61dc06ff to your computer and use it in GitHub Desktop.
Save pengelbrecht/253d4966e74b61dc06ff to your computer and use it in GitHub Desktop.
wise words...
// Global variables
var BASEURI = 'https://app.firmafon.dk/api/v1';
var OPENTIME, CLOSETIME;
var SS = SpreadsheetApp.getActiveSpreadsheet();
function readConfig() {
OPENTIME = new Date();
OPENTIME.setHours(SS.getRangeByName("OpenHour").getValue());
OPENTIME.setMinutes(SS.getRangeByName("OpenMinute").getValue());
OPENTIME.setSeconds(0);
OPENTIME.setMilliseconds(0);
CLOSETIME = new Date();
CLOSETIME.setHours(SS.getRangeByName("CloseHour").getValue());
CLOSETIME.setMinutes(SS.getRangeByName("CloseMinute").getValue());
CLOSETIME.setSeconds(0);
CLOSETIME.setMilliseconds(0);
}
function getIncomingCallStats(from, to, number, userKey) {
var thisCall, open;
var callTime = new Date();
var fromstr = from.toISOString();
var tostr = to.toISOString();
var stats = new Object();
var missed = 0, answered = 0, voicemail = 0, answeredDuration = 0, closed = 0;
var options = {
"method" : "get",
"headers" : {"Accept" : "application/json", "Firmafon-User-Key": userKey}
};
var callLogParams = {
"number" : "45" + number,
"direction" : "incoming",
"from" : fromstr,
"to" : tostr,
"limit" : 999999
}
var URL = BuildURL(BASEURI+"/call_logs", callLogParams);
var response = Utilities.jsonParse(UrlFetchApp.fetch(URL, options).getContentText());
for (var i = 0; i < response.call_logs.length; i++) {
thisCall = response.call_logs[i];
if (thisCall.direction != "incoming") continue;
callTime = getDateFromISOString(thisCall.started_at);
open = isOpen(callTime, OPENTIME, CLOSETIME)
if (!open) {
closed++;
continue;
}
if (thisCall.status == "answered") {
answered++;
answeredDuration += thisCall.duration;
}
if (thisCall.status == "missed") missed++;
if (thisCall.status == "voicemail") voicemail++;
}
stats.missed = missed; stats.answered = answered; stats.voicemail = voicemail; stats.answeredDurationMins = answeredDuration/60; stats.closed = closed, stats.open = answered+missed+voicemail;
stats.avgDurationMins = (answeredDuration/60)/answered; stats.answerRate = answered/stats.open;
if (answered == 0) stats.avgDurationMins = 0;
if (stats.open == 0) stats.answerRate = 0;
return(stats);
}
function loadHistorical() {
readConfig();
var userKey = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("APIKey").getValue();
var number = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("Number").getValue().toString();
var endDate = new Date();
var days = 366;
var beginDate = new Date(endDate.getTime() - ((days - 1) * 24 * 60 * 60 * 1000));
for (var i = 0; i < days; i++) {
from = new Date(beginDate.getTime() + (i * 24 * 60 * 60 * 1000));
from.setHours(0); from.setMinutes(0); from.setSeconds(0), from.setMilliseconds(0);
to = new Date(from.getTime() + (24 * 60 * 60 * 1000));
var stats = getIncomingCallStats(from, to, number, userKey)
var statsarr = objectToArray(stats);
statsarr.unshift(from);
prependRow(SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Daily Data"), statsarr)
}
}
function updateDaily() {
// Adds _yesterdays_ calls to 'Daily Data' sheet. To be triggered early am
readConfig();
var userKey = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("APIKey").getValue();
var number = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("Number").getValue().toString();
var from = new Date(); // today
from = new Date(from.getTime() - (24 * 60 * 60 * 1000)); // yesterday
from.setHours(0); from.setMinutes(0); from.setSeconds(0), from.setMilliseconds(0);
var to = new Date(from.getTime() + (24 * 60 * 60 * 1000));
var stats = getIncomingCallStats(from, to, number, userKey)
var statsarr = objectToArray(stats);
statsarr.unshift(from);
prependRow(SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Daily Data"), statsarr)
}
function updateToday() {
// Adds todays stats until now, also last weeks stats for same time interval and weekday. To be triggered regularly
readConfig();
var userKey = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("APIKey").getValue();
var number = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("Number").getValue().toString();
var to = new Date();
var from = new Date();
from.setHours(0); from.setMinutes(0); from.setSeconds(0), from.setMilliseconds(0);
var fromLastWeek = new Date(from.getTime() - (7 * 24 * 60 * 60 * 1000));
var toLastWeek = new Date(to.getTime() - (7 * 24 * 60 * 60 * 1000));
var stats = getIncomingCallStats(from, to, number, userKey)
var statsarr = objectToArray(stats);
statsarr.unshift(from);
var statsLastWeek = getIncomingCallStats(fromLastWeek, toLastWeek, number, userKey)
var statsarrLastWeek = objectToArray(statsLastWeek);
statsarrLastWeek.unshift(fromLastWeek);
replaceRow(SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Today Data"), 2, statsarr)
replaceRow(SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Today Data"), 3, statsarrLastWeek)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment