mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 10:47:07 +00:00
Add endpoints for setting and retrieving metadata
This commit is contained in:
@@ -129,6 +129,19 @@ app.map({
|
||||
get: [ mw.info.get ],
|
||||
// post: [ mw.info.post ],
|
||||
},
|
||||
'/project/:project/meta/': {
|
||||
put: [ mw.meta.put ],
|
||||
},
|
||||
'/project/:project/meta/:path(*)': {
|
||||
// Path examples:
|
||||
// GET:
|
||||
// `/raw/sequences/qc/missing_shots`,
|
||||
// `/final/points/qc/sync_warn/results
|
||||
get: [ mw.meta.get ],
|
||||
// // PUT:
|
||||
// // `/raw/qc/missing_shots` ← { sequence: …, value: … }
|
||||
// put: [ mw.meta.put ]
|
||||
},
|
||||
//
|
||||
// '/project/:id/permissions/:mode(read|write)?': {
|
||||
// get: [ mw.permissions.get ],
|
||||
|
||||
@@ -9,5 +9,6 @@ module.exports = {
|
||||
label: require('./label'),
|
||||
navdata: require('./navdata'),
|
||||
configuration: require('./configuration'),
|
||||
info: require('./info')
|
||||
info: require('./info'),
|
||||
meta: require('./meta')
|
||||
};
|
||||
|
||||
0
lib/www/server/api/middleware/meta/delete.js
Normal file
0
lib/www/server/api/middleware/meta/delete.js
Normal file
18
lib/www/server/api/middleware/meta/get.js
Normal file
18
lib/www/server/api/middleware/meta/get.js
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
const { meta } = require('../../../lib/db');
|
||||
|
||||
module.exports = async function (req, res, next) {
|
||||
|
||||
try {
|
||||
res.status(200).json(await meta.get(req.params.project, req.params.path, req.query));
|
||||
} catch (err) {
|
||||
if (err instanceof TypeError) {
|
||||
res.status(404).json(null);
|
||||
} else {
|
||||
next(err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
next();
|
||||
|
||||
};
|
||||
8
lib/www/server/api/middleware/meta/index.js
Normal file
8
lib/www/server/api/middleware/meta/index.js
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
module.exports = {
|
||||
list: require('./list'),
|
||||
get: require('./get'),
|
||||
post: require('./post'),
|
||||
put: require('./put'),
|
||||
delete: require('./delete')
|
||||
}
|
||||
0
lib/www/server/api/middleware/meta/list.js
Normal file
0
lib/www/server/api/middleware/meta/list.js
Normal file
0
lib/www/server/api/middleware/meta/post.js
Normal file
0
lib/www/server/api/middleware/meta/post.js
Normal file
16
lib/www/server/api/middleware/meta/put.js
Normal file
16
lib/www/server/api/middleware/meta/put.js
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
const { meta } = require('../../../lib/db');
|
||||
|
||||
module.exports = async function (req, res, next) {
|
||||
|
||||
try {
|
||||
const payload = req.body;
|
||||
|
||||
await meta.put(req.params.project, payload);
|
||||
res.status(201).send();
|
||||
next();
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
|
||||
};
|
||||
@@ -8,5 +8,6 @@ module.exports = {
|
||||
label: require('./label'),
|
||||
configuration: require('./configuration'),
|
||||
info: require('./info'),
|
||||
meta: require('./meta'),
|
||||
navdata: require('./navdata')
|
||||
};
|
||||
|
||||
0
lib/www/server/lib/db/meta/delete.js
Normal file
0
lib/www/server/lib/db/meta/delete.js
Normal file
43
lib/www/server/lib/db/meta/get.js
Normal file
43
lib/www/server/lib/db/meta/get.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const { setSurvey } = require('../connection');
|
||||
|
||||
async function get (projectId, path, opts = {}) {
|
||||
const client = await setSurvey(projectId);
|
||||
const [type, kind, ...jsonpath] = path.split("/");
|
||||
|
||||
const tables = {
|
||||
sequences: {
|
||||
raw: ["raw_lines", ["sequence"]],
|
||||
final: ["final_lines", ["sequence"]],
|
||||
},
|
||||
points: {
|
||||
raw: ["raw_shots", ["sequence", "point"]],
|
||||
final: ["final_shots", ["sequence", "point"]],
|
||||
},
|
||||
lines: {
|
||||
preplot: ["preplot_points", ["line", "point"]],
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
const table = tables[kind][type][0];
|
||||
const fields = tables[kind][type][1];
|
||||
|
||||
const text = `
|
||||
SELECT ${fields.join(", ")}, meta #> $1 AS data
|
||||
FROM ${table}
|
||||
WHERE meta #> $1 IS NOT NULL
|
||||
ORDER BY ${fields.join(", ")};
|
||||
`;
|
||||
|
||||
console.log(text, jsonpath);
|
||||
const res = await client.query(text, [jsonpath]);
|
||||
|
||||
return res.rows;
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = get;
|
||||
7
lib/www/server/lib/db/meta/index.js
Normal file
7
lib/www/server/lib/db/meta/index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
module.exports = {
|
||||
get: require('./get'),
|
||||
post: require('./post'),
|
||||
put: require('./put'),
|
||||
delete: require('./delete')
|
||||
}
|
||||
0
lib/www/server/lib/db/meta/post.js
Normal file
0
lib/www/server/lib/db/meta/post.js
Normal file
77
lib/www/server/lib/db/meta/put.js
Normal file
77
lib/www/server/lib/db/meta/put.js
Normal file
@@ -0,0 +1,77 @@
|
||||
const { setSurvey, transaction } = require('../connection');
|
||||
|
||||
async function put (projectId, payload, opts = {}) {
|
||||
const client = await setSurvey(projectId);
|
||||
// const [type, ...jsonpath] = (path||"").split("/");
|
||||
|
||||
const tables = {
|
||||
sequences: {
|
||||
raw: ["raw_lines", ["sequence"]],
|
||||
final: ["final_lines", ["sequence"]],
|
||||
},
|
||||
points: {
|
||||
raw: ["raw_shots", ["sequence", "point"]],
|
||||
final: ["final_shots", ["sequence", "point"]],
|
||||
},
|
||||
lines: {
|
||||
preplot: ["preplot_points", ["line", "point"]],
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
|
||||
if (!Array.isArray(payload)) {
|
||||
payload = [payload];
|
||||
}
|
||||
|
||||
transaction.begin(client);
|
||||
for (const rec of payload) {
|
||||
console.log("RECORD", rec);
|
||||
const type = rec.type || "raw";
|
||||
if (rec.point && rec.sequence) {
|
||||
const table = tables.points[type][0]
|
||||
const text = `
|
||||
UPDATE ${table}
|
||||
SET meta = jsonb_set(meta, $1, $2)
|
||||
WHERE sequence = $3 AND point = $4;
|
||||
`
|
||||
const values = [ rec.path, JSON.stringify(rec.value), rec.sequence, rec.point ];
|
||||
|
||||
await client.query(text, values);
|
||||
} else if (rec.sequence) {
|
||||
const table = tables.sequences[type][0]
|
||||
const text = `
|
||||
UPDATE ${table}
|
||||
SET meta = jsonb_set(meta, $1, $2)
|
||||
WHERE sequence = $3;
|
||||
`
|
||||
const values = [ rec.path, JSON.stringify(rec.value), rec.sequence ];
|
||||
|
||||
await client.query(text, values);
|
||||
} else if (rec.point && rec.line) {
|
||||
const table = tables.points[type][0]
|
||||
const text = `
|
||||
UPDATE ${table}
|
||||
SET meta = jsonb_set(meta, $1, $2)
|
||||
WHERE line = $3 AND point = $4;
|
||||
`
|
||||
const values = [ rec.path, JSON.stringify(rec.value), rec.line, rec.point ];
|
||||
|
||||
await client.query(text, values);
|
||||
} else {
|
||||
// Ignore, but report
|
||||
console.error("I do not know where to store this meta", record);
|
||||
}
|
||||
}
|
||||
transaction.commit(client);
|
||||
|
||||
} catch (err) {
|
||||
console.error("ERROR", err);
|
||||
transaction.rollback(client);
|
||||
throw err;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = put;
|
||||
Reference in New Issue
Block a user