/
task_status_sl.ts
217 lines (198 loc) · 6.78 KB
/
task_status_sl.ts
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
/**
* Provide a service to poll the status of a Map/Reduce task.
*
* @NScriptType Suitelet
* @NApiVersion 2.x
* @NAmdConfig /SuiteScripts/amdconfig.json
*/
import { EntryPoints } from 'N/types';
import { ServerResponse } from 'N/https';
import { Status, SuiteletResponse, TaskStatusString, MapReduceStageString } from './lib/task_status_types';
import * as task from 'N/task';
import * as search from 'N/search';
import * as file from 'N/file';
import * as serverWidget from 'N/ui/serverWidget';
import * as runtime from 'N/runtime';
import * as url from 'N/url';
/**
* Provide a service to poll the status of a Map/Reduce task.
*
* @param ctx
* query parameters
* - taskid: Which task to check. mandatory
* - query: Pass any value to get a snapshot of the task status as json.
* omit to get a UI that displays the task status.
* - ifrmcntnr: Pass any value to hide the NetSuite navbar.
* NetSuite has some behind the scenes behavior with this parameter
* to make the suitelet ui suitable to be embedded in an iframe.
*
*/
export function onRequest(ctx: EntryPoints.Suitelet.onRequestContext) {
const taskId = ctx.request.parameters.taskId || ctx.request.parameters.taskid;
const query = !!ctx.request.parameters.query;
const isEmbedded = !!ctx.request.parameters.ifrmcntnr;
try {
if (typeof taskId !== 'string' || taskId === '') {
throw new Error(`Invalid task id: ${taskId}`);
}
if (!query) {
const form = createForm(taskId, isEmbedded);
ctx.response.writePage({ pageObject: form });
return;
}
const status = task.checkStatus({ taskId });
if (!isMapReduceScriptTaskStatus(status)) {
throw new Error('Unsupported task type');
}
// these are null when the taskId was not a valid taskId
if (!status.scriptId || !status.deploymentId) {
throw new Error(`Invalid task id: ${taskId}`);
}
respondWith(ctx.response, { success: true, status: snapshot(status) });
} catch (exc) {
respondWith(ctx.response, { success: false, message: exc.toString() });
}
}
function respondWith(resp: ServerResponse, body: SuiteletResponse) {
resp.setHeader({ name: 'content-type', value: 'application/json' });
resp.write(JSON.stringify(body));
}
function isMapReduceScriptTaskStatus(
status: ReturnType<typeof task.checkStatus>
): status is task.MapReduceScriptTaskStatus {
return status.hasOwnProperty('stage');
}
function snapshot(status: task.MapReduceScriptTaskStatus): Status {
// status.getPercentageCompleted might throw an exception for completed tasks
let pct = 0;
try { pct = status.getPercentageCompleted(); } catch { }
return {
// netsuite docs say these "might be" numbers
scriptId: String(status.scriptId),
deploymentId: String(status.deploymentId),
status: N_TaskStatusToString(status.status),
stage: N_TaskMapReduceStageToString(status.stage),
stagePercentComplete: pct,
size: status.getCurrentTotalSize(),
map: {
pending: status.getPendingMapCount(),
pendingBytes: status.getPendingMapSize(),
total: status.getTotalMapCount()
},
reduce: {
pending: status.getPendingReduceCount(),
pendingBytes: status.getPendingReduceSize(),
total: status.getTotalReduceCount()
},
summarize: {
pending: status.getPendingOutputCount(),
pendingBytes: status.getPendingOutputSize(),
total: status.getTotalOutputCount()
}
};
}
/**
* Client scripts cannot import N/task.
* Provide constant strings known at compile time that correspond to the keys of N/task.TaskStatus.
* @param status
*/
function N_TaskStatusToString(status: task.TaskStatus): TaskStatusString {
const map: {
[x: string]: TaskStatusString
[x: number]: TaskStatusString
} = {
[task.TaskStatus.PENDING]: 'PENDING',
[task.TaskStatus.PROCESSING]: 'PROCESSING',
[task.TaskStatus.FAILED]: 'FAILED',
[task.TaskStatus.COMPLETE]: 'COMPLETE',
};
return map[status];
}
/**
* Client scripts cannot import N/task.
* Provide constant strings known at compile time that correspond to the keys of N/task.MapReduceStage.
* @param stage
*/
function N_TaskMapReduceStageToString(stage: task.MapReduceStage): MapReduceStageString {
const map: {
[x: string]: MapReduceStageString
[x: number]: MapReduceStageString
} = {
[task.MapReduceStage.GET_INPUT]: 'GET_INPUT',
[task.MapReduceStage.MAP]: 'MAP',
[task.MapReduceStage.REDUCE]: 'REDUCE',
[task.MapReduceStage.SHUFFLE]: 'SHUFFLE',
[task.MapReduceStage.SUMMARIZE]: 'SUMMARIZE'
};
return map[stage] || null;
}
/**
* @param taskId
* @param isEmbedded
*/
function createForm(taskId: string, isEmbedded: boolean) {
const form = serverWidget.createForm({
title: 'Task Status',
hideNavBar: isEmbedded
});
// when you set form.clientScriptModulePath the server attempts to evaluate scripts that are
// statically imported (i.e. in define([...])) by the client script
// client script should take care not to statically import scripts that fail to evaluate on the server
form.clientScriptModulePath = './task_status_sl_cl';
// put the taskId and the url of this suitelet on the form for use in the attached client script
const taskField = form.addField({
id: 'custpage_taskid',
type: serverWidget.FieldType.TEXT,
label: 'Task',
});
taskField.defaultValue = taskId;
if (isEmbedded) {
taskField.updateDisplayType({ displayType: serverWidget.FieldDisplayType.HIDDEN });
} else {
taskField.updateDisplayType({ displayType: serverWidget.FieldDisplayType.INLINE });
taskField.updateLayoutType({ layoutType: serverWidget.FieldLayoutType.OUTSIDEABOVE });
}
const suiteletURLField = form.addField({
id: 'custpage_suitelet_url',
type: serverWidget.FieldType.TEXT,
label: 'Suitelet URL'
});
suiteletURLField.defaultValue = suiteletURL();
suiteletURLField.updateDisplayType({ displayType: serverWidget.FieldDisplayType.HIDDEN });
const html = file.load({ id: `${suiteletFolderPath()}/lib/task_status.html`}).getContents()
const taskStatusWidget = form.addField({
id: 'custpage_react_root',
type: serverWidget.FieldType.INLINEHTML,
label: 'react'
});
taskStatusWidget.defaultValue = html;
return form;
}
/**
* Get the url of the currently executing suitelet.
*/
function suiteletURL() {
const script = runtime.getCurrentScript();
return url.resolveScript({
scriptId: script.id,
deploymentId: script.deploymentId
});
}
/**
* Get the folder path of the currently executing script file.
*/
function suiteletFolderPath() {
const script = runtime.getCurrentScript();
const results = search.create({
type: 'script',
filters: ['scriptid', 'is', script.id],
columns: ['scriptfile']
}).run().getRange({ start: 0, end: 1 });
if (results.length === 0) {
throw new Error('Failed to look up script file.');
}
const scriptFileId = results[0].getValue('scriptfile') as string;
const scriptFilePath = file.load({ id: scriptFileId }).path;
const lastSlash = scriptFilePath.lastIndexOf('/');
return scriptFilePath.substring(0, lastSlash);
}