Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions src/backend/InvenTree/stock/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3811,6 +3811,8 @@ 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
39 changes: 39 additions & 0 deletions src/frontend/src/functions/comparison.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,42 @@ export function isEquivalent(a: any, b: any): boolean {

return false;
}

export function 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 uploadTimeA = a.date ? new Date(a.date).getTime() : null;
const uploadTimeB = b.date ? new Date(b.date).getTime() : null;

if (uploadTimeA !== uploadTimeB) {
if (uploadTimeA === null) return 1;
if (uploadTimeB === null) return -1;
return uploadTimeB - uploadTimeA;
}

return b.pk - a.pk;
}
5 changes: 2 additions & 3 deletions src/frontend/src/tables/build/BuildOutputTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
useStockFields,
useStockItemSerializeFields
} from '../../forms/StockForms';
import { compareTestResults } from '../../functions/comparison';
import { InvenTreeIcon } from '../../functions/icons';
import useBackgroundTask from '../../hooks/UseBackgroundTask';
import {
Expand Down Expand Up @@ -277,9 +278,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
11 changes: 4 additions & 7 deletions src/frontend/src/tables/stock/StockItemTestResultTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import RowExpansionIcon from '../../components/tables/RowExpansionIcon';
import { useApi } from '../../contexts/ApiContext';
import { formatDate } from '../../defaults/formatters';
import { compareTestResults } from '../../functions/comparison';
import { useTestResultFields } from '../../forms/StockForms';
import {
useCreateApiFormModal,
Expand Down Expand Up @@ -110,13 +111,9 @@
});

// 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"
records
.sort((a: any, b: any) => {
return a.pk > b.pk ? 1 : -1;
})
.forEach((record) => {
// Sort test results using the same priority as the backend:
// finished_datetime -> started_datetime -> date -> pk
records.sort(compareTestResults).forEach((record) => {

Check warning on line 116 in src/frontend/src/tables/stock/StockItemTestResultTable.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move this array "sort" operation to a separate statement or replace it with "toSorted".

See more on https://sonarcloud.io/project/issues?id=inventree_InvenTree&issues=AZ-96asnVL7xxLuozPXP&open=AZ-96asnVL7xxLuozPXP&pullRequest=12533
// Find matching template
const idx = results.findIndex(
(r: any) => r.templateId == record.template
Expand Down
Loading