@publidata/utils-data-manager
Version:
Collection of methods to extract data from publidata
301 lines (251 loc) • 9.01 kB
JavaScript
const DataSource = require("../Source");
const { isEmpty } = require("lodash");
const { encode } = require("@publidata/utils-elasticsearch");
const { getId } = require("@publidata/utils-mapper");
const { instanciatePublidataObject } = require("../types");
const DATA_STORE_MARKERS_MAX_CAPACITY = 3000;
const DATA_STORE_OBJECTS_MAX_CAPACITY = 3000;
const DATA_STORE_COLLECTIONS_MAX_CAPACITY = 100;
class DataManager {
constructor(isDemo, uid, instance, storeData = true) {
instance.custom_settings.url ||= {};
instance.custom_settings.url.analytics ||= "https://analytics.publidata.io";
instance.custom_settings.url.api ||= "https://api.publidata.io";
instance.custom_settings.url.app ||= "https://next.publidata.io";
instance.custom_settings.url.beacon ||= "https://beacon.publidata.io";
instance.custom_settings.url.booking ||= "https://booking.publidata.io";
instance.custom_settings.url.cdn ||= "https://cdn.publidata.io";
instance.custom_settings.url.citizen ||= "https://citizen.publidata.io";
instance.custom_settings.url.form ||= "https://form.publidata.io";
instance.custom_settings.url.forms ||= "https://forms.publidata.io";
instance.custom_settings.url.medias ||= "https://medias.publidata.io";
instance.custom_settings.url.medias_host ||=
"https://publidata-prod.s3.amazonaws.com";
instance.custom_settings.url.utils ||= "https://utils.publidata.io";
instance.custom_settings.url.widget ||= "https://next.publidata.io";
this.instance = instance;
this.source = new DataSource(isDemo, uid, instance);
this.dataStore = {
collections: {
data: new Map(),
maxCapacity: DATA_STORE_COLLECTIONS_MAX_CAPACITY
},
objects: {
data: new Map(),
maxCapacity: DATA_STORE_OBJECTS_MAX_CAPACITY
},
markers: {
data: new Map(),
maxCapacity: DATA_STORE_MARKERS_MAX_CAPACITY
}
};
this.storeData = storeData;
}
verifyDataStoreCapacity(dataStoreType) {
if (dataStoreType.data.length)
return dataStoreType.data.length < dataStoreType.maxCapacity;
return true;
}
removeFirstItemFromDataStore(dataStoreType) {
const keysIterator = dataStoreType.data.keys();
const firstKey = keysIterator.next().value;
dataStoreType.data.delete(firstKey);
}
saveDataInDataStore(uri, value, marker) {
if (!this.storeData) return;
const { collections, objects, markers } = this.dataStore;
if (!this.verifyDataStoreCapacity(collections))
this.removeFirstItemFromDataStore(collections);
collections.data.set(uri, value);
if (marker) {
if (!this.verifyDataStoreCapacity(markers))
this.removeFirstItemFromDataStore(markers);
if (Array.isArray(value.data)) {
value.data.forEach(object => {
const uid = getId(object);
markers.data.set(uid, [object]);
});
} else {
const uid = getId(value.data);
markers.data.set(uid, [value.data]);
}
} else {
if (!this.verifyDataStoreCapacity(objects))
this.removeFirstItemFromDataStore(objects);
if (Array.isArray(value.data)) {
value.data.forEach(object => {
const uid = getId(object);
const data = objects.data.get(uid) || {};
const methodName = uri.split("methodName=")[1].split("&")[0];
data[methodName] = [object];
objects.data.set(uid, data);
});
} else {
const uid = getId(value.data);
const data = objects.data.get(uid) || {};
const methodName = uri.split("methodName=")[1].split("&")[0];
data[methodName] = [value.data];
objects.data.set(uid, data);
}
}
}
getDataFromDataStore(uri, uid, marker) {
const { collections, objects, markers } = this.dataStore;
const dataInCollections = collections.data.get(uri);
if (dataInCollections) return dataInCollections;
if (marker) {
const dataInMarkers = markers.data.get(uid);
if (dataInMarkers) return dataInMarkers;
} else {
const dataInObjects = objects.data.get(uid);
const methodName = uri.split("methodName=")[1].split("&")[0];
if (dataInObjects && dataInObjects[0]) return dataInObjects;
}
return null;
}
getObject(params, getMethodName) {
const uri = encode({ methodName: getMethodName, ...params });
const { ids, marker } = params;
let object = null;
const uid = !isEmpty(ids) && ids.length === 1 ? parseInt(ids[0], 10) : null;
if (this.storeData) object = this.getDataFromDataStore(uri, uid, marker);
if (object)
return new Promise(resolve => {
if (Array.isArray(object)) {
resolve({ data: object });
} else {
resolve(object);
}
});
return new Promise((resolve, reject) => {
this.source[getMethodName](params)
.then(response => {
let resultAsPublidataObject = response.data;
if (Array.isArray(response.data)) {
resultAsPublidataObject = response.data.map(value =>
instanciatePublidataObject(value, this.instance, this)
);
}
const responseWithPublidataObject = {
...response,
data: resultAsPublidataObject
};
this.saveDataInDataStore(uri, responseWithPublidataObject, marker);
resolve(responseWithPublidataObject);
})
.catch(err => reject(err));
});
}
getData(params) {
return this.getObject(params, "getData");
}
getInstance() {
return this.getObject({ instances: [this.instanceId] }, "getInstance");
}
getWasteCollections(params = {}) {
return this.getObject(params, "getWasteCollections");
}
getWasteCollection(params = {}) {
return this.getObject(params, "getWasteCollections");
}
getWasteCollectionById(params = {}) {
return this.getObject(params, "getWasteCollectionById");
}
getWasteCollectionFacilities(params = {}) {
return this.getObject(params, "getWasteCollectionFacilities");
}
getWasteCollectionFacility(params = {}) {
return this.getObject(params, "getWasteCollectionFacility");
}
getRecyclingBin(params = {}) {
return this.getObject(params, "getRecyclingBin");
}
getRecyclingBins(params = {}) {
return this.getObject(params, "getRecyclingBins");
}
getRecyclingCenters(params = {}) {
return this.getObject(params, "getRecyclingCenters");
}
getRecyclingCenter(params = {}) {
return this.getObject(params, "getRecyclingCenter");
}
getFacilities(params = {}) {
return this.getObject(params, "getFacilities");
}
getFacilityById(params = {}) {
return this.getObject(params, "getFacilityById");
}
getFacilitiesByType(params = {}) {
return this.getObject(params, "getFacilitiesByType");
}
getFacilityByType(params = {}) {
return this.getObject(params, "getFacilityByType");
}
getFacilityByTypeAndId(params = {}) {
return this.getObject(params, "getFacilityByTypeAndId");
}
getServiceFacilityAggregations(params = {}) {
return this.getObject(params, "getServiceFacilityAggregations");
}
getCanteenMenuServices(params = {}) {
return this.getObject(params, "getCanteenMenuServices");
}
getCanteenMenuById(params = {}) {
return this.getObject(params, "getCanteenMenuById");
}
getMealsByMenuId(params = {}) {
return this.getObject(params, "getMealsByMenuId");
}
getDocumentsByInstanceId(params = {}) {
return this.getObject(params, "getDocumentsByInstanceId");
}
getDocumentsByIds(params = {}) {
return this.getObject(params, "getDocumentsByIds");
}
getProceduresByIds(params = {}) {
return this.getObject(params, "getProceduresByIds");
}
getRisks(params = {}) {
return this.getObject(params, "getRisks");
}
getPosts(params = {}) {
return this.getObject(params, "getPosts");
}
getNews(params = {}) {
return this.getObject(params, "getNews");
}
getNewsTotalPages(params = {}) {
return this.getObject(params, "getNewsTotalPages");
}
getElected(params = {}) {
return this.getObject(params, "getElected");
}
getCities(params = {}) {
return this.getObject(params, "getCities");
}
getSectors(params = {}) {
return this.getObject(params, "getSectors");
}
getSlots(params = {}) {
return this.getObject(params, "getSlots");
}
getItems(params = {}) {
return this.getObject(params, "getItems");
}
getAccountById(params = {}) {
return this.getObject(params, "getAccountById");
}
getAlerts(params = {}) {
return this.getObject(params, "getAlerts");
}
getAlert(params = {}) {
return this.getObject(params, "getAlert");
}
getFacilitiesAlerts(params = {}) {
return this.getObject(params, "getFacilitiesAlerts");
}
getWasteCollectionAlerts(params = {}) {
return this.getObject(params, "getWasteCollectionAlerts");
}
}
module.exports = DataManager;