Skip to content

Commit

Permalink
Merge branch 'feat/conditionally-query-cell' of github.com:influxdata…
Browse files Browse the repository at this point in the history
…/influxdb into feat/conditionally-query-cell
  • Loading branch information
asalem1 committed Jun 22, 2020
2 parents db370b9 + 101f2e9 commit 638733e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 56 deletions.
10 changes: 5 additions & 5 deletions ui/src/dashboards/components/EditVEO.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import VEOHeader from 'src/dashboards/components/VEOHeader'
// Actions
import {setName} from 'src/timeMachine/actions'
import {saveVEOView} from 'src/dashboards/actions/thunks'
import {setQueryResultsForCell} from 'src/views/actions/thunks'
import {getViewAndResultsForVEO} from 'src/views/actions/thunks'

// Utils
import {getActiveTimeMachine} from 'src/timeMachine/selectors'
Expand All @@ -21,9 +21,9 @@ import {getActiveTimeMachine} from 'src/timeMachine/selectors'
import {AppState, RemoteDataState, QueryView, TimeMachineID} from 'src/types'

interface DispatchProps {
getViewAndResultsForVEO: typeof getViewAndResultsForVEO
onSetName: typeof setName
onSaveView: typeof saveVEOView
setQueryResultsForCell: typeof setQueryResultsForCell
}

interface StateProps {
Expand All @@ -35,18 +35,18 @@ type Props = DispatchProps & StateProps & WithRouterProps

const EditViewVEO: FunctionComponent<Props> = ({
activeTimeMachineID,
getViewAndResultsForVEO,
onSaveView,
onSetName,
params: {orgID, cellID, dashboardID},
router,
setQueryResultsForCell,
view,
}) => {
useEffect(() => {
// TODO split this up into "loadView" "setActiveTimeMachine"
// and something to tell the component to pull from the context
// of the dashboardID
setQueryResultsForCell(dashboardID, cellID, 'veo')
getViewAndResultsForVEO(dashboardID, cellID, 'veo')
}, [])

const handleClose = () => {
Expand Down Expand Up @@ -98,9 +98,9 @@ const mstp = (state: AppState): StateProps => {
}

const mdtp: DispatchProps = {
getViewAndResultsForVEO: getViewAndResultsForVEO,
onSetName: setName,
onSaveView: saveVEOView,
setQueryResultsForCell: setQueryResultsForCell,
}

export default connect<StateProps, DispatchProps, {}>(
Expand Down
6 changes: 6 additions & 0 deletions ui/src/shared/components/TimeSeries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ class TimeSeries extends Component<Props & WithRouterProps, State> {
onSetQueryResultsByQueryID(queryID, files)
}

const queryText = queries.map(({text}) => text).join('')
const queryID = hashCode(queryText)
if (queryID && files.length) {
onSetQueryResultsByQueryID(queryID, files)
}

this.setState({
giraffeResult,
errorMessage,
Expand Down
62 changes: 11 additions & 51 deletions ui/src/views/actions/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const updateViewAndVariables = (
}
}

export const getViewForTimeMachine = (
export const getViewAndResultsForVEO = (
dashboardID: string,
cellID: string,
timeMachineID: TimeMachineID
Expand All @@ -115,61 +115,21 @@ export const getViewForTimeMachine = (
view,
})
)
} catch (error) {
console.error(error)
dispatch(notify(copy.getViewFailed(error.message)))
dispatch(setView(cellID, RemoteDataState.Error))
}
}

export const setQueryResultsByQueryID = (queryID: string) => (
dispatch,
getState: GetState
): Promise<void> => {
try {
const state = getState()
const files = state.queryCache.queryResultsByQueryID[queryID]
if (files) {
dispatch(setQueryResults(RemoteDataState.Done, files, null, null))
return
}
dispatch(executeQueries())
} catch (error) {
// if the files don't exist in the cache, we want to execute the query
dispatch(executeQueries())
}
}

export const setQueryResultsForCell = (
dashboardID: string,
cellID: string,
timeMachineID: TimeMachineID
) => async (dispatch, getState: GetState): Promise<void> => {
try {
const state = getState()

let view = getByID<View>(state, ResourceType.Views, cellID) as QueryView

if (!view) {
dispatch(setView(cellID, RemoteDataState.Loading))
view = (await getViewAJAX(dashboardID, cellID)) as QueryView
}

dispatch(
setActiveTimeMachine(timeMachineID, {
contextID: dashboardID,
view,
})
)
const {view} = getActiveTimeMachine(state)
const queries = view.properties.queries.filter(({text}) => !!text.trim())
if (!queries.length) {
dispatch(setQueryResults(RemoteDataState.Done, [], null))
}
const queryText = queries.map(({text}) => text).join('')
const queryID = hashCode(queryText)
if (queryID) {
dispatch(setQueryResultsByQueryID(queryID))
const files = get(
state,
['queryCache', 'queryResultsByQueryID', queryID],
undefined
)
if (files) {
dispatch(setQueryResults(RemoteDataState.Done, files, null, null))
return
}
dispatch(executeQueries())
} catch (error) {
console.error(error)
dispatch(notify(copy.getViewFailed(error.message)))
Expand Down

0 comments on commit 638733e

Please sign in to comment.