From 97bfef94a8833dc8906b4d8fe3a43024c9b3b78e Mon Sep 17 00:00:00 2001 From: Igor Krupenja Date: Fri, 13 Dec 2024 15:23:39 +0200 Subject: [PATCH] Remove excess rule fetching on /training/intents page (#754) * Merge relevant from old branch * Fix guard * Fix response logic * Fix deps * Remove deupe code * TODOs * Revert unrelated changes * Clean up * Clean up * Clean up * Fix deletion * Clean up * Move entities * Clean up * Clean up * Fix adding example * Fix examples * Clean up * Clean up * Clean up * Fix rule * Fix editing example * gsa * Clean up * Clean up * Clean up * Fix the issue * PoC without data mapping * PoC with poor format * PoC with better format * Clean up * Cle * PoC component * Clean up * Fix name * Fix query * Fix fetch * Revert unrelated changes * Clean up * Clean up * Clean up * Clean up * PoC request * Finish --- .../hbs/get_rule_by_intent_id.handlebars | 3 ++ .../response-with-name-and-text.json | 6 ++- .../GET/rasa/rule-by-intent-id.yml | 42 +++++++++++++++++++ .../pages/Training/Intents/IntentDetails.tsx | 33 +++++---------- 4 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 DSL/DMapper/hbs/get_rule_by_intent_id.handlebars create mode 100644 DSL/Ruuter.private/GET/rasa/rule-by-intent-id.yml diff --git a/DSL/DMapper/hbs/get_rule_by_intent_id.handlebars b/DSL/DMapper/hbs/get_rule_by_intent_id.handlebars new file mode 100644 index 00000000..71a54921 --- /dev/null +++ b/DSL/DMapper/hbs/get_rule_by_intent_id.handlebars @@ -0,0 +1,3 @@ +{ + "id": "{{hits.0._source.rule}}" +} \ No newline at end of file diff --git a/DSL/OpenSearch/templates/response-with-name-and-text.json b/DSL/OpenSearch/templates/response-with-name-and-text.json index 18270ae5..ce90ec8f 100644 --- a/DSL/OpenSearch/templates/response-with-name-and-text.json +++ b/DSL/OpenSearch/templates/response-with-name-and-text.json @@ -11,13 +11,15 @@ "query": "*{{response_name}}*", "default_field": "name" } - }, + } + {{#response_text}}, { "query_string": { "query": "*{{response_text}}*", "default_field": "response.text" } } + {{/response_text}} ] } } @@ -27,4 +29,4 @@ "response_text": "" } } -} +} \ No newline at end of file diff --git a/DSL/Ruuter.private/GET/rasa/rule-by-intent-id.yml b/DSL/Ruuter.private/GET/rasa/rule-by-intent-id.yml new file mode 100644 index 00000000..e4e3db21 --- /dev/null +++ b/DSL/Ruuter.private/GET/rasa/rule-by-intent-id.yml @@ -0,0 +1,42 @@ +declaration: + call: declare + version: 0.1 + description: "Get rule by intent name" + method: get + accepts: json + returns: json + namespace: training + allowlist: + params: + - field: intent + type: string + description: "Intent ID" + +assign_values: + assign: + rule: ${"rule_" + incoming.params.intent} + +getRules: + call: http.post + args: + url: "[#TRAINING_OPENSEARCH]/rules/_search/template" + body: + id: "rule-with-name" + params: + rule: ${rule} + result: getRulesResult + +mapRulesData: + call: http.post + args: + url: "[#TRAINING_DMAPPER]/hbs/training/get_rule_by_intent_id" + headers: + type: "json" + body: + hits: ${getRulesResult.response.body.hits.hits} + result: rulesData + # next: returnSuccess + +returnSuccess: + return: ${rulesData.response.body} + next: end diff --git a/GUI/src/pages/Training/Intents/IntentDetails.tsx b/GUI/src/pages/Training/Intents/IntentDetails.tsx index 177effb7..41fbf20f 100644 --- a/GUI/src/pages/Training/Intents/IntentDetails.tsx +++ b/GUI/src/pages/Training/Intents/IntentDetails.tsx @@ -41,8 +41,8 @@ interface IntentResponse { response: Intent; } -interface RulesResponse { - response: Rule[]; +interface RuleResponse { + response: { id: string }; } interface IntentDetailsProps { @@ -80,18 +80,6 @@ const IntentDetails: FC = ({ intentId, setSelectedIntent, li queryKey: [`intents/is-marked-for-service?intent=${intentId}`], }); - const addIntentRule = useCallback( - (rulesResponse: RulesResponse | undefined) => { - if (!rulesResponse) return; - - const intentRule = rulesResponse.response.find((rule: Rule) => rule.id === `rule_${intentId}`); - if (intentRule) { - setIntentRule(intentRule.id); - } - }, - [intentId] - ); - const queryRefresh = useCallback( async (intent?: string) => { const intentsResponse = await queryClient.fetchQuery([ @@ -105,10 +93,10 @@ const IntentDetails: FC = ({ intentId, setSelectedIntent, li ]); setResponse(responseResponse.response); - const rulesResponse = await queryClient.fetchQuery(['rules']); - addIntentRule(rulesResponse); + const rulesResponse = await queryClient.fetchQuery([`rule-by-intent-id?intent=${intentId}`]); + setIntentRule(rulesResponse.response.id); }, - [addIntentRule, intentId, queryClient, setResponse, setSelectedIntent] + [intentId, queryClient, setResponse, setSelectedIntent] ); const { data: responseResponse } = useQuery({ @@ -122,14 +110,13 @@ const IntentDetails: FC = ({ intentId, setSelectedIntent, li const responseText = response?.text ?? ''; const responseName = response?.name ?? ''; - // TODO: need to fetch rules for the selected intent only - const { data: rulesResponse } = useQuery({ - queryKey: ['rules'], + const { data: rulesResponse } = useQuery({ + queryKey: [`rule-by-intent-id?intent=${intentId}`], }); useEffect(() => { - addIntentRule(rulesResponse); - }, [rulesResponse, addIntentRule]); + if (rulesResponse) setIntentRule(rulesResponse.response.id); + }, [rulesResponse]); const markIntentServiceMutation = useMutation({ mutationFn: (data: { name: string; isForService: boolean }) => markForService(data), @@ -462,7 +449,7 @@ const IntentDetails: FC = ({ intentId, setSelectedIntent, li }, onSuccess: async () => { await queryClient.invalidateQueries([`intents/is-marked-for-service?intent=${intentId}`]); - await queryClient.invalidateQueries(['rules']); + await queryClient.invalidateQueries([`rule-by-intent-id?intent=${intentId}`]); toast.open({ type: 'success', title: t('global.notification'),