Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/backend/InvenTree/stock/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3799,6 +3799,13 @@ class Meta:

verbose_name = _('Stock Item Test Result')

ordering = (
'-finished_datetime',
'-started_datetime',
'-date',
'-pk',
)

def __str__(self):
"""Return string representation."""
return f'{self.test_name} - {self.result}'
Expand Down
43 changes: 40 additions & 3 deletions src/frontend/src/tables/build/BuildOutputTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,45 @@ type TestResultOverview = {
result: boolean;
};

const compareTestResults = (a: any, b: any) => {
const finishedA = a.finished_datetime
? new Date(a.finished_datetime).getTime()
: null;
const finishedB = b.finished_datetime
? new Date(b.finished_datetime).getTime()
: null;

if (finishedA !== finishedB) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this repeated comparison pattern could be refactored into a helper? It's repeated three times here, and three times again in the other file

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. I've refactored the comparison logic into a shared helper and updated both BuildOutputTable and StockItemTestResultTable to use it.

if (finishedA === null) return 1;
if (finishedB === null) return -1;
return finishedB - finishedA;
}

const startedA = a.started_datetime
? new Date(a.started_datetime).getTime()
: null;
const startedB = b.started_datetime
? new Date(b.started_datetime).getTime()
: null;

if (startedA !== startedB) {
if (startedA === null) return 1;
if (startedB === null) return -1;
return startedB - startedA;
}

const dateA = a.date ? new Date(a.date).getTime() : null;
const dateB = b.date ? new Date(b.date).getTime() : null;

if (dateA !== dateB) {
if (dateA === null) return 1;
if (dateB === null) return -1;
return dateB - dateA;
}

return b.pk - a.pk;
};

/**
* Detail drawer view for allocating stock against a specific build output
*/
Expand Down Expand Up @@ -277,9 +316,7 @@ export default function BuildOutputTable({
// Find the "newest" result for this template in the returned data
const result = record.tests
?.filter((test: any) => test.template == template.pk)
.sort((a: any, b: any) => {
return a.pk < b.pk ? 1 : -1;
})
.sort(compareTestResults)
.shift();

if (template?.required && result?.result) {
Expand Down
47 changes: 42 additions & 5 deletions src/frontend/src/tables/stock/StockItemTestResultTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,45 @@ export default function StockItemTestResultTable({
table.refreshTable();
}, [testTemplates]);

const compareTestResults = (a: any, b: any) => {
const finishedA = a.finished_datetime
? new Date(a.finished_datetime).getTime()
: null;
const finishedB = b.finished_datetime
? new Date(b.finished_datetime).getTime()
: null;

if (finishedA !== finishedB) {
if (finishedA === null) return 1;
if (finishedB === null) return -1;
return finishedB - finishedA;
}

const startedA = a.started_datetime
? new Date(a.started_datetime).getTime()
: null;
const startedB = b.started_datetime
? new Date(b.started_datetime).getTime()
: null;

if (startedA !== startedB) {
if (startedA === null) return 1;
if (startedB === null) return -1;
return startedB - startedA;
}

const dateA = a.date ? new Date(a.date).getTime() : null;
const dateB = b.date ? new Date(b.date).getTime() : null;

if (dateA !== dateB) {
if (dateA === null) return 1;
if (dateB === null) return -1;
return dateB - dateA;
}

return b.pk - a.pk;
};

// Format the test results based on the returned data
const formatRecords = useCallback(
(records: any[]): any[] => {
Expand All @@ -110,12 +149,10 @@ export default function StockItemTestResultTable({
});

// Iterate through the returned records
// Note that the results are sorted by oldest first,
// to ensure that the most recent result is displayed "on top"
// Sort test results using the same priority as the backend:
// finished_datetime -> started_datetime -> date -> pk
records
.sort((a: any, b: any) => {
return a.pk > b.pk ? 1 : -1;
})
.sort(compareTestResults)
.forEach((record) => {
// Find matching template
const idx = results.findIndex(
Expand Down
Loading