-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
index.ts
72 lines (63 loc) · 2.51 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { Dispatch, MiddlewareAPI, PayloadAction } from '@reduxjs/toolkit';
import moment from 'moment';
import { DataPublicPluginStart } from '../../../../../../src/plugins/data/public';
import { setState, LensDispatch, LensStoreDeps, navigateAway } from '..';
import { LensAppState } from '../types';
import { getResolvedDateRange, containsDynamicMath } from '../../utils';
import { subscribeToExternalContext } from './subscribe_to_external_context';
export const contextMiddleware = (storeDeps: LensStoreDeps) => (store: MiddlewareAPI) => {
const unsubscribeFromExternalContext = subscribeToExternalContext(
storeDeps.lensServices.data,
store.getState,
store.dispatch
);
return (next: Dispatch) => (action: PayloadAction<Partial<LensAppState>>) => {
if (!action.payload?.searchSessionId) {
updateTimeRange(storeDeps.lensServices.data, store.dispatch);
}
if (navigateAway.match(action)) {
return unsubscribeFromExternalContext();
}
next(action);
};
};
const TIME_LAG_PERCENTAGE_LIMIT = 0.02;
const TIME_LAG_MIN_LIMIT = 10000; // for a small timerange to avoid infinite data refresh timelag minimum is TIME_LAG_ABSOLUTE ms
/**
* checks if TIME_LAG_PERCENTAGE_LIMIT passed to renew searchSessionId
* and request new data.
*/
function updateTimeRange(data: DataPublicPluginStart, dispatch: LensDispatch) {
const timefilter = data.query.timefilter.timefilter;
const unresolvedTimeRange = timefilter.getTime();
if (
!containsDynamicMath(unresolvedTimeRange.from) &&
!containsDynamicMath(unresolvedTimeRange.to)
) {
return;
}
const { min, max } = timefilter.getBounds();
if (!min || !max) {
// bounds not fully specified, bailing out
return;
}
// calculate length of currently configured range in ms
const timeRangeLength = moment.duration(max.diff(min)).asMilliseconds();
// calculate lag of managed "now" for date math
const nowDiff = Date.now() - data.nowProvider.get().valueOf();
// if the lag is significant, start a new session to clear the cache
if (nowDiff > Math.max(timeRangeLength * TIME_LAG_PERCENTAGE_LIMIT, TIME_LAG_MIN_LIMIT)) {
dispatch(
setState({
searchSessionId: data.search.session.start(),
resolvedDateRange: getResolvedDateRange(timefilter),
})
);
}
}