From bf9b6018e63c048a22008dd359d0d292b35530a0 Mon Sep 17 00:00:00 2001 From: John Konecny <24961694+jfkonecn@users.noreply.github.com> Date: Fri, 13 Sep 2024 15:00:43 -0400 Subject: [PATCH] a --- .../oncokb/web/rest/CronJobController.java | 13 -- .../web/rest/UsageAnalysisController.java | 128 +++++------------- .../rest/vm/usageAnalysis/UsageSummary.java | 20 ++- 3 files changed, 46 insertions(+), 115 deletions(-) diff --git a/src/main/java/org/mskcc/cbio/oncokb/web/rest/CronJobController.java b/src/main/java/org/mskcc/cbio/oncokb/web/rest/CronJobController.java index 6fe3a6089..c9e25b959 100644 --- a/src/main/java/org/mskcc/cbio/oncokb/web/rest/CronJobController.java +++ b/src/main/java/org/mskcc/cbio/oncokb/web/rest/CronJobController.java @@ -187,19 +187,6 @@ public void moveTokenStatsToS3() throws IOException { log.info("Finished the cronjob to move token stats to s3."); } - private void calculateUsageSummary(UsageSummary usageSummary, String key, int count, String time) { - // Deal with year summary - usageSummary.getYear().put(key, usageSummary.getYear().getOrDefault(key, Long.valueOf(0)) + count); - // Deal with month summary - if (!usageSummary.getMonth().containsKey(time)) { - usageSummary.getMonth().put(time, new JSONObject()); - } - if (!usageSummary.getMonth().get(time).containsKey(key)) { - usageSummary.getMonth().get(time).put(key, new Integer(0)); - } - usageSummary.getMonth().get(time).put(key, (Integer) usageSummary.getMonth().get(time).get(key) + (Integer) count); - } - /** * {@code GET /check-trial-accounts} : Check the status of trial accounts */ diff --git a/src/main/java/org/mskcc/cbio/oncokb/web/rest/UsageAnalysisController.java b/src/main/java/org/mskcc/cbio/oncokb/web/rest/UsageAnalysisController.java index d8406bf9b..c6cf1d87f 100644 --- a/src/main/java/org/mskcc/cbio/oncokb/web/rest/UsageAnalysisController.java +++ b/src/main/java/org/mskcc/cbio/oncokb/web/rest/UsageAnalysisController.java @@ -119,35 +119,31 @@ private Map getYearSummaries(String yearFilePrefix) } - public Map>> getUserJson() throws UnsupportedEncodingException, IOException, ParseException { + public Map getUserJson() throws UnsupportedEncodingException, IOException, ParseException { Map months = getMonthSummaries(MONTH_USERS_USAGE_SUMMARY_FILE_PREFIX); Map years = getYearSummaries(YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX); - Map>> userData = new HashMap<>(); + Map userData = new HashMap<>(); Set users = years.entrySet().iterator().next().getValue().keySet(); String yearKey = "year"; String monthKey = "month"; String dayKey = "day"; for (String user : users) { - Map> userMap = new HashMap<>(); - userMap.put(yearKey, new HashMap<>()); - userMap.put(monthKey, new HashMap<>()); - userMap.put(dayKey, new HashMap<>()); - userData.put(user, userMap); + userData.put(user, new UsageSummary()); } for (Map.Entry yearEntry : years.entrySet()) { String year = yearEntry.getKey(); for (Object rawUserEntry : yearEntry.getValue().entrySet()) { Map.Entry userEntry = (Map.Entry)rawUserEntry; String user = userEntry.getKey(); - Map> userMap = userData.get(user); - Map yearMap = userMap.get(yearKey); - Map monthMap = userMap.get(monthKey); + UsageSummary userMap = userData.get(user); + Map> yearMap = userMap.getYear(); + Map> monthMap = userMap.getMonth(); yearMap.put(year, (JSONObject)userEntry.getValue().get(yearKey)); JSONObject monthJson = (JSONObject)userEntry.getValue().get(monthKey); for (Object rawMonthEntry : monthJson.entrySet()) { Map.Entry monthEntry = (Map.Entry)rawMonthEntry; String month = monthEntry.getKey(); - monthMap.put(month, (JSONObject)monthEntry.getValue()); + monthMap.put(month, monthEntry.getValue()); } } @@ -156,51 +152,45 @@ public Map>> getUserJson() throws Un for (Object rawUserEntry : monthEntry.getValue().entrySet()) { Map.Entry userEntry = (Map.Entry)rawUserEntry; String user = userEntry.getKey(); - Map> userMap = userData.get(user); - Map dayMap = userMap.get(dayKey); + UsageSummary userMap = userData.get(user); + Map> dayMap = userMap.getDay(); JSONObject dayJson = (JSONObject)userEntry.getValue().get(dayKey); for (Object rawDayEntry : dayJson.entrySet()) { Map.Entry dayEntry = (Map.Entry)rawDayEntry; String day = dayEntry.getKey(); - dayMap.put(day, (JSONObject)dayEntry.getValue()); + dayMap.put(day, dayEntry.getValue()); } } } return userData; } - public Map> getResourceJson() throws UnsupportedEncodingException, IOException, ParseException { + public UsageSummary getResourceJson() throws UnsupportedEncodingException, IOException, ParseException { Map months = getMonthSummaries(MONTH_RESOURCES_USAGE_SUMMARY_FILE_PREFIX); - Map years = getYearSummaries(YEAR_RESOURCES_USAGE_SUMMARY_FILE_PREFIX); - Map> resourceData = new HashMap<>(); + Map years = getYearSummaries(YEAR_RESOURCES_USAGE_SUMMARY_FILE_PREFIX); UsageSummary resourceData = new UsageSummary(); String yearKey = "year"; String monthKey = "month"; String dayKey = "day"; - resourceData.put(yearKey, new HashMap<>()); - resourceData.put(monthKey, new HashMap<>()); - resourceData.put(dayKey, new HashMap<>()); - for (Map.Entry yearEntry : years.entrySet()) { String year = yearEntry.getKey(); - JSONObject yearJson = yearEntry.getValue(); - Map yearMap = resourceData.get(yearKey); - Map monthMap = resourceData.get(monthKey); + Map> yearMap = resourceData.getYear(); + Map> monthMap = resourceData.getMonth(); yearMap.put(year, (JSONObject)yearEntry.getValue().get(yearKey)); JSONObject monthJson = (JSONObject)yearEntry.getValue().get(monthKey); for (Object rawMonthEntry : monthJson.entrySet()) { Map.Entry monthEntry = (Map.Entry)rawMonthEntry; String month = monthEntry.getKey(); - monthMap.put(month, (JSONObject)monthEntry.getValue()); + monthMap.put(month, monthEntry.getValue()); } } for (Map.Entry monthEntry : months.entrySet()) { - Map dayMap = resourceData.get(dayKey); + Map> dayMap = resourceData.getDay(); JSONObject dayJson = (JSONObject)monthEntry.getValue().get(dayKey); for (Object rawDayEntry : dayJson.entrySet()) { Map.Entry dayEntry = (Map.Entry)rawDayEntry; String day = dayEntry.getKey(); - dayMap.put(day, (JSONObject)dayEntry.getValue()); + dayMap.put(day, dayEntry.getValue()); } } return resourceData; @@ -216,62 +206,27 @@ public Map> getResourceJson() throws Unsupported @GetMapping("/usage/users/{userId}") public ResponseEntity userUsageGet(@PathVariable @NotNull Long userId) throws IOException, ParseException { - HttpStatus status = HttpStatus.OK; - if (userId != null) { - int year = TimeUtil.getCurrentNYTime(clock).getYear(); - JSONObject yearSummary = requestData(YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension.JSON_FILE.getExtension()); - Map monthSummaries = new HashMap<>(); - int monthsBack = 0; - JSONObject monthSummary; - do { - String month = TimeUtil.getCurrentNYTime(clock).minus(monthsBack, ChronoUnit.MONTHS).format(DateTimeFormatter.ofPattern("yyyy-MM")); - monthSummary = requestData(MONTH_USERS_USAGE_SUMMARY_FILE_PREFIX + month + FileExtension.JSON_FILE.getExtension()); - if (monthSummary != null) { - monthSummaries.put(month, monthSummary); - } - monthsBack++; - } while (monthsBack < 12); + + Map userSummaries = getUserJson(); Optional user = userService.getUserById(userId); String email = user.map(User::getEmail).orElse(null); - if (yearSummary != null){ - UsageSummary usageSummary = new UsageSummary(); - if (yearSummary.containsKey(email)) { - JSONObject yearUsageObject = (JSONObject) yearSummary.get(email); - Gson gson = new Gson(); - usageSummary = gson.fromJson(yearUsageObject.toString(), UsageSummary.class); - if (!monthSummaries.isEmpty()) { - Map dayUsage = new HashMap<>(); - Map monthUsage = new HashMap<>(); - for (Map.Entry entry : monthSummaries.entrySet()) { - if (entry.getValue().containsKey(email)) { - JSONObject userUsageObject = (JSONObject) entry.getValue().get(email); - JSONObject dayUsageObject = (JSONObject) userUsageObject.get("day"); - dayUsageObject.forEach((day, summary) -> dayUsage.put((String) day, (JSONObject) summary)); - JSONObject monthUsageObject = (JSONObject) userUsageObject.get("month"); - monthUsage.put((String) entry.getKey(), monthUsageObject); - } - } - usageSummary.setDay(dayUsage); - usageSummary.setMonth(monthUsage); - } - } - - UserUsage userUsage = new UserUsage(); - userUsage.setUserFirstName(user.get().getFirstName()); - userUsage.setUserLastName(user.get().getLastName()); - userUsage.setUserEmail(email); - userUsage.setLicenseType(Objects.nonNull(userMapper.userToUserDTO(user.get()).getLicenseType()) ? userMapper.userToUserDTO(user.get()).getLicenseType().getName() : null); - userUsage.setJobTitle(userMapper.userToUserDTO(user.get()).getJobTitle()); - userUsage.setCompany(userMapper.userToUserDTO(user.get()).getCompanyName()); - userUsage.setSummary(usageSummary); - return new ResponseEntity(userUsage, status); - } + UsageSummary usageSummary = userSummaries.getOrDefault(email, new UsageSummary()); + + UserUsage userUsage = new UserUsage(); + userUsage.setUserFirstName(user.get().getFirstName()); + userUsage.setUserLastName(user.get().getLastName()); + userUsage.setUserEmail(email); + userUsage.setLicenseType(Objects.nonNull(userMapper.userToUserDTO(user.get()).getLicenseType()) ? userMapper.userToUserDTO(user.get()).getLicenseType().getName() : null); + userUsage.setJobTitle(userMapper.userToUserDTO(user.get()).getJobTitle()); + userUsage.setCompany(userMapper.userToUserDTO(user.get()).getCompanyName()); + userUsage.setSummary(usageSummary); + return new ResponseEntity(userUsage, HttpStatus.OK); } - return new ResponseEntity(new UserUsage(), status); + return new ResponseEntity(new UserUsage(), HttpStatus.OK); } /** @@ -330,7 +285,8 @@ public ResponseEntity> userOverviewUsageGet(@RequestPara String noPrivateEndpoint = ""; long noPrivateMaxUsage = 0; long totalUsage = 0; - Map summary = usageSummary.getYear(); + //Map summary = usageSummary.getYear(); + Map summary = new HashMap<>(); for (String resource : summary.keySet()) { totalUsage += summary.get(resource); if (summary.get(resource) > maxUsage) { @@ -392,19 +348,9 @@ public ResponseEntity> userOverviewUsageGet(@RequestPara @GetMapping("/usage/summary/resources") public ResponseEntity resourceUsageGet() throws IOException, ParseException { - HttpStatus status = HttpStatus.OK; getUserJson(); - getResourceJson(); - - int year = TimeUtil.getCurrentNYTime(clock).getYear(); - JSONObject jsonObject = requestData(YEAR_RESOURCES_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension.JSON_FILE.getExtension()); - - Gson gson = new Gson(); - UsageSummary summary = new UsageSummary(); - if (jsonObject != null) { - summary = gson.fromJson(jsonObject.toString(), UsageSummary.class); - } - return new ResponseEntity(summary, status); + UsageSummary summary = getResourceJson(); + return new ResponseEntity(summary, HttpStatus.OK); } /** @@ -442,9 +388,9 @@ public ResponseEntity resourceDetailGet(@RequestParam String endpo yearUsage += monthUsage; } } - resourceDetail.getYear().put(user.toString(), yearUsage); + //resourceDetail.getYear().put(user.toString(), yearUsage); }); - resourceDetail.setMonth(monthResourceDetail); + //resourceDetail.setMonth(monthResourceDetail); return new ResponseEntity(resourceDetail, status); } } diff --git a/src/main/java/org/mskcc/cbio/oncokb/web/rest/vm/usageAnalysis/UsageSummary.java b/src/main/java/org/mskcc/cbio/oncokb/web/rest/vm/usageAnalysis/UsageSummary.java index c82202fe4..c7e3b3e28 100644 --- a/src/main/java/org/mskcc/cbio/oncokb/web/rest/vm/usageAnalysis/UsageSummary.java +++ b/src/main/java/org/mskcc/cbio/oncokb/web/rest/vm/usageAnalysis/UsageSummary.java @@ -10,33 +10,31 @@ */ public class UsageSummary { - private Map day = new HashMap<>(); - private Map month = new HashMap<>(); - private Map year = new HashMap<>(); + private Map> day = new HashMap<>(); + private Map> month = new HashMap<>(); + private Map> year = new HashMap<>(); - public Map getDay() { + public Map> getDay() { return day; } - public void setDay(Map day) { + public void setDay(Map> day) { this.day = day; } - public Map getMonth() { + public Map> getMonth() { return month; } - public void setMonth(Map month) { + public void setMonth(Map> month) { this.month = month; } - public Map getYear() { + public Map> getYear() { return year; } - public void setYear(Map year) { + public void setYear(Map> year) { this.year = year; } - - }