Optimize CSV download
This commit is contained in:
1 parent
de759b8d93
commit
75a5a42296
3 files changed
+20
-26
No files matched your search
+8
-16
@@ -139,7 +139,7 @@ for (let [resource, flags] of Object.entries(resources)) {
|
|||||||
|
|
||||||
// CSV download
|
// CSV download
|
||||||
router.get(`/${resource}.csv`, async (ctx, next) => {
|
router.get(`/${resource}.csv`, async (ctx, next) => {
|
||||||
let options = {};
|
let options = { projection: {} };
|
||||||
let filter = true;
|
let filter = true;
|
||||||
if (ctx.request.query.filter)
|
if (ctx.request.query.filter)
|
||||||
filter = expression.parse(ctx.request.query.filter);
|
filter = expression.parse(ctx.request.query.filter);
|
||||||
@@ -154,8 +154,7 @@ for (let [resource, flags] of Object.entries(resources)) {
|
|||||||
filter: ctx.request.query.filter,
|
filter: ctx.request.query.filter,
|
||||||
limit: options.limit,
|
limit: options.limit,
|
||||||
skip: options.skip,
|
skip: options.skip,
|
||||||
sort: options.sort,
|
sort: options.sort
|
||||||
projection: options.projection
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!ctx.state.authorizer.hasAccess(resource, 2)) {
|
if (!ctx.state.authorizer.hasAccess(resource, 2)) {
|
||||||
@@ -165,12 +164,16 @@ for (let [resource, flags] of Object.entries(resources)) {
|
|||||||
|
|
||||||
const columns = JSON.parse(ctx.request.query.columns);
|
const columns = JSON.parse(ctx.request.query.columns);
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
for (let [k, v] of Object.entries(columns))
|
|
||||||
columns[k] = expression.evaluate(
|
for (let [k, v] of Object.entries(columns)) {
|
||||||
|
const e = expression.evaluate(
|
||||||
expression.parse(v),
|
expression.parse(v),
|
||||||
null,
|
null,
|
||||||
now
|
now
|
||||||
);
|
);
|
||||||
|
columns[k] = e;
|
||||||
|
for (let p of expression.extractParams(e)) options.projection[p] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
// Exclude temporary tasks and faults
|
// Exclude temporary tasks and faults
|
||||||
if (resource === "tasks" || resource === "faults")
|
if (resource === "tasks" || resource === "faults")
|
||||||
@@ -392,17 +395,6 @@ router.put("/files/:id", async (ctx, next) => {
|
|||||||
log.metadata = metadata;
|
log.metadata = metadata;
|
||||||
logger.accessInfo(log);
|
logger.accessInfo(log);
|
||||||
|
|
||||||
db.putAudit({
|
|
||||||
username: ctx.state.user.username,
|
|
||||||
action: "put",
|
|
||||||
objectType: resource,
|
|
||||||
objectId: id
|
|
||||||
}).catch(err => {
|
|
||||||
setTimeout(() => {
|
|
||||||
throw err;
|
|
||||||
}, 0);
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.body = "";
|
ctx.body = "";
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+10
-9
@@ -60,14 +60,15 @@ function query(resource, filter, options, callback) {
|
|||||||
const collection = client
|
const collection = client
|
||||||
.db(RESOURCE_DB[resource])
|
.db(RESOURCE_DB[resource])
|
||||||
.collection(RESOURCE_COLLECTION[resource] || resource);
|
.collection(RESOURCE_COLLECTION[resource] || resource);
|
||||||
let cursor = collection.find(
|
const cursor = collection.find(q);
|
||||||
q,
|
if (options.projection)
|
||||||
resource === "Devices"
|
cursor.project(
|
||||||
? mongodbFunctions.processDeviceProjection(options.projection)
|
resource === "devices"
|
||||||
: options.projection
|
? mongodbFunctions.processDeviceProjection(options.projection)
|
||||||
);
|
: options.projection
|
||||||
if (options.skip) cursor = cursor.skip(options.skip);
|
);
|
||||||
if (options.limit) cursor = cursor.limit(options.limit);
|
if (options.skip) cursor.skip(options.skip);
|
||||||
|
if (options.limit) cursor.limit(options.limit);
|
||||||
|
|
||||||
if (options.sort) {
|
if (options.sort) {
|
||||||
let s = Object.entries(options.sort)
|
let s = Object.entries(options.sort)
|
||||||
@@ -79,7 +80,7 @@ function query(resource, filter, options, callback) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (resource === "devices") s = mongodbFunctions.processDeviceSort(s);
|
if (resource === "devices") s = mongodbFunctions.processDeviceSort(s);
|
||||||
cursor = cursor.sort(s);
|
cursor.sort(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!callback)
|
if (!callback)
|
||||||
|
|||||||
@@ -332,7 +332,8 @@ function flattenDevice(device) {
|
|||||||
let r = root ? `${root}.${name}` : name;
|
let r = root ? `${root}.${name}` : name;
|
||||||
output[r] = attrs;
|
output[r] = attrs;
|
||||||
|
|
||||||
if (attrs["object"]) recursive(tree, r, output, childrenTimestamp);
|
if (attrs["object"] || tree["object"] == null)
|
||||||
|
recursive(tree, r, output, childrenTimestamp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user