Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
jfkonecn committed Sep 13, 2024
1 parent bf9b601 commit 4a904d9
Showing 1 changed file with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private Map<String, JSONObject> getYearSummaries(String yearFilePrefix)
}


public Map<String, UsageSummary> getUserJson() throws UnsupportedEncodingException, IOException, ParseException {
public Map<String, UsageSummary> getUserSummaries() throws UnsupportedEncodingException, IOException, ParseException {
Map<String, JSONObject> months = getMonthSummaries(MONTH_USERS_USAGE_SUMMARY_FILE_PREFIX);
Map<String, JSONObject> years = getYearSummaries(YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX);
Map<String, UsageSummary> userData = new HashMap<>();
Expand Down Expand Up @@ -165,7 +165,7 @@ public Map<String, UsageSummary> getUserJson() throws UnsupportedEncodingExcepti
return userData;
}

public UsageSummary getResourceJson() throws UnsupportedEncodingException, IOException, ParseException {
public UsageSummary getResourceSummaries() throws UnsupportedEncodingException, IOException, ParseException {
Map<String, JSONObject> months = getMonthSummaries(MONTH_RESOURCES_USAGE_SUMMARY_FILE_PREFIX);
Map<String, JSONObject> years = getYearSummaries(YEAR_RESOURCES_USAGE_SUMMARY_FILE_PREFIX); UsageSummary resourceData = new UsageSummary();
String yearKey = "year";
Expand Down Expand Up @@ -208,7 +208,7 @@ public ResponseEntity<UserUsage> userUsageGet(@PathVariable @NotNull Long userId
throws IOException, ParseException {
if (userId != null) {

Map<String, UsageSummary> userSummaries = getUserJson();
Map<String, UsageSummary> userSummaries = getUserSummaries();

Optional<User> user = userService.getUserById(userId);
String email = user.map(User::getEmail).orElse(null);
Expand Down Expand Up @@ -348,8 +348,8 @@ public ResponseEntity<List<UserOverviewUsage>> userOverviewUsageGet(@RequestPara
@GetMapping("/usage/summary/resources")
public ResponseEntity<UsageSummary> resourceUsageGet()
throws IOException, ParseException {
getUserJson();
UsageSummary summary = getResourceJson();
getUserSummaries();
UsageSummary summary = getResourceSummaries();
return new ResponseEntity<UsageSummary>(summary, HttpStatus.OK);
}

Expand All @@ -364,36 +364,42 @@ public ResponseEntity<UsageSummary> resourceUsageGet()
@GetMapping("/usage/resources")
public ResponseEntity<UsageSummary> resourceDetailGet(@RequestParam String endpoint)
throws UnsupportedEncodingException, IOException, ParseException {
HttpStatus status = HttpStatus.OK;

int year = TimeUtil.getCurrentNYTime(clock).getYear();
JSONObject resourceSummary = requestData(YEAR_RESOURCES_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension.JSON_FILE.getExtension());
JSONObject userSummary = requestData(YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension.JSON_FILE.getExtension());
if (resourceSummary != null && userSummary != null ){
Gson gson = new Gson();
UsageSummary resourceUsageSummary = gson.fromJson(resourceSummary.toString(), UsageSummary.class);
if (resourceUsageSummary.getYear().containsKey(endpoint)) {
UsageSummary resourceDetail = new UsageSummary();
Map<String, JSONObject> monthResourceDetail = new HashMap<>();
userSummary.keySet().forEach(user ->
{
UsageSummary userUsageSummary = gson.fromJson(userSummary.get(user.toString()).toString(), UsageSummary.class);
Map<String, UsageSummary> userSummaries = getUserSummaries();
UsageSummary resourceSummaries = getResourceSummaries();

UsageSummary resourceDetail = new UsageSummary();
Map<String, Map<String, Long>> yearMap = resourceDetail.getYear();
Map<String, Map<String, Long>> monthResourceDetail = new HashMap<>();
for (Map.Entry<String, Map<String, Long>> entry : resourceSummaries.getYear().entrySet()) {

String year = entry.getKey();
Map<String, Long> currentYearMap = new HashMap<>();
yearMap.put(year, currentYearMap);
Map<String, Long> resourceUsageSummary = entry.getValue();

if (resourceUsageSummary.containsKey(endpoint)) {
for (Map.Entry<String, UsageSummary> userEntry : userSummaries.entrySet()) {

String user = userEntry.getKey();
UsageSummary userUsageSummary = userEntry.getValue();
long yearUsage = 0;

for (String month : userUsageSummary.getMonth().keySet()){
if (userUsageSummary.getMonth().get(month).containsKey(endpoint)) {
int monthUsage = (int) Double.parseDouble(userUsageSummary.getMonth().get(month).get(endpoint).toString());
if (!monthResourceDetail.containsKey(month))
monthResourceDetail.put(month, new JSONObject());
long monthUsage = (long) Double.parseDouble(userUsageSummary.getMonth().get(month).get(endpoint).toString());
if (!monthResourceDetail.containsKey(month)) {
monthResourceDetail.put(month, new HashMap<>());
}
monthResourceDetail.get(month).put(user, monthUsage);
yearUsage += monthUsage;
}
}
//resourceDetail.getYear().put(user.toString(), yearUsage);
});
//resourceDetail.setMonth(monthResourceDetail);
return new ResponseEntity<UsageSummary>(resourceDetail, status);
currentYearMap.put(user, yearUsage);
}
}
resourceDetail.setMonth(monthResourceDetail);
}
return new ResponseEntity<>(new UsageSummary(), status);
return new ResponseEntity<>(resourceDetail, HttpStatus.OK);
}
}

0 comments on commit 4a904d9

Please sign in to comment.