Skip to content

Commit

Permalink
Updating interrupted branch count query
Browse files Browse the repository at this point in the history
  • Loading branch information
ktuite committed Sep 25, 2024
1 parent 3904abf commit 44c3eb0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/model/query/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,22 +496,23 @@ WITH sortedRows AS (
"entityId",
"version",
"branchId",
LAG("branchId") OVER (PARTITION BY "entityId" ORDER BY "version") AS prevBranchId
LAG("branchId") OVER (PARTITION BY "entityId" ORDER BY "version") AS "prevBranchId"
FROM entity_defs
),
distinctRuns AS (
SELECT
"entityId",
"branchId"
FROM sortedRows
WHERE "branchId" != prevBranchId OR prevBranchId IS NULL -- Keep first row and changes
WHERE "version" = 1 OR "branchId" IS DISTINCT FROM "prevBranchId" -- Keep first row and changes
),
duplicateRuns AS (
SELECT
"entityId",
"branchId",
COUNT(*) AS runCount
FROM distinctRuns
WHERE "branchId" IS NOT NULL
GROUP BY "entityId", "branchId"
HAVING COUNT(*) > 1 -- Selects branchIds that occur more than once
)
Expand Down
36 changes: 35 additions & 1 deletion test/integration/other/analytics-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,7 @@ describe('analytics task queries', function () {
{ uuid: '12345678-1234-4123-8234-123456789aaa', label: 'aaa' },
{ uuid: '12345678-1234-4123-8234-123456789bbb', label: 'bbb' },
{ uuid: '12345678-1234-4123-8234-123456789ccc', label: 'ccc' },
{ uuid: '12345678-1234-4123-8234-123456789ddd', label: 'ddd' },
],
source: { name: 'api', size: 3 }
})
Expand Down Expand Up @@ -1555,8 +1556,41 @@ describe('analytics task queries', function () {

await exhaust(container);

// ddd entity, branches A, (api update), A
const dddBranchA = uuid();
await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions')
.send(testData.instances.offlineEntity.one
.replace('id="12345678-1234-4123-8234-123456789abc', 'id="12345678-1234-4123-8234-123456789ddd')
.replace('one', 'ddd-v1')
.replace('branchId=""', `branchId="${dddBranchA}"`)
)
.set('Content-Type', 'application/xml')
.expect(200);

await exhaust(container);

await asAlice.patch('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789ddd?baseVersion=2')
.send({ label: 'ddd update' })
.expect(200);

await asAlice.patch('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789ddd?baseVersion=3')
.send({ label: 'ddd update2' })
.expect(200);

await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions')
.send(testData.instances.offlineEntity.one
.replace('id="12345678-1234-4123-8234-123456789abc', 'id="12345678-1234-4123-8234-123456789ddd')
.replace('one', 'ddd-v2')
.replace('baseVersion="1"', 'baseVersion="2"')
.replace('branchId=""', `branchId="${dddBranchA}"`)
)
.set('Content-Type', 'application/xml')
.expect(200);

await exhaust(container);

const countInterruptedBranches = await container.Analytics.countInterruptedBranches();
countInterruptedBranches.should.equal(3);
countInterruptedBranches.should.equal(4);
}));

it('should count number of submission.reprocess events (submissions temporarily in the backlog)', testService(async (service, container) => {
Expand Down

0 comments on commit 44c3eb0

Please sign in to comment.