forked from FIWARE-TMForum/business-ecosystem-logic-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fill_indexes.js
executable file
·177 lines (153 loc) · 5.08 KB
/
fill_indexes.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
/* Copyright (c) 2015 - 2016 CoNWeT Lab., Universidad Politécnica de Madrid
*
* This file belongs to the business-ecosystem-logic-proxy of the
* Business API Ecosystem
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var indexes = require('./lib/indexes.js'),
request = require('request'),
utils = require('./lib/utils'),
Promiz = require('promiz');
var createUrl = function createUrl(api, extra) {
console.log('-------------------------------------------------------------------------------------------------')
console.log(api)
console.log(extra)
console.log('-------------------------------------------------------------------------------------------------')
if (api === 'DSProductCatalog') {
return (config.endpoints.catalog.appSsl == true ? 'https://' : 'http://') + config.endpoints.catalog.host + ':' + config.endpoints.catalog.port + extra
}
return utils.getAPIProtocol(api) + '://' + utils.getAPIHost(api) + ':' + utils.getAPIPort(api) + extra;
};
var genericRequest = function genericRequest(options, extra) {
var p = new Promiz();
request(options, function(err, response, body) {
if (err) {
console.log(err);
p.reject(err);
return;
}
if (response.statusCode == 200) {
var parsedBody = JSON.parse(body);
if (extra) {
parsedBody.forEach(function(element) {
element[extra.field] = extra.value;
});
}
p.resolve(parsedBody);
} else {
p.reject('Unexpected HTTP error code: ' + response.statusCode);
return;
}
});
return p;
};
var getProducts = function getProducts() {
var url = createUrl('DSProductCatalog', '/productSpecification');
// var url = 'http://' + config.endpoints.catalog.host + config.endpoints.catalog.path + ':' + config.endpoints.catalog.port + '/productSpecification'
return genericRequest(url);
};
var getOfferings = function getOfferings(catalog, qstring) {
// For every catalog!
var url = createUrl('DSProductCatalog', 'productOffering');
if (qstring) {
url += qstring;
}
return genericRequest(url, {
field: 'catalog',
value: catalog
});
};
var getCatalogs = function getCatalogs() {
var url = createUrl('DSProductCatalog', 'catalog');
return genericRequest(url);
};
var getInventory = function getInventory() {
var url = createUrl('inventory', 'product');
return genericRequest(url);
};
var getOrders = function getOrders() {
var url = createUrl('DSProductOrdering', '/DSProductOrdering/api/productOrdering/v2/productOrder');
return genericRequest(url);
};
var downloadProducts = function downloadProducts() {
return getProducts().then(indexes.saveIndexProduct);
};
var downloadOfferings = function downloadOfferings(catalog, qstring) {
return getOfferings(catalog, qstring).then(indexes.saveIndexOffering);
};
var downloadCatalogOfferings = function downloadCatalogOfferings(catalogs) {
var promise = Promiz.resolve();
if (catalogs.length) {
catalogs.forEach(function(catalog) {
promise = promise.then(function() {
return downloadOfferings(catalog.id, '?isBundle=false');
});
});
catalogs.forEach(function(catalog) {
promise = promise.then(function() {
return downloadOfferings(catalog.id, '?isBundle=true');
});
});
} else {
promise = promise.then(function() {
return downloadOfferings();
});
}
promise = promise.then(function() {
return indexes.saveIndexCatalog(catalogs);
});
return promise;
};
var downloadCatalogs = function downloadCatalogs() {
return getCatalogs().then(downloadCatalogOfferings);
};
var downloadInventory = function downloadInventory() {
return getInventory().then(indexes.saveIndexInventory);
};
var downloadOrdering = function downloadOrdering() {
return getOrders().then(indexes.saveIndexOrder);
};
var logAllIndexes = function logAllIndexes(path) {
return indexes
.search(path, { AND: { '*': ['*'] } })
.catch((err) => console.log(err))
.then((results) => {
console.log(results);
results.hits.forEach((x) => console.log(x));
});
};
function load() {
indexes
.init()
.then(downloadProducts)
.then(downloadCatalogs)
.then(downloadInventory)
.then(downloadOrdering)
.then(indexes.close)
.then(() => console.log('All saved!'))
.catch((e) => console.log('Error: ', e));
}
async function loadAll() {
for (let i = 0; i < 3; i++) {
load();
await new Promise(r => setTimeout(r, 20000));;
}
}
loadAll();
// logAllIndexes(indexes.siTables.catalogs);
// logAllIndexes(indexes.siTables.products);
// logAllIndexes(indexes.siTables.offerings);
// logAllIndexes(indexes.siTables.inventory);
// logAllIndexes(indexes.siTables.orders);