Skip to content

Commit

Permalink
Extra calc optimized with parallel requests to the model
Browse files Browse the repository at this point in the history
  • Loading branch information
juliarobles committed Sep 19, 2024
1 parent cb55773 commit 6c5dbc8
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 49 deletions.
3 changes: 3 additions & 0 deletions src/components/editors/extraCalcForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ export const ExtraCalcForm: React.FC<Props> = ({ extraCalc, updateFunction, dele
<InlineField label="Maximum iterations" labelWidth={17} grow disabled={disabled}>
<Input {...register("maxIterations")} value={currentCalc.maxIterations} disabled={disabled} onChange={handleOnChangeCalc} required />
</InlineField>
<InlineField label="Parallel requests" labelWidth={17} grow disabled={disabled}>
<Input {...register("numRequests")} value={currentCalc.numRequests} disabled={disabled} onChange={handleOnChangeCalc} required />
</InlineField>
<Collapse label="Dynamic fields" collapsible={false} isOpen={true} className={css({ color: useTheme2().colors.text.primary })}>
{dynamicFieldsList()}
{dynamicFieldsAdd()}
Expand Down
1 change: 0 additions & 1 deletion src/components/step2_ImportData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ export const ImportData: React.FC<Props> = ({ model, collections, addCollection,
}

const handleOnClickAddData = () => {
//console.log("addDATA")
switch (mode.value) {
case ImportDataEnum.EXCEL:
importDataFromCSV()
Expand Down
86 changes: 53 additions & 33 deletions src/utils/datasources/extraCalc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,23 @@ import vm from 'vm'
import { getMean } from "utils/utils"
import { dateTime, DateTime } from "@grafana/data"

const replaceVariables = (text: string, results: IResult[], dyn?: number[]) => {
const lastResult = results[results.length - 1]

const replaceVariables = (text: string, data: IDataPred, dyn?: number[], lastResult?: number) => {
// $out
if (lastResult.result) text = text.replace(/\$out/g, lastResult.result as string)
if (lastResult) text = text.replace(/\$out/g, lastResult.toString())

// $dyn
if (dyn !== undefined)
dyn.forEach((d: number, idx: number) => {
let searchValue = new RegExp('\\$dyn' + (idx + 1), 'g'); // /\$dyn/g
console.log(searchValue)
text = text.replace(searchValue, d.toString())
})


// $[X]
text = text.replace(/\$\[(\w+)\]/g, (match, varName) => {
return (lastResult.data.hasOwnProperty(varName) && lastResult.data[varName] !== undefined) ? lastResult.data[varName][0].toString() : match;
return (data.hasOwnProperty(varName) && data[varName] !== undefined) ? data[varName][0].toString() : match;
});

console.log("text", text)
return text
}

Expand Down Expand Up @@ -67,46 +63,70 @@ const applyFormatToRes = (res: number, format: ExtraCalcFormat, selectedDate?: D
return res.toString()
}

const createRequests = (num: number, calcValue: number, data: IDataPred, tag: string, calc: Calc, idNumber: number) => {
let res: IResult[] = []
for (let i = 0; i < num; i++) {
let mean = getMean(data[tag])
let newValue = applyCalcValue(mean, calcValue, calc)
data = {
...data,
[tag]: getListValuesFromNew(newValue, mean, data[tag])
}
res.push({
id: ("extraCalc_" + (idNumber + i)),
data: { ...data },
correspondsWith: { tag: tag, intervalValue: newValue }
})
}
return res
}

export const extraCalcCollection = async (model: IModel, col: IDataCollection, dyn?: number[]): Promise<IDataCollection> => {
if (model.extraCalc && model.tags.some((tag: ITag) => tag.id === model.extraCalc?.tag)) {
let res: IResult[] = []
const tag = model.extraCalc.tag

// Preparamos los datos iniciales y obtenemos el valor para los calculos
let data: IDataPred = prepareInitialData(col, model.numberOfValues)
const calcValue = executeString(replaceVariables(model.extraCalc.calcValue, data, dyn))

// Creamos el resultado inicial
const iniResult: IResult = {
id: "extraCalc",
data: { ...data },
correspondsWith: { tag: tag, intervalValue: getMean(data[tag]) }
}
let results: IResult[] = await predictResults(model, [iniResult])

let num_iter = 0
let condition = replaceVariables(model.extraCalc.until, results, dyn)
const calcValue = executeString(replaceVariables(model.extraCalc.calcValue, results, dyn))

while (!executeString(condition)) {
num_iter += 1
const mean = getMean(data[tag])
let newValue = applyCalcValue(mean, calcValue, model.extraCalc.calc)
data = {
...data,
[tag]: getListValuesFromNew(newValue, mean, data[tag])

// Generamos el resto de resultados sumando calc de forma recursiva al tag correspondiente y predecimos
let results = [iniResult, ...createRequests((model.extraCalc.numRequests-1), calcValue, data, tag, model.extraCalc.calc, 1)]
results = await predictResults(model, results)

// Comprobamos si se cumple la condicion en alguno
const check = (r: IResult) => {
if(r.result && model.extraCalc && typeof r.result === 'number') {
const condition = replaceVariables(model.extraCalc.until, r.data, dyn, r.result)
return executeString(condition)
}
let newRes: IResult[] = await predictResults(model, [{
id: ("extraCalc_" + num_iter),
data: { ...data },
correspondsWith: {
tag: tag,
intervalValue: newValue
}
}])

results = [...results, ...newRes]
condition = replaceVariables(model.extraCalc.until, results, dyn)
return false
}
col.resultsExtraCalc = results
col.conclusionExtraCalc = applyFormatToRes(num_iter, model.extraCalc.resFormat, col.dateTime, model.extraCalc.resProcess)
let idxFin = results.findIndex(check)

while (idxFin < 0) { // Probamos hasta que se cumpla alguna condicion
data = results[results.length-1].data
res = [...res, ...results]
results = [...createRequests(model.extraCalc.numRequests, calcValue, data, tag, model.extraCalc.calc, res.length)]
results = await predictResults(model, results)
idxFin = results.findIndex(check)
}

res = (idxFin + 1 < results.length) ? [...res, ...results.slice(0, (idxFin+1))] : [...res, ...results]

col.resultsExtraCalc = res
col.conclusionExtraCalc = applyFormatToRes(res.length-1, model.extraCalc.resFormat, col.dateTime, model.extraCalc.resProcess)
} else {
col.conclusionExtraCalc = "ERROR"
}
console.log("Result col extra calc", col)
return col
}

Expand Down
27 changes: 14 additions & 13 deletions src/utils/datasources/predictions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const predictAllCollections = async (model: IModel, allData: IDataCollect
for (const [i, d] of allData.entries()) {
allData[i] = { ...d, results: await predictData(model, d) }
}
console.log("Results", allData)
return allData
}

Expand All @@ -19,15 +20,15 @@ export const predictResults = async (model: IModel, results: IResult[]) => {
for (const [i, r] of results.entries()) {
let finalData = deepCopy(r.data)
if (model.preprocess) {
console.log('Initial data - preprocess', deepCopy(finalData))
//console.log('Initial data - preprocess', deepCopy(finalData))
finalData = await applyPreprocess(model.preprocess, finalData)
console.log('Final data - preprocess', deepCopy(finalData))
//console.log('Final data - preprocess', deepCopy(finalData))
//console.log("preprocess", finalData)
}
if (model.scaler) {
console.log('Initial data - scaler', deepCopy(finalData))
//console.log('Initial data - scaler', deepCopy(finalData))
finalData = await applyScaler(model.scaler, finalData)
console.log('Final data - scaler', deepCopy(finalData))
//console.log('Final data - scaler', deepCopy(finalData))
}
dataToPredict.push(finalData)
results[i] = { ...r, processedData: finalData }
Expand All @@ -37,8 +38,8 @@ export const predictResults = async (model: IModel, results: IResult[]) => {
}

const predictData = async (model: IModel, dataCollection: IDataCollection) => {
console.log("numValues", model.numberOfValues)
console.log("dataCollection", dataCollection)
//console.log("numValues", model.numberOfValues)
//console.log("dataCollection", dataCollection)
let results: IResult[] = prepareData(dataCollection, model.numberOfValues)
return await predictResults(model, results)
}
Expand Down Expand Up @@ -151,7 +152,7 @@ const prepareData = (dataCollection: IDataCollection, numberOfValues?: number):
})
}

console.log(res)
//console.log(res)
return res
}

Expand Down Expand Up @@ -193,7 +194,7 @@ const addFormatInput = (data: IDataPred[], isListValues: boolean, isTransposeLis
}
return dataPred
})
console.log("AUX", aux)
//console.log("AUX", aux)
let body = ""

if (format !== undefined) {
Expand All @@ -210,19 +211,19 @@ const addFormatInput = (data: IDataPred[], isListValues: boolean, isTransposeLis
return eachFormat.replace(varInput, str)
})
body = allInputs.join(",")
console.log(body)
//console.log(body)
allFormat = format.input.slice(0, startIndex) + " $input " + format.input.slice(endIndex + varEachInputEnd.length)
console.log(allFormat)
//console.log(allFormat)
} else {
console.log("No hay each")
//console.log("No hay each")
body = JSON.stringify(aux)
body = body.substring(1, body.length - 1)
}
body = allFormat.replace(varInput, body)
} else {
body = JSON.stringify(aux)
}
console.log(body)
//console.log(body)
return body
}

Expand Down Expand Up @@ -257,7 +258,7 @@ const sendRequest = async (model: IModel, data: IDataPred[]) => {
let response = await fetch(model.url, requestOptions)
if (response.ok) {
let text: string = await response.text()
console.log(text)
//console.log(text)
return removeFormatOutput(text, model.format)
} else {
console.error(await response.text())
Expand Down
3 changes: 2 additions & 1 deletion src/utils/default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const ExtraCalcDefault: IExtraCalc = {
until: "",
resProcess: "$res",
maxIterations: 1000,
resFormat: ExtraCalcFormat.raw
resFormat: ExtraCalcFormat.raw,
numRequests: 10
}

export const ModelDefault: IModel = {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ export interface IExtraCalc {
until: string,
resProcess: string,
maxIterations: number,
resFormat: ExtraCalcFormat
resFormat: ExtraCalcFormat,
numRequests: number
}

export class DateRes {
Expand Down

0 comments on commit 6c5dbc8

Please sign in to comment.