mirror of https://github.com/grafana/grafana
Dynamic dashboards: Persist conditional rendering (#102022)
* Dashboards: Add conditional rendering * Updates * Fixes * Code improvements * Code improvements * limit condition choices, add delete and clean up ui * add basic variable condition * add conditional rendering based on time range interval * adjust failing test * remove deprecated pseudo locale file * extract conditional rendering from behaviour to state property * clean up behaviour initialisation * clean up ts errors * adjust data condition to account for RowItem * persist-conditional-rendering * fix group value name and kind type * Fix types in base * minor style fix * Fix subscribes * notify change when deleting condition * fix hidden row item error * Remove option to have groups in groups * fix merge issue * address comments * subscribe to panel data change in data condition * Remove loop labels * only persist conditional rendering if root group has items * update backend types * Serialize variable conditional rendering operator as equals notEquals --------- Co-authored-by: Bogdan Matei <bogdan.matei@grafana.com> Co-authored-by: Sergej-Vlasov <sergej.s.vlasov@gmail.com>pull/92618/merge
parent
8fd2a12670
commit
d07b1851c7
@ -0,0 +1,96 @@ |
|||||||
|
import { Registry, RegistryItem } from '@grafana/data'; |
||||||
|
import { |
||||||
|
ConditionalRenderingGroupKind, |
||||||
|
ConditionalRenderingVariableKind, |
||||||
|
ConditionalRenderingDataKind, |
||||||
|
ConditionalRenderingTimeIntervalKind, |
||||||
|
} from '@grafana/schema/dist/esm/schema/dashboard/v2alpha0'; |
||||||
|
|
||||||
|
import { ConditionalRenderingData } from './ConditionalRenderingData'; |
||||||
|
import { ConditionalRenderingGroup } from './ConditionalRenderingGroup'; |
||||||
|
import { ConditionalRenderingInterval } from './ConditionalRenderingInterval'; |
||||||
|
import { ConditionalRenderingVariable } from './ConditionalRenderingVariable'; |
||||||
|
|
||||||
|
export type ConditionalRenderingKindTypes = |
||||||
|
| ConditionalRenderingGroupKind |
||||||
|
| ConditionalRenderingVariableKind |
||||||
|
| ConditionalRenderingDataKind |
||||||
|
| ConditionalRenderingTimeIntervalKind; |
||||||
|
|
||||||
|
export interface ConditionalRenderingSerializer { |
||||||
|
deserialize( |
||||||
|
model: ConditionalRenderingKindTypes |
||||||
|
): ConditionalRenderingGroup | ConditionalRenderingVariable | ConditionalRenderingData | ConditionalRenderingInterval; |
||||||
|
} |
||||||
|
|
||||||
|
interface ConditionalRenderingSerializerRegistryItem extends RegistryItem { |
||||||
|
serializer: ConditionalRenderingSerializer; |
||||||
|
} |
||||||
|
|
||||||
|
export class ConditionalRenderingGroupSerializer implements ConditionalRenderingSerializer { |
||||||
|
deserialize(model: ConditionalRenderingGroupKind): ConditionalRenderingGroup { |
||||||
|
return new ConditionalRenderingGroup({ |
||||||
|
condition: model.spec.condition, |
||||||
|
value: model.spec.items.map((item: ConditionalRenderingKindTypes) => { |
||||||
|
const serializerRegistryItem = conditionalRenderingSerializerRegistry.getIfExists(item.kind); |
||||||
|
if (!serializerRegistryItem) { |
||||||
|
throw new Error(`No serializer found for conditional rendering kind: ${item.kind}`); |
||||||
|
} |
||||||
|
return serializerRegistryItem.serializer.deserialize(item); |
||||||
|
}), |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export class ConditionalRenderingVariableSerializer implements ConditionalRenderingSerializer { |
||||||
|
deserialize(model: ConditionalRenderingVariableKind): ConditionalRenderingVariable { |
||||||
|
return new ConditionalRenderingVariable({ |
||||||
|
value: { |
||||||
|
name: model.spec.variable, |
||||||
|
operator: model.spec.operator === 'equals' ? '=' : '!=', |
||||||
|
value: model.spec.value, |
||||||
|
}, |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export class ConditionalRenderingDataSerializer implements ConditionalRenderingSerializer { |
||||||
|
deserialize(model: ConditionalRenderingDataKind): ConditionalRenderingData { |
||||||
|
return new ConditionalRenderingData({ |
||||||
|
value: model.spec.value, |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export class ConditionalRenderingIntervalSerializer implements ConditionalRenderingSerializer { |
||||||
|
deserialize(model: ConditionalRenderingTimeIntervalKind): ConditionalRenderingInterval { |
||||||
|
return new ConditionalRenderingInterval({ |
||||||
|
value: model.spec.value, |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const conditionalRenderingSerializerRegistry = new Registry<ConditionalRenderingSerializerRegistryItem>(() => { |
||||||
|
return [ |
||||||
|
{ |
||||||
|
id: 'ConditionalRenderingGroup', |
||||||
|
name: 'Group', |
||||||
|
serializer: new ConditionalRenderingGroupSerializer(), |
||||||
|
}, |
||||||
|
{ |
||||||
|
id: 'ConditionalRenderingVariable', |
||||||
|
name: 'Variable', |
||||||
|
serializer: new ConditionalRenderingVariableSerializer(), |
||||||
|
}, |
||||||
|
{ |
||||||
|
id: 'ConditionalRenderingData', |
||||||
|
name: 'Data', |
||||||
|
serializer: new ConditionalRenderingDataSerializer(), |
||||||
|
}, |
||||||
|
{ |
||||||
|
id: 'ConditionalRenderingTimeInterval', |
||||||
|
name: 'Time Interval', |
||||||
|
serializer: new ConditionalRenderingIntervalSerializer(), |
||||||
|
}, |
||||||
|
]; |
||||||
|
}); |
||||||
Loading…
Reference in new issue