Skip to content

Commit

Permalink
Merge c5739ff into a1ccf41
Browse files Browse the repository at this point in the history
  • Loading branch information
fdelavega committed May 6, 2020
2 parents a1ccf41 + c5739ff commit 8dc6347
Show file tree
Hide file tree
Showing 36 changed files with 470 additions and 7,429 deletions.
5 changes: 3 additions & 2 deletions config.js
Expand Up @@ -146,8 +146,9 @@ config.billingAccountOwnerRole = 'bill receiver';
config.publicPaths = [];

config.indexes = {
'engine': 'local', // local or elasticsearch
'elasticHost': 'elastic.docker:9200'
'engine': 'elasticsearch', // local or elasticsearch
'elasticHost': 'elastic.docker:9200',
'apiVersion': '7.5'
};

config.magicKey = undefined;
Expand Down
4 changes: 4 additions & 0 deletions default_locales/en.json
Expand Up @@ -139,6 +139,10 @@
"Bundled Offerings": "Bundled Offerings",
"Relationships": "Relationships",
"No price plans included.": "No price plans included.",
"The offering can be open or free": "The offering can be open or free",
"The offering is free": "The offering is free",
"The offering is open": "The offering is open",
"Is an open offering?": "Is an open offering?",
"No relationships included.": "No relationships included.",
"No offerings found.": "No offerings found.",
"Search by catalog": "Search by catalog",
Expand Down
6 changes: 5 additions & 1 deletion default_locales/es.json
Expand Up @@ -138,7 +138,11 @@
"Added": "Incluido",
"Bundled Offerings": "Ofertas incluidas",
"Relationships": "Relaciones",
"No price plans included.": "No se han incluido planes de precios",
"No price plans included.": "No se han incluido planes de precios.",
"The offering can be open or free": "La oferta puede ser abierta o gratuíta",
"The offering is free": "La oferta es gratuita",
"The offering is open": "La oferta es abierta",
"Is an open offering?": "¿Es una oferta abierta?",
"No relationships included.": "No se han incluido relaciones.",
"No offerings found.": "No se han encontrado ofertas.",
"Search by catalog": "Búsqueda por catálogo",
Expand Down
11 changes: 4 additions & 7 deletions docker-dev/docker-compose.yml
@@ -1,14 +1,11 @@
version: '3'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.4.2
image: docker.elastic.co/elasticsearch/elasticsearch:7.5.0
environment:
- discovery.type=single-node
# - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
# ulimits:
# memlock:
# soft: -1
# hard: -1
- 'node.name=BAE'
- 'discovery.type=single-node'
- 'ES_JAVA_OPTS=-Xms256m -Xmx256m'
ports:
- "127.0.0.1:9200:9200"
networks:
Expand Down
11 changes: 4 additions & 7 deletions docker/Dockerfile
Expand Up @@ -4,7 +4,7 @@ LABEL Author="CoNWeT Lab. Universidad Politécnica de Madrid"

ENV VERSION master

RUN apt-get update && apt-get install -y git xinetd python-pip wget && \
RUN apt-get update && apt-get install -y git xinetd python-pip wget curl && \
pip install sh requests && \
git clone https://github.com/FIWARE-TMForum/business-ecosystem-logic-proxy

Expand All @@ -23,17 +23,14 @@ RUN wget https://nodejs.org/dist/v8.12.0/node-v8.12.0-linux-x64.tar.xz && \
cp config.js etc/config.js && \
echo "module.exports = require('./etc/config');" > config.js

VOLUME /business-ecosystem-logic-proxy/indexes
VOLUME /business-ecosystem-logic-proxy/themes
VOLUME /business-ecosystem-logic-proxy/static
VOLUME /business-ecosystem-logic-proxy/locales

COPY ./entrypoint.sh /
COPY ./cleanIndex.sh /
COPY ./getConfig.js /business-ecosystem-logic-proxy

COPY ./serviceIndexes /etc/xinetd.d/

EXPOSE 8000
EXPOSE 8004

HEALTHCHECK CMD curl --fail http://localhost:8004/version || exit 1

ENTRYPOINT ["/entrypoint.sh"]
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Expand Up @@ -68,7 +68,7 @@ services:
- BAE_LP_MONGO_PORT=27017
- BAE_LP_MONGO_DB=belp

- BAE_LP_REVENUE_MODEL=30 # Default market owner precentage for Revenue Sharing models
- BAE_LP_REVENUE_MODEL=0 # Default market owner precentage for Revenue Sharing models

# ----- APIs Configuration -----
# If provided, it supports configuring the contection to the different APIs managed by the logic proxy, by default
Expand Down
34 changes: 22 additions & 12 deletions fill_indexes.js
Expand Up @@ -21,14 +21,14 @@ var config = require('./config'),
indexes = require('./lib/indexes.js'),
request = require('request'),
utils = require('./lib/utils'),
Promise = require('promiz');
Promiz = require('promiz');

var createUrl = function createUrl(api, extra) {
return utils.getAPIProtocol(api) + '://' + utils.getAPIHost(api) + ':' + utils.getAPIPort(api) + extra;
};

var genericRequest = function genericRequest(options, extra) {
var p = new Promise();
var p = new Promiz();

request(options, function(err, response, body) {
if (err) {
Expand Down Expand Up @@ -106,7 +106,7 @@ var downloadOfferings = function downloadOfferings(catalog, qstring) {
};

var downloadCatalogOfferings = function downloadCatalogOfferings(catalogs) {
var promise = Promise.resolve();
var promise = Promiz.resolve();
if (catalogs.length) {
catalogs.forEach(function(catalog) {
promise = promise.then(function() {
Expand Down Expand Up @@ -151,16 +151,26 @@ var logAllIndexes = function logAllIndexes(path) {
});
};

indexes
.init()
.then(downloadProducts)
.then(downloadCatalogs)
.then(downloadInventory)
.then(downloadOrdering)
.then(indexes.close)
.then(() => console.log('All saved!'))
.catch((e) => console.log('Error: ', e.stack));
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);
Expand Down
78 changes: 57 additions & 21 deletions lib/elastic_indexes.js
Expand Up @@ -41,23 +41,25 @@ exports.init = function () {

elasticClient = new elasticsearch.Client({
hosts: exports.elasticHost,
log: 'debug'
log: 'debug',
apiVersion: config.indexes.apiVersion
});

elasticClient.ping({ requestTimeout: elasticTimeout }).then(function () {
logger.info("Connected to elasticSearch! (" + exports.elasticHost + ")");

buildIndexes(Object.values(exports.siTables)).then(function (indexes) {
buildIndexes(Object.values(exports.siTables)).then((indexes) => {
return indexes;
}, function (err) {
p.reject(err);
throw new Error(err);
});

}, function (e) {
logger.info("Cannot connect to elasticSearch! (" + exports.elasticHost + ")");
return p.reject(e);
});

return p.resolve();
};

Expand All @@ -73,25 +75,37 @@ var elasticExists = function elasticExists(indexName, objectID){
// logger.info(" ---- elasticExists ----");
return elasticClient.exists({
index: indexName,
type: indexName,
//type: indexName,
id: objectID
});
};

var buildIndex = function (ind) {
const buildIndex = function (ind) {
// logger.info(" ---- buildIndex ----");
var p = new Promise();
let p = new Promise();

elasticExists(ind, 1).then(function (exists) {
elasticClient.indices.exists({
index: ind,
//type: ind
}).then((exists) => {
if(!exists) {
elasticClient.create({
elasticClient.indices.create({
index: ind,
type: ind,
id: 1,
body: {}
});
//type: ind,
body: {
mappings: {
properties: {
body: {
type: 'text',
analyzer: 'english',
search_analyzer: 'english'
}
}
}
}
})
}
}, function (err) {
}, (err) => {
p.reject(err);
});

Expand All @@ -118,21 +132,27 @@ exports.saveIndex = function saveIndex(path, batchdata, opt) {
batch.state = batch.state.toLowerCase();
}

if (batch.body) {
batch.body = batch.body.join(' ');
}

elasticExists(path, batch.originalId).then(exists => {
if (!exists) {
elasticClient.create({
let doc = {
index: path,
type: path,
//type: path,
id: batch.originalId,
body: batch
}).then(function(body){},
};

elasticClient.create(doc).then(function(body){},
function(error){
p.reject(error);
});
} else {
elasticClient.update({
index: path,
type: path,
//type: path,
id: batch.originalId,
body: { doc: batch }
}).then(function(body) {},
Expand Down Expand Up @@ -167,16 +187,30 @@ var searchElastic = function searchElastic(path, originalQuery) {
}

Object.keys(proccesedQuery.query).forEach(q => {
var key = proccesedQuery.query[q];
let key = proccesedQuery.query[q];

if (Object.keys(key)[0] === "AND") {
key = proccesedQuery.query[q]["AND"];
}
stringQuery = stringQuery.concat("(");

queryFields = []
Object.keys(key).forEach(k => {
queryFields.push(k);
});
});

// Sort query fields
queryFields.sort((a, b) => a.localeCompare(b));

Object.keys(proccesedQuery.query).forEach(q => {
let key = proccesedQuery.query[q];

if (Object.keys(key)[0] === "AND") {
key = proccesedQuery.query[q]["AND"];
}
stringQuery = stringQuery.concat("(");

queryFields.forEach(k => {
stringQuery = stringQuery.concat(key[k][0] + " AND ");
});

Expand All @@ -187,7 +221,7 @@ var searchElastic = function searchElastic(path, originalQuery) {

var req = {
index: path,
type: path,
//type: path,
sort: [translateSortToElastic(proccesedQuery.sort)],
from: proccesedQuery.offset,
size: proccesedQuery.pageSize,
Expand Down Expand Up @@ -222,7 +256,9 @@ var convertElasticSearch = function convertElasticSearch(req) {

exports.count = function count(path, query) {
return Promise.resolve(searchElastic(path, query)).then( r => {
return r.hits.total;
return r.hits.total.value;
}).catch(err => {
logger.error("Query error");
});
};

Expand Down
15 changes: 11 additions & 4 deletions lib/indexes.js
Expand Up @@ -246,6 +246,7 @@ var convertOffer = function convertOffer(data, user) {
body: searchable,
userId: exports.fixUserId(user.id),
lastUpdate: new Date(data.lastUpdate).getTime(),
isBundle: data.isBundle ? 'T' : 'F',
productSpecification:
typeof data.productSpecification.id !== 'undefined'
? leftPad(data.productSpecification.id, 12, 0)
Expand All @@ -270,11 +271,11 @@ var convertOffer = function convertOffer(data, user) {
}).then((categories) => {
initialData.categoriesId = categories.map((x) => leftPad(x.id, 12, 0));
initialData.categoriesName = categories.map((x) => md5(x.name.toLowerCase()));
return indexCreateData(['href', 'name', 'lifecycleStatus', 'isBundle'], data, initialData);
return indexCreateData(['href', 'name', 'lifecycleStatus'], data, initialData);
});
}

var newData = indexCreateData(['href', 'name', 'lifecycleStatus', 'isBundle'], data, initialData);
var newData = indexCreateData(['href', 'name', 'lifecycleStatus'], data, initialData);

return Promise.resolve(newData);
};
Expand Down Expand Up @@ -457,8 +458,14 @@ exports.genericCreateQuery = function genericCreateQuery(extras, name, f, req) {

extras.forEach((key) => {
if (typeof req.query[key] !== 'undefined') {
var newq = {};
newq[key] = [req.query[key]];
let newq = {};
let value = req.query[key]

if (key == 'isBundle') {
value = req.query[key] == 'true' ? 'T' : 'F';
}

newq[key] = [value];
if (typeof newq[key][0] === 'string') {
newq[key][0] = newq[key][0].toLowerCase();
}
Expand Down

0 comments on commit 8dc6347

Please sign in to comment.