Skip to content

Commit

Permalink
feat(conditionally-query-cell): Added cache clearing when the compone…
Browse files Browse the repository at this point in the history
…nt unmounts. Also separate thunk concerns to default query execution when files are undefined
  • Loading branch information
asalem1 committed Jun 17, 2020
1 parent a818276 commit cb0a5ae
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 57 deletions.
21 changes: 17 additions & 4 deletions ui/src/dashboards/components/DashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ import RateLimitAlert from 'src/cloud/components/RateLimitAlert'
// Utils
import {pageTitleSuffixer} from 'src/shared/utils/pageTitles'

// Selectors
// Selectors & Actions
import {resetCachedQueryResults} from 'src/data/actions'
import {getByID} from 'src/resources/selectors'

// Types
import {AppState, AutoRefresh, ResourceType, Dashboard} from 'src/types'
import {ManualRefreshProps} from 'src/shared/components/ManualRefresh'

interface DispatchProps {
resetCachedQueryResults: typeof resetCachedQueryResults
}

interface StateProps {
dashboard: Dashboard
}
Expand All @@ -31,10 +36,14 @@ interface OwnProps {
autoRefresh: AutoRefresh
}

type Props = OwnProps & StateProps & ManualRefreshProps
type Props = OwnProps & StateProps & ManualRefreshProps & DispatchProps

@ErrorHandling
class DashboardPage extends Component<Props> {
public componentWillUnmount() {
this.props.resetCachedQueryResults()
}

public render() {
const {autoRefresh, manualRefresh, onManualRefresh, children} = this.props

Expand Down Expand Up @@ -76,7 +85,11 @@ const mstp = (state: AppState): StateProps => {
}
}

export default connect<StateProps, {}>(
const mdtp = {
resetCachedQueryResults: resetCachedQueryResults,
}

export default connect<StateProps, DispatchProps>(
mstp,
null
mdtp
)(ManualRefresh<OwnProps>(DashboardPage))
18 changes: 18 additions & 0 deletions ui/src/data/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type Action =
| ReturnType<typeof resetCachedQueryResults>
| ReturnType<typeof setQueryResultsByQueryID>

export const hashCode = s =>
s.split('').reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0)

export const setQueryResultsByQueryID = (queryID: string, files: string[]) =>
({
type: 'SET_QUERY_RESULTS_BY_QUERY',
queryID: hashCode(queryID),
files,
} as const)

export const resetCachedQueryResults = () =>
({
type: 'RESET_CACHED_QUERY_RESULTS',
} as const)
35 changes: 0 additions & 35 deletions ui/src/data/actions/thunks.ts

This file was deleted.

18 changes: 7 additions & 11 deletions ui/src/data/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
import {produce} from 'immer'

// Actions
import {Action} from 'src/data/actions/thunks'

interface QueryResultsState {
files: string[] | null
timeInterval: string
}
import {Action} from 'src/data/actions'

export interface DataState {
queryResultsByQueryID: {[queryID: string]: QueryResultsState}
queryResultsByQueryID: {[queryID: string]: string[]}
}

export const initialState: DataState = {
Expand All @@ -26,13 +21,14 @@ export const dataReducer = (
return produce(state, draftState => {
const {queryID, files} = action
if (queryID && files.length) {
draftState.queryResultsByQueryID[queryID] = {
files,
timeInterval: 'now',
}
draftState.queryResultsByQueryID[queryID] = files
}
})
}

case 'RESET_CACHED_QUERY_RESULTS': {
return initialState
}
}

return state
Expand Down
2 changes: 1 addition & 1 deletion ui/src/shared/components/TimeSeries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {TIME_RANGE_START, TIME_RANGE_STOP} from 'src/variables/constants'

// Actions
import {notify as notifyAction} from 'src/shared/actions/notifications'
import {setQueryResultsByQueryID} from 'src/data/actions/thunks'
import {setQueryResultsByQueryID} from 'src/data/actions'

// Types
import {
Expand Down
27 changes: 21 additions & 6 deletions ui/src/views/actions/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {notify} from 'src/shared/actions/notifications'
import {setActiveTimeMachine} from 'src/timeMachine/actions'
import {executeQueries} from 'src/timeMachine/actions/queries'
import {setView, Action} from 'src/views/actions/creators'
import {hashCode} from 'src/data/actions/thunks'
import {hashCode} from 'src/data/actions'
import {getActiveTimeMachine} from 'src/timeMachine/selectors'
import {setQueryResults} from 'src/timeMachine/actions/queries'

Expand Down Expand Up @@ -122,6 +122,25 @@ export const getViewForTimeMachine = (
}
}

export const setQueryResultsByQueryID = (queryID: string) => async (
dispatch,
getState: GetState
): Promise<void> => {
try {
const state = getState()
const files = state.data.queryResultsByQueryID[hashCode(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
console.error(error)
dispatch(executeQueries())
}
}

export const setQueryResultsForCell = (
dashboardID: string,
cellID: string,
Expand All @@ -134,11 +153,7 @@ export const setQueryResultsForCell = (
const queries = view.properties.queries.filter(({text}) => !!text.trim())
const queryID = get(queries, '[0].text', '')
if (queryID) {
const {files, timeInterval} = state.data.queryResultsByQueryID[
hashCode(queryID)
]
console.log('timeInterval: ', timeInterval)
dispatch(setQueryResults(RemoteDataState.Done, files, null, null))
dispatch(setQueryResultsByQueryID(queryID))
return
}
dispatch(executeQueries())
Expand Down

0 comments on commit cb0a5ae

Please sign in to comment.