Skip to content

Commit

Permalink
chore(PR-suggestions): made suggested PR changes by
Browse files Browse the repository at this point in the history
Adding context to where the hashing function was found
Renaming the data src to queryCache
Renaming the imported function in TimeSeries
Renaming some of the input parameters to be a little more relevant
  • Loading branch information
asalem1 committed Jun 18, 2020
1 parent 8c612be commit 57c49a0
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 41 deletions.
2 changes: 1 addition & 1 deletion ui/src/dashboards/components/DashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import RateLimitAlert from 'src/cloud/components/RateLimitAlert'
import {pageTitleSuffixer} from 'src/shared/utils/pageTitles'

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

// Types
Expand Down
18 changes: 0 additions & 18 deletions ui/src/data/actions/index.ts

This file was deleted.

33 changes: 33 additions & 0 deletions ui/src/queryCache/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export type Action =
| ReturnType<typeof resetCachedQueryResults>
| ReturnType<typeof setQueryResultsByQueryID>

// Hashing function found here:
// https://jsperf.com/hashcodelordvlad
// Through this thread:
// https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
export const hashCode = (queryText: string): string => {
let hash = 0,
char
if (!queryText) {
return `${hash}`
}
for (let i = 0; i < queryText.length; i++) {
char = queryText.charCodeAt(i)
hash = (hash << 5) - hash + char
hash |= 0 // Convert to 32bit integer
}
return `${hash}`
}

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)
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@
import {produce} from 'immer'

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

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

export const initialState: DataState = {
export const initialState: QueryCacheState = {
queryResultsByQueryID: {},
}

export const dataReducer = (
state: DataState = initialState,
export const queryCacheReducer = (
state: QueryCacheState = initialState,
action: Action
): DataState => {
): QueryCacheState => {
switch (action.type) {
case 'SET_QUERY_RESULTS_BY_QUERY': {
return produce(state, draftState => {
const {queryID, files} = action
if (queryID && files.length) {
draftState.queryResultsByQueryID[queryID] = files
}
draftState.queryResultsByQueryID[queryID] = files
})
}

case 'RESET_CACHED_QUERY_RESULTS': {
return initialState
return {queryResultsByQueryID: {}}
}
}

Expand Down
20 changes: 15 additions & 5 deletions ui/src/shared/components/TimeSeries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
isDemoDataAvailabilityError,
demoDataError,
} from 'src/cloud/utils/demoDataErrors'
import {hashCode} from 'src/queryCache/actions'

// Constants
import {
Expand All @@ -43,7 +44,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'
import {setQueryResultsByQueryID} from 'src/queryCache/actions'

// Types
import {
Expand Down Expand Up @@ -88,7 +89,7 @@ interface OwnProps {

interface DispatchProps {
notify: typeof notifyAction
setQueryResults: typeof setQueryResultsByQueryID
onSetQueryResultsByQueryID: typeof setQueryResultsByQueryID
}

type Props = StateProps & OwnProps & DispatchProps
Expand Down Expand Up @@ -183,7 +184,13 @@ class TimeSeries extends Component<Props & WithRouterProps, State> {
}

private reload = async () => {
const {variables, setQueryResults, notify, check, buckets} = this.props
const {
buckets,
check,
notify,
onSetQueryResultsByQueryID,
variables,
} = this.props
const queries = this.props.queries.filter(({text}) => !!text.trim())

if (!queries.length) {
Expand Down Expand Up @@ -274,7 +281,10 @@ class TimeSeries extends Component<Props & WithRouterProps, State> {
this.pendingReload = false
// set the results per the view
const queryText = queries[0].text
setQueryResults(queryText, files)
const queryID = hashCode(queryText)
if (queryID && files.length) {
onSetQueryResultsByQueryID(queryID, files)
}

this.setState({
giraffeResult,
Expand Down Expand Up @@ -349,7 +359,7 @@ const mstp = (state: AppState, props: OwnProps): StateProps => {

const mdtp: DispatchProps = {
notify: notifyAction,
setQueryResults: setQueryResultsByQueryID,
onSetQueryResultsByQueryID: setQueryResultsByQueryID,
}

export default connect<StateProps, {}, OwnProps>(
Expand Down
4 changes: 2 additions & 2 deletions ui/src/store/configureStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import alertBuilderReducer from 'src/alerting/reducers/alertBuilder'

// Types
import {AppState, LocalStorage} from 'src/types'
import {dataReducer} from 'src/data/reducers'
import {queryCacheReducer} from 'src/queryCache/reducers'

type ReducerState = Pick<AppState, Exclude<keyof AppState, 'timeRange'>>

Expand All @@ -74,7 +74,6 @@ export const rootReducer = combineReducers<ReducerState>({
}),
currentPage: currentPageReducer,
currentDashboard: currentDashboardReducer,
data: dataReducer,
dataLoading: dataLoadingReducer,
me: meReducer,
flags: flagReducer,
Expand All @@ -83,6 +82,7 @@ export const rootReducer = combineReducers<ReducerState>({
overlays: overlaysReducer,
plugins: pluginsResourceReducer,
predicates: predicatesReducer,
queryCache: queryCacheReducer,
ranges: rangesReducer,
resources: combineReducers({
buckets: bucketsReducer,
Expand Down
4 changes: 2 additions & 2 deletions ui/src/types/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {AlertBuilderState} from 'src/alerting/reducers/alertBuilder'
import {CurrentPage} from 'src/shared/reducers/currentPage'
import {DemoDataState} from 'src/cloud/reducers/demodata'
import {OrgSettingsState} from 'src/cloud/reducers/orgsettings'
import {DataState} from 'src/data/reducers'
import {QueryCacheState} from 'src/queryCache/reducers'

export interface AppState {
alertBuilder: AlertBuilderState
Expand All @@ -43,7 +43,7 @@ export interface AppState {
}
currentPage: CurrentPage
currentDashboard: CurrentDashboardState
data: DataState
queryCache: QueryCacheState
dataLoading: DataLoadingState
links: Links
me: MeState
Expand Down
5 changes: 2 additions & 3 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'
import {hashCode} from 'src/queryCache/actions'
import {getActiveTimeMachine} from 'src/timeMachine/selectors'
import {setQueryResults} from 'src/timeMachine/actions/queries'

Expand Down Expand Up @@ -128,15 +128,14 @@ export const setQueryResultsByQueryID = (queryID: string) => (
): Promise<void> => {
try {
const state = getState()
const files = state.data.queryResultsByQueryID[hashCode(queryID)]
const files = state.queryCache.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())
}
}
Expand Down

0 comments on commit 57c49a0

Please sign in to comment.