Repository navigation
Expand file tree
/
Copy pathCode.js
More file actions
225 lines (210 loc) · 7.4 KB
/
Copy pathCode.js
File metadata and controls
225 lines (210 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* Replio Sheets sync: copies the training sessions of a Replio organization
* into a Google Sheet, once an hour.
*
* Standalone Apps Script project (not bound to the sheet): the API key lives
* in this project's Script Properties and is readable only by people who can
* edit the SCRIPT, not by everyone who can edit the sheet.
*
* Functions to run from the editor:
* refreshSessions fetch and upsert the sessions of the configured projects
* installHourlyTrigger run refreshSessions every hour
* removeTriggers stop the hourly run
* clearSessions empty the Sessions tab (headers kept)
* showConfig log the effective configuration
*/
var SESSIONS_SHEET = "Sessions";
var CONFIG_SHEET = "_config";
var DEFAULT_BASE_URL = "https://api.replio.eu/v1";
var DEFAULT_LOOKBACK_DAYS = 30;
var PAGE_SIZE = 100;
var COLUMNS = [
"session_id",
"project_name",
"trainee_name",
"trainee_email",
"persona_name",
"mode",
"status",
"started_at",
"duration_seconds",
"score",
"evidence_level",
"conversation_outcome",
"scoring_override",
"analyzed",
"trainee_page",
];
function refreshSessions() {
var config = readConfig_();
var since = new Date(Date.now() - config.lookbackDays * 24 * 60 * 60 * 1000);
var fetched = [];
config.projectIds.forEach(function (projectId) {
fetched = fetched.concat(fetchSessions_(config, projectId, since));
});
var written = upsertSessions_(config.sheet, fetched);
Logger.log("Fetched " + fetched.length + " sessions, " + written.added + " added, " + written.updated + " updated.");
}
function installHourlyTrigger() {
removeTriggers();
ScriptApp.newTrigger("refreshSessions").timeBased().everyHours(1).create();
Logger.log("Hourly trigger installed.");
}
function removeTriggers() {
ScriptApp.getProjectTriggers().forEach(function (trigger) {
if (trigger.getHandlerFunction() === "refreshSessions") ScriptApp.deleteTrigger(trigger);
});
}
function clearSessions() {
var sheet = sessionsSheet_(readConfig_().sheet);
if (sheet.getLastRow() > 1) sheet.deleteRows(2, sheet.getLastRow() - 1);
Logger.log("Sessions tab cleared.");
}
function showConfig() {
var config = readConfig_();
Logger.log(
JSON.stringify(
{
baseUrl: config.baseUrl,
sheetId: config.sheet.getId(),
projectIds: config.projectIds,
lookbackDays: config.lookbackDays,
apiKey: config.apiKey ? "set (" + config.apiKey.slice(0, 8) + "...)" : "MISSING",
},
null,
2
)
);
}
// ---------------------------------------------------------------------------
function readConfig_() {
var props = PropertiesService.getScriptProperties();
var apiKey = props.getProperty("REPLIO_API_KEY");
var sheetId = props.getProperty("SHEET_ID");
if (!apiKey) throw new Error("Script property REPLIO_API_KEY is missing.");
if (!sheetId) throw new Error("Script property SHEET_ID is missing.");
var sheet = SpreadsheetApp.openById(sheetId);
var configTab = sheet.getSheetByName(CONFIG_SHEET);
if (!configTab) throw new Error('The spreadsheet has no "' + CONFIG_SHEET + '" tab.');
var values = configTab.getDataRange().getValues();
var projectIds = [];
var lookbackDays = DEFAULT_LOOKBACK_DAYS;
values.forEach(function (row, index) {
var key = String(row[0] || "").trim();
var value = String(row[1] || "").trim();
if (index === 0 && key.toLowerCase() === "key") return;
if (key === "project_id" && value) projectIds.push(value);
if (key === "lookback_days" && Number(value) > 0) lookbackDays = Math.min(365, Math.floor(Number(value)));
});
if (projectIds.length === 0) throw new Error('No "project_id" rows in the ' + CONFIG_SHEET + " tab.");
return {
apiKey: apiKey,
baseUrl: (props.getProperty("REPLIO_BASE_URL") || DEFAULT_BASE_URL).replace(/\/$/, ""),
sheet: sheet,
projectIds: projectIds,
lookbackDays: lookbackDays,
};
}
function fetchSessions_(config, projectId, since) {
var rows = [];
var cursor = null;
var guard = 0;
do {
var url =
config.baseUrl +
"/sessions?project_id=" + encodeURIComponent(projectId) +
"&from=" + encodeURIComponent(since.toISOString()) +
"&limit=" + PAGE_SIZE +
(cursor ? "&cursor=" + encodeURIComponent(cursor) : "");
var response = UrlFetchApp.fetch(url, {
method: "get",
headers: { Authorization: "Bearer " + config.apiKey, Accept: "application/json" },
muteHttpExceptions: true,
});
var status = response.getResponseCode();
if (status === 401) throw new Error("Replio refused the API key (401). Check REPLIO_API_KEY.");
if (status === 403) throw new Error("The API key lacks the sessions:read scope (403).");
if (status !== 200) throw new Error("Replio answered " + status + " for project " + projectId + ".");
var body = JSON.parse(response.getContentText());
rows = rows.concat(body.data || []);
cursor = body.next_cursor || null;
guard += 1;
} while (cursor && guard < 200);
return rows;
}
function upsertSessions_(spreadsheet, sessions) {
var sheet = sessionsSheet_(spreadsheet);
var lastRow = sheet.getLastRow();
var index = {};
if (lastRow > 1) {
var ids = sheet.getRange(2, 1, lastRow - 1, 1).getValues();
ids.forEach(function (row, i) {
if (row[0]) index[String(row[0])] = i + 2;
});
}
var added = 0;
var updated = 0;
var appendBuffer = [];
sessions.forEach(function (session) {
var row = toRow_(session);
var existing = index[session.session_id];
if (existing) {
sheet.getRange(existing, 1, 1, COLUMNS.length).setValues([row]);
updated += 1;
} else {
appendBuffer.push(row);
index[session.session_id] = lastRow + appendBuffer.length + 1;
added += 1;
}
});
if (appendBuffer.length > 0) {
sheet.getRange(lastRow + 1, 1, appendBuffer.length, COLUMNS.length).setValues(appendBuffer);
}
return { added: added, updated: updated };
}
function sessionsSheet_(spreadsheet) {
var sheet = spreadsheet.getSheetByName(SESSIONS_SHEET);
if (!sheet) {
sheet = spreadsheet.insertSheet(SESSIONS_SHEET);
}
if (sheet.getLastRow() === 0) {
sheet.getRange(1, 1, 1, COLUMNS.length).setValues([COLUMNS]);
sheet.setFrozenRows(1);
}
return sheet;
}
function toRow_(session) {
var traineePage = session.trainee_membership_id
? "https://replio.eu/en/dashboard/trainees/" + session.trainee_membership_id
: "";
return [
literal_(session.session_id),
literal_(session.project_name),
literal_(session.trainee_name),
literal_(session.trainee_email),
literal_(session.persona_name),
literal_(session.mode),
literal_(session.status),
session.started_at ? new Date(session.started_at) : "",
numberOrBlank_(session.duration_seconds),
numberOrBlank_(session.score),
literal_(session.evidence_level),
literal_(session.conversation_outcome),
literal_(session.scoring_override),
session.analyzed === true,
traineePage,
];
}
/**
* Every string coming from the API is written as plain text. A value that
* starts with =, +, -, @, a tab or a carriage return would otherwise be read
* by Sheets as a formula, so a trainee or persona name can never execute.
*/
function literal_(value) {
if (value === null || value === undefined) return "";
var text = String(value);
return /^[=+\-@\t\r]/.test(text) ? "'" + text : text;
}
function numberOrBlank_(value) {
return typeof value === "number" && isFinite(value) ? value : "";
}