forked from nuagenetworks/js-bambou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVSDService.js
173 lines (138 loc) · 5.64 KB
/
VSDService.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
import objectPath from 'object-path';
import { getLogger } from './Logger';
import NUTemplateParser from "service/NUTemplateParser";
import * as tabification from './tabify';
const ERROR_MESSAGE = 'Unable to fetch data';
export default class VSDService {
constructor(service) {
this.service = service;
}
VSDSearchConvertor = (expressions) => {
let expression = '';
expressions.forEach(e => {
if (e.operator) {
expression += ` ${e.operator} `;
} else if (e.bracket) {
expression += `${e.bracket}`;
} else {
expression += `${e.element.category} ${e.element.operator} "${e.element.value}"`;
}
});
return expression;
}
getParentEntity = (configuration) => {
const parentEntity = { resourceName: configuration.parentResource};
if (configuration.hasOwnProperty("parentID")) {
parentEntity.ID = configuration.parentID;
}
return parentEntity;
}
getEntity = (configuration) => {
const entity = {
ID: null
};
if (configuration.hasOwnProperty("resource")) {
entity.resourceName = configuration.resource;
}
return entity;
}
getRequestID = (configuration, context = {}) => {
const tmpConfiguration = NUTemplateParser.parameterizedConfiguration(configuration, context);
if (!tmpConfiguration)
return;
let endPoint = tmpConfiguration.query.parentResource;
if (tmpConfiguration.query.hasOwnProperty("parentID"))
endPoint += "/" + tmpConfiguration.query.parentID;
if (tmpConfiguration.query.hasOwnProperty("resource"))
endPoint += "/" + tmpConfiguration.query.resource;
endPoint = configuration.id ? `${configuration.vizID}-${configuration.id}-${endPoint}` : endPoint;
if (!tmpConfiguration.query.filter) {
return endPoint;
}
return endPoint + "-" + tmpConfiguration.query.filter;
}
/**
* check and update query for next request if sum of totolCaptured (already fetched data count)
* and current data count (header.hits) is less than total count
* and increased page by 1 for next request.
*/
getNextRequest = (header, query, pageSize) => {
let nextQuery = {},
nextPage = 0;
if (((pageSize * (header.page + 1)) + header.hits) < header.count) {
nextPage = header.page + 1;
nextQuery = { ...query };
nextQuery.query.nextPage = nextPage;
}
return nextQuery;
}
// TODO - refactor later by using existing service
fetch = (configuration) => {
const filter = configuration.query.filter || null,
page = configuration.query.nextPage || 0,
orderBy = configuration.query.sort || null,
api = this.service.buildURL(this.getEntity(configuration.query), null, this.getParentEntity(configuration.query)),
pageSize = configuration.query.pageSize || this.service.pageSize;
return this.service.invokeRequest({
verb: 'GET',
requestURL: api,
headers: this.service.computeHeaders(page, filter, orderBy, undefined, pageSize),
requestData: undefined,
ignoreRequestIdle: true,
}).then(response => {
const header = {
page: parseInt(response.headers['x-nuage-page'], 10) || 0,
count: parseInt(response.headers['x-nuage-count'], 10) || 0,
hits: (response.data && response.data.length) || 0,
}
return {
response: this.tabify(response.data, configuration) || [],
nextQuery: this.getNextRequest(header, configuration, pageSize),
length: header.count
}
}
).catch(error => {
getLogger().error(error.message || error);
return Promise.reject(ERROR_MESSAGE);
});
}
tabify = (response, queryConfiguration) => {
if (queryConfiguration) {
const customTabify = objectPath.get(queryConfiguration, 'tabify');
if (customTabify) {
const tabificationFunction = tabification[customTabify];
return tabificationFunction(response)
}
}
return response;
}
// Add custom sorting into VSD query
addSorting = (queryConfiguration, sort) => {
if (!queryConfiguration)
return null
if (!sort || sort.order === '' || !sort.column)
return queryConfiguration;
queryConfiguration.query.sort = `${sort.column} ${sort.order}`
return queryConfiguration;
}
// Add custom searching from searchbox into VSD query
addSearching = (queryConfiguration, search = []) => {
if (!queryConfiguration)
return null;
if (search.length) {
let filter = objectPath.get(queryConfiguration, 'query.filter');
objectPath.push(queryConfiguration, 'query.filter', (filter ? `(${filter}) AND ` : '') + this.VSDSearchConvertor(search));
}
return queryConfiguration;
}
getPageSizePath = () => 'query.pageSize';
updatePageSize = (queryConfiguration, pageSize) => {
objectPath.set(queryConfiguration, this.getPageSizePath(), pageSize);
return queryConfiguration;
}
getNextPageQuery = (queryConfiguration, nextPage = 1) => {
queryConfiguration.query.nextPage = nextPage;
return queryConfiguration;
}
isConnected = () => this.service._connection && this.service._connection._isConnected;
}