Skip to content

Commit f5d94c7

Browse files
Neha ShelkeNeha Shelke
authored andcommitted
Merge remote-tracking branch 'origin/processor-template-with-openapi-cookiecutter' into processor-template-with-openapi-cookiecutter
2 parents a6b44cd + ca58fc4 commit f5d94c7

File tree

3 files changed

+86
-43
lines changed

3 files changed

+86
-43
lines changed

{{cookiecutter.project_slug}}/connect_processor/app/report_usage.py

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,65 +18,64 @@ def process_usage(self):
1818
usage_data = Usage._get_usage_records()
1919

2020
# Customize with Vendor API response
21-
if len(usage_data['subscriptions']) == 0:
21+
if Usage._count_usage_records(usage_data) == 0:
2222
# no data to report
2323
return
2424

2525
contracts = self._get_contracts()
2626
counter = 0
27-
# Retrieve the subscriptions for each contract
27+
# For each contract report the usage
2828
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)
5730

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.
6133
raise ValueError()
6234

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
6759

6860
def _get_contracts(self):
6961
# Loads the distribution contracts to find the subscriptions with data to build the reports
7062
query = R()
7163
query &= R().type.oneof(['distribution'])
7264
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
7574

7675
@staticmethod
7776
def _get_usage_data(subscription, usage_data):
7877
# type: (Any, [Any]) -> UsageData
79-
# Retrieves th
78+
# Fills the UsageDate object with the subscription and usage data from Vendor.
8079
subs_usage_data = Utils.get_item_by_id(usage_data['subscriptions'], subscription['id'])
8180
usage_data_object = UsageData()
8281
usage_data_object.record_description = subscription['product']['name'] + " Period: " + subs_usage_data[
@@ -96,7 +95,7 @@ def _validate_ppu_schema(self, product_id):
9695
if schema != "QT":
9796
raise NotImplementedError()
9897

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.
10099
@staticmethod
101100
def _get_usage_records():
102101
# Customize this to call vendor System and load the usage to report for each subscription.
@@ -116,6 +115,17 @@ def _get_usage_records():
116115
}
117116
return usage_data
118117

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+
119129
def _create_usage(self, contract, product_id, record_data):
120130
# type: (Any, str, [UsageData]) -> None
121131
# Add a usage file for a Contract and product
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
[
3+
{
4+
"id": "AS-8055-3268-5577",
5+
"status": "active",
6+
"external_id": "QOOJ78FHEZ",
7+
"external_uid": "03b1f9ec-41d9-4c49-98c1-afd07fc7ca10",
8+
"product": {
9+
"id": "PRD-658-160-426",
10+
"icon": "/media/VA-589-589/PRD-658-160-426/media/PRD-658-160-426-logo_vFoiITz.png",
11+
"name": "Test product Name",
12+
"status": "published"
13+
}
14+
}
15+
]

{{cookiecutter.project_slug}}/tests/test_report_usage.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,26 @@ class TestUsage(unittest.TestCase):
2828
}
2929
]
3030
}))
31+
@patch('connect_processor.app.report_usage.Usage._get_contracts',
32+
MagicMock(return_value={
33+
"contracts": [
34+
{
35+
"id": "CRD-00000-00000-00000"
36+
}
37+
]
38+
}))
39+
@patch('connect_processor.app.report_usage.Usage._get_subscriptions',
40+
MagicMock(return_value=TestUtils.get_response("usage_records_response.json")))
41+
@patch('connect_processor.app.report_usage.Usage._validate_ppu_schema', MagicMock(return_value=True))
42+
@patch('connect_processor.app.report_usage.Usage._create_usage', MagicMock(return_value=True))
3143
def test_report_usage_pass(self):
32-
with self.assertRaises(ValueError):
33-
Usage(client).process_usage()
44+
Usage(client).process_usage()
45+
test_config_file = TestUtils.get_config_file()
46+
usage_path = test_config_file['rootPathUsage']
47+
if os.path.exists(usage_path):
48+
files = [f for f in os.listdir(usage_path) if f.endswith(".xlsx")]
49+
for f in files:
50+
os.remove(os.path.join(usage_path, f))
3451

3552
def test_create_excel_file(self):
3653
usage_data = UsageData()
@@ -44,3 +61,4 @@ def test_create_excel_file(self):
4461
path = Usage.UsageFileExcelCreator().create_usage_excel(record_data)
4562
self.assertTrue(os.path.exists(path))
4663
os.remove(path)
64+

0 commit comments

Comments
 (0)