Compare commits

..
21 Commits
Author SHA1 Message Date
Zaid Abdulla 91d7152ca7 1.2.2 2020-10-03 00:37:35 -07:00
Zaid Abdulla 3fccc1d8a4 Changelog for v1.2.2 2020-10-03 00:36:59 -07:00
Zaid Abdulla 5bb6f60c4d Update dependencies and fix new lint errors 2020-10-02 23:21:07 -07:00
Zaid Abdulla c2f002b95a Better ordering of config YAML 2020-10-02 14:50:17 -07:00
Zaid Abdulla c55a256d52 Show proper login failure message 2020-09-30 15:43:55 -07:00
Zaid Abdulla 3fc0b1740d Limit max page size 2020-09-28 12:47:56 -07:00
Zaid Abdulla 1956441d52 Limit number of tasks in queue 2020-09-28 12:47:15 -07:00
Zaid Abdulla 01e01dae69 Set max height for task queue 2020-09-28 12:40:55 -07:00
Zaid Abdulla f14e6ceb97 Bulk push file from device list page 2020-09-27 11:20:30 -07:00
Zaid Abdulla 82124d471e Fix invalid arguments fault in refresh preset
Happens when upgrading from v1.1 where genieacs-gui creates refresh
configuration with 'age' passed as a string.
2020-09-23 23:15:00 -07:00
Zaid Abdulla 9286bd6aa0 Fix unhandled promise rejections 2020-09-17 17:00:41 -07:00
Zaid Abdulla 328bfa03e0 Allow refreshObject task on root param 2020-09-15 12:50:02 -07:00
Zaid Abdulla de65ca0921 Fix crash when deserializing operation objects 2020-09-14 15:58:47 -07:00
Zaid Abdulla 50e9731920 1.2.1 2020-09-08 21:40:11 -07:00
Zaid Abdulla 97c817f199 Changelog for v1.2.1 2020-09-08 21:39:34 -07:00
Zaid Abdulla 58bb8fdb14 Update MongoDB driver
Fixing this vulnerability https://npmjs.com/advisories/1555
2020-09-08 21:35:56 -07:00
Zaid Abdulla a6f9e3d0de Renaming yebool to espresso-iisojs 2020-09-08 20:11:00 -07:00
Zaid Abdulla 0b60406b43 Add lint rule for unhandled promise rejections 2020-09-08 17:33:48 -07:00
Zaid Abdulla ad682d0b69 Fix deleting items not reflecting in local store 2020-09-08 11:20:02 -07:00
Zaid Abdulla c6f4f9480c Better handling of negation in filterToMongoQuery
Similar to SQL, negating a null should still be null.
2020-09-08 00:13:48 -07:00
Zaid Abdulla dd9cd01681 Fix querying faults in UI showing no results 2020-09-08 00:11:14 -07:00
37 changed files with 2873 additions and 1304 deletions

No files matched your search

+1
View File
@@ -21,6 +21,7 @@
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-use-before-define": ["error", { "functions": false }],
"@typescript-eslint/explicit-function-return-type": ["error", { "allowExpressions": true }],
"@typescript-eslint/no-floating-promises": "error",
"require-atomic-updates": 0,
"no-prototype-builtins": 0
}
+15
View File
@@ -1,5 +1,20 @@
# Change Log
## 1.2.2 (2020-10-03)
- Added button to push files to selected devices from device listing page.
- A few minor UI improvements.
- Fix exception that can happen and persist after a Download request.
- Fix validation bug preventing running refreshObject task on data model root.
- Fix invalid arguments fault in refresh preset configuration when upgrading
from v1.1.
## 1.2.1 (2020-09-08)
- Fix bug causing faults to not be displayed in the UI.
- Fix bug where deleting objects does not get reflected immediately in the UI.
- Improve conversion between filters written in the expression format and
MongoDB queries. There should now be fewer edge cases where the two are not
equisatisfiable.
## 1.2.0 (2020-09-01)
- Support GetParameterAttributes and SetParameterAttributes TR-069 methods.
+1 -1
View File
@@ -43,7 +43,7 @@ function exitWorkerGracefully(): void {
}
function exitWorkerUngracefully(): void {
extensions.killAll().then(() => {
extensions.killAll().finally(() => {
process.exit(1);
});
}
+1 -1
View File
@@ -43,7 +43,7 @@ function exitWorkerGracefully(): void {
}
function exitWorkerUngracefully(): void {
extensions.killAll().then(() => {
extensions.killAll().finally(() => {
process.exit(1);
});
}
+4 -2
View File
@@ -45,7 +45,7 @@ function exitWorkerGracefully(): void {
}
function exitWorkerUngracefully(): void {
extensions.killAll().then(() => {
extensions.killAll().finally(() => {
process.exit(1);
});
}
@@ -99,7 +99,9 @@ if (!cluster.worker) {
const _listener = (req, res): void => {
if (stopping) res.setHeader("Connection", "close");
listener(req, res);
listener(req, res).catch((err) => {
throw err;
});
};
const initPromise = Promise.all([db2.connect(), cache.connect()])
+9 -5
View File
@@ -81,7 +81,7 @@ const externals = [
"codemirror/mode/yaml/yaml",
"ipaddr.js",
"jsbi",
"yebool",
"espresso-iisojs",
];
function rmDirSync(dirPath): void {
@@ -297,7 +297,7 @@ async function generateFrontendJs(): Promise<void> {
const inputFile = path.resolve(INPUT_DIR, "ui/app.ts");
const outputFile = path.resolve(OUTPUT_DIR, "public/app.js");
const inlineDeps = ["mithril", "parsimmon", "jsbi", "yebool"];
const inlineDeps = ["mithril", "parsimmon", "jsbi", "espresso-iisojs"];
const bundle = await rollup({
input: inputFile,
external: externals.filter((e) => !inlineDeps.includes(e)),
@@ -306,10 +306,14 @@ async function generateFrontendJs(): Promise<void> {
{
name: "",
resolveId: function (importee, importer) {
if (importee.endsWith("/bigint"))
if (importee.endsWith("/bigint")) {
return this.resolve(importee + "-jsbi", importer);
else if (importee === "yebool")
return this.resolve("yebool/dist/yebool-jsbi.mjs", importer);
} else if (importee === "espresso-iisojs") {
return this.resolve(
"espresso-iisojs/dist/espresso-iisojs-jsbi.mjs",
importer
);
}
return null;
},
},
+4
View File
@@ -236,6 +236,10 @@ function sanitizeTask(task): void {
break;
case "refreshObject":
if (typeof task.objectName !== "string")
throw new Error("Missing 'objectName' property");
break;
case "deleteObject":
if (typeof task.objectName !== "string" || !task.objectName.length)
throw new Error("Missing 'objectName' property");
+1 -1
View File
@@ -18,7 +18,7 @@
*/
import * as BI from "./bigint";
import { espresso, complement, tautology } from "yebool";
import { espresso, complement, tautology } from "espresso-iisojs";
import { Expression } from "../types";
import { map } from "./expression-parser";
import { and, evaluateCallback } from "./expression";
+2 -2
View File
@@ -42,7 +42,7 @@ function interpretEscapes(str): string {
export function map(
exp: Expression,
callback: (exp: Expression) => Expression
callback: (e: Expression) => Expression
): Expression {
if (!Array.isArray(exp)) return callback(exp);
@@ -60,7 +60,7 @@ export function map(
export async function mapAsync(
exp: Expression,
callback: (exp: Expression) => Promise<Expression>
callback: (e: Expression) => Promise<Expression>
): Promise<Expression> {
if (!Array.isArray(exp)) return callback(exp);
+9 -9
View File
@@ -264,21 +264,21 @@ export function evaluateCallback(exp: Expression): Expression {
export function evaluate(
exp: Expression,
obj: Record<string, unknown> | ((exp: string) => Expression),
obj: Record<string, unknown> | ((e: string) => Expression),
now: number,
cb?: (exp: Expression) => Expression
cb?: (e: Expression) => Expression
): string | number | boolean | null;
export function evaluate(
exp: Expression,
obj?: Record<string, unknown> | ((exp: string) => Expression),
obj?: Record<string, unknown> | ((e: string) => Expression),
now?: number,
cb?: (exp: Expression) => Expression
cb?: (e: Expression) => Expression
): Expression;
export function evaluate(
exp: Expression,
obj?: Record<string, unknown> | ((exp: string) => Expression),
obj?: Record<string, unknown> | ((e: string) => Expression),
now?: number,
cb?: (exp: Expression) => Expression
cb?: (e: Expression) => Expression
): Expression {
return map(exp, (e) => {
if (cb) e = cb(e);
@@ -306,19 +306,19 @@ export async function evaluateAsync(
exp: Expression,
obj: Record<string, unknown>,
now: number,
cb?: (exp: Expression) => Promise<Expression>
cb?: (e: Expression) => Promise<Expression>
): Promise<string | number | boolean | null>;
export async function evaluateAsync(
exp: Expression,
obj?: Record<string, unknown>,
now?: number,
cb?: (exp: Expression) => Promise<Expression>
cb?: (e: Expression) => Promise<Expression>
): Promise<Expression>;
export async function evaluateAsync(
exp: Expression,
obj?: Record<string, unknown>,
now?: number,
cb?: (exp: Expression) => Promise<Expression>
cb?: (e: Expression) => Promise<Expression>
): Promise<Expression> {
return mapAsync(exp, async (e) => {
if (cb) e = await cb(e);
+7 -2
View File
@@ -1211,12 +1211,17 @@ export function listener(
})
.catch((err) => {
currentSessions.delete(httpResponse.connection);
httpResponse.writeHead(500, { Connection: "close" });
httpResponse.end(`${err.name}: ${err.message}`);
stats.concurrentRequests -= 1;
setTimeout(() => {
throw err;
});
try {
httpResponse.connection.unref();
httpResponse.writeHead(500, { Connection: "close" });
httpResponse.end(`${err.name}: ${err.message}`);
} catch (err) {
// Ignore
}
});
}
+6 -1
View File
@@ -691,6 +691,11 @@ export async function getOperations(
for (const r of res) {
const commandKey = r._id.slice(deviceId.length + 1);
delete r._id;
// Workaround for a bug in v1.2.1 where operation object is saved without deserialization
if (typeof r.provisions !== "string") {
operations[commandKey] = r;
continue;
}
r.timestamp = +r.timestamp;
if (r.args) r.args = JSON.parse(r.args);
r.provisions = JSON.parse(r.provisions);
@@ -712,7 +717,7 @@ export async function saveOperation(
o["provisions"] = JSON.stringify(operation.provisions);
o["retries"] = JSON.stringify(operation.retries);
o["args"] = JSON.stringify(operation.args);
await operationsCollection.replaceOne({ _id: id }, operation, {
await operationsCollection.replaceOne({ _id: id }, o, {
upsert: true,
});
}
+1
View File
@@ -58,6 +58,7 @@ export function run(args: string[]): Promise<{ fault: Fault; value: any }> {
});
}
// eslint-disable-next-line @typescript-eslint/no-floating-promises
kill(processes[scriptName]);
delete processes[scriptName];
}
+6 -4
View File
@@ -22,7 +22,7 @@ import * as crypto from "crypto";
import * as config from "./config";
import * as db from "./db";
import * as cache from "./cache";
import { mongoQueryToFilter } from "./mongodb-functions";
import { convertOldPrecondition } from "./mongodb-functions";
import * as logger from "./logger";
import * as scheduling from "./scheduling";
import * as expression from "./common/expression";
@@ -173,7 +173,9 @@ async function fetchPresets(): Promise<Preset[]> {
try {
precondition = parse(preset["precondition"]);
} catch (error) {
precondition = mongoQueryToFilter(JSON.parse(preset["precondition"]));
precondition = convertOldPrecondition(
JSON.parse(preset["precondition"])
);
}
// Simplify expression
@@ -186,7 +188,7 @@ async function fetchPresets(): Promise<Preset[]> {
for (const c of preset["configurations"]) {
switch (c.type) {
case "age":
_provisions.push(["refresh", c.name, c.age]);
_provisions.push(["refresh", c.name, +c.age]);
break;
case "value":
@@ -473,7 +475,7 @@ export function getConfig(
key: string,
context: Record<string, unknown>,
now: number,
cb?: (Expression) => Expression
cb?: (e: Expression) => Expression
): string | number | boolean | null {
const snapshot = snapshots.get(snapshotKey);
if (!snapshot) throw new Error("Cache snapshot does not exist");
+166 -166
View File
@@ -19,7 +19,7 @@
import { ObjectID } from "mongodb";
import { map, parse, stringify, parseList } from "./common/expression-parser";
import { likePatternToRegExp, evaluate } from "./common/expression";
import { likePatternToRegExp } from "./common/expression";
import { Expression, Fault, Task } from "./types";
const isArray = Array.isArray;
@@ -44,12 +44,10 @@ export function processDeviceFilter(filter: Expression): Expression {
exp[1][1].startsWith("Tags.")
) {
const t = exp[1][1].slice(5);
if (exp[0] === "IS NULL") return ["<>", ["PARAM", "_tags"], t];
else if (exp[0] === "IS NOT NULL") return ["=", ["PARAM", "_tags"], t];
else if (exp[0] === "=" && exp[2] === true)
return ["=", ["PARAM", "_tags"], t];
else if (exp[0] === "<>" && exp[2] !== true)
return ["=", ["PARAM", "_tags"], t];
if (exp[0] === "IS NULL") return ["_tags", t, false];
else if (exp[0] === "IS NOT NULL") return ["_tags", t, true];
else if (exp[0] === "=" && exp[2] === true) return ["_tags", t, true];
else if (exp[0] === "<>" && exp[2] !== true) return ["_tags", t, true];
} else if (
[
"=",
@@ -117,140 +115,131 @@ export function processFaultsFilter(filter: Expression): Expression {
}
export function filterToMongoQuery(exp: Expression): Record<string, unknown> {
const ops = {
OR: 0,
AND: 0,
NOT: 0,
"=": 1,
"<>": 1,
">": 1,
">=": 1,
"<": 1,
"<=": 1,
LIKE: 1,
"NOT LIKE": 1,
"IS NULL": 1,
"IS NOT NULL": 1,
};
function recursive(filter, negate, res = {}): Record<string, unknown> {
function recursive(filter, negate): Record<string, unknown> {
const op = filter[0];
if (ops[op] === 0) {
if (op === "AND") filter = filter.filter((f) => f !== true);
if (op === "OR") filter = filter.filter((f) => f !== false);
if (filter.length === 2) return recursive(filter[1], negate, res);
for (let i = 1; i < filter.length; ++i) {
if (!isArray(filter[i]) || ops[filter[i][0]] == null)
throw new Error(`Invalid expression in ${op} clause`);
}
if ((!negate && op === "AND") || (negate && op === "OR")) {
res["$and"] = res["$and"] || [];
for (let i = 1; i < filter.length; ++i)
res["$and"].push(recursive(filter[i], negate));
} else if ((!negate && op === "OR") || (negate && op === "AND")) {
res["$or"] = res["$or"] || [];
for (let i = 1; i < filter.length; ++i)
res["$or"].push(recursive(filter[i], negate));
} else if (op === "NOT") {
recursive(filter[1], !negate, res);
}
} else if (ops[op] === 1) {
if (isArray(filter[2])) {
throw new Error(`Right operand of ${op} clause is not a primitive`);
} else if (
["LIKE", "NOT LIKE"].includes(op) &&
isArray(filter[1]) &&
filter[1][0] === "FUNC" &&
["UPPER", "LOWER"].includes(filter[1][1])
) {
if (
(filter[1][1] === "UPPER" && filter[2] !== filter[2].toUpperCase()) ||
(filter[1][1] === "LOWER" && filter[2] !== filter[2].toLowerCase())
) {
throw new Error(
`Cannot compare ${
filter[1][1]
}() against non-${filter[1][1].toLowerCase()}case pattern`
);
}
} else if (!isArray(filter[1]) || filter[1][0] !== "PARAM") {
throw new Error(`Left operand of ${op} clause is not a parameter`);
}
if (op === "=") {
const param = filter[1][1];
const p = (res[param] = res[param] || {});
if (negate) p["$ne"] = filter[2];
else p["$eq"] = filter[2];
} else if (op === "<>") {
const param = filter[1][1];
let p = (res[param] = res[param] || {});
if (negate) p = p["$not"] = p["$not"] || {};
p["$ne"] = filter[2];
if (param !== "_tags") p["$exists"] = true;
} else if (op === ">") {
const param = filter[1][1];
let p = (res[param] = res[param] || {});
if (negate) p = p["$not"] = p["$not"] || {};
p["$gt"] = filter[2];
} else if (op === ">=") {
const param = filter[1][1];
let p = (res[param] = res[param] || {});
if (negate) p = p["$not"] = p["$not"] || {};
p["$gte"] = filter[2];
} else if (op === "<") {
const param = filter[1][1];
let p = (res[param] = res[param] || {});
if (negate) p = p["$not"] = p["$not"] || {};
p["$lt"] = filter[2];
} else if (op === "<=") {
const param = filter[1][1];
let p = (res[param] = res[param] || {});
if (negate) p = p["$not"] = p["$not"] || {};
p["$lte"] = filter[2];
} else if (op === "IS NULL") {
const param = filter[1][1];
res[param] = { $exists: negate };
} else if (op === "IS NOT NULL") {
const param = filter[1][1];
res[param] = { $exists: !negate };
} else {
if (op === "NOT LIKE") negate = !negate;
let param;
let flags;
if (filter[1][0] === "FUNC" && filter[1][1] === "UPPER") {
param = filter[1][2][1];
flags = "i";
} else if (filter[1][0] === "FUNC" && filter[1][1] === "LOWER") {
param = filter[1][2][1];
flags = "i";
} else {
param = filter[1][1];
flags = "";
}
const r = likePatternToRegExp(filter[2], filter[3], flags);
if (negate) res[param] = { $not: r };
else res[param] = r;
}
} else {
throw new Error(`Unrecognized operator ${op}`);
if (op === "AND") {
filter = filter.filter((f) => f !== true);
if (filter.length === 2) return recursive(filter[1], negate);
}
return res;
if (op === "OR") {
filter = filter.filter((f) => f !== false);
if (filter.length === 2) return recursive(filter[1], negate);
}
if ((!negate && op === "AND") || (negate && op === "OR"))
return { $and: filter.slice(1).map((f) => recursive(f, negate)) };
if ((!negate && op === "OR") || (negate && op === "AND"))
return { $or: filter.slice(1).map((f) => recursive(f, negate)) };
if (op === "NOT") return recursive(filter[1], !negate);
if (isArray(filter[2]))
throw new Error(`Invalid RHS operand of ${op} clause`);
if (op === "LIKE" || op === "NOT LIKE") {
if (op === "NOT LIKE") negate = !negate;
let param;
let flags;
if (!Array.isArray(filter[1]))
throw new Error(`Invalid LHS operand of ${op} clause`);
if (filter[1][0] === "FUNC" && filter[1][1] === "UPPER") {
if (filter[2] !== filter[2].toUpperCase())
throw new Error(`Invalid RHS operand of ${op} clause`);
param = filter[1][2][1];
flags = "i";
} else if (filter[1][0] === "FUNC" && filter[1][1] === "LOWER") {
if (filter[2] !== filter[2].toLowerCase())
throw new Error(`Invalid RHS operand of ${op} clause`);
param = filter[1][2][1];
flags = "i";
} else if (filter[1][0] === "PARAM") {
param = filter[1][1];
flags = "";
} else {
throw new Error(`Invalid LHS operand of ${op} clause`);
}
const r = likePatternToRegExp(filter[2], filter[3], flags);
if (negate) return { [param]: { $nin: [r, null] } };
else return { [param]: r };
}
if (op === "_tags") {
let t = filter[2];
if (negate) t = !t;
if (t) return { _tags: filter[1] };
else return { _tags: { $ne: filter[1] } };
}
if (!isArray(filter[1]) || filter[1][0] !== "PARAM")
throw new Error(`Invalid LHS operand of ${op} clause`);
if (op === "IS NULL") {
const p = filter[1][1];
return { [p]: null };
}
if (op === "IS NOT NULL") {
const p = filter[1][1];
return { [p]: { $ne: null } };
}
if (isArray(filter[2]) || filter[2] == null)
throw new Error(`Invalid RHS operand of ${op} clause`);
if (op === "=") {
const p = filter[1][1];
const v = filter[2];
if (!negate) return { [p]: v };
else return { [p]: { $nin: [v, null] } };
}
if (op === "<>") {
const p = filter[1][1];
const v = filter[2];
if (negate) return { [p]: v };
else return { [p]: { $nin: [v, null] } };
}
if (op === ">") {
const p = filter[1][1];
const v = filter[2];
if (negate) return { [p]: { $lte: v } };
else return { [p]: { $gt: v } };
}
if (op === ">=") {
const p = filter[1][1];
const v = filter[2];
if (negate) return { [p]: { $lt: v } };
else return { [p]: { $gte: v } };
}
if (op === "<") {
const p = filter[1][1];
const v = filter[2];
if (negate) return { [p]: { $gte: v } };
else return { [p]: { $lt: v } };
}
if (op === "<=") {
const p = filter[1][1];
const v = filter[2];
if (negate) return { [p]: { $gt: v } };
else return { [p]: { $lte: v } };
}
throw new Error(`Unrecognized operator ${op}`);
}
const _exp = evaluate(exp, null, Date.now());
if (!isArray(_exp)) {
if (_exp === true) return {};
if (!isArray(exp)) {
if (exp === true) return {};
throw new Error("Primitives are not valid queries");
}
return recursive(_exp, false);
return recursive(exp, false);
}
export function processDeviceProjection(
@@ -495,7 +484,9 @@ export function flattenTask(task: unknown): Task {
return t;
}
export function mongoQueryToFilter(query: Record<string, unknown>): Expression {
export function convertOldPrecondition(
query: Record<string, unknown>
): Expression {
function recursive(_query): Expression {
const expressions: Expression[] = [];
for (const [k, v] of Object.entries(_query)) {
@@ -514,12 +505,22 @@ export function mongoQueryToFilter(query: Record<string, unknown>): Expression {
} else if (k === "_tags") {
if (typeof v === "object") {
if (isArray(v)) throw new Error(`Invalid type`);
if (v.hasOwnProperty("$ne"))
expressions.push(["IS NULL", ["PARAM", `Tags.${v["$ne"]}`]]);
else if (v.hasOwnProperty("$eq"))
expressions.push(["IS NOT NULL", ["PARAM", `Tags.${v["$eq"]}`]]);
else throw new Error(`Invalid tag query`);
const conjs: Expression[] = [];
for (const [op, val] of Object.entries(v)) {
if (op === "$ne") {
if (typeof v["$ne"] !== "string")
throw new Error("Only string values are allowed for _tags");
conjs.push(["IS NULL", ["PARAM", `Tags.${val}`]]);
} else if (op === "$eq") {
if (typeof v["$eq"] !== "string")
throw new Error("Only string values are allowed for _tags");
conjs.push(["IS NOT NULL", ["PARAM", `Tags.${val}`]]);
} else {
throw new Error(`Invalid tag query`);
}
}
if (conjs.length === 1) expressions.push(conjs[0]);
else if (conjs.length > 1) expressions.push(["AND", ...conjs]);
} else {
expressions.push(["IS NOT NULL", ["PARAM", `Tags.${v}`]]);
}
@@ -537,34 +538,31 @@ export function mongoQueryToFilter(query: Record<string, unknown>): Expression {
const exps: Expression[] = [];
for (const [kk, vv] of Object.entries(v)) {
let op;
switch (kk) {
case "$eq":
op = "=";
break;
case "$ne":
op = "<>";
break;
case "$lt":
op = "<";
break;
case "$lte":
op = "<=";
break;
case "$gt":
op = ">";
break;
case "$gte":
op = ">=";
break;
default:
throw new Error(`Operator ${kk} not supported`);
if (kk === "$eq") {
exps.push(["=", ["PARAM", k], vv]);
} else if (kk === "$ne") {
exps.push([
"OR",
["<>", ["PARAM", k], vv],
["IS NULL", ["PARAM", k]],
]);
} else if (kk === "$lt") {
exps.push(["<", ["PARAM", k], vv]);
} else if (kk === "$lte") {
exps.push(["<=", ["PARAM", k], vv]);
} else if (kk === "$gt") {
exps.push([">", ["PARAM", k], vv]);
} else if (kk === "$gte") {
exps.push([">=", ["PARAM", k], vv]);
} else {
throw new Error(`Operator ${kk} not supported`);
}
exps.push([op, ["PARAM", k], vv]);
if (!["string", "number", "boolean"].includes(typeof vv))
throw new Error(`Invalid value for ${kk} operator`);
}
if (exps.length === 1) {
expressions.push(exps[0]);
} else {
} else if (exps.length > 1) {
const and: Expression = ["AND"];
expressions.push(and.concat(exps));
}
@@ -573,8 +571,8 @@ export function mongoQueryToFilter(query: Record<string, unknown>): Expression {
}
}
if (expressions.length === 1) return expressions[0];
const and: Expression = ["AND"];
return and.concat(expressions);
if (expressions.length === 0) return true;
return ["AND", ...expressions];
}
// empty filter
@@ -592,7 +590,9 @@ export function flattenPreset(
// Try parse to check expression validity
parse(p.precondition as string);
} catch (error) {
p.precondition = mongoQueryToFilter(JSON.parse(p.precondition as string));
p.precondition = convertOldPrecondition(
JSON.parse(p.precondition as string)
);
p.precondition = (p.precondition as string).length
? stringify(p.precondition as Expression)
: "";
+3 -3
View File
@@ -299,7 +299,7 @@ export async function transferComplete(
sessionContext.operationsTouched[commandKey] = 1;
if (rpcReq.faultStruct && rpcReq.faultStruct.faultCode !== "0") {
await revertDownloadParameters(sessionContext, operation.args.instance);
revertDownloadParameters(sessionContext, operation.args.instance);
const fault: Fault = {
code: `cwmp.${rpcReq.faultStruct.faultCode}`,
@@ -398,10 +398,10 @@ export async function transferComplete(
};
}
async function revertDownloadParameters(
function revertDownloadParameters(
sessionContext: SessionContext,
instance
): Promise<void> {
): void {
const timestamp = sessionContext.timestamp + sessionContext.iteration + 1;
let p;
+3 -1
View File
@@ -60,7 +60,9 @@ const getAuthorizer = memoize(
);
koa.on("error", async (err) => {
throw err;
setTimeout(() => {
throw err;
});
});
koa.use(async (ctx, next) => {
+3 -12
View File
@@ -110,7 +110,7 @@ export async function postTasks(
try {
await connectionRequest(deviceId, device);
} catch (err) {
for (const t of statuses) db.deleteTask(new ObjectID(t._id));
await Promise.all(statuses.map((t) => db.deleteTask(new ObjectID(t._id))));
return {
connectionRequest: err.message,
tasks: statuses,
@@ -138,20 +138,11 @@ export async function postTasks(
r.status = "fault";
r["fault"] = res[i * 2 + 1][0];
}
db.deleteTask(new ObjectID(r._id));
}
return { connectionRequest: "OK", tasks: statuses };
}
await Promise.all(statuses.map((t) => db.deleteTask(new ObjectID(t._id))));
interface PingResponse {
packetsTransmitted: number;
packetsReceived: number;
packetLoss: string;
min: number;
avg: number;
max: number;
mdev: number;
return { connectionRequest: "OK", tasks: statuses };
}
export async function putResource(
+9 -6
View File
@@ -147,8 +147,9 @@ for (const [resource, flags] of Object.entries(resources)) {
// Exclude temporary tasks and faults
if (resource === "tasks" || resource === "faults") {
filter = and(filter, [
"NOT",
["<", ["PARAM", "expiry"], Date.now() + 60000],
"OR",
[">=", ["PARAM", "expiry"], Date.now() + 60000],
["IS NULL", ["PARAM", "expiry"]],
]);
}
@@ -195,8 +196,9 @@ for (const [resource, flags] of Object.entries(resources)) {
// Exclude temporary tasks and faults
if (resource === "tasks" || resource === "faults") {
filter = and(filter, [
"NOT",
["<", ["PARAM", "expiry"], Date.now() + 60000],
"OR",
[">=", ["PARAM", "expiry"], Date.now() + 60000],
["IS NULL", ["PARAM", "expiry"]],
]);
}
@@ -254,8 +256,9 @@ for (const [resource, flags] of Object.entries(resources)) {
// Exclude temporary tasks and faults
if (resource === "tasks" || resource === "faults") {
filter = and(filter, [
"NOT",
["<", ["PARAM", "expiry"], Date.now() + 60000],
"OR",
[">=", ["PARAM", "expiry"], Date.now() + 60000],
["IS NULL", ["PARAM", "expiry"]],
]);
}
+74 -70
View File
@@ -128,78 +128,80 @@ export function query(
}
return new Promise((resolve, reject) => {
getClient().then((client) => {
const collection = client
.db()
.collection(RESOURCE_COLLECTION[resource] || resource);
const cursor = collection.find(q);
if (options.projection) {
let projection = options.projection;
if (resource === "devices") {
projection = mongodbFunctions.processDeviceProjection(
options.projection
);
getClient()
.then((client) => {
const collection = client
.db()
.collection(RESOURCE_COLLECTION[resource] || resource);
const cursor = collection.find(q);
if (options.projection) {
let projection = options.projection;
if (resource === "devices") {
projection = mongodbFunctions.processDeviceProjection(
options.projection
);
}
if (resource === "presets") projection.configurations = 1;
cursor.project(projection);
}
if (resource === "presets") projection.configurations = 1;
cursor.project(projection);
}
if (resource === "users") cursor.project({ password: 0, salt: 0 });
if (resource === "users") cursor.project({ password: 0, salt: 0 });
if (options.skip) cursor.skip(options.skip);
if (options.limit) cursor.limit(options.limit);
if (options.skip) cursor.skip(options.skip);
if (options.limit) cursor.limit(options.limit);
if (options.sort) {
let s = Object.entries(options.sort)
.sort((a, b) => Math.abs(b[1]) - Math.abs(a[1]))
.reduce(
(obj, [k, v]) =>
Object.assign(obj, { [k]: Math.min(Math.max(v, -1), 1) }),
{}
);
if (options.sort) {
let s = Object.entries(options.sort)
.sort((a, b) => Math.abs(b[1]) - Math.abs(a[1]))
.reduce(
(obj, [k, v]) =>
Object.assign(obj, { [k]: Math.min(Math.max(v, -1), 1) }),
{}
);
if (resource === "devices") s = mongodbFunctions.processDeviceSort(s);
cursor.sort(s);
}
if (resource === "devices") s = mongodbFunctions.processDeviceSort(s);
cursor.sort(s);
}
if (!callback) {
cursor.toArray((err, docs) => {
if (err) return reject(err);
if (resource === "devices")
docs = docs.map((d) => mongodbFunctions.flattenDevice(d));
else if (resource === "faults")
docs = docs.map((d) => mongodbFunctions.flattenFault(d));
else if (resource === "tasks")
docs = docs.map((d) => mongodbFunctions.flattenTask(d));
else if (resource === "presets")
docs = docs.map((d) => mongodbFunctions.flattenPreset(d));
else if (resource === "files")
docs = docs.map((d) => mongodbFunctions.flattenFile(d));
return resolve(docs);
});
} else {
cursor.forEach(
(doc) => {
if (!callback) {
cursor.toArray((err, docs) => {
if (err) return reject(err);
if (resource === "devices")
doc = mongodbFunctions.flattenDevice(doc);
docs = docs.map((d) => mongodbFunctions.flattenDevice(d));
else if (resource === "faults")
doc = mongodbFunctions.flattenFault(doc);
docs = docs.map((d) => mongodbFunctions.flattenFault(d));
else if (resource === "tasks")
doc = mongodbFunctions.flattenTask(doc);
docs = docs.map((d) => mongodbFunctions.flattenTask(d));
else if (resource === "presets")
doc = mongodbFunctions.flattenPreset(doc);
docs = docs.map((d) => mongodbFunctions.flattenPreset(d));
else if (resource === "files")
doc = mongodbFunctions.flattenFile(doc);
callback(doc);
},
(err) => {
if (err) reject(err);
else resolve();
}
);
}
});
docs = docs.map((d) => mongodbFunctions.flattenFile(d));
return resolve(docs);
});
} else {
cursor.forEach(
(doc) => {
if (resource === "devices")
doc = mongodbFunctions.flattenDevice(doc);
else if (resource === "faults")
doc = mongodbFunctions.flattenFault(doc);
else if (resource === "tasks")
doc = mongodbFunctions.flattenTask(doc);
else if (resource === "presets")
doc = mongodbFunctions.flattenPreset(doc);
else if (resource === "files")
doc = mongodbFunctions.flattenFile(doc);
callback(doc);
},
(err) => {
if (err) reject(err);
else resolve();
}
);
}
})
.catch(reject);
});
}
@@ -221,15 +223,17 @@ export function count(resource: string, filter: Expression): Promise<number> {
}
return new Promise((resolve, reject) => {
getClient().then((client) => {
const collection = client
.db()
.collection(RESOURCE_COLLECTION[resource] || resource);
collection.find(q).count((err, c) => {
if (err) reject(err);
else resolve(c);
});
});
getClient()
.then((client) => {
const collection = client
.db()
.collection(RESOURCE_COLLECTION[resource] || resource);
collection.find(q).count((err, c) => {
if (err) reject(err);
else resolve(c);
});
})
.catch(reject);
});
}
+2157 -856
View File
File diff suppressed because it is too large. Load diff
+23 -23
View File
@@ -1,6 +1,6 @@
{
"name": "genieacs",
"version": "1.2.0",
"version": "1.2.2",
"description": "A TR-069 Auto Configuration Server (ACS)",
"repository": {
"type": "git",
@@ -20,6 +20,7 @@
"tools": "tools"
},
"dependencies": {
"espresso-iisojs": "^1.0.3",
"iconv-lite": "^0.6.2",
"ipaddr.js": "^2.0.0",
"jsonwebtoken": "^8.5.1",
@@ -30,48 +31,47 @@
"koa-router": "^9.4.0",
"koa-static": "^5.0.0",
"later": "^1.2.0",
"mongodb": "^3.6.0",
"mongodb": "^3.6.2",
"parsimmon": "^1.16.0",
"seedrandom": "^3.0.5",
"yaml": "^1.10.0",
"yebool": "^1.0.2"
"yaml": "^1.10.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^15.0.0",
"@rollup/plugin-commonjs": "^15.1.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-typescript": "^5.0.2",
"@rollup/plugin-typescript": "^6.0.0",
"@types/jsonwebtoken": "^8.5.0",
"@types/koa": "^2.11.4",
"@types/koa-compress": "^4.0.0",
"@types/koa-compress": "^4.0.1",
"@types/koa-router": "^7.4.1",
"@types/koa-static": "^4.0.1",
"@types/mithril": "^2.0.3",
"@types/mongodb": "^3.5.26",
"@types/parsimmon": "^1.10.2",
"@types/mithril": "^2.0.4",
"@types/mongodb": "^3.5.27",
"@types/parsimmon": "^1.10.3",
"@types/seedrandom": "^2.4.28",
"@types/svgo": "^1.3.3",
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"ava": "^3.12.1",
"codemirror": "^5.57.0",
"@typescript-eslint/eslint-plugin": "^4.3.0",
"@typescript-eslint/parser": "^4.3.0",
"ava": "^3.13.0",
"codemirror": "^5.58.1",
"cssnano": "^4.1.10",
"eslint": "^7.8.0",
"eslint-config-prettier": "^6.11.0",
"eslint": "^7.10.0",
"eslint-config-prettier": "^6.12.0",
"esm": "^3.2.25",
"jsbi": "^3.1.3",
"jsbi": "^3.1.4",
"mithril": "^2.0.4",
"postcss": "^7.0.32",
"postcss": "^8.1.1",
"postcss-import": "^12.0.1",
"postcss-preset-env": "^6.7.0",
"prettier": "^2.1.1",
"rollup": "^2.26.9",
"rollup-plugin-terser": "^7.0.1",
"prettier": "^2.1.2",
"rollup": "^2.28.2",
"rollup-plugin-terser": "^7.0.2",
"source-map-loader": "^1.1.0",
"svgo": "^1.3.2",
"ts-node": "^9.0.0",
"typescript": "^4.0.2",
"webpack": "^4.44.1"
"typescript": "^4.0.3",
"webpack": "^4.44.2"
},
"engines": {
"node": ">=12.13.0"
+79 -9
View File
@@ -1,13 +1,13 @@
import ava from "ava";
import * as mongodbFunctions from "../lib/mongodb-functions";
import { stringify } from "../lib/common/expression-parser";
import { stringify, parse } from "../lib/common/expression-parser";
ava("mongoQueryToFilter", (t) => {
ava("convertOldPrecondition", (t) => {
const tests = [
[{}, "true"],
[{ test: "test" }, 'test = "test"'],
[{ test: { $eq: "test" } }, 'test = "test"'],
[{ test: { $ne: "test" } }, 'test <> "test"'],
[{ test: { $ne: "test" } }, 'test <> "test" OR test IS NULL'],
[{ test: { $gte: "test" } }, 'test >= "test"'],
[{ "Tags.test": true }, "Tags.test IS NOT NULL"],
[{ "Tags.test": false }, "Tags.test IS NULL"],
@@ -22,20 +22,20 @@ ava("mongoQueryToFilter", (t) => {
[{ _tags: { $eq: "test" } }, "Tags.test IS NOT NULL"],
[
{ $and: [{ test: "test" }, { test: { $ne: "test" } }] },
'test = "test" AND test <> "test"',
'test = "test" AND (test <> "test" OR test IS NULL)',
],
[{ test: "test", test2: "test2" }, 'test = "test" AND test2 = "test2"'],
[
{ $or: [{ test: "test" }, { test: { $ne: "test" } }] },
'test = "test" OR test <> "test"',
'test = "test" OR (test <> "test" OR test IS NULL)',
],
[
{ test: { $gte: "test1", $ne: "test2" } },
'test >= "test1" AND test <> "test2"',
'test >= "test1" AND (test <> "test2" OR test IS NULL)',
],
[
{ test: "test", test2: { $ne: "test2" } },
'test = "test" AND test2 <> "test2"',
'test = "test" AND (test2 <> "test2" OR test2 IS NULL)',
],
];
@@ -50,13 +50,83 @@ ava("mongoQueryToFilter", (t) => {
t.plan(tests.length + 2 * shouldFailTests.length);
for (const test of tests)
t.is(stringify(mongodbFunctions.mongoQueryToFilter(test[0])), test[1]);
t.is(stringify(mongodbFunctions.convertOldPrecondition(test[0])), test[1]);
for (const test of shouldFailTests) {
const func = (): void => {
mongodbFunctions.mongoQueryToFilter(test[0] as Record<string, unknown>);
mongodbFunctions.convertOldPrecondition(
test[0] as Record<string, unknown>
);
};
const error = t.throws(func, { instanceOf: Error });
t.is(error.message, test[1]);
}
});
ava("filterToMongoQuery", async (t) => {
const queries: [string, Record<string, unknown>][] = [
["true", {}],
["Tags.tag1 = true", { _tags: "tag1" }],
["Tags.tag1 <> false", { _tags: "tag1" }],
["Tags.tag1 IS NULL", { _tags: { $ne: "tag1" } }],
["Tags.tag1 = 123", { "Tags.tag1": 123 }],
["Param1 = 'value1'", { "Param1._value": "value1" }],
["Param1 <> 'value1'", { "Param1._value": { $nin: ["value1", null] } }],
["Param1 > 'value'", { "Param1._value": { $gt: "value" } }],
["Param1 IS NOT NULL", { "Param1._value": { $ne: null } }],
["Param1 LIKE 'value'", { "Param1._value": /^value$/ }],
["LOWER(Param1) LIKE 'value'", { "Param1._value": /^value$/i }],
[
"Param1 <> 'value2' OR NOT (Param2 = 'value1' OR Param1 < 'value2')",
{
$or: [
{ "Param1._value": { $nin: ["value2", null] } },
{
$and: [
{ "Param2._value": { $nin: ["value1", null] } },
{ "Param1._value": { $gte: "value2" } },
],
},
],
},
],
[
"Param1 <> 'value2' OR Param1 IS NULL",
{
$or: [
{ "Param1._value": { $nin: ["value2", null] } },
{ "Param1._value": null },
],
},
],
];
for (const [expStr, expect] of queries) {
const exp = parse(expStr);
const query = mongodbFunctions.filterToMongoQuery(
mongodbFunctions.processDeviceFilter(exp)
);
t.deepEqual(query, expect);
}
const failQueries: [any, string][] = [
["Param1 = Param2", "Invalid RHS operand of = clause"],
["Param1 LIKE Param2", "Invalid RHS operand of LIKE clause"],
["1 LIKE '1'", "Invalid LHS operand of LIKE clause"],
["1 = 2", "Invalid LHS operand of = clause"],
["1 IS NULL", "Invalid LHS operand of IS NULL clause"],
["false", "Primitives are not valid queries"],
["UPPER(Param1) LIKE 'value'", "Invalid RHS operand of LIKE clause"],
];
for (const [expStr, err] of failQueries) {
const exp = parse(expStr);
t.throws(
() =>
mongodbFunctions.filterToMongoQuery(
mongodbFunctions.processDeviceFilter(exp)
),
{ message: err }
);
}
});
+1 -1
View File
@@ -71,7 +71,7 @@ let fulfillTimeout = null;
function fulfill(): void {
clearTimeout(fulfillTimeout);
fulfillTimeout = setTimeout(() => {
store.fulfill(lastRenderTimestamp, pageVisitTimestamp).then((updated) => {
store.fulfill(lastRenderTimestamp, pageVisitTimestamp, (updated) => {
if (updated) m.redraw();
});
}, 100);
+1 -1
View File
@@ -70,7 +70,7 @@ const component: ClosureComponent = (): Component => {
onclick: () => {
taskQueue.stageDownload({
name: "download",
device: device["DeviceID.ID"].value[0],
devices: [device["DeviceID.ID"].value[0]],
});
},
},
+1 -1
View File
@@ -90,7 +90,7 @@ const component: ClosureComponent = (): Component => {
onclick: () => {
taskQueue.stageSpv({
name: "setParameterValues",
device: device["DeviceID.ID"].value[0],
devices: [device["DeviceID.ID"].value[0]],
parameterValues: [
[
parameter,
+20 -15
View File
@@ -27,23 +27,28 @@ const component: ClosureComponent = (vn): Component => {
let interval: ReturnType<typeof setInterval>;
let host: string;
const refresh = async (): Promise<void> => {
let status = "";
if (host) {
try {
const res = await store.ping(host);
if (res["avg"] != null) status = `${Math.trunc(res["avg"])} ms`;
else status = "Unreachable";
} catch (err) {
setTimeout(() => {
throw err;
}, 0);
}
const refresh = (): void => {
if (!host) {
const dom = (vn as VnodeDOM).dom;
if (dom) dom.innerHTML = "";
return;
}
const dom = (vn as VnodeDOM).dom;
if (dom) dom.innerHTML = `Pinging ${host}: ${status}`;
let status = "";
store
.ping(host)
.then((res) => {
if (res["avg"] != null) status = `${Math.trunc(res["avg"])} ms`;
else status = "Unreachable";
})
.catch(() => {
status = "Error!";
clearInterval(interval);
})
.finally(() => {
const dom = (vn as VnodeDOM).dom;
if (dom) dom.innerHTML = `Pinging ${host}: ${status}`;
});
};
return {
+4
View File
@@ -79,6 +79,10 @@ const component: ClosureComponent = (): Component => {
.then(() => {
e.target.disabled = false;
store.fulfill(0, Date.now());
})
.catch((err) => {
e.target.disabled = false;
notifications.push("error", err.message);
});
},
},
+32 -1
View File
@@ -42,6 +42,35 @@ export function flattenConfig(config: Record<string, unknown>): any {
return flatten;
}
// Order keys such that nested objects come last
function orderKeys(config: any): number {
let res = 1;
if (typeof config !== "object") return res;
if (Array.isArray(config)) {
for (const c of config) res += orderKeys(c);
return res;
}
const weights: [string, number][] = Object.entries(config).map(([k, v]) => [
k,
orderKeys(v),
]);
weights.sort((a, b) => {
if (a[1] !== b[1]) return a[1] - b[1];
if (b[0] > a[0]) return -1;
else return 1;
});
for (const [k, w] of weights) {
res += w;
const v = config[k];
delete config[k];
config[k] = v;
}
return res;
}
export function structureConfig(config: Config[]): any {
config.sort((a, b) => (a._id > b._id ? 1 : a._id < b._id ? -1 : 0));
const _config = {};
@@ -93,7 +122,9 @@ export function structureConfig(config: Config[]): any {
return object;
};
return toArray(_config);
const res = toArray(_config);
orderKeys(res);
return res;
}
export function diffConfig(
+2
View File
@@ -178,6 +178,8 @@ body {
}
& > .queue {
max-height: 400px;
overflow-y: auto;
& > .pending {
animation: 0.5s linear 0s infinite alternate pending-blink;
border-radius: 0.4em;
+28 -13
View File
@@ -23,7 +23,7 @@ import config from "./config";
import indexTableComponent from "./index-table-component";
import filterComponent from "./filter-component";
import * as store from "./store";
import * as taskQueue from "./task-queue";
import { queueTask, stageDownload } from "./task-queue";
import * as notifications from "./notifications";
import { parse, stringify } from "../lib/common/expression-parser";
import { evaluate, extractParams } from "../lib/common/expression";
@@ -91,12 +91,11 @@ function renderActions(selected: Set<string>): Children {
title: "Reboot selected devices",
disabled: !selected.size,
onclick: () => {
for (const d of selected) {
taskQueue.queueTask({
name: "reboot",
device: d,
});
}
const tasks = [...selected].map((s) => ({
name: "reboot",
device: s,
}));
queueTask(...tasks);
},
},
"Reboot"
@@ -110,18 +109,34 @@ function renderActions(selected: Set<string>): Children {
title: "Factory reset selected devices",
disabled: !selected.size,
onclick: () => {
for (const d of selected) {
taskQueue.queueTask({
name: "factoryReset",
device: d,
});
}
const tasks = [...selected].map((s) => ({
name: "factoryReset",
device: s,
}));
queueTask(...tasks);
},
},
"Reset"
)
);
buttons.push(
m(
"button.critical",
{
title: "Push a firmware or a config file",
disabled: !selected.size,
onclick: () => {
stageDownload({
name: "download",
devices: [...selected],
});
},
},
"Push file"
)
);
buttons.push(
m(
"button.primary",
+85 -57
View File
@@ -24,10 +24,21 @@ import m, {
Component,
VnodeDOM,
} from "mithril";
import * as taskQueue from "./task-queue";
import * as store from "./store";
import * as notifications from "./notifications";
import { getIcon } from "./icons";
import {
clear,
commit,
deleteTask,
getQueue,
getStaging,
QueueTask,
queueTask,
StageTask,
} from "./task-queue";
const invalid: WeakSet<StageTask> = new WeakSet();
function mparam(param): Children {
return m("span.parameter", { title: param }, `${param}`);
@@ -37,7 +48,7 @@ function mval(val): Children {
return m("span.value", { title: val }, `${val}`);
}
function renderStagingSpv(task, queueFunc, cancelFunc): Children {
function renderStagingSpv(task: StageTask, queueFunc, cancelFunc): Children {
function keydown(e): void {
if (e.keyCode === 13) queueFunc();
else if (e.keyCode === 27) cancelFunc();
@@ -90,13 +101,24 @@ function renderStagingSpv(task, queueFunc, cancelFunc): Children {
return [m("span", "Editing ", mparam(task.parameterValues[0][0])), input];
}
function renderStagingDownload(task): Children {
task.invalid = !task.fileName || !task.fileType;
function renderStagingDownload(task: StageTask): Children {
if (!task.fileName || !task.fileType) invalid.add(task);
else invalid.delete(task);
const files = store.fetch("files", true);
const idParts = task.device.split("-");
const oui = decodeURIComponent(idParts[0]);
const productClass =
idParts.length === 3 ? decodeURIComponent(idParts[1]) : "";
let oui = "";
let productClass = "";
for (const d of task.devices) {
const parts = d.split("-");
if (oui === "") oui = parts[0];
else if (oui !== parts[0]) oui = null;
if (parts.length === 3) {
if (productClass === "") productClass = parts[1];
else if (productClass !== parts[1]) productClass = null;
}
}
if (oui) oui = decodeURIComponent(oui);
if (productClass) productClass = decodeURIComponent(productClass);
const typesList = [
"",
@@ -162,13 +184,17 @@ function renderStagingDownload(task): Children {
];
}
function renderStaging(staging): Child[] {
function renderStaging(staging: Set<StageTask>): Child[] {
const elements: Child[] = [];
for (const s of staging) {
const queueFunc = (): void => {
staging.delete(s);
taskQueue.queueTask(s);
for (const d of s.devices) {
const t = Object.assign({ device: d }, s);
delete t.devices;
queueTask(t);
}
};
const cancelFunc = (): void => {
staging.delete(s);
@@ -181,7 +207,7 @@ function renderStaging(staging): Child[] {
const queue = m(
"button.primary",
{ title: "Queue task", onclick: queueFunc, disabled: s.invalid },
{ title: "Queue task", onclick: queueFunc, disabled: invalid.has(s) },
"Queue"
);
const cancel = m(
@@ -195,7 +221,7 @@ function renderStaging(staging): Child[] {
return elements;
}
function renderQueue(queue): Child[] {
function renderQueue(queue: Set<QueueTask>): Child[] {
const details: Child[] = [];
const devices: { [deviceId: string]: any[] } = {};
for (const t of queue) {
@@ -215,7 +241,7 @@ function renderQueue(queue): Child[] {
{
title: "Retry this task",
onclick: () => {
taskQueue.queueTask(t);
queueTask(t);
},
},
getIcon("retry")
@@ -229,7 +255,7 @@ function renderQueue(queue): Child[] {
{
title: "Remove this task",
onclick: () => {
taskQueue.deleteTask(t);
deleteTask(t);
},
},
getIcon("remove")
@@ -347,8 +373,8 @@ function renderNotifications(notifs): Child[] {
const component: ClosureComponent = (): Component => {
return {
view: (vnode) => {
const queue = taskQueue.getQueue();
const staging = taskQueue.getStaging();
const queue = getQueue();
const staging = getStaging();
const notifs = notifications.getNotifications();
let drawerElement, statusElement;
@@ -394,53 +420,55 @@ const component: ClosureComponent = (): Component => {
title: "Commit queued tasks",
disabled: !statusCount.queued,
onclick: () => {
const tasks = Array.from(taskQueue.getQueue()).filter(
const tasks = Array.from(getQueue()).filter(
(t) => t["status"] === "queued"
);
taskQueue
.commit(
tasks,
(deviceId, err, connectionRequestStatus, tasks2) => {
if (err) {
notifications.push(
"error",
`${deviceId}: ${err.message}`
);
return;
}
if (connectionRequestStatus !== "OK") {
notifications.push(
"error",
`${deviceId}: ${connectionRequestStatus}`
);
return;
}
for (const t of tasks2) {
if (t.status === "stale") {
notifications.push(
"error",
`${deviceId}: No contact from device`
);
return;
} else if (t.status === "fault") {
notifications.push(
"error",
`${deviceId}: Task(s) faulted`
);
return;
}
}
commit(
tasks,
(deviceId, err, connectionRequestStatus, tasks2) => {
if (err) {
notifications.push(
"success",
`${deviceId}: Task(s) committed`
"error",
`${deviceId}: ${err.message}`
);
return;
}
)
if (connectionRequestStatus !== "OK") {
notifications.push(
"error",
`${deviceId}: ${connectionRequestStatus}`
);
return;
}
for (const t of tasks2) {
if (t.status === "stale") {
notifications.push(
"error",
`${deviceId}: No contact from device`
);
return;
} else if (t.status === "fault") {
notifications.push(
"error",
`${deviceId}: Task(s) faulted`
);
return;
}
}
notifications.push(
"success",
`${deviceId}: Task(s) committed`
);
}
)
.then(() => {
store.fulfill(0, Date.now());
})
.catch((err) => {
notifications.push("error", err.message);
});
},
},
@@ -450,7 +478,7 @@ const component: ClosureComponent = (): Component => {
"button",
{
title: "Clear tasks",
onclick: taskQueue.clear,
onclick: clear,
disabled: !queueElements.length,
},
"Clear"
+5 -2
View File
@@ -27,6 +27,8 @@ interface Attribute {
type?: string;
}
const MAX_PAGE_SIZE = 200;
function renderTable(
attributes: Attribute[],
data: Record<string, any>[],
@@ -37,7 +39,7 @@ function renderTable(
onSortChange: (obj: Record<string, number>) => void,
downloadUrl?: string,
valueCallback?: (attr: Attribute, record: Record<string, any>) => Children,
actionsCallback?: Children | ((selected: Set<string>) => Children),
actionsCallback?: Children | ((sel: Set<string>) => Children),
recordActionsCallback?: Children | ((record: Record<string, any>) => Children)
): Children {
const records = data || [];
@@ -180,7 +182,8 @@ function renderTable(
{
title: "Show more records",
onclick: showMoreCallback,
disabled: !data.length || records.length >= total,
disabled:
!data.length || records.length >= Math.min(MAX_PAGE_SIZE, total),
},
"More"
)
+1 -1
View File
@@ -81,7 +81,7 @@ export const component: ClosureComponent = (): Component => {
location.reload();
})
.catch((err) => {
notifications.push("error", err.message);
notifications.push("error", err.response || err.message);
e.target.disabled = false;
});
return false;
+48 -16
View File
@@ -152,6 +152,8 @@ function checkConnection(): void {
}
}
},
}).catch((err) => {
notifications.push("error", err.message);
});
}
@@ -169,11 +171,25 @@ export async function xhrRequest(
): any => {
if (typeof extract === "function") return extract(xhr, _options);
// https://mithril.js.org/request.html#error-handling
if (xhr.status !== 304 && Math.floor(xhr.status / 100) !== 2) {
const err = new Error();
err["message"] =
xhr.status === 0
? "Server is unrachable"
: `Unexpected response status code ${xhr.status}`;
err["code"] = xhr.status;
err["response"] = xhr.responseText;
throw err;
}
let response: any;
if (typeof deserialize === "function") {
response = deserialize(xhr.responseText);
} else if (
xhr.getResponseHeader("content-type").startsWith("application/json")
(xhr.getResponseHeader("content-type") || "").startsWith(
"application/json"
)
) {
try {
response = xhr.responseText ? JSON.parse(xhr.responseText) : null;
@@ -184,15 +200,6 @@ export async function xhrRequest(
response = xhr.responseText;
}
// https://mithril.js.org/request.html#error-handling
if (xhr.status !== 304 && Math.floor(xhr.status / 100) !== 2) {
const err = new Error();
err["message"] = xhr.responseText;
err["code"] = xhr.status;
err["response"] = response;
throw err;
}
return response;
};
@@ -340,7 +347,7 @@ export function fetch(
return queryResponse;
}
export function fulfill(
function _fulfill(
accessTimestamp: number,
_fulfillTimestamp: number
): Promise<boolean> {
@@ -377,7 +384,16 @@ export function fulfill(
m.buildQueryString({
filter: memoizedStringify(filter),
}),
extract: (xhr) => +xhr.getResponseHeader("x-total-count"),
extract: (xhr) => {
if (!xhr.status) {
throw new Error("Server is unrachable");
} else if (xhr.status !== 200) {
throw new Error(
`Unexpected response status code ${xhr.status}`
);
}
return +xhr.getResponseHeader("x-total-count");
},
background: true,
})
.then((c) => {
@@ -497,14 +513,14 @@ export function fulfill(
continue;
}
const combinedFilterDiff = diff;
resources[resourceType].combinedFilter = union;
updated = true;
let deleted = new Set<string>();
if (!resources[resourceType].combinedFilter)
deleted = new Set(resources[resourceType].objects.keys());
const combinedFilterDiff = diff;
resources[resourceType].combinedFilter = union;
allPromises2.push(
new Promise((resolve2, reject2) => {
xhrRequest({
@@ -560,6 +576,17 @@ export function fulfill(
});
}
export function fulfill(
accessTimestamp: number,
_fulfillTimestamp: number,
callback?: (updated: boolean) => void
): void {
_fulfill(accessTimestamp, _fulfillTimestamp).then(callback, (err) => {
notifications.push("error", err.message);
if (callback) callback(false);
});
}
export function getTimestamp(): number {
return fulfillTimestamp;
}
@@ -647,7 +674,12 @@ export function resourceExists(resource: string, id: string): Promise<number> {
m.buildQueryString({
filter: memoizedStringify(filter),
}),
extract: (xhr) => +xhr.getResponseHeader("x-total-count"),
extract: (xhr) => {
if (!xhr.status) throw new Error("Server is unrachable");
else if (xhr.status !== 200)
throw new Error(`Unexpected response status code ${xhr.status}`);
return +xhr.getResponseHeader("x-total-count");
},
background: true,
});
}
+44 -13
View File
@@ -19,23 +19,41 @@
import * as store from "./store";
import { Task } from "../lib/types";
import * as notifications from "./notifications";
export interface QueueTask extends Task {
status?: string;
device?: string;
device: string;
}
const queue: Set<QueueTask> = new Set();
const staging: Set<QueueTask> = new Set();
export interface StageTask extends Task {
devices: string[];
}
export function queueTask(task: QueueTask): void {
staging.delete(task);
task.status = "queued";
queue.add(task);
const MAX_QUEUE = 100;
const queue: Set<QueueTask> = new Set();
const staging: Set<StageTask> = new Set();
function canQueue(tasks: QueueTask[]): boolean {
let count = queue.size;
for (const task of tasks) if (!queue.has(task)) ++count;
return count <= MAX_QUEUE;
}
export function queueTask(...tasks: QueueTask[]): void {
if (!canQueue(tasks)) {
notifications.push("error", "Too many tasks in queue");
return;
}
for (const task of tasks) {
task.status = "queued";
queue.add(task);
}
}
export function deleteTask(task: QueueTask): void {
staging.delete(task);
queue.delete(task);
}
@@ -47,7 +65,7 @@ export function clear(): void {
queue.clear();
}
export function getStaging(): Set<QueueTask> {
export function getStaging(): Set<StageTask> {
return staging;
}
@@ -55,11 +73,19 @@ export function clearStaging(): void {
staging.clear();
}
export function stageSpv(task: QueueTask): void {
export function stageSpv(task: StageTask): void {
if (queue.size + task.devices.length > MAX_QUEUE) {
notifications.push("error", "Too many tasks in queue");
return;
}
staging.add(task);
}
export function stageDownload(task: QueueTask): void {
export function stageDownload(task: StageTask): void {
if (queue.size + task.devices.length > MAX_QUEUE) {
notifications.push("error", "Too many tasks in queue");
return;
}
staging.add(task);
}
@@ -69,14 +95,19 @@ export function commit(
deviceId: string,
err: Error,
conReqStatus: string,
tasks: QueueTask[]
_tasks: QueueTask[]
) => void
): Promise<void> {
const devices: { [deviceId: string]: any[] } = {};
if (!canQueue(tasks))
return Promise.reject(new Error("Too many tasks in queue"));
for (const t of tasks) {
devices[t.device] = devices[t.device] || [];
devices[t.device].push(t);
queueTask(t);
t.status = "queued";
queue.add(t);
}
return new Promise((resolve) => {
+17 -9
View File
@@ -87,16 +87,24 @@ export const component: ClosureComponent = (vnode): Component => {
})
.then(() => {
setTimeout(() => {
m.request({ url: "init" }).then((o) => {
e.target.disabled = false;
options = o;
notifications.push("success", "Initialization complete", {
"Open Sesame!": () => {
m.route.set("/login");
window.location.reload();
},
m.request({ url: "init" })
.then((o) => {
e.target.disabled = false;
options = o;
notifications.push(
"success",
"Initialization complete",
{
"Open Sesame!": () => {
m.route.set("/login");
window.location.reload();
},
}
);
})
.catch((err) => {
notifications.push("error", err.message);
});
});
}, 3000);
if (opts["users"]) {
alert(