From 00357f9787a37eb58448af8b92c50551d52d2de0 Mon Sep 17 00:00:00 2001 From: dbarrbc <89422790+dbarrbc@users.noreply.github.com> Date: Fri, 20 Sep 2024 14:16:22 -0600 Subject: [PATCH] Update useSendVertexMessage.ts Added code to enable backwards compatability with url examples. c --- .../src/hooks/useSendVertexMessage.ts | 95 ++++++++++++++++++- 1 file changed, 91 insertions(+), 4 deletions(-) diff --git a/explore-assistant-extension/src/hooks/useSendVertexMessage.ts b/explore-assistant-extension/src/hooks/useSendVertexMessage.ts index f81a610b..43333c5a 100644 --- a/explore-assistant-extension/src/hooks/useSendVertexMessage.ts +++ b/explore-assistant-extension/src/hooks/useSendVertexMessage.ts @@ -432,10 +432,97 @@ ${ } const currentDateTime = new Date().toISOString() - let exampleText = '' - if(exploreGenerationExamples && exploreGenerationExamples.length > 0) { - exampleText = exploreGenerationExamples.map((item) => `input: "${item.input}" ; output: ${item.output}`).join('\n') - } +const parseLookerURL = (url: string): { [key: string]: any } => { + // Split URL and extract model & explore + const urlSplit = url.split("?"); + let model = "" + let explore = "" + let queryString = "" + if (urlSplit.length == 2) { + const rootURL = urlSplit[0] + queryString = urlSplit[1] + const rootURLElements = rootURL.split("/"); + model = rootURLElements[rootURLElements.length - 2]; + explore = rootURLElements[rootURLElements.length - 1]; + } + else if (urlSplit.length == 1) { + model = "tbd" + explore = "tbd" + queryString = urlSplit[0] + } + // Initialize lookerEncoding object + const lookerEncoding: { [key: string]: any } = {}; + lookerEncoding['model'] = "" + lookerEncoding['explore'] = "" + lookerEncoding['fields'] = [] + lookerEncoding['pivots'] = [] + lookerEncoding['fill_fields'] = [] + lookerEncoding['filters'] = {} + lookerEncoding['filter_expression'] = null + lookerEncoding['sorts'] = [] + lookerEncoding['limit'] = 500 + lookerEncoding['column_limit'] = 50 + lookerEncoding['total'] = null + lookerEncoding['row_total'] = null + lookerEncoding['subtotals'] = null + lookerEncoding['vis'] = [] + // Split query string and iterate key-value pairs + const keyValuePairs = queryString.split("&"); + for (const qq of keyValuePairs) { + const [key, value] = qq.split('='); + console.log(qq) + lookerEncoding['model'] = model + lookerEncoding['explore'] = explore + switch (key) { + case "fields": + case "pivots": + case "fill_fields": + case "sorts": + lookerEncoding[key] = value.split(","); + break; + case "filter_expression": + case "total": + case "row_total": + case "subtotals": + lookerEncoding[key] = value; + break; + case "limit": + case "column_limit": + lookerEncoding[key] = parseInt(value); + break; + case "vis": + lookerEncoding[key] = JSON.parse(decodeURIComponent(value)); + break; + default: + if (key.startsWith("f[")) { + const filterKey = key.slice(2, -1); + lookerEncoding.filters[filterKey] = value; + } else if (key.includes(".")) { + const path = key.split("."); + let currentObject = lookerEncoding; + for (let i = 0; i < path.length - 1; i++) { + const segment = path[i]; + if (!currentObject[segment]) { + currentObject[segment] = {}; + } + currentObject = currentObject[segment]; + } + currentObject[path[path.length - 1]] = value; + } + } + } + return lookerEncoding; + }; + + + + + + +let exampleText = '' +if(exploreGenerationExamples && exploreGenerationExamples.length > 0) { + exampleText = exploreGenerationExamples.map((item) => `input: "${item.input}" ; output: ${JSON.stringify(parseLookerURL(item.output))}`).join('\n') +} const contents = ` # Context