Commit 2405ccff authored by Roque's avatar Roque

[with mock data] extend monitoring app storage

- use slapos master as remote sub storage
- handle all data maniputation within the storage
- sync cleans objects of non-present slapos masters
- add slapos_master_url in all opml tree objects
- new storage layer to handle storage url and limit
- separate replicate and webhttp storage files
- extend test coverage
parent d5ae73d6
/*
* Copyright 2016, Nexedi SA
* Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html
*/
/*jslint nomen: true*/
/*global jIO, RSVP */
(function (jIO, RSVP) {
"use strict";
var LIMIT = 100; //default
/**
* Monitor erp5 layer to wrap erp5 storages for monitor app
*
* @class ERP55Monitor
* @constructor
*/
function ERP55Monitor(spec) {
if (!spec.sub_storage || spec.sub_storage.type !== 'erp5') {
throw new TypeError("ERP55Monitor subtorage must be erp5 type");
}
if (spec.limit) {
LIMIT = spec.limit;
}
this._storage_definition = spec.sub_storage;
this._sub_storage = jIO.createJIO(spec.sub_storage);
}
ERP55Monitor.prototype.get = function (id) {
return this._sub_storage.get.apply(this._sub_storage, arguments);
};
ERP55Monitor.prototype.post = function () {
return this._sub_storage.post.apply(this._sub_storage, arguments);
};
ERP55Monitor.prototype.put = function () {
return this._sub_storage.put.apply(this._sub_storage, arguments);
};
ERP55Monitor.prototype.remove = function () {
return this._sub_storage.remove.apply(this._sub_storage, arguments);
};
ERP55Monitor.prototype.hasCapacity = function (capacity) {
return (capacity === "list") || (capacity === "limit") || (capacity === "include") || (capacity === "query") || (capacity === "select");
};
ERP55Monitor.prototype.buildQuery = function () {
var sub_storage = this._sub_storage, args = arguments, master_url = this._storage_definition.url, i;
if (!arguments[0].limit) {
arguments[0].limit = [0, LIMIT];
}
return new RSVP.Queue()
.push(function () {
return sub_storage.buildQuery.apply(sub_storage, args);
})
.push(function (result) {
for (i = 0; i < result.length; i += 1) {
result[i].master_url = master_url;
}
return result;
});
};
jIO.addStorage('erp5monitor', ERP55Monitor);
}(jIO, RSVP));
\ No newline at end of file
This diff is collapsed.
<!DOCTYPE html>
<!--
Copyright 2014, Nexedi SA
This program is free software: you can Use, Study, Modify and Redistribute
it under the terms of the GNU General Public License version 3, or (at your
option) any later version, as published by the Free Software Foundation.
You can also Link and Combine this program with other software covered by
the terms of any of the Free Software licenses or any of the Open Source
Initiative approved licenses and Convey the resulting work. Corresponding
source of such a combination shall include the source code for all other
software used.
This program is distributed WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See COPYING file for full licensing terms.
See https://www.nexedi.com/licensing for rationale and options.
-->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jIO Coverage for Monitoring union storage</title>
<script src="../external/rsvp-2.0.4.js"></script>
<script src="../external/renderjs-latest.js"></script>
<script src="../dist/jio-latest.js"></script>
<link rel="stylesheet" href="../external/qunit.css" type="text/css" media="screen"/>
<script src="../external/qunit.js" type="text/javascript"></script>
<script src="monitoring-union.js"></script>
<script src="replicateopml.js"></script>
<script src="webhttpstorage.js"></script>
<script src="erp5monitorapp.js"></script>
</head>
<body>
<h1 id="qunit-header">jIO Coverage Monitoring union storage</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
</body>
</html>
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*
* Copyright 2016, Nexedi SA
* Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html
*/
/*jslint nomen: true*/
/*global jIO, RSVP */
(function (jIO, RSVP) {
"use strict";
function ajax(storage, options) {
if (options === undefined) {
options = {};
}
if (storage._authorization !== undefined) {
if (options.headers === undefined) {
options.headers = {};
}
options.headers.Authorization = storage._authorization;
}
if (storage._with_credentials !== undefined) {
if (options.xhrFields === undefined) {
options.xhrFields = {};
}
options.xhrFields.withCredentials = storage._with_credentials;
}
if (storage._timeout !== undefined) {
options.timeout = storage._timeout;
}
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax(options);
});
}
function restrictDocumentId(id) {
var slash_index = id.indexOf("/");
if (slash_index !== 0 && slash_index !== -1) {
throw new jIO.util.jIOError("id " + id + " is forbidden (no begin /)",
400);
}
if (id.lastIndexOf("/") === (id.length - 1)) {
throw new jIO.util.jIOError("id " + id + " is forbidden (no end /)",
400);
}
return id;
}
function getJsonDocument(context, id) {
return new RSVP.Queue()
.push(function () {
return ajax(context, {
type: "GET",
url: context._url + "/" + id + ".json",
dataType: "text"
});
})
.push(function (response) {
return {id: id, doc: JSON.parse(response.target.responseText)};
}, function (error) {
if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find document '" + id + "'", 404);
}
throw error;
});
}
/**
* The JIO WEB HTTP Storage extension
*
* @class WEBHTTPStorage
* @constructor
*/
function WEBHTTPStorage(spec) {
if (typeof spec.url !== 'string') {
throw new TypeError("WEBHTTPStorage 'url' is not of type string");
}
this._url = spec.url.replace(new RegExp("[/]+$"), "");
if (typeof spec.basic_login === 'string') {
this._authorization = "Basic " + spec.basic_login;
}
this._with_credentials = spec.with_credentials;
this._timeout = spec.timeout;
}
WEBHTTPStorage.prototype.get = function (id) {
var context = this;
id = restrictDocumentId(id);
return getJsonDocument(context, id)
.push(function (element) {
return element.doc;
}, function (error) {
if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
throw error;
});
};
WEBHTTPStorage.prototype.hasCapacity = function (capacity) {
return (capacity === "list") || (capacity === "include");
};
WEBHTTPStorage.prototype.buildQuery = function (options) {
var context = this,
item_list = [],
push_item;
if (options.include_docs === true) {
push_item = function (id, item) {
item_list.push({
"id": id,
"value": {},
"doc": item
});
};
} else {
push_item = function (id) {
item_list.push({
"id": id,
"value": {}
});
};
}
return new RSVP.Queue()
.push(function () {
return ajax(context, {
type: "GET",
url: context._url + "/_document_list",
dataType: "text"
});
})
.push(function (response) {
var document_list = [],
promise_list = [],
i;
document_list = response.target.responseText.split('\n');
for (i = 0; i < document_list.length; i += 1) {
if (document_list[i]) {
promise_list.push(getJsonDocument(context, document_list[i]));
}
}
return RSVP.all(promise_list);
}, function (error) {
if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
throw error;
})
.push(function (result_list) {
var i;
for (i = 0; i < result_list.length; i += 1) {
push_item(result_list[i].id, result_list[i].doc);
}
return item_list;
});
};
jIO.addStorage('webhttp', WEBHTTPStorage);
}(jIO, RSVP));
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment