diff --git a/libs/shared/lib/data-access/store/insightSharingSlice.ts b/libs/shared/lib/data-access/store/insightSharingSlice.ts index a3a4105e0b9ec084fb22ac743a90be452b6bcf55..0bd1064e658bbc4423b4904d0ea57ac0eb207ff2 100644 --- a/libs/shared/lib/data-access/store/insightSharingSlice.ts +++ b/libs/shared/lib/data-access/store/insightSharingSlice.ts @@ -9,9 +9,10 @@ export type InsightRequest = { recipients: string[]; frequency: string; template: string; + statsCheck: { node: string; statistic: string; condition: string; value: number }; saveStateId: string; type: 'report' | 'alert'; -} +}; export type InsightResponse = { id: string; @@ -21,10 +22,10 @@ export type InsightResponse = { type InsightState = { insights: InsightResponse[]; -} +}; const initialState: InsightState = { - insights: [] + insights: [], }; const insightSharingSlice = createSlice({ @@ -56,7 +57,7 @@ const insightSharingSlice = createSlice({ state.insights.push(action.payload); } }, - deleteInsight(state, action: PayloadAction<{ id: string; }>) { + deleteInsight(state, action: PayloadAction<{ id: string }>) { const index = state.insights.findIndex((i) => i.id === action.payload.id); if (index !== -1) { state.insights.splice(index, 1); diff --git a/libs/shared/lib/insight-sharing/FormReport.tsx b/libs/shared/lib/insight-sharing/FormReport.tsx index de77445955f305551a60cf19c78c201a0b356ed9..9cae770903fb3005a1ed6df249ea7f53c4d7ed0f 100644 --- a/libs/shared/lib/insight-sharing/FormReport.tsx +++ b/libs/shared/lib/insight-sharing/FormReport.tsx @@ -8,7 +8,7 @@ import { deleteInsight, InsightRequest, } from '@graphpolaris/shared/lib/data-access/store/insightSharingSlice'; -import { useAppDispatch, useSessionCache } from '@graphpolaris/shared/lib/data-access'; +import { useAppDispatch, useSessionCache, useGraphQueryResult } from '@graphpolaris/shared/lib/data-access'; import { MonitorType } from './components/Sidebar'; import { EditorState } from 'lexical'; import { wsCreateInsight, wsDeleteInsight, wsUpdateInsight } from '@graphpolaris/shared/lib/data-access/broker/wsInsightSharing'; @@ -31,12 +31,20 @@ export function FormReport(props: Props) { const [frequency, setFrequency] = useState(props.insight.frequency || 'Daily'); const [editorState, setEditorState] = useState<EditorState | null>(null); + //stats + const [statsCheck, setStatsCheck] = useState<InsightRequest['statsCheck']>(props.insight.statsCheck); + const result = useGraphQueryResult(); + const nodeTypes = Object.keys(result.metaData?.nodes.types || {}); + const optionsComparison = ['Greater than', 'Equal than', 'Smaller than']; + useEffect(() => { setName(props.insight.name); setDescription(props.insight.description); setRecipientInput(props.insight.recipients ? props.insight.recipients.join(', ') : ''); setRecipients(props.insight.recipients || []); setFrequency(props.insight.frequency || 'Daily'); + setStatsCheck(props.insight.statsCheck || { node: '', statistic: '', condition: '', value: 0 }); + if (props.insight.template) { try { const parsedTemplate = JSON.parse(props.insight.template); @@ -62,6 +70,36 @@ export function FormReport(props: Props) { setDescription(value as string); }; + const handleSetStatNode = (value: string | number) => { + setStatsCheck((prevState) => ({ + ...prevState, + node: String(value), + })); + }; + + const handleSetStatStatistic = (value: string | number) => { + setStatsCheck((prevState) => ({ + ...prevState, + statistic: String(value), + })); + }; + + const handleSetStatComparison = (value: string | number) => { + setStatsCheck((prevState) => ({ + ...prevState, + condition: String(value), + })); + }; + + const handleSetStatValue = (value: string) => { + if (!isNaN(Number(value)) && value.trim() !== '') { + setStatsCheck((prevState) => ({ + ...prevState, + value: Number(value), + })); + } + }; + const handleSave = async () => { if (!name || name.trim() === '') { dispatch(addError('Please enter a name for the report.')); @@ -73,6 +111,7 @@ export function FormReport(props: Props) { description, recipients, frequency, + statsCheck, template: JSON.stringify(editorState), saveStateId: session.currentSaveState || '', type: 'report' as const, @@ -170,6 +209,33 @@ export function FormReport(props: Props) { options={['Daily', 'Weekly']} className="mb-1" /> + <div className="flex flex-row mt-1 gap-96"> + <div> + <label className="label p-0"> + <span className={'text-sm font-medium text-secondary-700'}>{'Statistical Check'}</span> + </label> + </div> + <div className="w-60 ml-4"> + <Input + label="Node" + type="dropdown" + value={statsCheck.node} + onChange={handleSetStatNode} + options={nodeTypes} + className="m-1" + /> + <Input label="Stat" type="dropdown" value={'Count'} onChange={handleSetStatStatistic} options={['Count']} className="m-1" /> + <Input + label="Comparison" + type="dropdown" + value={statsCheck.condition} + onChange={handleSetStatComparison} + options={optionsComparison} + className="m-1" + /> + <Input type="text" size="sm" className="m-1" value={statsCheck.value.toString()} onChange={handleSetStatValue} /> + </div> + </div> </AccordionBody> </AccordionItem> <AccordionItem className="pt-2 pb-4">