diff --git a/ui-react/src/lib/datasets/__tests__/upload-preview.test.ts b/ui-react/src/lib/datasets/__tests__/upload-preview.test.ts new file mode 100644 index 00000000..3eeb1965 --- /dev/null +++ b/ui-react/src/lib/datasets/__tests__/upload-preview.test.ts @@ -0,0 +1,122 @@ +import { describe, expect, it } from 'vitest'; +import type { GetModelTreeWithRegionsQuery, ModelConfigInfo } from '@/graphql/generated/modeling'; +import { rankSuggestionsByUse, suggestionUseText } from '@/lib/datasets/upload-preview'; + +const SV = 'https://w3id.org/okn/i/mint/'; + +function input(id: string, variable: string, format: string | null) { + return { + configuration_id: 'c', + input_id: id, + is_optional: false, + input: { + id, + label: id, + has_format: format, + presentations: [ + { + dataset_specification_id: id, + presentation_id: `${id}-p`, + presentation: { + id: `${id}-p`, + standard_variable: { id: SV + variable, label: variable }, + }, + }, + ], + }, + }; +} + +function config(id: string, inputs: ReturnType[], flow = false): ModelConfigInfo { + return { + id, + label: id, + flow: flow ? { configuration_id: id, thread_bindings_json: null } : null, + regions: [], + inputs, + outputs: [], + child_configurations: [], + } as unknown as ModelConfigInfo; +} + +const tree: GetModelTreeWithRegionsQuery = { + modelcatalog_software: [ + { + id: 's', + label: 's', + versions: [ + { + id: 'v', + label: 'v', + configurations: [ + config( + 'Dictionary flow', + [input('corpus', 'document_collection__text', 'directory')], + true, + ), + config( + 'Narrative flow', + [input('corpus2', 'document_collection__text', 'directory')], + true, + ), + config('HAND', [input('dem', 'land_surface__elevation', 'tif')]), + config('Narratives model', [input('nlp', 'corpus_nlp', null)]), + ], + }, + ], + }, + ], +}; + +// The semantic order the service returned for "RGV Transcripts — oral histories". +const hits = [ + { id: SV + 'corpus_nlp', label: 'corpus_nlp' }, + { + id: SV + 'document_collection_narrative_frame__description', + label: 'document_collection_narrative_frame__description', + }, + { id: SV + 'land_surface__elevation', label: 'land_surface__elevation' }, + { id: SV + 'document_collection__text', label: 'document_collection__text' }, +]; + +const labels = (ranked: { item: { label: string } }[]) => ranked.map((r) => r.item.label); + +describe('rankSuggestionsByUse', () => { + it('puts the variable a flow input accepts these files under first', () => { + const ranked = rankSuggestionsByUse(tree, hits, [{ name: 'transcript.json' }]); + expect(labels(ranked)).toEqual([ + 'document_collection__text', + 'corpus_nlp', + 'land_surface__elevation', + 'document_collection_narrative_frame__description', + ]); + expect(ranked.map((r) => r.fit)).toEqual([0, 1, 2, 3]); + }); + + it('keeps the semantic order among variables that fit equally', () => { + const ranked = rankSuggestionsByUse(tree, hits, []); + // No files yet: anything an input takes ranks above what nothing takes. + expect(labels(ranked)).toEqual([ + 'corpus_nlp', + 'land_surface__elevation', + 'document_collection__text', + 'document_collection_narrative_frame__description', + ]); + }); + + it('ranks nothing without the model tree but keeps every suggestion', () => { + expect(labels(rankSuggestionsByUse(undefined, hits, []))).toEqual(hits.map((h) => h.label)); + }); +}); + +describe('suggestionUseText', () => { + it('says who takes the variable and when the format is wrong', () => { + const ranked = rankSuggestionsByUse(tree, hits, [{ name: 'transcript.json' }]); + expect(ranked.map(suggestionUseText)).toEqual([ + 'Input to 2 flows', + 'Input to 1 model', + 'Input to 1 model, but not in this file format', + 'No registered flow or model takes this as an input', + ]); + }); +}); diff --git a/ui-react/src/lib/datasets/upload-preview.ts b/ui-react/src/lib/datasets/upload-preview.ts index ace22e0b..71ad08fe 100644 --- a/ui-react/src/lib/datasets/upload-preview.ts +++ b/ui-react/src/lib/datasets/upload-preview.ts @@ -76,6 +76,74 @@ export function consumersOfVariable( ); } +/** + * How useful a suggested variable is for these files, best first: + * 0 an input that declares a format takes these files, + * 1 an input takes the variable but declares no format (or no files are chosen yet), + * 2 inputs take the variable but none takes these files, + * 3 no registered flow or model takes it. + */ +export type SuggestionFit = 0 | 1 | 2 | 3; + +export interface RankedSuggestion { + item: T; + fit: SuggestionFit; + /** Flows and models with an input that takes the variable, whatever the format. */ + flows: number; + models: number; +} + +function suggestionFit(consumers: UploadConsumer[]): SuggestionFit { + if (consumers.length === 0) return 3; + if (consumers.some((c) => c.formatFits === true && (c.format ?? '').trim())) return 0; + if (consumers.some((c) => c.formatFits !== false)) return 1; + return 2; +} + +/** + * Order the semantic suggestions by whether a registered flow or model could use + * the files under that variable, keeping the semantic order within each group. + * Text similarity alone cannot tell "a corpus of transcripts" from "frames drawn + * from transcripts"; what the catalog's inputs accept can. + */ +export function rankSuggestionsByUse( + tree: GetModelTreeWithRegionsQuery | undefined, + suggestions: T[], + files: Array<{ name: string }>, +): RankedSuggestion[] { + return suggestions + .map((item, index) => { + const consumers = consumersOfVariable( + tree, + { id: item.id, label: item.label?.trim() || item.id }, + files, + ); + return { + index, + ranked: { + item, + fit: suggestionFit(consumers), + flows: consumers.filter((c) => c.kind === 'flow').length, + models: consumers.filter((c) => c.kind === 'model').length, + }, + }; + }) + .sort((a, b) => a.ranked.fit - b.ranked.fit || a.index - b.index) + .map(({ ranked }) => ranked); +} + +/** "Input to 6 flows and 1 model", or why nothing can use it. */ +export function suggestionUseText(ranked: RankedSuggestion): string { + const count = (n: number, word: string) => `${n} ${word}${n === 1 ? '' : 's'}`; + const parts = [ + ranked.flows ? count(ranked.flows, 'flow') : '', + ranked.models ? count(ranked.models, 'model') : '', + ].filter(Boolean); + if (parts.length === 0) return 'No registered flow or model takes this as an input'; + const users = parts.join(' and '); + return ranked.fit === 2 ? `Input to ${users}, but not in this file format` : `Input to ${users}`; +} + export interface CloseConsumer extends UploadConsumer { /** The close variable whose input this matched on. */ via: string; diff --git a/ui-react/src/pages/datasets/DatasetUploadPage.tsx b/ui-react/src/pages/datasets/DatasetUploadPage.tsx index 4c2a8bc7..ba476a23 100644 --- a/ui-react/src/pages/datasets/DatasetUploadPage.tsx +++ b/ui-react/src/pages/datasets/DatasetUploadPage.tsx @@ -35,7 +35,12 @@ import { } from '@/lib/ckan-upload'; import { useDebouncedValue } from '@/hooks/useDebouncedValue'; import { useSemanticSearch, useSimilarVariables } from '@/hooks/useSemanticSearch'; -import { closeConsumers, consumersOfVariable } from '@/lib/datasets/upload-preview'; +import { + closeConsumers, + consumersOfVariable, + rankSuggestionsByUse, + suggestionUseText, +} from '@/lib/datasets/upload-preview'; import { unionBoundingBox, type BoundingBox } from '@/lib/geo/bbox'; import { standardVariableName } from '@/lib/standard-variable-display'; import { useAuth } from '@/lib/auth/useAuth'; @@ -177,12 +182,21 @@ export function DatasetUploadPage() { // Standard variables suggested from what the user wrote. Never chosen for them. const aboutText = useDebouncedValue(`${title}\n${description}`.trim(), SUGGEST_DELAY_MS); const suggested = useSemanticSearch(aboutText, { target: 'svo', limit: 20, minLength: 4 }); + // Ranked by whether a registered flow or model takes these files under the + // variable, then by how close it is to the text. const suggestions = useMemo( - () => (suggested.results ?? []).filter((r) => r.label?.trim()), - [suggested.results], + () => + rankSuggestionsByUse( + tree, + (suggested.results ?? []).filter((r) => r.label?.trim()), + files, + ), + [tree, suggested.results, files], ); - const suggestionIds = useMemo(() => suggestions.map((r) => r.id), [suggestions]); - const shownSuggestions = suggestions.filter((r) => r.id !== variable?.id).slice(0, SUGGEST_SHOWN); + const suggestionIds = useMemo(() => suggestions.map((r) => r.item.id), [suggestions]); + const shownSuggestions = suggestions + .filter((r) => r.item.id !== variable?.id) + .slice(0, SUGGEST_SHOWN); const similar = useSimilarVariables(variable?.id, { limit: 5 }); const closeMatches = useMemo( @@ -532,32 +546,43 @@ export function DatasetUploadPage() {

Suggested standard variables

    - {shownSuggestions.map((s) => ( -
  • - - {s.label} - {s.description && ( - {s.description} - )} - - -
  • - ))} + {shownSuggestions.map((ranked) => { + const s = ranked.item; + return ( +
  • + + {s.label} + {s.description && ( + + {s.description} + + )} + + {suggestionUseText(ranked)} + + + +
  • + ); + })}
)}