From b6dd235e267c6f46489afd334356ba0098c211b5 Mon Sep 17 00:00:00 2001 From: jfrer Date: Tue, 2 Jul 2024 17:24:38 +0200 Subject: [PATCH] feat: add sorting by label, metric (wip), year --- .../workflows/WorkflowsTimeline.vue | 18 ++++-- .../workflows/timeline/TimelineSorting.vue | 55 ++++++++++++++++ src/helpers/sorting.ts | 64 +++++++++++++++++++ src/locales/de.json | 8 ++- src/locales/en.json | 8 ++- src/types/index.d.ts | 10 +-- 6 files changed, 152 insertions(+), 11 deletions(-) create mode 100644 src/components/workflows/timeline/TimelineSorting.vue create mode 100644 src/helpers/sorting.ts diff --git a/src/components/workflows/WorkflowsTimeline.vue b/src/components/workflows/WorkflowsTimeline.vue index c3c4d63..cd31198 100644 --- a/src/components/workflows/WorkflowsTimeline.vue +++ b/src/components/workflows/WorkflowsTimeline.vue @@ -1,7 +1,7 @@ + \ No newline at end of file diff --git a/src/helpers/sorting.ts b/src/helpers/sorting.ts new file mode 100644 index 0000000..04037af --- /dev/null +++ b/src/helpers/sorting.ts @@ -0,0 +1,64 @@ +import workflowsStore from "@/store/workflows-store" +import type { EvaluationResultsDocumentWide, EvaluationRun, GroundTruth, TimeSpan } from "@/types" + +const GTTimelineSortingOptions = { + LABEL_ASC: 'label_asc', + LABEL_DESC: 'label_desc', + METRIC_DESC: 'metric_desc', + METRIC_ASC: 'metric_asc', + YEAR_ASC: 'year_asc', + YEAR_DESC: 'year_desc' +} + +function sortByOption(gtList: GroundTruth[], sortingOption: string, metric: keyof EvaluationResultsDocumentWide): GroundTruth[] { + if (sortingOption === GTTimelineSortingOptions.METRIC_DESC) return sortByMetric(gtList, true, metric) + if (sortingOption === GTTimelineSortingOptions.METRIC_ASC) return sortByMetric(gtList, false, metric) + if (sortingOption === GTTimelineSortingOptions.LABEL_DESC) return sortByLabel(gtList, true) + if (sortingOption === GTTimelineSortingOptions.LABEL_ASC) return sortByLabel(gtList, false) + if (sortingOption === GTTimelineSortingOptions.YEAR_DESC) return sortByYear(gtList, true) + if (sortingOption === GTTimelineSortingOptions.YEAR_ASC) return sortByYear(gtList, false) + return gtList +} + +function sortByMetric(gtList: GroundTruth[], desc: boolean, metric: keyof EvaluationResultsDocumentWide): GroundTruth[] { + return gtList.sort((left, right) => { + const compareMetric = (left: GroundTruth, right: GroundTruth) => { + const leftRuns = workflowsStore.getRuns(left.id) //assume getLatestRuns(id) TODO -> change to latestRuns + const rightRuns = workflowsStore.getRuns(right.id) //same here + + const getAverageValue = (runs: EvaluationRun[]) => { + if (runs.length === 0) return 0 + return runs.reduce((acc, curr) => { + const value = curr.evaluation_results.document_wide[metric] + return acc += value ?? 0 + }, 0) / runs.length + } + + return getAverageValue(leftRuns) - getAverageValue(rightRuns) + } + return desc ? compareMetric(right, left) : compareMetric(left, right) + }) +} + +function sortByLabel(gtList: GroundTruth[], desc: boolean): GroundTruth[] { + return gtList.sort((left, right) => { + const leftLabel = left.label.toLocaleLowerCase() + const rightLabel = right.label.toLocaleLowerCase() + return desc ? rightLabel.localeCompare(leftLabel) : leftLabel.localeCompare(rightLabel) + }) +} + +function sortByYear(gtList: GroundTruth[], desc: boolean): GroundTruth[] { + return gtList.sort((left, right) => { + const compareTimeSpan = (leftTime: TimeSpan, rightTime: TimeSpan) => { + return (leftTime.notBefore > rightTime.notBefore) ? 1 : ((rightTime.notBefore > leftTime.notBefore) ? -1 : 0) + } + return desc ? compareTimeSpan(right.metadata.time, left.metadata.time) : compareTimeSpan(left.metadata.time, right.metadata.time) + }) +} + + + +export { + GTTimelineSortingOptions, sortByOption +} \ No newline at end of file diff --git a/src/locales/de.json b/src/locales/de.json index db55ba1..46abdec 100644 --- a/src/locales/de.json +++ b/src/locales/de.json @@ -86,5 +86,11 @@ "filter_by_processor": "Nach Prozessor filtern", "select_a_date_range": "Zeitraum auswählen", "select_a_workflow": "Workflow auswählen", - "select_a_processor": "Prozessor auswählen" + "select_a_processor": "Prozessor auswählen", + "label_asc": "Titel (a - z)", + "label_desc": "Titel (z - a)", + "metric_desc": "Ausgewählte Metrik (absteigend)", + "metric_asc": "Ausgewählte Metrik (aufsteigend)", + "year_asc": "Zeitraum (ältester - neuster)", + "year_desc": "Zeitraum (neuster - ältester)" } diff --git a/src/locales/en.json b/src/locales/en.json index 2fc3f36..95866bb 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -80,5 +80,11 @@ "filter_by_processor": "Filter by processor", "select_a_date_range": "Select a date range", "select_a_workflow": "Select a workflow", - "select_a_processor": "Select a processor" + "select_a_processor": "Select a processor", + "label_asc": "Label (a - z)", + "label_desc": "Label (z - a)", + "metric_desc": "Selected metric (descending)", + "metric_asc": "Selected metric (ascending)", + "year_asc": "Time period (oldest - latest)", + "year_desc": "Time period (latest - oldest)" } diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 2776794..f7bc246 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -32,10 +32,7 @@ export interface GroundTruthMetadata { reference: string, link: string }[], - time: { - notBefore: string, - notAfter: string - }, + time: TimeSpan title: string, 'transcription-guidelines': string, url: string, @@ -54,6 +51,11 @@ export interface GroundTruthMetadata { }[], } +export interface TimeSpan { + notBefore: string, + notAfter: string +} + export interface Workflow { id: string, label: string,