-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathconsole.component.ts
436 lines (376 loc) · 14.9 KB
/
console.component.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {FormBuilder} from '@angular/forms';
import {TableConfig} from '../../../components/data-view/data-table/table-config';
import {CrudService} from '../../../services/crud.service';
import {ResultSet} from '../../../components/data-view/models/result-set.model';
import {QueryHistory} from './query-history.model';
import {KeyValue} from '@angular/common';
import {QueryRequest, SchemaRequest} from '../../../models/ui-request.model';
import {SidebarNode} from '../../../models/sidebar-node.model';
import {LeftSidebarService} from '../../../components/left-sidebar/left-sidebar.service';
import {InformationObject, InformationPage} from '../../../models/information-page.model';
import {BreadcrumbService} from '../../../components/breadcrumb/breadcrumb.service';
import {BreadcrumbItem} from '../../../components/breadcrumb/breadcrumb-item';
import {WebuiSettingsService} from '../../../services/webui-settings.service';
import {Subscription} from 'rxjs';
import {UtilService} from '../../../services/util.service';
import {WebSocket} from '../../../services/webSocket';
import {BsModalService} from 'ngx-bootstrap/modal';
import {ToastService} from '../../../components/toast/toast.service';
import {ViewInformation} from '../../../components/data-view/data-view.component';
class Namespace {
name: string;
id: string;
}
@Component({
selector: 'app-console',
templateUrl: './console.component.html',
styleUrls: ['./console.component.scss']
})
export class ConsoleComponent implements OnInit, OnDestroy {
@ViewChild('editor', {static: false}) codeEditor;
@ViewChild('historySearchInput') historySearchInput;
history: Map<string, QueryHistory> = new Map<string, QueryHistory>();
readonly MAX_HISTORY = 50;//maximum items in history
private readonly LOCAL_STORAGE_HISTORY_KEY = 'query-history';
private readonly LOCAL_STORAGE_NAMESPACE_KEY = 'polypheny-namespace';
resultSets: ResultSet[];
collapsed: boolean[];
queryAnalysis: InformationPage;
analyzeQuery = true;
useCache = true;
private originalCache: boolean = null;
showingAnalysis = false;
websocket: WebSocket;
private subscriptions = new Subscription();
loading = false;
lang = 'sql';
saveInHistory = true;
showSearch = false;
historySearchQuery = '';
confirmDeletingHistory;
activeNamespace: string;
namespaces = [];
tableConfig: TableConfig = {
create: false,
update: false,
delete: false,
sort: false,
search: false,
exploring: false
};
private existingNamespaces: String[];
showNamespaceConfig: boolean;
constructor(
private formBuilder: FormBuilder,
private _crud: CrudService,
private _leftSidebar: LeftSidebarService,
private _breadcrumb: BreadcrumbService,
private _settings: WebuiSettingsService,
public _util: UtilService,
public modalService: BsModalService,
public _toast: ToastService
) {
this.websocket = new WebSocket(_settings);
// @ts-ignore
if (window.Cypress) {
(<any>window).executeQuery = (query: string) => {
this.codeEditor.setCode(query);
this.submitQuery();
};
}
this.initWebsocket();
}
ngOnInit() {
QueryHistory.fromJson(localStorage.getItem(this.LOCAL_STORAGE_HISTORY_KEY), this.history);
this._breadcrumb.hide();
this.updateExistingNamespaces();
this.loadAndSetNamespaceDB();
}
private updateExistingNamespaces() {
this._crud.getSchema(new SchemaRequest('views/querying/console/', false, 1, false)).subscribe(
res => {
this.namespaces = [];
for (const namespace of <Namespace[]>res) {
this.namespaces.push(namespace.name);
}
this.loadAndSetNamespaceDB();
}
);
}
private loadAndSetNamespaceDB() {
let db = localStorage.getItem(this.LOCAL_STORAGE_NAMESPACE_KEY);
if (db === null || (this.namespaces && this.namespaces.length > 0 && !this.namespaces.includes(db))) {
if (this.namespaces && this.namespaces.length > 0) {
db = this.namespaces[0];
} else {
db = 'public';
}
}
this.setDefaultDB(db);
}
ngOnDestroy() {
this._leftSidebar.close();
this.subscriptions.unsubscribe();
this.websocket.close();
this._breadcrumb.hide();
window.onbeforeunload = null;
window.onkeydown = null;
}
submitQuery() {
const code = this.codeEditor.getCode();
if (!code) {
return;
}
if (this.saveInHistory) {
this.addToHistory(code, this.lang);
}
if (this.usesAdvancedConsole(this.lang)) { // maybe adjust
const matchGraph = code.toLowerCase().match('use graph [a-zA-Z][a-zA-Z0-1]*');
if (matchGraph !== null && matchGraph.length >= 0) {
const database = matchGraph[matchGraph.length - 1].replace('use ', '');
this.setDefaultDB(database);
}
const match = code.toLowerCase().match('use [a-zA-Z][a-zA-Z0-1]*');
if (match !== null && match.length >= 0) {
const database = match[match.length - 1].replace('use ', '');
if (database !== 'placement') {
this.setDefaultDB(database);
}
}
if (code.match('show db')) {
this._crud.getDocumentDatabases().subscribe(res => {
this.existingNamespaces = [];
for (const entry of (<ResultSet>res).data) {
this.existingNamespaces.push(entry[0]);
}
this.loading = false;
this.resultSets = [<ResultSet>res];
});
return;
}
}
this._leftSidebar.setNodes([]);
if (this.analyzeQuery) {
this._leftSidebar.open();
} else {
this._leftSidebar.close();
}
this.queryAnalysis = null;
this.loading = true;
console.log(code);
console.log(this.activeNamespace);
if (!this._crud.anyQuery(this.websocket, new QueryRequest(code, this.analyzeQuery, this.useCache, this.lang, this.activeNamespace))) {
this.loading = false;
this.resultSets = [new ResultSet('Could not establish a connection with the server.', code)];
}
}
collapseAll(collapse: boolean) {
this.collapsed.fill(collapse);
}
addToHistory(query: string, lang: string): void {
if (this.history.size >= this.MAX_HISTORY) {
let h: QueryHistory = new QueryHistory('');
this.history.forEach((val, key) => {
if (val.time < h.time) {
h = val;
}
});
this.history.delete(h.query);
}
const newHistory = new QueryHistory(query, null, lang);
this.history.set(newHistory.query, newHistory);
localStorage.setItem(this.LOCAL_STORAGE_HISTORY_KEY, JSON.stringify(Array.from(this.history.values())));
}
applyHistory(query: string, lang: string, run: boolean) {
this.lang = lang;
this.codeEditor.setCode(query);
if (run) {
this.submitQuery();
}
}
deleteHistoryItem(key, e) {
if (this.confirmDeletingHistory === key) {
this.history.delete(key);
localStorage.setItem(this.LOCAL_STORAGE_HISTORY_KEY, JSON.stringify(Array.from(this.history.values())));
} else {
this.confirmDeletingHistory = key;
}
e.stopPropagation();
}
//from: https://stackoverflow.com/questions/52793944/angular-keyvalue-pipe-sort-properties-iterate-in-order
orderHistory(a: KeyValue<string, QueryHistory>, b: KeyValue<string, QueryHistory>) {
return a.value.time > b.value.time ? -1 : (b.value.time > a.value.time ? 1 : 0);
}
openHistorySearch() {
this.showSearch = true;
setTimeout(
() => this.historySearchInput.nativeElement.focus(),
1
);
}
closeHistorySearch() {
this.showSearch = false;
this.historySearchQuery = '';
}
initWebsocket() {
//function to define behavior when clicking on a page link
const nodeBehavior = (tree, node, $event) => {
if (node.data.id === 'console') {
//this.queryAnalysis = null;
this.showingAnalysis = false;
this._breadcrumb.hide();
node.setIsActive(true);
return;
}
const split = node.data.routerLink.split('/');
const analyzerId = split[0];
const analyzerPage = split[1];
if (analyzerId !== undefined && analyzerPage !== undefined) {
this._crud.getAnalyzerPage(analyzerId, analyzerPage).subscribe(
res => {
this.queryAnalysis = <InformationPage>res;
this.showingAnalysis = true;
this._breadcrumb.setBreadcrumbs([new BreadcrumbItem(node.data.name)]);
if (this.queryAnalysis.fullWidth) {
this._breadcrumb.hideZoom();
}
node.setIsActive(true);
}, err => {
console.log(err);
}
);
}
};
const sub = this.websocket.onMessage().subscribe(
msg => {
//if msg contains nodes of the sidebar
if (Array.isArray(msg) && msg[0].hasOwnProperty('routerLink')) {
const sidebarNodesTemp: SidebarNode[] = <SidebarNode[]>msg;
const sidebarNodes: SidebarNode[] = [];
const labels = new Set();
sidebarNodesTemp.sort(this._leftSidebar.sortNodes).forEach((s) => {
if (s.label) {
labels.add(s.label);
} else {
sidebarNodes.push(SidebarNode.fromJson(s, {allowRouting: false, action: nodeBehavior}));
}
});
for (const l of [...labels].sort()) {
sidebarNodes.push(new SidebarNode(l, l).asSeparator());
sidebarNodesTemp.filter((n) => n.label === l).sort(this._leftSidebar.sortNodes).forEach((n) => {
sidebarNodes.push(SidebarNode.fromJson(n, {allowRouting: false, action: nodeBehavior}));
});
}
sidebarNodes.unshift(new SidebarNode('console', 'console', 'fa fa-keyboard-o').setAction(nodeBehavior));
this._leftSidebar.setNodes(sidebarNodes);
if (sidebarNodes.length > 0) {
this._leftSidebar.open();
} else {
this._leftSidebar.close();
}
}
// array of ResultSets
else if (Array.isArray(msg) && (msg[0].hasOwnProperty('data') || msg[0].hasOwnProperty('affectedRows') || msg[0].hasOwnProperty('error'))) {
this.loading = false;
this.resultSets = <ResultSet[]>msg;
this.collapsed = new Array(this.resultSets.length);
this.collapsed.fill(false);
}
//if msg contains a notification of a changed information object
else if (msg.hasOwnProperty('type')) {
const iObj = <InformationObject>msg;
if (this.queryAnalysis) {
const group = this.queryAnalysis.groups[iObj.groupId];
if (group != null) {
group.informationObjects[iObj.id] = iObj;
}
}
}
},
err => {
//this._leftSidebar.setError('Lost connection with the server.');
setTimeout(() => {
this.initWebsocket();
}, +this._settings.getSetting('reconnection.timeout'));
});
this.subscriptions.add(sub);
}
createView(info: ViewInformation) {
this.codeEditor.setCode(info.fullQuery);
}
executeView(info: ViewInformation) {
this.codeEditor.setCode(info.fullQuery);
this.submitQuery();
}
formatQuery() {
let code = this.codeEditor.getCode();
if (!code) {
return;
}
let before = '';
const after = ')';
// here we replace the Json incompatible types with placeholders
const temp = code.match(/NumberDecimal\([^)]*\)/g);
if (temp !== null) {
for (let i = 0; i < temp.length; i++) {
code = code.replace(temp[i], '"___' + i + '"');
}
}
const splits = code.split('(');
before = splits.shift() + '(';
try {
let json = this.parse(splits.join('(').slice(0, -1));
// we have to translate them back
if (temp !== null) {
for (let i = 0; i < temp.length; i++) {
json = json.replace('"___' + i + '"', temp[i]);
}
}
this.codeEditor.setCode(before + json + after);
} catch (e) {
this._toast.warn(e);
}
}
parse(code: string) {
console.log(code);
const formatted = JSON.stringify(JSON.parse('[' + code + ']'), null, 4);
return formatted.substring(1, formatted.length - 1);
}
private setDefaultDB(name: string) {
name = name.trim();
if (!this.namespaces.includes(name)) {
this.namespaces.push(name);
}
this.activeNamespace = name;
localStorage.setItem(this.LOCAL_STORAGE_NAMESPACE_KEY, name);
}
toggleCollapsed(i: number) {
if (this.collapsed !== undefined && this.collapsed[i] !== undefined) {
this.collapsed[i] = !this.collapsed[i];
}
}
clearConsole() {
this.codeEditor.setCode('');
}
toggleCache(b: boolean) {
if (this.originalCache === null) {
this.originalCache = this.useCache;
}
this.useCache = b;
}
revertCache() {
console.log('revert');
this.useCache = this.originalCache;
this.originalCache = null;
}
usesAdvancedConsole(lang: string) {
return lang === 'mql' || lang === 'cypher';
}
toggleNamespaceField() {
this.showNamespaceConfig = !this.showNamespaceConfig;
}
changedDefaultDB() {
this.setDefaultDB(this.activeNamespace);
}
}