@@ -3,6 +3,7 @@ import '../../services/api';
3
3
import _ from 'lodash' ;
4
4
import { createSelector } from 'reselect' ;
5
5
import { calcUptime } from '../../utils' ;
6
+ import { getUsage } from '../../containers/Storage/utils' ;
6
7
7
8
export const VisibleEntities = {
8
9
All : 'All' ,
@@ -24,13 +25,15 @@ export const StorageTypes = {
24
25
const FETCH_STORAGE = createRequestActionTypes ( 'storage' , 'FETCH_STORAGE' ) ;
25
26
const SET_INITIAL = 'storage/SET_INITIAL' ;
26
27
const SET_FILTER = 'storage/SET_FILTER' ;
28
+ const SET_USAGE_FILTER = 'storage/SET_USAGE_FILTER' ;
27
29
const SET_VISIBLE_GROUPS = 'storage/SET_VISIBLE_GROUPS' ;
28
30
const SET_STORAGE_TYPE = 'storage/SET_STORAGE_TYPE' ;
29
31
30
32
const initialState = {
31
33
loading : true ,
32
34
wasLoaded : false ,
33
35
filter : '' ,
36
+ usageFilter : [ ] ,
34
37
visible : VisibleEntities . Missing ,
35
38
type : StorageTypes . groups ,
36
39
} ;
@@ -70,6 +73,12 @@ const storage = function z(state = initialState, action) {
70
73
filter : action . data ,
71
74
} ;
72
75
}
76
+ case SET_USAGE_FILTER : {
77
+ return {
78
+ ...state ,
79
+ usageFilter : action . data ,
80
+ } ;
81
+ }
73
82
case SET_VISIBLE_GROUPS : {
74
83
return {
75
84
...state ,
@@ -82,6 +91,8 @@ const storage = function z(state = initialState, action) {
82
91
return {
83
92
...state ,
84
93
type : action . data ,
94
+ filter : '' ,
95
+ usageFilter : [ ] ,
85
96
wasLoaded : false ,
86
97
error : undefined ,
87
98
} ;
@@ -117,6 +128,14 @@ export function setStorageFilter(value) {
117
128
data : value ,
118
129
} ;
119
130
}
131
+
132
+ export function setUsageFilter ( value ) {
133
+ return {
134
+ type : SET_USAGE_FILTER ,
135
+ data : value ,
136
+ } ;
137
+ }
138
+
120
139
export function setVisibleEntities ( value ) {
121
140
return {
122
141
type : SET_VISIBLE_GROUPS ,
@@ -135,6 +154,7 @@ export const getStorageNodesCount = (state) => ({
135
154
found : state . storage . data ?. FoundNodes || 0 ,
136
155
} ) ;
137
156
export const getStorageFilter = ( state ) => state . storage . filter ;
157
+ export const getUsageFilter = ( state ) => state . storage . usageFilter ;
138
158
export const getVisibleEntities = ( state ) => state . storage . visible ;
139
159
export const getStorageType = ( state ) => state . storage . type ;
140
160
export const getNodesObject = ( state ) =>
@@ -271,26 +291,67 @@ export const getVisibleEntitiesList = createSelector(
271
291
} ,
272
292
) ;
273
293
274
- export const getFilteredEntities = createSelector (
275
- [ getStorageFilter , getStorageType , getVisibleEntitiesList ] ,
276
- ( filter , type , entities ) => {
277
- const cleanedFilter = filter . trim ( ) . toLowerCase ( ) ;
278
- if ( ! cleanedFilter ) {
279
- return entities ;
280
- } else {
281
- return _ . filter ( entities , ( e ) => {
282
- if ( type === StorageTypes . groups ) {
283
- return (
284
- e . PoolName . toLowerCase ( ) . includes ( cleanedFilter ) ||
285
- e . GroupID ?. toString ( ) . includes ( cleanedFilter )
286
- ) ;
287
- }
288
- return (
289
- e . NodeId . toString ( ) . includes ( cleanedFilter ) ||
290
- e . FQDN . toLowerCase ( ) . includes ( cleanedFilter )
291
- ) ;
292
- } ) ;
294
+ const filterByText = ( entities , type , text ) => {
295
+ const cleanedFilter = text . trim ( ) . toLowerCase ( ) ;
296
+
297
+ if ( ! cleanedFilter ) {
298
+ return entities ;
299
+ }
300
+
301
+ return entities . filter ( ( entity ) => {
302
+ if ( type === StorageTypes . groups ) {
303
+ return (
304
+ entity . PoolName . toLowerCase ( ) . includes ( cleanedFilter ) ||
305
+ entity . GroupID ?. toString ( ) . includes ( cleanedFilter )
306
+ ) ;
293
307
}
308
+
309
+ return (
310
+ entity . NodeId . toString ( ) . includes ( cleanedFilter ) ||
311
+ entity . FQDN . toLowerCase ( ) . includes ( cleanedFilter )
312
+ ) ;
313
+ } ) ;
314
+ } ;
315
+
316
+ const filterByUsage = ( entities , usage ) => {
317
+ if ( ! Array . isArray ( usage ) || usage . length === 0 ) {
318
+ return entities ;
319
+ }
320
+
321
+ return entities . filter ( ( entity ) => {
322
+ const entityUsage = getUsage ( entity , 5 ) ;
323
+ return usage . some ( ( val ) => Number ( val ) <= entityUsage && entityUsage < Number ( val ) + 5 ) ;
324
+ } ) ;
325
+ } ;
326
+
327
+ export const getFilteredEntities = createSelector (
328
+ [ getStorageFilter , getUsageFilter , getStorageType , getVisibleEntitiesList ] ,
329
+ ( textFilter , usageFilter , type , entities ) => {
330
+ let result = entities ;
331
+ result = filterByText ( result , type , textFilter ) ;
332
+ result = filterByUsage ( result , usageFilter ) ;
333
+ return result ;
334
+ } ,
335
+ ) ;
336
+
337
+ export const getUsageFilterOptions = createSelector (
338
+ getVisibleEntitiesList ,
339
+ ( entities ) => {
340
+ const items = { } ;
341
+
342
+ entities . forEach ( ( entity ) => {
343
+ const usage = getUsage ( entity , 5 ) ;
344
+
345
+ if ( ! Object . hasOwn ( items , usage ) ) {
346
+ items [ usage ] = 0 ;
347
+ }
348
+
349
+ items [ usage ] += 1 ;
350
+ } ) ;
351
+
352
+ return Object . entries ( items )
353
+ . map ( ( [ threshold , count ] ) => ( { threshold, count} ) )
354
+ . sort ( ( a , b ) => b . threshold - a . threshold ) ;
294
355
} ,
295
356
) ;
296
357
0 commit comments