@@ -8,11 +8,30 @@ import {
88}  from  '@elastic-suite/gally-admin-shared' 
99
1010import  {  IActiveFilters  }  from  '../types' 
11+ import  {  add ,  format ,  parse  }  from  'date-fns' 
1112
13+ type  DurationKey  =  'Y'  |  'M'  |  'W'  |  'D'  |  'H'  |  'm'  |  'S' 
14+ 
15+ const  durationFormat : Record < DurationKey ,  string >  =  { 
16+   Y : 'years' , 
17+   M : 'months' , 
18+   W : 'weeks' , 
19+   D : 'days' , 
20+   H : 'hours' , 
21+   m : 'minutes' , 
22+   S : 'seconds' , 
23+ } 
24+ 
25+ /* eslint-disable no-underscore-dangle */ 
1226export  function  getProductFilters ( 
1327  activeFilters : IActiveFilters 
14- ) : IProductFieldFilterInput  { 
15-   return  activeFilters . reduce < IProductFieldFilterInput > ( ( acc ,  activeFilter )  =>  { 
28+ ) : IProductFieldFilterInput [ ]  { 
29+   const  data : IProductFieldFilterInput [ ]  =  [ ] 
30+   activeFilters . forEach ( ( activeFilter )  =>  { 
31+     const  precAcc  =  data . find ( 
32+       ( value )  =>  Object . keys ( value ) [ 0 ]  ===  activeFilter . filter . field 
33+     ) 
34+     const  acc : IProductFieldFilterInput  =  precAcc  ||  { } 
1635    if  ( activeFilter . filter . type  ===  AggregationType . CATEGORY )  { 
1736      acc [ activeFilter . filter . field  as  keyof  IProductFieldFilterInput ]  =  { 
1837        eq : activeFilter . value , 
@@ -36,7 +55,67 @@ export function getProductFilters(
3655          activeFilter . filter . field  as  keyof  IProductFieldFilterInput 
3756        ]  as  ISelectTypeDefaultFilterInputType 
3857      ) . in . push ( activeFilter . value ) 
58+     }  else  if  ( activeFilter . filter . type  ===  AggregationType . HISTOGRAM_DATE )  { 
59+       if  ( ! acc . boolFilter ?. _should )  { 
60+         acc . boolFilter  =  {  _should : [ ]  } 
61+       } 
62+       const  field  =  activeFilter . filter . field  as  keyof  IProductFieldFilterInput 
63+       const  date  =  parse ( 
64+         activeFilter . value , 
65+         activeFilter . filter . date_format , 
66+         new  Date ( ) 
67+       ) 
68+       // TODO: use default_date_range_interval in graphql endpoint 
69+       const  incrementString  =  activeFilter . filter . default_date_range_interval  ||  '1Y' 
70+       const  incrementNumber  =  Number ( 
71+         incrementString . substring ( 0 ,  incrementString . length  -  1 ) 
72+       ) 
73+       const  incrementType  =  incrementString . substring ( 
74+         incrementString . length  -  1 
75+       )  as  DurationKey 
76+ 
77+       const  gt  =  format ( date ,  activeFilter . filter . date_format ) 
78+       const  lt  =  format ( 
79+         add ( date ,  { 
80+           [ durationFormat [ incrementType ] ] : incrementNumber , 
81+         } ) , 
82+         activeFilter . filter . date_format 
83+       ) 
84+ 
85+       acc . boolFilter . _should . push ( { 
86+         [ field ] : { 
87+           gt, 
88+           lt, 
89+         } , 
90+       } ) 
91+     }  else  if  ( activeFilter . filter . type  ===  AggregationType . HISTOGRAM )  { 
92+       if  ( ! acc . boolFilter ?. _should )  { 
93+         acc . boolFilter  =  {  _should : [ ]  } 
94+       } 
95+       const  field  =  activeFilter . filter . field  as  keyof  IProductFieldFilterInput 
96+       const  arrayValue  =  activeFilter . value . split ( '-' ) 
97+       const  [ firstValue ,  secondValue ]  =  arrayValue 
98+ 
99+       if  ( arrayValue . length  >  1 )  { 
100+         const  isFirstValueIsAsterisk  =  firstValue  ===  '*' 
101+         const  isSecondValueIsAsterisk  =  secondValue  ===  '*' 
102+         let  data : Record < string ,  number >  =  { } 
103+ 
104+         if  ( isFirstValueIsAsterisk  &&  ! isSecondValueIsAsterisk )  { 
105+           data  =  {  lt : Number ( secondValue )  } 
106+         }  else  if  ( ! isFirstValueIsAsterisk  &&  ! isSecondValueIsAsterisk )  { 
107+           data  =  {  gte : Number ( firstValue ) ,  lte : Number ( secondValue )  } 
108+         }  else  { 
109+           data  =  {  gt : Number ( firstValue )  } 
110+         } 
111+ 
112+         acc . boolFilter . _should . push ( { 
113+           [ field ] : data , 
114+         } ) 
115+       } 
39116    } 
40-     return  acc 
41-   } ,  { } ) 
42- } 
117+     if  ( ! precAcc )  data . push ( acc ) 
118+   } ) 
119+ 
120+   return  data 
121+ } 
0 commit comments