You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dlab.apache.org by of...@apache.org on 2020/06/11 14:15:47 UTC

[incubator-dlab] branch develop updated: [DLAB-1742] Billing report bug fix (#770)

This is an automated email from the ASF dual-hosted git repository.

ofuks pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-dlab.git


The following commit(s) were added to refs/heads/develop by this push:
     new 8926f92  [DLAB-1742] Billing report bug fix (#770)
8926f92 is described below

commit 8926f924ff335ac5520c9c5ddb6edf26f0cbd1b3
Author: Pavel Papou <pp...@gmail.com>
AuthorDate: Thu Jun 11 10:15:38 2020 -0400

    [DLAB-1742] Billing report bug fix (#770)
    
    [DLAB-1742] Billing report bug fix
---
 .../epam/dlab/backendapi/domain/BillingReport.java |  2 +-
 .../service/impl/BillingServiceImpl.java           | 27 +++++++------
 .../epam/dlab/backendapi/util/BillingUtils.java    | 44 ++++++++++++++++------
 3 files changed, 50 insertions(+), 23 deletions(-)

diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/domain/BillingReport.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/domain/BillingReport.java
index 2bb2062..5d1d076 100644
--- a/services/self-service/src/main/java/com/epam/dlab/backendapi/domain/BillingReport.java
+++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/domain/BillingReport.java
@@ -41,5 +41,5 @@ public class BillingReport {
     private double totalCost;
     private String currency;
     @JsonProperty("is_full")
-    private boolean isFull;
+    private boolean isReportHeaderCompletable;
 }
diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/BillingServiceImpl.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/BillingServiceImpl.java
index 8eae49b..10adf4d 100644
--- a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/BillingServiceImpl.java
+++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/BillingServiceImpl.java
@@ -116,20 +116,21 @@ public class BillingServiceImpl implements BillingService {
                 .usageDateTo(max)
                 .totalCost(new BigDecimal(sum).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue())
                 .currency(currency)
-                .isFull(isFullReport(user))
+                .isReportHeaderCompletable(hasUserBillingRole(user))
                 .build();
     }
 
     @Override
     public String downloadReport(UserInfo user, BillingFilter filter) {
-        boolean isFull = isFullReport(user);
         BillingReport report = getBillingReport(user, filter);
-        StringBuilder builder = new StringBuilder(BillingUtils.getFirstLine(report.getSbn(), report.getUsageDateFrom(), report.getUsageDateTo()));
-        builder.append(BillingUtils.getHeader(isFull));
+        boolean isReportComplete =report.isReportHeaderCompletable();
+        StringBuilder reportHead = new StringBuilder(BillingUtils.getFirstLine(report.getSbn(), report.getUsageDateFrom(), report.getUsageDateTo()));
+        String stringOfAdjustedHeader = BillingUtils.getHeader(isReportComplete);
+        reportHead.append(stringOfAdjustedHeader);
         try {
-            report.getReportLines().forEach(r -> builder.append(BillingUtils.printLine(r, isFull)));
-            builder.append(BillingUtils.getTotal(report.getTotalCost(), report.getCurrency()));
-            return builder.toString();
+            report.getReportLines().forEach(r -> reportHead.append(BillingUtils.printLine(r, isReportComplete)));
+            reportHead.append(BillingUtils.getTotal(report.getTotalCost(), report.getCurrency(), stringOfAdjustedHeader));
+            return reportHead.toString();
         } catch (Exception e) {
             log.error("Cannot write billing data ", e);
             throw new DlabException("Cannot write billing file ", e);
@@ -304,13 +305,17 @@ public class BillingServiceImpl implements BillingService {
         }
     }
 
-    private boolean isFullReport(UserInfo userInfo) {
-        return UserRoles.checkAccess(userInfo, RoleType.PAGE, "/api/infrastructure_provision/billing",
-                userInfo.getRoles());
+    /**
+     *
+     * @param userInfo user's properties for current session
+     * @return true, if user has be billing role
+     */
+    private boolean hasUserBillingRole(UserInfo userInfo) {
+        return UserRoles.checkAccess(userInfo, RoleType.PAGE, "/api/infrastructure_provision/billing", userInfo.getRoles());
     }
 
     private void setUserFilter(UserInfo userInfo, BillingFilter filter) {
-        if (!isFullReport(userInfo)) {
+        if (!hasUserBillingRole(userInfo)) {
             filter.setUsers(Lists.newArrayList(userInfo.getName()));
         }
     }
diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/util/BillingUtils.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/util/BillingUtils.java
index 7e46a09..72e5a77 100644
--- a/services/self-service/src/main/java/com/epam/dlab/backendapi/util/BillingUtils.java
+++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/util/BillingUtils.java
@@ -48,7 +48,8 @@ import static com.epam.dlab.dto.billing.BillingResourceType.VOLUME;
 
 public class BillingUtils {
     private static final String[] AVAILABLE_NOTEBOOKS = {"zeppelin", "tensor-rstudio", "rstudio", "tensor", "superset", "jupyterlab", "jupyter", "deeplearning"};
-    private static final String[] REPORT_HEADERS = {"DLab ID", "User", "Project", "DLab Resource Type", "Status", "Shape", "Product", "Cost"};
+    private static final String[] BILLING_FILTERED_REPORT_HEADERS = {"DLab ID", "Project", "DLab Resource Type", "Status", "Shape", "Product", "Cost"};
+    private static final String[] COMPLETE_REPORT_REPORT_HEADERS = {"DLab ID", "User", "Project", "DLab Resource Type", "Status", "Shape", "Product", "Cost"};
     private static final String REPORT_FIRST_LINE = "Service base name: %s. Available reporting period from: %s to: %s";
     private static final String TOTAL_LINE = "Total: %s %s";
     private static final String SSN_FORMAT = "%s-ssn";
@@ -182,6 +183,14 @@ public class BillingUtils {
         return list.stream();
     }
 
+    /**
+     *
+     * @param sbn Service Base Name
+     * @param from formatted date, like 2020-04-07
+     * @param to formatted date, like 2020-05-07
+     * @return line, like:
+     * "Service base name: SERVICE_BASE_NAME. Available reporting period from: 2020-04-07 to: 2020-04-07"
+     */
     public static String getFirstLine(String sbn, LocalDate from, LocalDate to) {
         return CSVFormatter.formatLine(Lists.newArrayList(String.format(REPORT_FIRST_LINE, sbn,
                 Optional.ofNullable(from).map(date -> date.format(DateTimeFormatter.ISO_DATE)).orElse(StringUtils.EMPTY),
@@ -189,18 +198,23 @@ public class BillingUtils {
                 CSVFormatter.SEPARATOR, '\"');
     }
 
-    public static String getHeader(boolean isFull) {
-        List<String> headers = new ArrayList<>(Arrays.asList(BillingUtils.REPORT_HEADERS));
-        if (!isFull) {
-            headers.remove(1);
+    /**
+     * headerType there are two types of header according user role
+     * @return line, like DLab ID,User,Project,DLab Resource Type,Status,Shape,Product,Cost
+     * in case of additional header type, the ENUM object will be propagated from the Service Impl Class
+     */
+    public static String getHeader(boolean isReportHeaderCompletable) {
+        if (!isReportHeaderCompletable){
+            return CSVFormatter.formatLine(Arrays.asList(BillingUtils.BILLING_FILTERED_REPORT_HEADERS), CSVFormatter.SEPARATOR);
         }
-        return CSVFormatter.formatLine(headers, CSVFormatter.SEPARATOR);
+        return CSVFormatter.formatLine(Arrays.asList(BillingUtils.COMPLETE_REPORT_REPORT_HEADERS), CSVFormatter.SEPARATOR);
     }
 
-    public static String printLine(BillingReportLine line, boolean isFull) {
+    public static String printLine(BillingReportLine line, boolean isReportHeaderCompletable) {
         List<String> lines = new ArrayList<>();
         lines.add(getOrEmpty(line.getDlabId()));
-        if (isFull) {
+        //if user does not have the billing role, the User field should not be present in report
+        if (isReportHeaderCompletable) {
             lines.add(getOrEmpty(line.getUser()));
         }
         lines.add(getOrEmpty(line.getProject()));
@@ -212,12 +226,20 @@ public class BillingUtils {
         return CSVFormatter.formatLine(lines, CSVFormatter.SEPARATOR);
     }
 
-    public static String getTotal(Double total, String currency) {
+    /**
+     *
+     * @param total monetary amount
+     * @param currency user's currency
+     * @param stringOfAdjustedHeader filtered fields of report header
+     * @return line with cost of resources
+     */
+    public static String getTotal(Double total, String currency, String stringOfAdjustedHeader) {
         List<String> totalLine = new ArrayList<>();
-        for (int i = 0; i < REPORT_HEADERS.length - 1; i++) {
+        String[] headerFieldsList = stringOfAdjustedHeader.split(String.valueOf(CSVFormatter.SEPARATOR));
+        for (int i = 0; i < headerFieldsList.length - 1; i++) {
             totalLine.add(StringUtils.EMPTY);
         }
-        totalLine.add(REPORT_HEADERS.length - 1, String.format(TOTAL_LINE, getOrEmpty(String.valueOf(total)), getOrEmpty(currency)));
+        totalLine.add(headerFieldsList.length - 1, String.format(TOTAL_LINE, getOrEmpty(String.valueOf(total)), getOrEmpty(currency)));
         return CSVFormatter.formatLine(totalLine, CSVFormatter.SEPARATOR);
 
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@dlab.apache.org
For additional commands, e-mail: commits-help@dlab.apache.org