Implement layout and device list page
This commit is contained in:
1 parent
f33717acb0
commit
1c9061ab5a
19 files changed
+1151
-30
No files matched your search
+51
-4
@@ -6,6 +6,7 @@ const url = require("url");
|
||||
const querystring = require("querystring");
|
||||
|
||||
const config = require("./config");
|
||||
const device = require("./device");
|
||||
|
||||
const commaAscii = ",".charCodeAt(0);
|
||||
const newLineAscii = "\n".charCodeAt(0);
|
||||
@@ -19,6 +20,7 @@ function filtersToQuery(filters) {
|
||||
for (let [k, v] of Object.entries(filter)) {
|
||||
let [param, op] = k.split(/([^a-zA-Z0-9\-_.].*)/, 2);
|
||||
if (!op || op === "=") q[param] = v;
|
||||
else if (op === ">") q[param] = { $gt: v };
|
||||
else throw new Error(`Operator "${op}" not recognized`);
|
||||
}
|
||||
queries.push(q);
|
||||
@@ -28,22 +30,63 @@ function filtersToQuery(filters) {
|
||||
else return { $or: queries };
|
||||
}
|
||||
|
||||
function count(resource, filters, limit) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let qs = {};
|
||||
if (filters) {
|
||||
let q = filtersToQuery(filters);
|
||||
if (resource === "devices") q = device.transposeQuery(q);
|
||||
qs.query = JSON.stringify(q);
|
||||
}
|
||||
|
||||
if (limit) qs.limit = limit;
|
||||
|
||||
let options = url.parse(
|
||||
`${config.GENIEACS_NBI}${resource}?${querystring.stringify(qs)}`
|
||||
);
|
||||
|
||||
options.method = "HEAD";
|
||||
|
||||
let _http = options.protocol === "https:" ? https : http;
|
||||
|
||||
_http
|
||||
.request(options, res => {
|
||||
if (res.statusCode !== 200) {
|
||||
reject(new Error(`Unexpected status code ${res.statusCode}`));
|
||||
res.resume();
|
||||
return;
|
||||
}
|
||||
resolve(+res.headers["total"]);
|
||||
})
|
||||
.end();
|
||||
});
|
||||
}
|
||||
|
||||
function query(resource, filters, limit, callback) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let ret;
|
||||
if (!callback) ret = [];
|
||||
let q = {};
|
||||
if (filters) q.query = JSON.stringify(filtersToQuery(filters));
|
||||
let qs = {};
|
||||
if (filters) {
|
||||
let q = filtersToQuery(filters);
|
||||
if (resource === "devices") q = device.transposeQuery(q);
|
||||
qs.query = JSON.stringify(q);
|
||||
}
|
||||
|
||||
if (limit) q.limit = limit;
|
||||
if (limit) qs.limit = limit;
|
||||
|
||||
let options = url.parse(
|
||||
`${config.GENIEACS_NBI}${resource}?${querystring.stringify(q)}`
|
||||
`${config.GENIEACS_NBI}${resource}?${querystring.stringify(qs)}`
|
||||
);
|
||||
|
||||
let _http = options.protocol === "https:" ? https : http;
|
||||
|
||||
_http.get(options, res => {
|
||||
if (res.statusCode !== 200) {
|
||||
reject(new Error(`Unexpected status code ${res.statusCode}`));
|
||||
res.resume();
|
||||
return;
|
||||
}
|
||||
let chunks = [];
|
||||
let bytes = 0;
|
||||
res.on("data", chunk => {
|
||||
@@ -69,6 +112,7 @@ function query(resource, filters, limit, callback) {
|
||||
|
||||
if (buf.length > 1) {
|
||||
let obj = JSON.parse(buf);
|
||||
if (resource === "devices") obj = device.transpose(obj);
|
||||
if (ret) ret.push(obj);
|
||||
else callback(obj);
|
||||
}
|
||||
@@ -89,6 +133,8 @@ function query(resource, filters, limit, callback) {
|
||||
}
|
||||
if (buf.length > 1) {
|
||||
let obj = JSON.parse(buf);
|
||||
if (resource === "devices") obj = device.transpose(obj);
|
||||
|
||||
if (ret) ret.push(obj);
|
||||
else callback(obj);
|
||||
}
|
||||
@@ -104,3 +150,4 @@ function query(resource, filters, limit, callback) {
|
||||
}
|
||||
|
||||
exports.query = query;
|
||||
exports.count = count;
|
||||
Reference in new issue
Block a user