Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

add cache

kruzhambusc90d38aee1ce
diff --git a/src/pages/Project/View/ViewProject.container.jsx b/src/pages/Project/View/ViewProject.container.jsx
index 4a5a8bde7..44b896355 100644
--- a/src/pages/Project/View/ViewProject.container.jsx
+++ b/src/pages/Project/View/ViewProject.container.jsx
@@ -68,6 +68,9 @@ const mapDispatchToProps = (dispatch) => ({
setProjectTab: (tabs) => {
dispatch(UIActions.setProjectTab(tabs))
},
+ setProjectForcastCache: (pid, data, key) => {
+ dispatch(UIActions.setProjectForcastCache(pid, data, key))
+ },
})
export default connect(mapStateToProps, mapDispatchToProps)(ViewProject)
diff --git a/src/pages/Project/View/ViewProject.jsx b/src/pages/Project/View/ViewProject.jsx
index a2ab07520..4bfb97135 100644
--- a/src/pages/Project/View/ViewProject.jsx
+++ b/src/pages/Project/View/ViewProject.jsx
@@ -33,7 +33,7 @@ import Title from 'components/Title'
import EventsRunningOutBanner from 'components/EventsRunningOutBanner'
import {
tbPeriodPairs, getProjectCacheKey, LIVE_VISITORS_UPDATE_INTERVAL, DEFAULT_TIMEZONE, CDN_URL, isDevelopment,
- timeBucketToDays, getProjectCacheCustomKey, roleViewer, MAX_MONTHS_IN_PAST, MAX_MONTHS_IN_PAST_FREE, PROJECT_TABS, TimeFormat,
+ timeBucketToDays, getProjectCacheCustomKey, roleViewer, MAX_MONTHS_IN_PAST, MAX_MONTHS_IN_PAST_FREE, PROJECT_TABS, TimeFormat, getProjectForcastCacheKey,
} from 'redux/constants'
import Button from 'ui/Button'
import Loader from 'ui/Loader'
@@ -69,7 +69,7 @@ const PROJECT_TABS_VALUES = _values(PROJECT_TABS)
const ViewProject = ({
projects, isLoading: _isLoading, showError, cache, cachePerf, setProjectCache, projectViewPrefs, setProjectViewPrefs, setPublicProject,
setLiveStatsForProject, authenticated, timezone, user, sharedProjects, isPaidTierUsed, extensions, generateAlert, setProjectCachePerf,
- projectTab, setProjectTab, setProjects,
+ projectTab, setProjectTab, setProjects, setProjectForcastCache,
}) => {
const { t, i18n: { language } } = useTranslation('common')
const [periodPairs, setPeriodPairs] = useState(tbPeriodPairs(t))
@@ -623,10 +623,19 @@ const ViewProject = ({
const onForecastSubmit = async (periodToForecast) => {
setIsForecastOpened(false)
setDataLoading(true)
+ const key = getProjectForcastCacheKey(period, timeBucket, periodToForecast)
+ const data = cache[id][key]
+
+ if (!_isEmpty(data)) {
+ setForecasedChartData(data)
+ setDataLoading(false)
+ return
+ }
try {
const result = await getChartPrediction(chartData, periodToForecast, timeBucket)
const transformed = transformAIChartData(result)
+ setProjectForcastCache(id, transformed, key)
setForecasedChartData(transformed)
} catch (e) {
console.error(`[onForecastSubmit] Error: ${e}`)
diff --git a/src/redux/actions/ui/index.js b/src/redux/actions/ui/index.js
index 091bf0db4..7ff55e4df 100644
--- a/src/redux/actions/ui/index.js
+++ b/src/redux/actions/ui/index.js
@@ -125,6 +125,13 @@ const setProjectCachePerf = (pid, data, key) => ({
},
})
+const setProjectForcastCache = (pid, data, key) => ({
+ type: types.SET_PROJECT_FORECAST_CACHE,
+ payload: {
+ pid, data, key,
+ },
+})
+
const deleteProjectCache = (pid, period, timeBucket) => ({
type: types.DELETE_PROJECT_CACHE,
payload: {
@@ -256,6 +263,7 @@ const UIActions = {
setProjectAlertsTotal,
setProjectAlertsLoading,
loadProjectAlerts,
+ setProjectForcastCache,
}
export default UIActions
diff --git a/src/redux/actions/ui/types.js b/src/redux/actions/ui/types.js
index 4674fbeff..bca5d82a8 100644
--- a/src/redux/actions/ui/types.js
+++ b/src/redux/actions/ui/types.js
@@ -33,4 +33,5 @@ export const types = {
SET_PROJECT_ALERTS: 'UI_SET_PROJECT_ALERTS',
SET_PROJECT_ALERTS_TOTAL: 'UI_SET_PROJECT_ALERTS_TOTAL',
SET_PROJECT_ALERTS_LOADING: 'UI_SET_PROJECT_ALERTS_LOADING',
+ SET_PROJECT_FORECAST_CACHE: 'UI_SET_PROJECT_FORECAST_CACHE',
}
diff --git a/src/redux/constants/index.js b/src/redux/constants/index.js
index 6f20b310f..180567c6c 100644
--- a/src/redux/constants/index.js
+++ b/src/redux/constants/index.js
@@ -144,6 +144,7 @@ export const GENERAL_STATS_UPDATE_INTERVAL = 60000
// Functions
export const getProjectCacheKey = (period, timeBucket) => `${period}${timeBucket}`
+export const getProjectForcastCacheKey = (period, timeBucket, periodToForecast) => `${period}${timeBucket}${periodToForecast}forecast`
export const getProjectCacheCustomKey = (from, to, timeBucket) => `${from}-${to}-${timeBucket}`
// Cookies
diff --git a/src/redux/reducers/ui/cache.js b/src/redux/reducers/ui/cache.js
index e1b3fa6a2..3598980b8 100644
--- a/src/redux/reducers/ui/cache.js
+++ b/src/redux/reducers/ui/cache.js
@@ -2,7 +2,7 @@ import { types } from 'redux/actions/ui/types'
import _filter from 'lodash/filter'
import _isEmpty from 'lodash/isEmpty'
-import { getProjectCacheKey, LS_VIEW_PREFS_SETTING } from 'redux/constants'
+import { getProjectCacheKey, getProjectForcastCacheKey, LS_VIEW_PREFS_SETTING } from 'redux/constants'
export const getInitialViewPrefs = () => {
if (typeof window !== 'undefined' && window.localStorage) {
@@ -137,6 +137,21 @@ const cacheReducer = (state = getInitialState(), { type, payload }) => {
}
}
+ case types.SET_PROJECT_FORECAST_CACHE: {
+ const { pid, data, key } = payload
+
+ return {
+ ...state,
+ analytics: {
+ ...state.analytics,
+ [pid]: {
+ ...state.analytics[pid],
+ [key]: data,
+ },
+ },
+ }
+ }
+
default:
return state
}