Skip to content

Commit

Permalink
Fix calc_dataset_view join with label values
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Lamparelli <[email protected]>
  • Loading branch information
lampajr committed Dec 5, 2024
1 parent 41c1fa5 commit 1d550ce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,6 @@ public void recalculateDatasets(int testId) {
RecalculationStatus status = new RecalculationStatus(RunDAO.count("testid = ?1 AND trashed = false", testId));
// we don't have to care about races with new runs
RecalculationStatus prev = recalculations.putIfAbsent(testId, status);
// ensure the recalculation is removed, with this approach we should guarantee
// it gets removed even if transaction-level exception occurs, e.g., timeout
Util.registerTxSynchronization(tm, txStatus -> recalculations.remove(testId, status));
if (prev != null) {
log.infof("Recalculation for test %d (%s) already in progress", testId, test.name);
return;
Expand All @@ -652,7 +649,8 @@ public void recalculateDatasets(int testId) {
log.debugf("Deleted %d datasets for trashed runs in test %s (%d)", deleted, test.name, (Object) testId);
}

try (ScrollableResults results = em
log.infof("Recalculating datasets for test %d (%s)", testId, test.name);
try (ScrollableResults<Integer> results = em
.createNativeQuery("SELECT id FROM run WHERE testid = ?1 AND NOT trashed ORDER BY start")
.setParameter(1, testId)
.unwrap(NativeQuery.class).setReadOnly(true).setFetchSize(100)
Expand Down
23 changes: 23 additions & 0 deletions horreum-backend/src/main/resources/db/changeLog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4665,4 +4665,27 @@
<dropColumn tableName="schema" columnName="token" />
<dropTable tableName="test_token" />
</changeSet>
<changeSet id="125" author="lampajr">
<validCheckSum>ANY</validCheckSum>
<sql>
-- still need db procedure until https://hibernate.atlassian.net/browse/HHH-17314 is fixed in quarkus
CREATE OR REPLACE PROCEDURE calc_dataset_view(datasetId bigint) AS $$
BEGIN
WITH view_agg AS (
SELECT
vc.view_id, vc.id as vcid, array_agg(DISTINCT label.id) as label_ids, jsonb_object_agg(label.name, lv.value) as value FROM dataset_schemas ds
JOIN label ON label.schema_id = ds.schema_id
JOIN viewcomponent vc ON vc.labels ? label.name
JOIN label_values lv ON lv.label_id = label.id AND lv.dataset_id = ds.dataset_id
WHERE ds.dataset_id = datasetId
AND vc.view_id IN (SELECT view.id FROM view JOIN dataset ON view.test_id = dataset.testid WHERE dataset.id = datasetId)
GROUP BY vc.view_id, vcid
)
INSERT INTO dataset_view (dataset_id, view_id, label_ids, value)
SELECT datasetId, view_id, array_agg(DISTINCT label_id), jsonb_object_agg(vcid, value) FROM view_agg, unnest(label_ids) as label_id
GROUP BY view_id;
END
$$ LANGUAGE plpgsql;
</sql>
</changeSet>
</databaseChangeLog>

0 comments on commit 1d550ce

Please sign in to comment.