11package clap .server .adapter .outbound .infrastructure .elastic ;
22
3- import clap .server .adapter .outbound .infrastructure .elastic .dto .PeriodConfig ;
43import clap .server .adapter .outbound .infrastructure .elastic .entity .ElasticTask ;
54import clap .server .adapter .outbound .infrastructure .elastic .repository .TaskElasticRepository ;
65import clap .server .application .port .outbound .task .ElasticTaskPort ;
76import clap .server .common .annotation .architecture .InfrastructureAdapter ;
87import co .elastic .clients .elasticsearch ._types .aggregations .AggregationBuilders ;
9- import co .elastic .clients .elasticsearch ._types .aggregations .CalendarInterval ;
108import co .elastic .clients .elasticsearch ._types .aggregations .MultiBucketBase ;
119import lombok .RequiredArgsConstructor ;
1210import org .springframework .data .elasticsearch .client .elc .ElasticsearchAggregations ;
@@ -32,30 +30,111 @@ public void saveStatistics(List<ElasticTask> statistics) {
3230
3331 @ Override
3432 public Map <String , Long > findPeriodTaskRequestByPeriod (String period ) {
35- PeriodConfig periodConfig = getPeriodConfig (period );
33+ PeriodConfig periodConfig = PeriodConfig . valueOf (period . toUpperCase () );
3634
37- NativeQuery query = buildQuery (periodConfig );
38- ElasticsearchAggregations result = executeQuery (query );
39- return processResults (result , periodConfig );
35+ NativeQuery query = buildPeriodTaskRequestQuery (periodConfig );
36+ return getPeriodTaskResults (executeQuery (query ), periodConfig );
4037 }
4138
42- private PeriodConfig getPeriodConfig (String period ) {
43- if (period .equals ("week" )) {
44- return new PeriodConfig (14 , CalendarInterval .Day , 0 , 10 );
45- }
46- return new PeriodConfig (1 , CalendarInterval .Hour , 11 , 19 );
39+ @ Override
40+ public Map <String , Long > findPeriodTaskProcessByPeriod (String period ) {
41+ PeriodConfig periodConfig = PeriodConfig .valueOf (period .toUpperCase ());
42+
43+ NativeQuery query = buildPeriodTaskProcessQuery (periodConfig );
44+ return getPeriodTaskResults (executeQuery (query ), periodConfig );
45+ }
46+
47+ @ Override
48+ public Map <String , Long > findCategoryTaskRequestByPeriod (String period ) {
49+ PeriodConfig periodConfig = PeriodConfig .valueOf (period .toUpperCase ());
50+
51+ NativeQuery query = buildCategoryTaskRequestQuery (periodConfig );
52+ return getCategoryTaskResults (executeQuery (query ));
4753 }
4854
49- private NativeQuery buildQuery (PeriodConfig config ) {
55+ @ Override
56+ public Map <String , Long > findSubCategoryTaskRequestByPeriod (String period , String mainCategory ) {
57+ PeriodConfig periodConfig = PeriodConfig .valueOf (period .toUpperCase ());
58+
59+ NativeQuery query = buildSubCategoryTaskRequestQuery (periodConfig , mainCategory );
60+ return getCategoryTaskResults (executeQuery (query ));
61+ }
62+
63+ private NativeQuery buildPeriodTaskRequestQuery (PeriodConfig config ) {
5064 return NativeQuery .builder ()
5165 .withQuery (q -> q
5266 .range (r -> r
5367 .date (d -> d
5468 .field ("created_at" )
55- .gte (String .valueOf (LocalDate .now ().minusDays (config .daysToSubtract ()))))))
56- .withAggregation ("period_task_request" , AggregationBuilders .dateHistogram ()
69+ .gte (String .valueOf (LocalDate .now ().minusDays (config .getDaysToSubtract ()))))))
70+ .withAggregation ("period_task" , AggregationBuilders .dateHistogram ()
71+ .field ("created_at" )
72+ .calendarInterval (config .getCalendarInterval ())
73+ .build ()._toAggregation ())
74+ .withMaxResults (0 )
75+ .build ();
76+ }
77+
78+ private NativeQuery buildPeriodTaskProcessQuery (PeriodConfig config ) {
79+ NativeQuery rangeQuery = NativeQuery .builder ()
80+ .withQuery (q -> q
81+ .range (r -> r
82+ .date (d -> d
83+ .field ("created_at" )
84+ .gte (String .valueOf (LocalDate .now ().minusDays (config .getDaysToSubtract ())))))).build ();
85+ NativeQuery statusQuery = NativeQuery .builder ()
86+ .withQuery (q -> q
87+ .term (v -> v
88+ .field ("status" )
89+ .value ("completed" ))).build ();
90+
91+ return NativeQuery .builder ()
92+ .withQuery (q -> q
93+ .bool (b -> b
94+ .must (rangeQuery .getQuery (), statusQuery .getQuery ()))
95+ )
96+ .withAggregation ("period_task" , AggregationBuilders .dateHistogram ()
5797 .field ("created_at" )
58- .calendarInterval (config .calendarInterval ())
98+ .calendarInterval (config .getCalendarInterval ())
99+ .build ()._toAggregation ())
100+ .withMaxResults (0 )
101+ .build ();
102+ }
103+
104+ private NativeQuery buildCategoryTaskRequestQuery (PeriodConfig config ) {
105+ return NativeQuery .builder ()
106+ .withQuery (q -> q
107+ .range (r -> r
108+ .date (d -> d
109+ .field ("created_at" )
110+ .gte (String .valueOf (LocalDate .now ().minusDays (config .getDaysToSubtract ()))))))
111+ .withAggregation ("category_task" , AggregationBuilders .terms ()
112+ .field ("main_category" )
113+ .build ()._toAggregation ())
114+ .withMaxResults (0 )
115+ .build ();
116+ }
117+
118+ private NativeQuery buildSubCategoryTaskRequestQuery (PeriodConfig config , String mainCategory ) {
119+ NativeQuery rangeQuery = NativeQuery .builder ()
120+ .withQuery (q -> q
121+ .range (r -> r
122+ .date (d -> d
123+ .field ("created_at" )
124+ .gte (String .valueOf (LocalDate .now ().minusDays (config .getDaysToSubtract ())))))).build ();
125+ NativeQuery categoryQuery = NativeQuery .builder ()
126+ .withQuery (q -> q
127+ .term (v -> v
128+ .field ("main_category" )
129+ .value (mainCategory ))).build ();
130+
131+ return NativeQuery .builder ()
132+ .withQuery (q -> q
133+ .bool (b -> b
134+ .must (rangeQuery .getQuery (), categoryQuery .getQuery ()))
135+ )
136+ .withAggregation ("category_task" , AggregationBuilders .terms ()
137+ .field ("sub_category" )
59138 .build ()._toAggregation ())
60139 .withMaxResults (0 )
61140 .build ();
@@ -67,9 +146,9 @@ private ElasticsearchAggregations executeQuery(NativeQuery query) {
67146 .getAggregations ();
68147 }
69148
70- private Map <String , Long > processResults (ElasticsearchAggregations aggregations , PeriodConfig config ) {
149+ private Map <String , Long > getPeriodTaskResults (ElasticsearchAggregations aggregations , PeriodConfig config ) {
71150 return new TreeMap <>(
72- aggregations .get ("period_task_request " )
151+ aggregations .get ("period_task " )
73152 .aggregation ()
74153 .getAggregate ()
75154 .dateHistogram ()
@@ -78,11 +157,27 @@ private Map<String, Long> processResults(ElasticsearchAggregations aggregations,
78157 .stream ()
79158 .collect (Collectors .toMap (
80159 bucket -> bucket .keyAsString ().substring (
81- config .substringStart (),
82- config .substringEnd ()
160+ config .getSubstringStart (),
161+ config .getSubstringEnd ()
83162 ),
84163 MultiBucketBase ::docCount
85164 ))
86165 );
87166 }
167+
168+ private Map <String , Long > getCategoryTaskResults (ElasticsearchAggregations aggregations ) {
169+ return new TreeMap <>(
170+ aggregations .get ("category_task" )
171+ .aggregation ()
172+ .getAggregate ()
173+ .sterms ()
174+ .buckets ()
175+ .array ()
176+ .stream ()
177+ .collect (Collectors .toMap (
178+ bucket -> bucket .key ().stringValue (),
179+ MultiBucketBase ::docCount
180+ ))
181+ );
182+ }
88183}
0 commit comments