3
3
import java .io .IOException ;
4
4
import java .io .InputStreamReader ;
5
5
import java .io .UnsupportedEncodingException ;
6
+ import java .time .Clock ;
6
7
import java .time .format .DateTimeFormatter ;
7
8
import java .time .temporal .ChronoUnit ;
8
9
import java .util .*;
@@ -52,6 +53,9 @@ public class UsageAnalysisController {
52
53
@ Autowired
53
54
private UserMapper userMapper ;
54
55
56
+ @ Autowired
57
+ private Clock clock ;
58
+
55
59
private JSONObject requestData (String file )
56
60
throws UnsupportedEncodingException , IOException , ParseException {
57
61
Optional <ResponseInputStream <GetObjectResponse >> s3object = s3Service .getObject (Constants .ONCOKB_S3_BUCKET , file );
@@ -66,7 +70,7 @@ private JSONObject requestData(String file)
66
70
/**
67
71
* API to get the detail usage info for specific user
68
72
* @param userId
69
- * @return user usage infomation of given user
73
+ * @return user usage information of given user
70
74
* @throws IOException
71
75
* @throws ParseException
72
76
*/
@@ -76,13 +80,13 @@ public ResponseEntity<UserUsage> userUsageGet(@PathVariable @NotNull Long userId
76
80
HttpStatus status = HttpStatus .OK ;
77
81
78
82
if (userId != null ) {
79
- int year = TimeUtil .getCurrentNYTime ().getYear ();
83
+ int year = TimeUtil .getCurrentNYTime (clock ).getYear ();
80
84
JSONObject yearSummary = requestData (YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension .JSON_FILE .getExtension ());
81
85
Map <String , JSONObject > monthSummaries = new HashMap <>();
82
86
int monthsBack = 0 ;
83
87
JSONObject monthSummary ;
84
88
do {
85
- String month = TimeUtil .getCurrentNYTime ().minus (monthsBack , ChronoUnit .MONTHS ).format (DateTimeFormatter .ofPattern ("yyyy-MM" ));
89
+ String month = TimeUtil .getCurrentNYTime (clock ).minus (monthsBack , ChronoUnit .MONTHS ).format (DateTimeFormatter .ofPattern ("yyyy-MM" ));
86
90
monthSummary = requestData (MONTH_USERS_USAGE_SUMMARY_FILE_PREFIX + month + FileExtension .JSON_FILE .getExtension ());
87
91
if (monthSummary != null ) {
88
92
monthSummaries .put (month , monthSummary );
@@ -143,13 +147,13 @@ public ResponseEntity<List<UserOverviewUsage>> userOverviewUsageGet(@RequestPara
143
147
throws IOException , ParseException {
144
148
HttpStatus status = HttpStatus .OK ;
145
149
146
- int year = TimeUtil .getCurrentNYTime ().getYear ();
150
+ int year = TimeUtil .getCurrentNYTime (clock ).getYear ();
147
151
JSONObject yearSummary = requestData (YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension .JSON_FILE .getExtension ());
148
152
Map <String , JSONObject > monthSummaries = new HashMap <>();
149
153
int monthsBack = 0 ;
150
154
JSONObject monthSummary ;
151
155
do {
152
- String month = TimeUtil .getCurrentNYTime ().minus (monthsBack , ChronoUnit .MONTHS ).format (DateTimeFormatter .ofPattern ("yyyy-MM" ));
156
+ String month = TimeUtil .getCurrentNYTime (clock ).minus (monthsBack , ChronoUnit .MONTHS ).format (DateTimeFormatter .ofPattern ("yyyy-MM" ));
153
157
monthSummary = requestData (MONTH_USERS_USAGE_SUMMARY_FILE_PREFIX + month + FileExtension .JSON_FILE .getExtension ());
154
158
if (monthSummary != null ) {
155
159
monthSummaries .put (month , monthSummary );
@@ -251,7 +255,7 @@ public ResponseEntity<UsageSummary> resourceUsageGet()
251
255
throws IOException , ParseException {
252
256
HttpStatus status = HttpStatus .OK ;
253
257
254
- int year = TimeUtil .getCurrentNYTime ().getYear ();
258
+ int year = TimeUtil .getCurrentNYTime (clock ).getYear ();
255
259
JSONObject jsonObject = requestData (YEAR_RESOURCES_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension .JSON_FILE .getExtension ());
256
260
257
261
Gson gson = new Gson ();
@@ -263,7 +267,7 @@ public ResponseEntity<UsageSummary> resourceUsageGet()
263
267
}
264
268
265
269
/**
266
- * API to get the usage of a sepcific resource
270
+ * API to get the usage of a specific resource
267
271
* @param endpoint
268
272
* @return usage of a specific endpoint
269
273
* @throws UnsupportedEncodingException
@@ -275,7 +279,7 @@ public ResponseEntity<UsageSummary> resourceDetailGet(@RequestParam String endpo
275
279
throws UnsupportedEncodingException , IOException , ParseException {
276
280
HttpStatus status = HttpStatus .OK ;
277
281
278
- int year = TimeUtil .getCurrentNYTime ().getYear ();
282
+ int year = TimeUtil .getCurrentNYTime (clock ).getYear ();
279
283
JSONObject resourceSummary = requestData (YEAR_RESOURCES_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension .JSON_FILE .getExtension ());
280
284
JSONObject userSummary = requestData (YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension .JSON_FILE .getExtension ());
281
285
if (resourceSummary != null && userSummary != null ){
0 commit comments