From b0f9bc9438987b5cd3115829dc713e4a5054d5e4 Mon Sep 17 00:00:00 2001 From: Julia Robles <48439828+juliarobles@users.noreply.github.com> Date: Fri, 13 Dec 2024 13:17:53 +0100 Subject: [PATCH] Now you can use only Date and not necessarily DateTime to import the data --- src/components/editors/modelForm.tsx | 32 +++++++++++++++++----------- src/components/step2_ImportData.tsx | 30 +++++++++++++++----------- src/components/step3_ModifyData.tsx | 6 +++--- src/utils/datasources/grafana.tsx | 1 - src/utils/default.tsx | 2 ++ src/utils/types.tsx | 2 ++ 6 files changed, 44 insertions(+), 29 deletions(-) diff --git a/src/components/editors/modelForm.tsx b/src/components/editors/modelForm.tsx index d947d60..94fb4b7 100644 --- a/src/components/editors/modelForm.tsx +++ b/src/components/editors/modelForm.tsx @@ -43,8 +43,6 @@ export const ModelForm: React.FC = ({ model, updateFunction, deleteFuncti const [code, setCode] = useState("") const [disabled, setDisabled] = useState(false) const [scaler, setScaler] = useState("") - const [listValues, setListValues] = useState(false) - const [transposeList, setTransposeList] = useState(false) const updateCurrentState = () => { setCurrentModel(model) @@ -61,8 +59,6 @@ export const ModelForm: React.FC = ({ model, updateFunction, deleteFuncti setSelectedQuotesListItems({ label: model.formatTags, value: model.formatTags }) setCode((model.preprocess) ? model.preprocess : "") setScaler((model.scaler) ? JSON.stringify(model.scaler, undefined, 4) : "") - setListValues(model.isListValues) - setTransposeList(model.isTransposeList) } @@ -73,6 +69,14 @@ export const ModelForm: React.FC = ({ model, updateFunction, deleteFuncti }) } + const handleOnChangeModelCheckBox = (key: string, ) => { + let k = key as keyof IModel + setCurrentModel({ + ...currentModel, + [k]: !currentModel[k] + }) + } + const handleOnChangeCredentials = (event: ChangeEvent) => { const oldCredentials: ICredentials = (currentModel.credentials) ? { ...currentModel.credentials } : { username: "", password: "" } setCurrentModel({ @@ -101,9 +105,7 @@ export const ModelForm: React.FC = ({ model, updateFunction, deleteFuncti format: selectedFormat?.value, extraCalc: selectedExtraCalc?.value, preprocess: code, - credentials: credentials, - isListValues: listValues, - isTransposeList: transposeList + credentials: credentials } newModel.scaler = (scaler.trim() !== "") ? JSON.parse(scaler) : undefined //console.log(newModel) @@ -308,6 +310,9 @@ export const ModelForm: React.FC = ({ model, updateFunction, deleteFuncti name="query" /> + + handleOnChangeModelCheckBox('onlyDate')} /> + @@ -315,13 +320,13 @@ export const ModelForm: React.FC = ({ model, updateFunction, deleteFuncti - setListValues(!listValues)} /> + handleOnChangeModelCheckBox('isListValues')} /> - - setTransposeList(!transposeList)} /> + + handleOnChangeModelCheckBox('isTransposeList')} /> - - + +

Extra info query

@@ -380,6 +385,9 @@ export const ModelForm: React.FC = ({ model, updateFunction, deleteFuncti name="varTimeRange" /> + + handleOnChangeModelCheckBox('onlyDateRange')} /> + diff --git a/src/components/step2_ImportData.tsx b/src/components/step2_ImportData.tsx index 45e9a4e..3eefadf 100644 --- a/src/components/step2_ImportData.tsx +++ b/src/components/step2_ImportData.tsx @@ -1,9 +1,9 @@ import { Button, DatePickerWithInput, InlineLabel, Select, TimeOfDayPicker, useTheme2, VerticalGroup } from '@grafana/ui' import React, { FormEvent, useContext, useEffect, useState } from 'react' -import { Context, dateTimeLocalToString, dateTimeToString, dateTimeToTimestamp, disabledByJS } from 'utils/utils' +import { Context, dateTimeLocalToString, dateTimeToString, dateTimeToTimestamp, dateToString, disabledByJS } from 'utils/utils' import { IData, IDataCollection, IInterval, IModel } from 'utils/types' import { getArrayOfData, getExtraInfo, saveVariableValue } from 'utils/datasources/grafana' -import { AppEvents, dateTime, DateTime, LoadingState, PanelData, SelectableValue } from '@grafana/data' +import { AppEvents, DataQueryError, dateTime, DateTime, LoadingState, PanelData, SelectableValue } from '@grafana/data' import Papa, { ParseError } from 'papaparse' import { DefaultImportData, ImportDataEnum, ImportDataOptions, Steps, VariablesGrafanaOptions } from 'utils/constants' import { IntervalDefault, ModelDefault } from 'utils/default' @@ -68,15 +68,16 @@ export const ImportData: React.FC = ({ model, collections, addCollection, const importDataFromDateTime = (dt?: DateTime) => { if (dt !== undefined && model !== undefined) { setHasToSaveNewData(dt) - saveVariableValue(locationService, model.varTime, dateTimeToString(dt)) + let dateStr = (model.onlyDate) ? dateToString(dt.toDate()) : dateTimeToString(dt) + saveVariableValue(locationService, model.varTime, dateStr) } } const importDataFromDateTimeRange = (start: DateTime, stop: DateTime) => { if (model !== undefined && model.varTimeStart !== undefined) { - saveVariableValue(locationService, model.varTimeStart, dateTimeToString(start)) + saveVariableValue(locationService, model.varTimeStart, ((model.onlyDateRange) ? dateToString(start.toDate()) : dateTimeToString(start))) setHasToSaveNewData(stop) - saveVariableValue(locationService, model.varTime, dateTimeToString(stop)) + saveVariableValue(locationService, model.varTime, ((model.onlyDateRange) ? dateToString(stop.toDate()) : dateTimeToString(stop))) } } @@ -208,19 +209,19 @@ export const ImportData: React.FC = ({ model, collections, addCollection, && (hasToSaveNewData === dateTimeInput || (selectedGrafanaVariable && hasToSaveNewData === selectedGrafanaVariable.value)) && model !== undefined && (data.state === LoadingState.Done || data.state === LoadingState.Error)) { - if (data.state === LoadingState.Done) { console.log("Done") console.log("Data", data) let extraInfo = undefined let arrayData: IData[] = [] - let name = dateTimeLocalToString(hasToSaveNewData) + let name = ((model.onlyDate && mode.value === ImportDataEnum.DATETIME_SET) + || (model.onlyDateRange && mode.value === ImportDataEnum.DATETIME_RANGE)) ? dateToString(hasToSaveNewData.toDate()) : dateTimeLocalToString(hasToSaveNewData) let text = "DateTime" let key = dateTimeToTimestamp(hasToSaveNewData).toString() let dtStart: DateTime | undefined = undefined if(mode.value === ImportDataEnum.DATETIME_RANGE && model.queryRangeId) { arrayData = getArrayOfData(data, model.queryRangeId, fieldTag, model.isListValues, model.numberOfValues) - name = dateTimeLocalToString(dateTimeInputStart) + " to " + name + name = ((model.onlyDateRange) ? dateToString(dateTimeInputStart.toDate()) : dateTimeLocalToString(dateTimeInputStart)) + " to " + name text = text + " range" dtStart = dateTimeInputStart key = dateTimeToTimestamp(dateTimeInputStart).toString() + "+" + key @@ -239,13 +240,16 @@ export const ImportData: React.FC = ({ model, collections, addCollection, }) } } else { + console.log("ERROR") + console.log("Data", data) const appEvents = getAppEvents(); + const msgsError = data.errors?.map((v: DataQueryError) => v.message) appEvents.publish({ type: AppEvents.alertError.name, - payload: [data.error] + payload: msgsError }) } - saveVariableValue(locationService, model.varTime, dateTimeToString(dateTime())) + saveVariableValue(locationService, model.varTime, dateToString(new Date())) setHasToSaveNewData(undefined) } }, [data]) @@ -272,7 +276,7 @@ export const ImportData: React.FC = ({ model, collections, addCollection, className='fullWidth' closeOnSelect /> -
+