This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
sessions.js
387 lines (357 loc) 路 10.6 KB
/
sessions.js
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
import fetch from 'isomorphic-fetch';
import uuid from 'uuid';
import {createAction} from 'redux-actions';
import {DIALECTS, INITIAL_CONNECTIONS} from '../constants/constants';
import {baseUrl} from '../utils/utils';
export const reset = createAction('RESET');
export const mergeTabMap = createAction('MERGE_TAB_MAP');
export const setTab = createAction('SET_TAB');
export const setTable = createAction('SET_TABLE');
export const setIndex = createAction('SET_INDEX');
export const mergeConnections = createAction('MERGE_CONNECTIONS');
export const updateConnection = createAction('UPDATE_CREDENTIAL');
export const deleteConnection = createAction('DELETE_CREDENTIAL');
export const updatePreview = createAction('UPDATE_PREVIEW');
const DELETE_TAB_MESSAGE = 'You are about to delete a connection. ' +
'If you have scheduled persistent queries with that connection, they ' +
'will stop refreshing. Are you sure you want to continue?';
const request = {GET, DELETE, POST, PUT};
function GET(path) {
return fetch(`${baseUrl()}/${path}`, {
method: 'GET',
credentials: 'include',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
});
}
function DELETE(path) {
return fetch(`${baseUrl()}/${path}`, {
method: 'DELETE',
credentials: 'include',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
});
}
function POST(path, body = {}) {
const payload = {
method: 'POST',
credentials: 'include',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: body ? JSON.stringify(body) : null
};
return fetch(`${baseUrl()}/${path}`, payload);
}
function PUT(path, body = {}) {
return fetch(`${baseUrl()}/${path}`, {
method: 'PUT',
credentials: 'include',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: body ? JSON.stringify(body) : null
});
}
function apiThunk(endpoint, method, store, id, body) {
return dispatch => {
dispatch({
type: store,
payload: {id, status: 'loading'}
});
return request[method](endpoint, body)
.then(res => res.json().then(
json => {
dispatch({
type: store,
payload: {
status: res.status,
content: json,
id
}
});
return json;
}
))
.catch(err => {
console.error(err);
dispatch({
type: store,
payload: {
id,
status: 500
}
});
throw new Error(err);
});
};
}
export function getSettings() {
return apiThunk(
'settings',
'GET',
'settingsRequest'
);
}
export function getConnectorUrls() {
return apiThunk(
'settings/urls',
'GET',
'connectorUrlsRequest'
);
}
export function getConnections() {
return apiThunk(
'connections',
'GET',
'connectionsRequest'
);
}
export function editConnections(connectionObject, connectionId) {
return apiThunk(
`connections/${connectionId}`,
'PUT',
'connectRequests',
connectionId,
connectionObject
);
}
export function connect(connectionId) {
return apiThunk(
`connections/${connectionId}/connect`,
'POST',
'connectRequests',
connectionId
);
}
export function saveConnections(connectionsObject, tabId) {
return dispatch => {
return dispatch(apiThunk(
'connections',
'POST',
'saveConnectionsRequests',
tabId,
connectionsObject
)).then(json => {
if (!json.error) {
dispatch(mergeTabMap({[tabId]: json.connectionId}));
}
return json;
});
};
}
export function getTables(connectionId) {
return apiThunk(
`connections/${connectionId}/sql-tables`,
'POST',
'tablesRequests',
connectionId
);
}
export function getElasticsearchMappings(connectionId) {
return apiThunk(
`connections/${connectionId}/elasticsearch-mappings`,
'POST',
'elasticsearchMappingsRequests',
connectionId
);
}
export function getS3Keys(connectionId) {
return apiThunk(
`connections/${connectionId}/s3-keys`,
'POST',
's3KeysRequests',
connectionId
);
}
export function getApacheDrillStorage(connectionId) {
return apiThunk(
`connections/${connectionId}/apache-drill-storage`,
'POST',
'apacheDrillStorageRequests',
connectionId
);
}
export function getApacheDrillS3Keys(connectionId) {
return apiThunk(
`connections/${connectionId}/apache-drill-s3-keys`,
'POST',
'apacheDrillS3KeysRequests',
connectionId
);
}
export function previewTable (connectionId, dialect, table, database) {
const body = {
query: PREVIEW_QUERY(dialect, table, database)
};
return apiThunk(
`connections/${connectionId}/query`,
'POST',
'previewTableRequest',
[connectionId, table],
body
);
}
export function getSqlSchema (connectionId, dialect, database) {
return apiThunk(
`connections/${connectionId}/sql-schemas`,
'POST',
'schemaRequests',
connectionId
);
}
export function runSqlQuery (connectionId, query) {
const body = {
query: query
};
return apiThunk(
`connections/${connectionId}/query`,
'POST',
'queryRequests',
connectionId,
body
);
}
export function initializeTabs() {
return function(dispatch, getState) {
const state = getState();
const {connectionsRequest} = state;
if (connectionsRequest.status !== 200) {
console.error(
"Can't initialize tabs - credentials haven't been retreived yet."
);
return;
}
const savedConnections = connectionsRequest.content;
if (savedConnections.length > 0) {
const tabs = savedConnections.map(() => uuid.v4());
const tabMap = {};
const connections = {};
savedConnections.forEach((cred, i) => {
tabMap[tabs[i]] = cred.id;
connections[tabs[i]] = cred;
});
dispatch(mergeTabMap(tabMap));
dispatch(mergeConnections(connections));
dispatch(setTab(tabs[0]));
savedConnections.forEach(cred => {
dispatch(connect(cred.id));
});
} else {
dispatch(newTab());
}
};
}
export function newTab() {
return function(dispatch, getState) {
const newId = uuid.v4();
dispatch(mergeConnections({
[newId]: INITIAL_CONNECTIONS
}));
dispatch(setTab(newId));
};
}
export function deleteTab(tabId) {
return function (dispatch, getState) {
const connectionId = getState().tabMap[tabId];
const tabIds = Object.keys(getState().connections);
const currentIdIndex = tabIds.indexOf(getState().selectedTab);
const connectRequest = getState().connectRequests[connectionId];
/*
* Logic to dermine which tab index we should switch to once
* the current tab is deleted.
*/
let nextIdIndex = currentIdIndex;
if (tabId === getState().selectedTab) {
if (currentIdIndex === 0) {
if (tabIds.length > 1) {
nextIdIndex = 1;
} else {
nextIdIndex = -1; // null out
}
} else {
nextIdIndex = currentIdIndex - 1;
}
} else {
// Stay on the same tab if deleting another one.
nextIdIndex = currentIdIndex;
}
const nextIdTab = tabIds[nextIdIndex];
/*
* If the connection has been successfully saved to disk,
* we want to send an API call to delete it. Delete the tab after.
* Otherwise, we simply want to delete the unsaved or failed connection.
*/
if (connectionId && connectRequest.status === 200) {
/*
* Throw a dialog box at the user because deleting a connection erases
* it from disk permanently and persistent queries may fail onwards.
*/
/* eslint no-alert: 0 */
if (confirm(DELETE_TAB_MESSAGE)) {
/* eslint no-alert: 0 */
dispatch(apiThunk(
`connections/${connectionId}`,
'DELETE',
'deleteConnectionsRequests',
connectionId
)).then(json => {
if (!json.error) {
dispatch(setTab(nextIdTab));
dispatch(deleteConnection(tabId));
}
});
} else {
return;
}
} else {
dispatch(setTab(nextIdTab));
dispatch(deleteConnection(tabId));
}
};
}
export function setConnectionNeedToBeSaved(tabId, bool) {
return dispatch => {
dispatch({
type: 'SET_CONNECTIONS_NEED_TO_BE_SAVED',
payload: {
tabId,
content: bool
}
});
};
}
function PREVIEW_QUERY (dialect, table, database = '') {
switch (dialect) {
case DIALECTS.IBM_DB2:
return `SELECT * FROM ${table} FETCH FIRST 1000 ROWS ONLY`;
case DIALECTS.APACHE_IMPALA:
case DIALECTS.APACHE_SPARK:
case DIALECTS.MYSQL:
case DIALECTS.SQLITE:
case DIALECTS.MARIADB:
case DIALECTS.POSTGRES:
case DIALECTS.REDSHIFT:
return `SELECT * FROM ${table} LIMIT 1000`;
case DIALECTS.MSSQL:
return 'SELECT TOP 1000 * FROM ' +
`${database}.dbo.${table}`;
case DIALECTS.ELASTICSEARCH:
return JSON.stringify({
index: database || '_all',
type: table || '_all',
body: {
query: { 'match_all': {} },
size: 1000
}
});
default:
throw new Error(`Dialect ${dialect} is not one of the DIALECTS`);
}
}