Skip to content

Commit

Permalink
eliminate sums totally
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Feb 7, 2025
1 parent 1b4d56c commit 245958e
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions lib/model/query/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,16 +470,15 @@ const getDatasets = () => ({ all }) => all(sql`
GROUP BY fa."datasetId"
) AS fa ON fa."datasetId" = ds.id
LEFT JOIN (
SELECT
COUNT (a.details -> 'submissionId'::TEXT) total,
SUM (CASE WHEN a."loggedAt" >= ${_cutoffDate} THEN 1 ELSE 0 END) recent,
dfd."datasetId"
FROM audits a
JOIN submissions AS s ON (a.details->'submissionId')::INT = s.id
JOIN submission_defs AS sd ON sd."submissionId" = s.id AND sd."current"
JOIN dataset_form_defs AS dfd ON sd."formDefId" = dfd."formDefId"
WHERE a."action" = 'entity.error'
GROUP BY dfd."datasetId"
SELECT COUNT(a.details->'submissionId') AS total
, COUNT(a."loggedAt" >= ${_cutoffDate} OR NULL) AS recent
, dfd."datasetId"
FROM audits a
JOIN submissions AS s ON (a.details->'submissionId')::INT = s.id
JOIN submission_defs AS sd ON sd."submissionId" = s.id AND sd."current"
JOIN dataset_form_defs AS dfd ON sd."formDefId" = dfd."formDefId"
WHERE a."action" = 'entity.error'
GROUP BY dfd."datasetId"
) AS errors ON ds.id = errors."datasetId"
LEFT JOIN (
SELECT COUNT(*) AS total
Expand All @@ -506,9 +505,9 @@ const getDatasets = () => ({ all }) => all(sql`
GROUP BY e."datasetId"
) AS creates ON ds.id = creates."datasetId"
LEFT JOIN (
SELECT COUNT(*) total,
SUM (CASE WHEN a."loggedAt" >= ${_cutoffDate} THEN 1 ELSE 0 END) recent,
e."datasetId"
SELECT COUNT(*) AS total
, COUNT(a."loggedAt" >= ${_cutoffDate} OR NULL) AS recent
, e."datasetId"
FROM audits a
JOIN entity_def_sources eds on (a.details->'sourceId')::INT = eds."id"
JOIN entity_defs ed on ed."sourceId" = eds.id AND root=true
Expand All @@ -517,13 +516,12 @@ const getDatasets = () => ({ all }) => all(sql`
GROUP BY e."datasetId"
) as bulk_creates on ds.id = bulk_creates."datasetId"
LEFT JOIN (
SELECT COUNT(*) conflicts,
SUM (CASE WHEN e."conflict" IS NULL THEN 1 ELSE 0 END) resolved,
e."datasetId"
FROM entities e
WHERE e.id IN
(SELECT DISTINCT "entityId" FROM entity_defs WHERE "conflictingProperties" IS NOT NULL)
GROUP BY e."datasetId"
SELECT COUNT(*) AS conflicts
, COUNT(e."conflict" IS NULL OR NULL) AS resolved
, e."datasetId"
FROM entities AS e
WHERE e.id IN (SELECT DISTINCT "entityId" FROM entity_defs WHERE "conflictingProperties" IS NOT NULL)
GROUP BY e."datasetId"
) AS conflict_stats ON ds.id = conflict_stats."datasetId"
WHERE ds."publishedAt" IS NOT NULL
`);
Expand Down

0 comments on commit 245958e

Please sign in to comment.