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


The following commit(s) were added to refs/heads/DLAB-1571 by this push:
     new 101fa77  Refactoring
101fa77 is described below

commit 101fa777e89c381a1c7ba50b5271cb11fc69697c
Author: Oleh Fuks <[email protected]>
AuthorDate: Fri Mar 13 12:02:51 2020 +0200

    Refactoring
---
 .../billing/gcp/dao/impl/BigQueryBillingDAO.java   | 48 ++++++++++++++--------
 1 file changed, 30 insertions(+), 18 deletions(-)

diff --git 
a/services/billing-gcp/src/main/java/com/epam/dlab/billing/gcp/dao/impl/BigQueryBillingDAO.java
 
b/services/billing-gcp/src/main/java/com/epam/dlab/billing/gcp/dao/impl/BigQueryBillingDAO.java
index cfb99de..ee917b5 100644
--- 
a/services/billing-gcp/src/main/java/com/epam/dlab/billing/gcp/dao/impl/BigQueryBillingDAO.java
+++ 
b/services/billing-gcp/src/main/java/com/epam/dlab/billing/gcp/dao/impl/BigQueryBillingDAO.java
@@ -25,6 +25,7 @@ import com.epam.dlab.billing.gcp.model.BillingHistory;
 import com.epam.dlab.billing.gcp.model.GcpBillingData;
 import com.epam.dlab.billing.gcp.repository.BillingHistoryRepository;
 import com.epam.dlab.dto.billing.BillingData;
+import com.epam.dlab.exceptions.DlabException;
 import com.google.cloud.bigquery.BigQuery;
 import com.google.cloud.bigquery.FieldValueList;
 import com.google.cloud.bigquery.QueryJobConfiguration;
@@ -104,31 +105,42 @@ public class BigQueryBillingDAO implements BillingDAO {
 
        @Override
        public List<BillingData> getBillingReport() {
-               GroupOperation groupOperation = group("product", "currency", 
"usageType", "dlabId")
-                               .min("from").as("from")
-                               .max("to").as("to")
-                               .sum("cost").as("cost");
-               Aggregation aggregation = newAggregation(groupOperation);
-
-               return mongoTemplate.aggregate(aggregation, "billing", 
GcpBillingData.class).getMappedResults()
-                               .stream()
-                               .map(this::toBillingData)
-                               .collect(Collectors.toList());
+               try {
+                       GroupOperation groupOperation = getGroupOperation();
+                       Aggregation aggregation = 
newAggregation(groupOperation);
+
+                       return mongoTemplate.aggregate(aggregation, "billing", 
GcpBillingData.class).getMappedResults()
+                                       .stream()
+                                       .map(this::toBillingData)
+                                       .collect(Collectors.toList());
+               } catch (Exception e) {
+                       log.error("Cannot retrieve billing information ", e);
+                       throw new DlabException("Cannot retrieve billing 
information", e);
+               }
        }
 
        @Override
        public List<BillingData> getBillingReport(List<String> dlabIds) {
-               GroupOperation groupOperation = group("product", "currency", 
"usageType", "dlabId")
+               try {
+                       GroupOperation groupOperation = getGroupOperation();
+                       MatchOperation matchOperation = 
Aggregation.match(Criteria.where("dlabId").in(dlabIds));
+                       Aggregation aggregation = 
newAggregation(matchOperation, groupOperation);
+
+                       return mongoTemplate.aggregate(aggregation, "billing", 
GcpBillingData.class).getMappedResults()
+                                       .stream()
+                                       .map(this::toBillingData)
+                                       .collect(Collectors.toList());
+               } catch (Exception e) {
+                       log.error("Cannot retrieve billing information ", e);
+                       throw new DlabException("Cannot retrieve billing 
information", e);
+               }
+       }
+
+       private GroupOperation getGroupOperation() {
+               return group("product", "currency", "usageType", "dlabId")
                                .min("from").as("from")
                                .max("to").as("to")
                                .sum("cost").as("cost");
-               MatchOperation matchOperation = 
Aggregation.match(Criteria.where("dlabId").in(dlabIds));
-               Aggregation aggregation = newAggregation(matchOperation, 
groupOperation);
-
-               return mongoTemplate.aggregate(aggregation, "billing", 
GcpBillingData.class).getMappedResults()
-                               .stream()
-                               .map(this::toBillingData)
-                               .collect(Collectors.toList());
        }
 
        private Stream<? extends GcpBillingData> bigQueryResultSetStream(Table 
table) {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to