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

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

commit 23d43885609cd8c084da6b39bcd4b4c7217815a7
Author: Oleh Fuks <olegfuk...@gmail.com>
AuthorDate: Fri Apr 3 14:57:07 2020 +0300

    Remote billing
---
 .../service/impl/BillingServiceImpl.java           | 15 ++++++++++-----
 .../epam/dlab/backendapi/util/BillingUtils.java    | 22 +++++++++++++++++-----
 2 files changed, 27 insertions(+), 10 deletions(-)

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 b687e62..794ac33 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
@@ -132,8 +132,13 @@ public class BillingServiceImpl implements BillingService {
     @Override
     public List<BillingReportLine> getBillingReportLines(UserInfo user, 
BillingFilter filter) {
         setUserFilter(user, filter);
-        Set<ProjectDTO> projects = new 
HashSet<>(projectService.getProjects(user));
-        projects.addAll(projectService.getUserProjects(user, false));
+        Set<ProjectDTO> projects;
+        if (isFullReport(user)) {
+            projects = new HashSet<>(projectService.getProjects());
+        } else {
+            projects = new HashSet<>(projectService.getProjects(user));
+            projects.addAll(projectService.getUserProjects(user, false));
+        }
 
         final Map<String, BillingReportLine> billableResources = 
getBillableResources(user, projects);
 
@@ -154,20 +159,20 @@ public class BillingServiceImpl implements BillingService 
{
                 .stream()
                 .filter(userInstance -> 
Objects.nonNull(userInstance.getExploratoryId()))
                 .flatMap(ui -> BillingUtils.exploratoryBillingDataStream(ui, 
configuration.getMaxSparkInstanceCount(), sbn));
-        final Stream<BillingReportLine> billingReportLineStream = projects
+        final Stream<BillingReportLine> customImages = projects
                 .stream()
                 .map(p -> imageExploratoryDao.getImagesForProject(p.getName()))
                 .flatMap(Collection::stream)
                 .flatMap(i -> BillingUtils.customImageBillingDataStream(i, 
sbn));
 
         if (UserRoles.isAdmin(user)) {
+            final Stream<BillingReportLine> ssnBillingDataStream = 
BillingUtils.ssnBillingDataStream(sbn);
             final Stream<BillingReportLine> billableEdges = projects
                     .stream()
                     .collect(Collectors.toMap(ProjectDTO::getName, 
ProjectDTO::getEndpoints))
                     .entrySet()
                     .stream()
                     .flatMap(e -> projectEdges(sbn, e.getKey(), e.getValue()));
-            final Stream<BillingReportLine> ssnBillingDataStream = 
BillingUtils.ssnBillingDataStream(sbn);
             final Stream<BillingReportLine> billableSharedEndpoints = 
endpointService.getEndpoints()
                     .stream()
                     .flatMap(endpoint -> 
BillingUtils.sharedEndpointBillingDataStream(endpoint.getName(), sbn));
@@ -176,7 +181,7 @@ public class BillingServiceImpl implements BillingService {
                     .flatMap(s -> s);
         }
 
-        final Map<String, BillingReportLine> billableResources = 
Stream.of(billableUserInstances, billingReportLineStream, 
billableAdminResources)
+        final Map<String, BillingReportLine> billableResources = 
Stream.of(billableUserInstances, customImages, billableAdminResources)
                 .flatMap(s -> s)
                 .collect(Collectors.toMap(BillingReportLine::getDlabId, b -> 
b));
         log.debug("Billable resources are: {}", billableResources);
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 2c5b87f..cc77ff1 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
@@ -47,6 +47,7 @@ import static 
com.epam.dlab.dto.billing.BillingResourceType.SSN;
 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", "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";
@@ -78,10 +79,12 @@ public class BillingUtils {
         final String edgeVolumeId = String.format(EDGE_VOLUME_FORMAT, sbn, 
project.toLowerCase(), endpoint);
         final String endpointBucketId = 
String.format(PROJECT_ENDPOINT_BUCKET_FORMAT, sbn, project.toLowerCase(), 
endpoint);
 
-        return Stream.of(
+        return Stream.concat(Stream.of(
                 BillingReportLine.builder().resourceName("EDGE 
node").user(SHARED_RESOURCE).project(project).dlabId(userEdgeId).resourceType(EDGE).status(UserInstanceStatus.of(status)).build(),
                 BillingReportLine.builder().resourceName("EDGE 
volume").user(SHARED_RESOURCE).project(project).dlabId(edgeVolumeId).resourceType(VOLUME).build(),
                 BillingReportLine.builder().resourceName("Project endpoint 
shared 
bucket").user(SHARED_RESOURCE).project(project).dlabId(endpointBucketId).resourceType(BUCKET).build()
+                ),
+                standardImageBillingDataStream(sbn, project, endpoint)
         );
     }
 
@@ -117,16 +120,13 @@ public class BillingUtils {
                         getSlaveVolumes(userInstance, cr, 
maxSparkInstanceCount)
                 ));
         final String exploratoryId = userInstance.getExploratoryId();
-        final String imageId1 = String.format(IMAGE_STANDARD_FORMAT1, sbn, 
userInstance.getProject(), userInstance.getEndpoint(), 
userInstance.getImageName().replace(IMAGE_NAME_PREFIX, ""));
-        final String imageId2 = String.format(IMAGE_STANDARD_FORMAT2, sbn, 
userInstance.getEndpoint(), 
userInstance.getImageName().replace(IMAGE_NAME_PREFIX, ""));
         final String primaryVolumeId = String.format(VOLUME_PRIMARY_FORMAT, 
exploratoryId);
         final String secondaryVolumeId = 
String.format(VOLUME_SECONDARY_FORMAT, exploratoryId);
         final Stream<BillingReportLine> exploratoryStream = Stream.of(
                 
withUserProject(userInstance).resourceName(userInstance.getExploratoryName()).dlabId(exploratoryId).resourceType(EXPLORATORY).status(UserInstanceStatus.of(userInstance.getStatus())).shape(userInstance.getShape()).build(),
-                
BillingReportLine.builder().resourceName(IMAGE_NAME).dlabId(imageId1).project(SHARED_RESOURCE).resourceType(IMAGE).build(),
-                
BillingReportLine.builder().resourceName(IMAGE_NAME).dlabId(imageId2).project(SHARED_RESOURCE).resourceType(IMAGE).build(),
                 
withUserProject(userInstance).resourceName(VOLUME_PRIMARY).dlabId(primaryVolumeId).resourceType(VOLUME).build(),
                 
withUserProject(userInstance).resourceName(VOLUME_SECONDARY).dlabId(secondaryVolumeId).resourceType(VOLUME).build());
+
         return Stream.concat(computationalStream, exploratoryStream);
     }
 
@@ -166,6 +166,18 @@ public class BillingUtils {
                 String.format(DATAENGINE_SERVICE_NAME_FORMAT, 
resource.getMasterNodeShape(), System.lineSeparator(), null, null);
     }
 
+    public static Stream<BillingReportLine> 
standardImageBillingDataStream(String sbn, String project, String endpoint) {
+        List<BillingReportLine> list = new ArrayList<>();
+        for (String notebook : AVAILABLE_NOTEBOOKS) {
+            
list.add(BillingReportLine.builder().resourceName(IMAGE_NAME).dlabId(String.format(IMAGE_STANDARD_FORMAT1,
 sbn, project, endpoint, notebook))
+                    .project(SHARED_RESOURCE).resourceType(IMAGE).build());
+            
list.add(BillingReportLine.builder().resourceName(IMAGE_NAME).dlabId(String.format(IMAGE_STANDARD_FORMAT2,
 sbn, endpoint, notebook))
+                    .project(SHARED_RESOURCE).resourceType(IMAGE).build());
+        }
+
+        return list.stream();
+    }
+
     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),


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

Reply via email to