22
22
import java .time .Duration ;
23
23
import java .time .LocalDateTime ;
24
24
import java .time .format .DateTimeFormatter ;
25
- import java .util .Collections ;
26
25
import java .util .List ;
27
26
28
27
@ Slf4j
29
28
@ Component
30
29
public class StatisticClient {
31
- private static final DateTimeFormatter DTF = DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss" );
30
+ private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss" );
32
31
33
- private final String application ;
34
- private final String statsServiceUri ;
32
+ @ Value ("${spring.application.name}" )
33
+ private String application ;
34
+ @ Value ("${services.stats-service.uri}" )
35
+ private String statsServiceUri ;
35
36
private final ObjectMapper json ;
36
37
private final HttpClient httpClient ;
37
38
38
- public StatisticClient (@ Value ("${spring.application.name}" ) String application ,
39
- @ Value ("${services.stats-service.uri}" ) String statsServiceUri ,
40
- ObjectMapper json ) {
41
- this .application = application ;
42
- this .statsServiceUri = statsServiceUri ;
39
+ public StatisticClient (ObjectMapper json ) {
43
40
this .json = json ;
44
41
this .httpClient = HttpClient .newBuilder ()
45
- .connectTimeout (Duration .ofSeconds (2 ))
42
+ .connectTimeout (Duration .ofSeconds (5 ))
46
43
.build ();
47
44
}
48
45
@@ -66,23 +63,25 @@ public void hit(HttpServletRequest userRequest) {
66
63
.header (HttpHeaders .ACCEPT , "application/json" )
67
64
.build ();
68
65
69
- HttpResponse <Void > response = httpClient .send (hitRequest , HttpResponse .BodyHandlers .discarding ());
70
- log .debug ("Response from stats-service: {}" , response );
66
+ httpClient .send (hitRequest , HttpResponse .BodyHandlers .discarding ());
71
67
} catch (Exception e ) {
72
- log .warn ( "Cannot record hit" , e );
68
+ log .error ( "Record hit error " , e );
73
69
}
74
70
}
75
71
76
72
public List <ViewStats > getStats (ViewsStatsRequest request ) {
77
73
try {
78
- String queryString = toQueryString (
79
- request .toBuilder ()
80
- .application (application )
81
- .build ()
82
- );
74
+ String start = URLEncoder .encode (DATE_TIME_FORMATTER .format (request .getStart ()), StandardCharsets .UTF_8 );
75
+ String end = URLEncoder .encode (DATE_TIME_FORMATTER .format (request .getEnd ()), StandardCharsets .UTF_8 );
76
+
77
+ String query = String .format ("?start=%s&end=%s&unique=%b&application=%s" , start , end , request .isUnique (), application );
78
+ query += "&uris=" + String .join ("," , request .getUris ());
79
+ if (request .hasLimitCondition ()) {
80
+ query += "&limit=" + request .getLimit ();
81
+ }
83
82
84
83
HttpRequest httpRequest = HttpRequest .newBuilder ()
85
- .uri (URI .create (statsServiceUri + "/stats" + queryString ))
84
+ .uri (URI .create (statsServiceUri + "/stats" + query ))
86
85
.header (HttpHeaders .ACCEPT , "application/json" )
87
86
.build ();
88
87
@@ -92,30 +91,9 @@ public List<ViewStats> getStats(ViewsStatsRequest request) {
92
91
return json .readValue (response .body (), new TypeReference <>() {
93
92
});
94
93
}
95
- log .debug ("Response from stats-service: {}" , response );
96
94
} catch (Exception e ) {
97
- log .warn ( "Cannot get view stats for request: " + request , e );
95
+ log .error ( "Get view stats error" , e );
98
96
}
99
- return Collections .emptyList ();
100
- }
101
-
102
- private String toQueryString (ViewsStatsRequest request ) {
103
- String start = encode (DTF .format (request .getStart ()));
104
- String end = encode (DTF .format (request .getEnd ()));
105
-
106
- String queryString = String .format ("?start=%s&end=%s&unique=%b&application=%s" ,
107
- start , end , request .isUnique (), application );
108
-
109
- queryString += "&uris=" + String .join ("," , request .getUris ());
110
-
111
- if (request .hasLimitCondition ()) {
112
- queryString += "&limit=" + request .getLimit ();
113
- }
114
-
115
- return queryString ;
116
- }
117
-
118
- private String encode (String value ) {
119
- return URLEncoder .encode (value , StandardCharsets .UTF_8 );
97
+ return List .of ();
120
98
}
121
99
}
0 commit comments