@datagrok/compute 0.6.3 → 0.6.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@datagrok/compute",
3
3
  "friendlyName": "Compute",
4
- "version": "0.6.3",
4
+ "version": "0.6.7",
5
5
  "description": "",
6
6
  "dependencies": {
7
7
  "cash-dom": "latest",
@@ -10,6 +10,7 @@
10
10
  "dayjs": "latest",
11
11
  "exceljs": "^4.3.0",
12
12
  "file-saver": "^2.0.5",
13
+ "rxjs": "^7.4.0",
13
14
  "ts-loader": "latest",
14
15
  "typescript": "latest",
15
16
  "wu": "latest"
@@ -49,7 +49,7 @@ export async function exportFuncCall(call: DG.FuncCall) {
49
49
  if (scalarInputs.length) {
50
50
  const inputScalarsSheet = exportWorkbook.addWorksheet('Input scalars');
51
51
  scalarInputs.forEach((scalarInput) => {
52
- inputScalarsSheet.addRow([scalarInput.name, call.inputs[scalarInput.name]]);
52
+ inputScalarsSheet.addRow([scalarInput.options['caption'] || scalarInput.name, call.inputs[scalarInput.name], scalarInput.options['units'] || '']);
53
53
  });
54
54
  }
55
55
 
@@ -64,186 +64,122 @@ export async function exportFuncCall(call: DG.FuncCall) {
64
64
  }
65
65
  });
66
66
 
67
- // UWAGA: very fragile solution
68
- const funcView = document.getElementsByClassName('grok-view grok-view-func ui-box')[0];
69
- const resultView = funcView.getElementsByClassName('d4-tab-host ui-box')[0];
70
- if (resultView) {
71
- const tabs = resultView.getElementsByClassName('d4-tab-header-stripe')[0].children;
72
- const selectedByUser = resultView.getElementsByClassName('d4-tab-header selected')[0];
73
- for (let i=0; i< tabs.length; i++) {
74
- (tabs[i] as HTMLElement).click();
75
- await new Promise((r) => setTimeout(r, 150));
67
+ const addToFile = (imageId: number, visibleTitle: string, width: number, height: number, plotCount?: number) => {
68
+ if (imageId === null || imageId === undefined) return;
76
69
 
77
- const titleDivs = document.getElementsByClassName('grok-func-results-header');
78
- if (!titleDivs.length) continue;
79
- let skipNext = false;
70
+ const worksheet = exportWorkbook.getWorksheet(getSheetName(visibleTitle, DIRECTION.INPUT)) ||
71
+ exportWorkbook.getWorksheet(getSheetName(visibleTitle, DIRECTION.OUTPUT));
72
+ if (worksheet) {
73
+ const columnForImage = (
74
+ ((call.inputs[realNames[visibleTitle]] ||
75
+ call.outputs[realNames[visibleTitle]]) as DG.DataFrame)
76
+ .columns as DG.ColumnList)
77
+ .length + 1 + (plotCount ? plotCount * (Math.ceil(width / 64.0) + 1): 0);
78
+ worksheet.addImage(imageId, {
79
+ tl: {col: columnForImage, row: 0},
80
+ ext: {width, height},
81
+ });
82
+ } else {
83
+ const newWorksheet = exportWorkbook.addWorksheet(`Output - Plot - ${visibleTitle}`);
84
+ newWorksheet.addImage(imageId, {
85
+ tl: {col: 0, row: 0},
86
+ ext: {width, height},
87
+ });
88
+ }
89
+ };
80
90
 
81
- for (let i = 0; i < titleDivs.length; i++) {
82
- if (skipNext) {
83
- skipNext = false;
84
- continue;
85
- }
86
- const titleDiv = titleDivs[i];
87
- if (titleDiv.parentElement?.style.display === 'none') continue;
91
+ const exportImage = async (imageDiv: Element, visibleTitle: string) => {
92
+ const regex = /background-image: url\(.*\)/;
93
+ const urlAttr = imageDiv.outerHTML.match(regex)?.[0];
94
+ if (!urlAttr) return;
95
+ const url = urlAttr.substring(23, urlAttr?.length - 2);
96
+ const pic = (await(await fetch(url)).arrayBuffer());
97
+ const imageId = exportWorkbook.addImage({
98
+ buffer: pic,
99
+ extension: 'png',
100
+ });
101
+ addToFile(imageId, visibleTitle, imageDiv.clientWidth, imageDiv.clientHeight);
102
+ };
88
103
 
89
- const visibleTitle = titleDiv.firstChild?.textContent;
90
- if (visibleTitle === null || visibleTitle === undefined) continue;
104
+ const exportPlot = async (plotDiv: ChildNode | null | undefined,
105
+ blockDiv: Element, visibleTitle: string, plotCount? : number) => {
106
+ const titleDiv = blockDiv.firstChild as HTMLElement;
107
+ if (!titleDiv) return;
91
108
 
92
- let imageId;
93
- let width = 0;
94
- let height = 0;
109
+ if (plotDiv && !titleDiv.getElementsByClassName('svg-table').length &&
110
+ (plotDiv as HTMLElement).getAttribute('name') === 'viewer-Grid') return;
95
111
 
96
- const imageDiv = titleDiv.parentElement?.getElementsByClassName('grok-scripting-image-container')[0];
97
- if (imageDiv) {
98
- const regex = /background-image: url\(.*\)/;
99
- const urlAttr = imageDiv.outerHTML.match(regex)?.[0];
100
- if (!urlAttr) continue;
101
- const url = urlAttr.substring(23, urlAttr?.length - 2);
102
- const pic = (await (await fetch(url)).arrayBuffer());
103
- imageId = exportWorkbook.addImage({
104
- buffer: pic,
105
- extension: 'png',
106
- });
107
- width = imageDiv.clientWidth;
108
- height = imageDiv.clientHeight;
109
- } else {
110
- let plot: ChildNode | null | undefined = titleDiv.nextSibling;
112
+ if (plotDiv && titleDiv.getElementsByClassName('svg-table').length) {
113
+ if ((plotDiv as HTMLElement).getAttribute('name') === 'viewer-OutliersSelectionViewer') {
114
+ return;
115
+ }
116
+ if ((plotDiv as HTMLElement).getAttribute('name') === 'viewer-Grid') {
117
+ (titleDiv.getElementsByClassName('svg-table')[0] as HTMLElement).click();
118
+ await new Promise((r) => setTimeout(r, 150));
119
+ plotDiv = titleDiv.nextSibling;
120
+ }
121
+ }
111
122
 
112
- if (plot && !titleDiv.getElementsByClassName('svg-table').length && (plot as HTMLElement).getAttribute('name') === 'viewer-Grid') continue;
123
+ if (plotDiv) {
124
+ const plot = (plotDiv as HTMLElement).getElementsByClassName('d4-layout-middle')[0];
125
+ const canvas = await DG.HtmlUtils.renderToCanvas(plot as HTMLElement);
126
+ const dataUrl = canvas.toDataURL('image/png');
113
127
 
114
- if (plot && titleDiv.getElementsByClassName('svg-table').length) {
115
- if ((plot as HTMLElement).getAttribute('name') === 'viewer-OutliersSelectionViewer') {
116
- plot = titleDiv.parentElement?.nextSibling?.firstChild?.nextSibling;
117
- skipNext = true;
118
- }
119
- if ((plot as HTMLElement).getAttribute('name') === 'viewer-Grid') {
120
- (titleDiv.getElementsByClassName('svg-table')[0] as HTMLElement).click();
121
- await new Promise((r) => setTimeout(r, 150));
122
- plot = titleDiv.nextSibling;
123
- }
124
- }
125
- console.log(plot);
128
+ const imageId = exportWorkbook.addImage({
129
+ base64: dataUrl,
130
+ extension: 'png',
131
+ });
132
+ addToFile(imageId, visibleTitle, canvas.width, canvas.height, plotCount);
133
+ }
134
+ };
126
135
 
127
- if (plot) {
128
- const canvas = await DG.HtmlUtils.renderToCanvas(plot as HTMLElement);
129
- const dataUrl = canvas.toDataURL('image/png');
136
+ const exportTab = async (tab: HTMLElement) => {
137
+ const blockDivs = document.getElementsByClassName('ui-div ui-block');
138
+ let visibleTitle: string = '';
139
+ let plotCount: number = 0;
130
140
 
131
- imageId = exportWorkbook.addImage({
132
- base64: dataUrl,
133
- extension: 'png',
134
- });
135
- width = canvas.width;
136
- height = canvas.height;
137
- }
138
- }
141
+ for (let i = 0; i < blockDivs.length; i++) {
142
+ const blockDiv = blockDivs[i];
139
143
 
140
- if (imageId === null || imageId === undefined) continue;
144
+ if ((blockDiv as HTMLElement).style.display === 'none') continue;
141
145
 
142
- const worksheet = exportWorkbook.getWorksheet(getSheetName(visibleTitle, DIRECTION.INPUT)) ||
143
- exportWorkbook.getWorksheet(getSheetName(visibleTitle, DIRECTION.OUTPUT));
144
- if (worksheet) {
145
- console.log(visibleTitle);
146
- console.log(realNames[visibleTitle]);
147
- const columnForImage = (((call
148
- .inputs[realNames[visibleTitle]] || call
149
- .outputs[realNames[visibleTitle]]) as DG.DataFrame)
150
- .columns as DG.ColumnList)
151
- .length + 1;
152
- worksheet.addImage(imageId, {
153
- tl: {col: columnForImage, row: 0},
154
- ext: {width, height},
155
- });
156
- } else {
157
- const newWorksheet = exportWorkbook.addWorksheet(`Output - Plot - ${visibleTitle}`);
158
- newWorksheet.addImage(imageId, {
159
- tl: {col: 0, row: 0},
160
- ext: {width, height},
161
- });
162
- }
146
+ if (blockDiv.firstChild?.firstChild?.textContent) {
147
+ visibleTitle = blockDiv.firstChild?.firstChild?.textContent;
148
+ plotCount = 0;
149
+ } else {
150
+ plotCount++;
163
151
  }
164
- }
165
- (selectedByUser as HTMLElement).click();
166
- } else {
167
- const resultView = document.getElementsByClassName('ui-panel grok-func-results')[0];
168
- const titleDivs = resultView.getElementsByClassName('grok-func-results-header');
169
- let skipNext = false;
170
152
 
171
- for (let i=0; i< titleDivs.length; i++) {
172
- if (skipNext) {
173
- skipNext = false;
174
- continue;
153
+ if ((blockDiv.firstChild?.nextSibling as HTMLElement).getAttribute('name') === 'viewer-OutliersSelectionViewer') {
154
+ plotCount--;
175
155
  }
176
156
 
177
- const titleDiv = titleDivs[i];
178
- const visibleTitle = titleDiv.firstChild?.textContent;
179
- if (visibleTitle === null || visibleTitle === undefined) continue;
180
-
181
- let imageId;
182
- let width = 0;
183
- let height = 0;
184
-
185
- const imageDiv = titleDiv.parentElement?.getElementsByClassName('grok-scripting-image-container')[0];
157
+ const imageDiv = blockDiv.getElementsByClassName('grok-scripting-image-container')[0];
186
158
  if (imageDiv) {
187
- const regex = /background-image: url\(.*\)/;
188
- const urlAttr = imageDiv.outerHTML.match(regex)?.[0];
189
- if (!urlAttr) continue;
190
- const url = urlAttr.substring(23, urlAttr?.length - 2);
191
- const pic = (await (await fetch(url)).arrayBuffer());
192
- imageId = exportWorkbook.addImage({
193
- buffer: pic,
194
- extension: 'png',
195
- });
196
- width = imageDiv.clientWidth;
197
- height = imageDiv.clientHeight;
159
+ await exportImage(imageDiv, visibleTitle);
198
160
  } else {
199
- let plot: ChildNode | null | undefined = titleDiv.nextSibling;
200
-
201
- console.log(plot);
202
-
203
- if (plot &&
204
- ((plot as HTMLElement).getAttribute('name') === 'viewer-Grid' ||
205
- (plot as HTMLElement).getAttribute('name') === 'viewer-OutliersSelectionViewer')) {
206
- if (titleDiv.getElementsByClassName('svg-table').length) {
207
- plot = titleDiv.parentElement?.nextSibling?.firstChild?.nextSibling;
208
- skipNext = true;
209
- } else {
210
- continue;
211
- }
212
- }
213
-
214
- if (plot) {
215
- const canvas = await DG.HtmlUtils.renderToCanvas(plot as HTMLElement);
216
- const dataUrl = canvas.toDataURL('image/png');
217
-
218
- imageId = exportWorkbook.addImage({
219
- base64: dataUrl,
220
- extension: 'png',
221
- });
222
- width = canvas.width;
223
- height = canvas.height;
224
- }
161
+ const plotDiv: ChildNode | null | undefined = blockDiv.firstChild?.nextSibling;
162
+ await exportPlot(plotDiv, blockDiv, visibleTitle, plotCount);
225
163
  }
164
+ };
165
+ };
226
166
 
227
- if (imageId === null || imageId === undefined) continue;
228
-
229
- const worksheet = exportWorkbook.getWorksheet(getSheetName(visibleTitle, DIRECTION.OUTPUT));
230
- if (worksheet) {
231
- const columnForImage = ((call
232
- .outputs[realNames[visibleTitle]] as DG.DataFrame)
233
- .columns as DG.ColumnList)
234
- .length + 1;
235
- worksheet.addImage(imageId, {
236
- tl: {col: columnForImage, row: 0},
237
- ext: {width, height},
238
- });
239
- } else {
240
- const newWorksheet = exportWorkbook.addWorksheet(`Output - Plot - ${visibleTitle}`);
241
- newWorksheet.addImage(imageId, {
242
- tl: {col: 0, row: 0},
243
- ext: {width, height},
244
- });
245
- }
167
+ // UWAGA: very fragile solution
168
+ const funcView = document.getElementsByClassName('grok-view grok-view-func ui-box')[0];
169
+ const resultView = funcView.getElementsByClassName('d4-tab-host ui-box')[0];
170
+ if (resultView) {
171
+ const tabs = resultView.getElementsByClassName('d4-tab-header-stripe')[0].children;
172
+ const selectedByUser = resultView.getElementsByClassName('d4-tab-header selected')[0];
173
+ for (let i=0; i< tabs.length; i++) {
174
+ const tab = tabs[i] as HTMLElement;
175
+ tab.click();
176
+ await new Promise((r) => setTimeout(r, 150));
177
+ await exportTab(tab);
246
178
  }
179
+ (selectedByUser as HTMLElement).click();
180
+ } else {
181
+ const resultView = document.getElementsByClassName('ui-panel grok-func-results')[0];
182
+ await exportTab(resultView as HTMLElement);
247
183
  }
248
184
 
249
185
  if (scalarOutputs.length) {
@@ -253,8 +189,12 @@ export async function exportFuncCall(call: DG.FuncCall) {
253
189
  });
254
190
  }
255
191
 
192
+ const metadataSheet = exportWorkbook.addWorksheet('Function run metadata');
193
+ metadataSheet.addRow(['Author', `${call.func.author.lastName} ${call.func.author.firstName}`]);
194
+ metadataSheet.addRow(['Login', call.func.author.toString()]);
195
+
256
196
  exportWorkbook.xlsx.writeBuffer().then((data) => {
257
197
  const blob = new Blob([data], {type: BLOB_TYPE});
258
- saveAs(blob, call.func.name);
198
+ saveAs(blob, `${call.func.name} - ${new Date().toLocaleString()}`);
259
199
  });
260
200
  }