mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 13:27:08 +00:00
Refactor project summaries to use database views
This commit is contained in:
@@ -4,7 +4,7 @@ const { project} = require('../../../lib/db');
|
||||
module.exports = async function (req, res, next) {
|
||||
|
||||
try {
|
||||
res.status(200).send(await project.summary(req.params.project));
|
||||
res.status(200).send(await project.get(req.params.project));
|
||||
next();
|
||||
} catch (err) {
|
||||
next(err);
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
const { setSurvey } = require('../connection');
|
||||
|
||||
async function get (projectId, opts = {}) {
|
||||
const client = await setSurvey(projectId);
|
||||
|
||||
const text = `
|
||||
SELECT *
|
||||
FROM project_summary;
|
||||
`;
|
||||
|
||||
const res = await client.query(text);
|
||||
client.release();
|
||||
return res.rows[0];
|
||||
}
|
||||
|
||||
module.exports = get;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
|
||||
module.exports = {
|
||||
summary: require('./summary'),
|
||||
list: require('./list'),
|
||||
get: require('./get'),
|
||||
post: require('./post'),
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
const { setSurvey } = require('../connection');
|
||||
|
||||
|
||||
async function summary (projectId, opts = {}) {
|
||||
const client = await setSurvey(projectId);
|
||||
|
||||
|
||||
const sortFields = [
|
||||
"pid", "name", "lines", "sequences", "unique_shots",
|
||||
"total_shots", "info"
|
||||
];
|
||||
const sortKey = opts.sortBy && sortFields.includes(opts.sortBy) && opts.sortBy || "pid";
|
||||
const sortDir = opts.sortDesc == "true" ? "DESC" : "ASC";
|
||||
const offset = Math.abs((opts.page-1)*opts.itemsPerPage) || 0;
|
||||
const limit = Math.abs(Number(opts.itemsPerPage)) || null;
|
||||
|
||||
// FIXME Make this into a PostgreSQL stored procedure
|
||||
const text = `
|
||||
SELECT * FROM projects,
|
||||
(SELECT
|
||||
(SELECT COUNT(DISTINCT line) FROM preplot_lines WHERE class='V') AS lines,
|
||||
(SELECT COUNT(DISTINCT sequence) FROM final_lines) AS sequences,
|
||||
(SELECT COUNT(*) from (
|
||||
SELECT DISTINCT fl.line, fs.point
|
||||
FROM final_lines fl INNER JOIN final_shots fs USING (sequence)) AS t) AS unique_shots,
|
||||
(SELECT COUNT(*) FROM final_shots) AS total_shots,
|
||||
(SELECT COUNT(*) FROM preplot_points WHERE class = 'S') AS preplot_points) info
|
||||
WHERE current_setting('search_path') LIKE projects.schema||'%';
|
||||
`;
|
||||
|
||||
const res = await client.query(text);
|
||||
client.release();
|
||||
return res.rows[0];
|
||||
}
|
||||
|
||||
|
||||
module.exports = summary;
|
||||
Reference in New Issue
Block a user