@@ -18,65 +18,64 @@ def process_usage(self):
18
18
usage_data = Usage ._get_usage_records ()
19
19
20
20
# Customize with Vendor API response
21
- if len (usage_data [ 'subscriptions' ] ) == 0 :
21
+ if Usage . _count_usage_records (usage_data ) == 0 :
22
22
# no data to report
23
23
return
24
24
25
25
contracts = self ._get_contracts ()
26
26
counter = 0
27
- # Retrieve the subscriptions for each contract
27
+ # For each contract report the usage
28
28
for contract in contracts :
29
- query = R ()
30
-
31
- # In preview all the subscription belongs to contract CRD-00000-00000-00000
32
- # use the next line for TEST purposes:
33
- # query &= R().contract.id.oneof(['CRD-00000-00000-00000'])
34
- query &= R ().contract .id .oneof ([contract ['id' ]])
35
- query &= R ().id .oneof ([self ._get_subscription_filter (usage_data )])
36
-
37
- subscriptions = self .client .collection ("assets" ).filter (query )
38
- subscriptions .order_by ('product.id' )
39
- product_id = None
40
- record_data = []
41
- # Finds the subscriptions with usage data for each contract, create the Usage for each different product
42
- for subscription in subscriptions :
43
- counter = counter + 1
44
- if product_id is None :
45
- product_id = subscription ['product' ]['id' ]
46
- self ._validate_ppu_schema (product_id )
47
- if product_id != subscription ['product' ]['id' ]:
48
- self ._create_usage (contract , product_id , record_data )
49
- product_id = subscription ['product' ]['id' ]
50
- self ._validate_ppu_schema (product_id )
51
- record_data .clear ()
52
- else :
53
- # Using subscription usage data, fills the records collection
54
- record_data .append (Usage ._get_usage_data (subscription , usage_data ))
55
- if product_id is not None :
56
- self ._create_usage (contract , product_id , record_data )
29
+ counter += self ._report_contract_usage (contract , usage_data )
57
30
58
- # Customize with Vendor API response
59
- if counter != len (usage_data ['subscriptions' ]):
60
- # Check if all the subscriptions have been reported in the files.
31
+ if counter != Usage ._count_usage_records (usage_data ):
32
+ # Not all the subscriptions with data have been reported in the files.
61
33
raise ValueError ()
62
34
63
- @staticmethod
64
- def _get_subscription_filter (usage_data ):
65
- # Customize with Vendor API response. From now assuming we have only one record to report
66
- return usage_data ['subscriptions' ][0 ]['id' ]
35
+ def _report_contract_usage (self , contract , usage_data ):
36
+ # For the contract report the usage, filtering the subscriptions with data to report.
37
+ # In preview environment all the subscription belongs to contract CRD-00000-00000-00000
38
+ subscriptions = self ._get_subscriptions (contract , self ._get_subscription_filter (usage_data ))
39
+ product_id = None
40
+ record_data = []
41
+ counter = 0
42
+ # Finds the subscriptions with usage data for each contract, create the Usage for each different product
43
+ for subscription in subscriptions :
44
+ counter = counter + 1
45
+ if product_id is None :
46
+ product_id = subscription ['product' ]['id' ]
47
+ self ._validate_ppu_schema (product_id )
48
+ if product_id != subscription ['product' ]['id' ]:
49
+ self ._create_usage (contract , product_id , record_data )
50
+ product_id = subscription ['product' ]['id' ]
51
+ self ._validate_ppu_schema (product_id )
52
+ record_data .clear ()
53
+ else :
54
+ # Using subscription usage data, fills the records collection
55
+ record_data .append (Usage ._get_usage_data (subscription , usage_data ))
56
+ if product_id is not None :
57
+ self ._create_usage (contract , product_id , record_data )
58
+ return counter
67
59
68
60
def _get_contracts (self ):
69
61
# Loads the distribution contracts to find the subscriptions with data to build the reports
70
62
query = R ()
71
63
query &= R ().type .oneof (['distribution' ])
72
64
query &= R ().status .oneof (['active' ])
73
- contracts = self .client .collection ("contracts" ).filter (query )
74
- return contracts
65
+ return self .client .collection ("contracts" ).filter (query )
66
+
67
+ def _get_subscriptions (self , contract , subs_id_filter ):
68
+ query = R ()
69
+ query &= R ().contract .id .oneof ([contract ['id' ]])
70
+ query &= R ().id .oneof ([subs_id_filter ])
71
+ subscriptions = self .client .collection ("assets" ).filter (query )
72
+ subscriptions .order_by ('product.id' )
73
+ return subscriptions
75
74
76
75
@staticmethod
77
76
def _get_usage_data (subscription , usage_data ):
78
77
# type: (Any, [Any]) -> UsageData
79
- # Retrieves th
78
+ # Fills the UsageDate object with the subscription and usage data from Vendor.
80
79
subs_usage_data = Utils .get_item_by_id (usage_data ['subscriptions' ], subscription ['id' ])
81
80
usage_data_object = UsageData ()
82
81
usage_data_object .record_description = subscription ['product' ]['name' ] + " Period: " + subs_usage_data [
@@ -96,7 +95,7 @@ def _validate_ppu_schema(self, product_id):
96
95
if schema != "QT" :
97
96
raise NotImplementedError ()
98
97
99
- # Loads the usage data from Vendor system calling Vendor API.
98
+ # Loads the usage data from Vendor system calling Vendor API. To be Customized.
100
99
@staticmethod
101
100
def _get_usage_records ():
102
101
# Customize this to call vendor System and load the usage to report for each subscription.
@@ -116,6 +115,17 @@ def _get_usage_records():
116
115
}
117
116
return usage_data
118
117
118
+ # Counts the subscriptions with Data to report from Vendor System. To be Customized.
119
+ @staticmethod
120
+ def _count_usage_records (usage_data ):
121
+ return len (usage_data ['subscriptions' ])
122
+
123
+ # Creates a filter with the Subscription ids to report. To be Customized.
124
+ @staticmethod
125
+ def _get_subscription_filter (usage_data ):
126
+ # Customize with Vendor API response. From now assuming we have only one record to report
127
+ return usage_data ['subscriptions' ][0 ]['id' ]
128
+
119
129
def _create_usage (self , contract , product_id , record_data ):
120
130
# type: (Any, str, [UsageData]) -> None
121
131
# Add a usage file for a Contract and product
0 commit comments