@@ -7,12 +7,14 @@ import timeRangeUtils from '@/utils/timeRangeUtils';
7
7
import moment from 'moment-timezone' ;
8
8
import { appStoreReducers , TimeRange , useAppStore } from '@/layouts/MainLayout/providers/AppProvider' ;
9
9
import { correlationStoreReducers , useCorrelationStore } from '../providers/CorrelationProvider' ;
10
+ import { getOffset } from '@/utils' ;
11
+ import { LOG_QUERY_LIMITS } from '@/pages/Stream/providers/LogsProvider' ;
10
12
11
13
const { getRelativeStartAndEndDate, formatDateWithTimezone, getLocalTimezone } = timeRangeUtils ;
12
- const { setCorrelationId } = correlationStoreReducers ;
14
+ const { setCorrelationId, setTargetPage , setCurrentOffset , setPerPage } = correlationStoreReducers ;
13
15
const { setTimeRange } = appStoreReducers ;
14
16
const timeRangeFormat = 'DD-MMM-YYYY_HH-mmz' ;
15
- const keys = [ 'id' , 'interval' , 'from' , 'to' ] ;
17
+ const keys = [ 'id' , 'interval' , 'from' , 'to' , 'page' , 'rows' ] ;
16
18
17
19
const dateToParamString = ( date : Date ) => {
18
20
return formatDateWithTimezone ( date , timeRangeFormat ) ;
@@ -48,10 +50,17 @@ const deriveTimeRangeParams = (timerange: TimeRange): { interval: string } | { f
48
50
}
49
51
} ;
50
52
51
- const storeToParamsObj = ( opts : { correlationId : string ; timeRange : TimeRange } ) : Record < string , string > => {
52
- const { correlationId, timeRange } = opts ;
53
+ const storeToParamsObj = ( opts : {
54
+ correlationId : string ;
55
+ timeRange : TimeRange ;
56
+ rows : string ;
57
+ page : string ;
58
+ } ) : Record < string , string > => {
59
+ const { correlationId, timeRange, page, rows } = opts ;
53
60
const params : Record < string , string > = {
54
61
id : correlationId ,
62
+ rows,
63
+ page,
55
64
...deriveTimeRangeParams ( timeRange ) ,
56
65
} ;
57
66
return _ . pickBy ( params , ( val , key ) => ! _ . isEmpty ( val ) && _ . includes ( keys , key ) ) ;
@@ -71,39 +80,87 @@ const paramsStringToParamsObj = (searchParams: URLSearchParams): Record<string,
71
80
const useParamsController = ( ) => {
72
81
const [ isStoreSynced , setStoreSynced ] = useState ( false ) ;
73
82
const [ { correlationId } , setCorrelationStore ] = useCorrelationStore ( ( store ) => store ) ;
83
+ const [ tableOpts ] = useCorrelationStore ( ( store ) => store . tableOpts ) ;
74
84
const [ timeRange , setAppStore ] = useAppStore ( ( store ) => store . timeRange ) ;
75
85
const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
76
86
87
+ const { currentOffset, currentPage, targetPage, perPage } = tableOpts ;
88
+
89
+ const pageOffset = Math . ceil ( currentOffset / perPage ) ;
90
+
77
91
useEffect ( ( ) => {
78
- const storeAsParams = storeToParamsObj ( { correlationId, timeRange } ) ;
92
+ const storeAsParams = storeToParamsObj ( {
93
+ correlationId,
94
+ timeRange,
95
+ rows : `${ perPage } ` ,
96
+ page : `${ targetPage ? targetPage : Math . ceil ( currentPage + pageOffset ) } ` ,
97
+ } ) ;
79
98
const presentParams = paramsStringToParamsObj ( searchParams ) ;
80
99
if ( storeAsParams . id !== presentParams . id ) {
81
100
setCorrelationStore ( ( store ) => setCorrelationId ( store , presentParams . id ) ) ;
82
101
}
102
+ if ( storeAsParams . rows !== presentParams . rows && LOG_QUERY_LIMITS . includes ( _ . toNumber ( presentParams . rows ) ) ) {
103
+ setCorrelationStore ( ( store ) => setPerPage ( store , _ . toNumber ( presentParams . rows ) ) ) ;
104
+ }
105
+ if ( storeAsParams . page !== presentParams . page && ! _ . isEmpty ( presentParams . page ) ) {
106
+ setCorrelationStore ( ( store ) => setTargetPage ( store , _ . toNumber ( presentParams . page ) ) ) ;
107
+ const offset = getOffset ( _ . toNumber ( presentParams . page ) , _ . toNumber ( presentParams . rows ) ) ;
108
+
109
+ if ( offset > 0 ) {
110
+ setCorrelationStore ( ( store ) => setCurrentOffset ( store , offset ) ) ;
111
+
112
+ setCorrelationStore ( ( store ) =>
113
+ setTargetPage (
114
+ store ,
115
+ Math . abs ( _ . toNumber ( presentParams . page ) - Math . ceil ( offset / _ . toNumber ( presentParams . rows ) ) ) ,
116
+ ) ,
117
+ ) ;
118
+ }
119
+ }
83
120
syncTimeRangeToStore ( storeAsParams , presentParams ) ;
84
121
setStoreSynced ( true ) ;
85
122
} , [ ] ) ;
86
123
87
124
useEffect ( ( ) => {
88
125
if ( isStoreSynced ) {
89
- const storeAsParams = storeToParamsObj ( { correlationId, timeRange } ) ;
126
+ const storeAsParams = storeToParamsObj ( {
127
+ correlationId,
128
+ timeRange,
129
+ rows : `${ perPage } ` ,
130
+ page : `${ targetPage ? targetPage : Math . ceil ( currentPage + pageOffset ) } ` ,
131
+ } ) ;
90
132
const presentParams = paramsStringToParamsObj ( searchParams ) ;
91
133
if ( _ . isEqual ( storeAsParams , presentParams ) ) return ;
92
134
93
135
setSearchParams ( storeAsParams ) ;
94
136
}
95
- } , [ correlationId , isStoreSynced , timeRange . startTime . toISOString ( ) , timeRange . endTime . toISOString ( ) ] ) ;
137
+ } , [
138
+ correlationId ,
139
+ isStoreSynced ,
140
+ timeRange . startTime . toISOString ( ) ,
141
+ timeRange . endTime . toISOString ( ) ,
142
+ targetPage ,
143
+ tableOpts ,
144
+ ] ) ;
96
145
97
146
useEffect ( ( ) => {
98
147
if ( ! isStoreSynced ) return ;
99
148
100
- const storeAsParams = storeToParamsObj ( { correlationId, timeRange } ) ;
149
+ const storeAsParams = storeToParamsObj ( {
150
+ correlationId,
151
+ timeRange,
152
+ rows : `${ perPage } ` ,
153
+ page : `${ targetPage ? targetPage : Math . ceil ( currentPage + pageOffset ) } ` ,
154
+ } ) ;
101
155
const presentParams = paramsStringToParamsObj ( searchParams ) ;
102
156
if ( _ . isEqual ( storeAsParams , presentParams ) ) return ;
103
157
104
158
if ( storeAsParams . id !== presentParams . id ) {
105
159
setCorrelationStore ( ( store ) => setCorrelationId ( store , presentParams . id ) ) ;
106
160
}
161
+ if ( storeAsParams . rows !== presentParams . rows && LOG_QUERY_LIMITS . includes ( _ . toNumber ( presentParams . rows ) ) ) {
162
+ setCorrelationStore ( ( store ) => setPerPage ( store , _ . toNumber ( presentParams . rows ) ) ) ;
163
+ }
107
164
108
165
syncTimeRangeToStore ( storeAsParams , presentParams ) ;
109
166
} , [ searchParams ] ) ;
0 commit comments