Skip to content

Commit

Permalink
feat: Allow most recent filter logic to be applied for TemporalOrdina…
Browse files Browse the repository at this point in the history
…lDimension
  • Loading branch information
bprusinowski committed Jan 19, 2024
1 parent 18125d3 commit 7669754
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
52 changes: 49 additions & 3 deletions app/configurator/components/field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
HierarchyValue,
ObservationValue,
TemporalDimension,
isTemporalOrdinalDimension,
} from "@/domain/data";
import { useTimeFormatLocale } from "@/formatters";
import { TimeUnit } from "@/graphql/query-hooks";
Expand Down Expand Up @@ -240,11 +241,56 @@ export const DataFilterSelect = ({
);
}

const canUseMostRecentValue = isTemporalOrdinalDimension(dimension);
const usesMostRecentValue = isDynamicMaxValue(fieldProps.value);
const maxValue = sortedValues[sortedValues.length - 1].value;

return (
<Select
id={id}
label={<FieldLabel label={label} isOptional={isOptional} />}
disabled={disabled}
label={
canUseMostRecentValue ? (
<Flex
sx={{
width: "100%",
justifyContent: "space-between",
alignItems: "center",
}}
>
<FieldLabel label={label} isOptional={isOptional} />
<FormGroup>
<FormControlLabel
control={
<MUISwitch
size="small"
checked={usesMostRecentValue}
onChange={() =>
fieldProps.onChange({
target: {
value: usesMostRecentValue
? `${maxValue}`
: VISUALIZE_MAX_VALUE,
},
})
}
/>
}
label={
<Typography variant="caption">
<Trans id="controls.filter.use-most-recent">
Use most recent
</Trans>
</Typography>
}
sx={{ mr: 0 }}
/>
</FormGroup>
</Flex>
) : (
<FieldLabel label={label} isOptional={isOptional} />
)
}
disabled={disabled || usesMostRecentValue}
options={allValues}
sortOptions={false}
controls={controls}
Expand All @@ -253,6 +299,7 @@ export const DataFilterSelect = ({
onOpen={handleOpen}
loading={loading}
{...fieldProps}
value={usesMostRecentValue ? maxValue : fieldProps.value}
/>
);
};
Expand Down Expand Up @@ -349,7 +396,6 @@ export const DataFilterTemporal = (props: DataFilterTemporalProps) => {
</OpenMetadataPanelWrapper>
}
/>
{/* FIXME: adapt to design */}
<FormGroup>
<FormControlLabel
control={
Expand Down
15 changes: 13 additions & 2 deletions app/rdf/query-dimension-values.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import RDF from "@rdfjs/data-model";
import { SELECT, sparql } from "@tpluscode/sparql-builder";
import keyBy from "lodash/keyBy";
import mapValues from "lodash/mapValues";
Expand Down Expand Up @@ -183,12 +184,22 @@ export async function loadMaxDimensionValue(
? getFiltersList(filters, dimensionIri?.value)
: [];

const query = SELECT`(MAX(?value) as ?value)`.WHERE`
// The following query works both for numeric, date and ordinal dimensions
const query = SELECT`?value`.WHERE`
${datasetIri} ${cubeNs.observationSet} ?observationSet .
?observationSet ${cubeNs.observation} ?observation .
?observation ${dimensionIri} ?value .
OPTIONAL {
?value <https://www.w3.org/TR/owl-time/hasEnd> ?hasEnd .
?value ${ns.schema.position} ?position .
}
${getQueryFilters(filterList, cube, dimensionIri?.value)}
`.prologue`${pragmas}`;
`
.ORDER()
.BY(RDF.variable("hasEnd"), true)
.THEN.BY(RDF.variable("value"), true)
.THEN.BY(RDF.variable("position"), true)
.LIMIT(1).prologue`${pragmas}`;

let result: Array<DimensionValue> = [];

Expand Down

0 comments on commit 7669754

Please sign in to comment.