From b3a16a3a7db5d0cc10be6b289b93a89d56d5c229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Mon, 18 Jun 2018 09:43:57 +0200 Subject: [PATCH] Added new data factory --- public/services/data-factory.js | 103 ++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 public/services/data-factory.js diff --git a/public/services/data-factory.js b/public/services/data-factory.js new file mode 100644 index 000000000..4a0646304 --- /dev/null +++ b/public/services/data-factory.js @@ -0,0 +1,103 @@ +/* + * Wazuh app - Wazuh data factory + * Copyright (C) 2018 Wazuh, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Find more information about this on the LICENSE file. + */ + +export default class DataFactory { + constructor(httpClient, path){ + this.httpClient = httpClient; + this.items = []; + this.path = path; + this.filters = []; + this.sortValue = false; + this.sortDir = false; + this.sortValue = false; + } + + addSorting(value) { + this.sortValue = value; + this.sortDir = !this.sortDir; + } + + removeFilters(){ + this.filters = []; + } + + serializeFilters(parameters) { + if(this.sortValue){ + parameters.sort = this.sortDir ? '-' + this.sortValue : this.sortValue; + } + + for(const filter of this.filters){ + if (filter.value !== '') parameters[filter.name] = filter.value; + } + } + + addFilter (filterName, value) { + this.filters = this.filters.filter(filter => filter.name !== filterName); + + if(typeof value !== 'undefined'){ + this.filters.push({ + name: filterName, + value: value + }); + } + } + + async fetch(options = {}) { + try { + this.items = []; + let offset = 0; + const limit = options.limit || 2000; + const parameters = { limit,offset }; + + this.serializeFilters(parameters); + + + const firstPage = await this.httpClient.request('GET', this.path, parameters) + + this.items.push(...firstPage.data.data.items) + if(options.limit) { + if(this.path === '/agents') this.items = this.items.filter(item => item.id !== '000') + return this.items; + } + + const totalItems = firstPage.data.data.totalItems; + const remaining = totalItems-firstPage.data.data.items.length; + + const float_ops_number = remaining / limit; + const int_ops_number = parseInt(float_ops_number); + const ops_number = int_ops_number < float_ops_number ? int_ops_number + 1 : int_ops_number; + + const ops = [] + for(let i=0; i this.items.push(...page.data.data.items)) + if(this.path === '/agents') this.items = this.items.filter(item => item.id !== '000') + return this.items; + + } catch (error) { + return Promise.reject(error); + } + } + + reset() { + this.items = []; + this.path = path; + this.filters = []; + this.sortValue = false; + this.sortDir = false; + this.sortValue = false; + } +} \ No newline at end of file