Skip to content

Commit d6000fb

Browse files
authored
Merge pull request #3030 from irontec/PROVIDER-2356-optimize-company-cgrates-queries
[PROVIDER-2356] Optimize Company CGRateS queries performance
2 parents e58bbf0 + 0c84c94 commit d6000fb

File tree

11 files changed

+3379
-527
lines changed

11 files changed

+3379
-527
lines changed

library/Ivoz/Provider/Domain/Assembler/Company/CompanyDtoAssembler.php

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -31,71 +31,63 @@ public function toDto(EntityInterface $entity, int $depth = 0, string $context =
3131

3232
$dto = $entity->toDto($depth);
3333

34+
$featureIds = array_map(
35+
function (FeaturesRelCompanyInterface $relFeature) {
36+
return (int)$relFeature
37+
->getFeature()
38+
->getId();
39+
},
40+
$entity->getRelFeatures()
41+
);
42+
3443
$domain = $entity->getDomain();
3544
if ($domain) {
3645
$dto->setDomainName(
3746
$domain->getDomain()
3847
);
3948
}
4049

41-
$skipRelationalFields = in_array($context, [
42-
'collection',
43-
CompanyDto::CONTEXT_BALANCES,
44-
CompanyDto::CONTEXT_DAILY_USAGE
45-
], true);
46-
47-
if (!$skipRelationalFields) {
48-
$featureIds = array_map(
49-
function (FeaturesRelCompanyInterface $relFeature) {
50-
return (int)$relFeature
51-
->getFeature()
52-
->getId();
53-
},
54-
$entity->getRelFeatures()
55-
);
56-
57-
$geoIpAllowedCountryIds = array_map(
58-
function (CompanyRelGeoIPCountryInterface $relCountry) {
59-
return (int)$relCountry
60-
->getCountry()
61-
->getId();
62-
},
63-
$entity->getRelCountries()
64-
);
65-
66-
$routingTagIds = array_map(
67-
function (CompanyRelRoutingTagInterface $relRoutingTag) {
68-
return (int)$relRoutingTag
69-
->getRoutingTag()
70-
->getId();
71-
},
72-
$entity->getRelRoutingTags()
73-
);
50+
$geoIpAllowedCountryIds = array_map(
51+
function (CompanyRelGeoIPCountryInterface $relCountry) {
52+
return (int)$relCountry
53+
->getCountry()
54+
->getId();
55+
},
56+
$entity->getRelCountries()
57+
);
58+
59+
$routingTagIds = array_map(
60+
function (CompanyRelRoutingTagInterface $relRoutingTag) {
61+
return (int)$relRoutingTag
62+
->getRoutingTag()
63+
->getId();
64+
},
65+
$entity->getRelRoutingTags()
66+
);
7467

75-
$codecIds = array_map(
76-
function (CompanyRelCodecInterface $relRelCodec) {
77-
return (int)$relRelCodec
68+
$codecIds = array_map(
69+
function (CompanyRelCodecInterface $relRelCodec) {
70+
return (int)$relRelCodec
7871
->getCodec()
7972
->getId();
80-
},
81-
$entity->getRelCodecs()
73+
},
74+
$entity->getRelCodecs()
75+
);
76+
77+
$dto
78+
->setFeatureIds(
79+
$featureIds
80+
)
81+
->setGeoIpAllowedCountries(
82+
$geoIpAllowedCountryIds
83+
)
84+
->setRoutingTagIds(
85+
$routingTagIds
86+
)
87+
->setCodecIds(
88+
$codecIds
8289
);
8390

84-
$dto
85-
->setFeatureIds(
86-
$featureIds
87-
)
88-
->setGeoIpAllowedCountries(
89-
$geoIpAllowedCountryIds
90-
)
91-
->setRoutingTagIds(
92-
$routingTagIds
93-
)
94-
->setCodecIds(
95-
$codecIds
96-
);
97-
}
98-
9991
if ($context === CompanyDto::CONTEXT_BALANCES) {
10092
$dto->setCurrencySymbol(
10193
$entity->getCurrencySymbol()

library/Ivoz/Provider/Domain/Model/Company/CompanyDto.php

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ class CompanyDto extends CompanyDtoAbstract
1515
public const CONTEXT_BALANCES = 'balances';
1616
public const CONTEXT_DAILY_USAGE = 'dailyUsage';
1717

18-
private const CONTEXTS_WITHOUT_EXTRA_FIELDS = [
19-
self::CONTEXT_COLLECTION,
20-
self::CONTEXT_BALANCES,
21-
self::CONTEXT_DAILY_USAGE,
22-
self::CONTEXT_SIMPLE
23-
];
24-
2518
/**
2619
* @var string
2720
* @AttributeDefinition(
@@ -115,6 +108,11 @@ public function normalize(string $context, string $role = ''): array
115108
$response['currentDayMaxUsage'] = $this->currentDayMaxUsage;
116109
$response['currentDayUsage'] = $this->getCurrentDayUsage();
117110
$response['accountStatus'] = $this->accountStatus;
111+
} else {
112+
$response['featureIds'] = $this->featureIds;
113+
$response['geoIpAllowedCountries'] = $this->geoIpAllowedCountries;
114+
$response['routingTagIds'] = $this->routingTagIds;
115+
$response['codecIds'] = $this->codecIds;
118116
}
119117
}
120118

@@ -285,37 +283,7 @@ private function filterBrandReadOnlyFields(array $data): array
285283
*/
286284
public static function getPropertyMap(string $context = self::CONTEXT_SIMPLE, string $role = null): array
287285
{
288-
if ($context === self::CONTEXT_BALANCES) {
289-
$response = [
290-
'id' => 'id',
291-
'name' => 'name',
292-
'type' => 'type',
293-
'billingMethod' => 'billingMethod',
294-
'currencySymbol' => 'currencySymbol',
295-
'accountStatus' => 'accountStatus',
296-
];
297-
298-
if ($role === 'ROLE_BRAND_ADMIN') {
299-
$response['domainUsers'] = 'domainUsers';
300-
$response['balance'] = 'balance';
301-
}
302-
} elseif ($context === self::CONTEXT_DAILY_USAGE) {
303-
$response = [
304-
'id' => 'id',
305-
'name' => 'name',
306-
'type' => 'type',
307-
'billingMethod' => 'billingMethod',
308-
'currentDayUsage' => 'currentDayUsage',
309-
'maxDailyUsage' => 'maxDailyUsage',
310-
'currencySymbol' => 'currencySymbol',
311-
'currentDayMaxUsage' => 'currentDayMaxUsage',
312-
'accountStatus' => 'accountStatus',
313-
];
314-
315-
if ($role === 'ROLE_BRAND_ADMIN') {
316-
$response['domainUsers'] = 'domainUsers';
317-
}
318-
} elseif ($context === self::CONTEXT_COLLECTION) {
286+
if ($context === self::CONTEXT_COLLECTION) {
319287
$response = [
320288
'id' => 'id',
321289
'name' => 'name',
@@ -355,12 +323,13 @@ public static function getPropertyMap(string $context = self::CONTEXT_SIMPLE, st
355323
}
356324

357325
if ($role === 'ROLE_BRAND_ADMIN') {
358-
if (in_array($context, self::CONTEXTS_WITHOUT_EXTRA_FIELDS, true)) {
359-
unset($response['featureIds']);
360-
unset($response['geoIpAllowedCountries']);
361-
unset($response['routingTagIds']);
362-
unset($response['codecIds']);
363-
}
326+
$response['featureIds'] = 'featureIds';
327+
$response['geoIpAllowedCountries'] = 'geoIpAllowedCountries';
328+
$response['routingTagIds'] = 'routingTagIds';
329+
$response['codecIds'] = 'codecIds';
330+
$response['corporationId'] = 'corporation';
331+
$response['applicationServerSetId'] = 'applicationServerSet';
332+
$response['mediaRelaySetId'] = 'mediaRelaySet';
364333
return self::filterFieldsForBrandAdmin($response);
365334
}
366335

@@ -375,13 +344,6 @@ public function toArray(bool $hideSensitiveData = false): array
375344
{
376345
$response = parent::toArray($hideSensitiveData);
377346
$response['domainName'] = $this->domainName;
378-
$response['featureIds'] = $this->featureIds;
379-
$response['geoIpAllowedCountries'] = $this->geoIpAllowedCountries;
380-
$response['routingTagIds'] = $this->routingTagIds;
381-
$response['codecIds'] = $this->codecIds;
382-
$response['currencySymbol'] = $this->currencySymbol;
383-
$response['currentDayMaxUsage'] = $this->currentDayMaxUsage;
384-
$response['accountStatus'] = $this->accountStatus;
385347

386348
return $response;
387349
}
@@ -426,12 +388,9 @@ private static function filterFieldsForBrandAdmin(array $response): array
426388
'maxDailyUsage',
427389
'maxDailyUsageEmail',
428390
'maxDailyUsageNotificationTemplateId',
429-
'currentDayUsage',
391+
'currentDayUsage' => 'currentDayUsage',
430392
'domainName',
431393
'corporationId',
432-
'currentDayMaxUsage',
433-
'accountStatus',
434-
'currencySymbol',
435394
'applicationServerSetId',
436395
'mediaRelaySetId',
437396
'locationId',

web/portal/brand/src/entities/CompanyBalances/Action/BalanceOperations.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import { CurrencyPropertyList } from 'entities/Currency/CurrencyProperties';
2828
import { useEffect, useState } from 'react';
2929
import { useStoreActions } from 'store';
3030

31-
import CompanyBalances from '../CompanyBalances';
32-
3331
const amountChoices = [
3432
{ id: '+', label: '+', operation: 'increment' },
3533
{ id: '-', label: '-', operation: 'decrement' },
@@ -55,7 +53,7 @@ const BalanceOperations: ActionFunctionComponent = (props: ActionItemProps) => {
5553

5654
useEffect(() => {
5755
apiGet({
58-
path: `${CompanyBalances.path}/${row.id}`,
56+
path: `/companies/${row.id}/balances`,
5957
params: {},
6058
successCallback: async (value: CompanyPropertyList<EntityValues>) => {
6159
const { symbol } = value.currency as CurrencyPropertyList<EntityValues>;
@@ -84,7 +82,7 @@ const BalanceOperations: ActionFunctionComponent = (props: ActionItemProps) => {
8482
?.operation as string;
8583

8684
apiPost({
87-
path: `${CompanyBalances.path}/${row.id}/modify_balance`,
85+
path: `/companies/${row.id}/modify_balance`,
8886
values: {
8987
operation,
9088
amount: amountValue,

web/rest/brand/config/api/raw/provider.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,22 @@ Ivoz\Provider\Domain\Model\Company\Company:
384384
get: ~
385385
put: ~
386386
delete: ~
387+
get_company_balances:
388+
method: 'GET'
389+
path: '/companies/{id}/balances'
390+
operation_normalization_context: !php/const Ivoz\Provider\Domain\Model\Company\CompanyDto::CONTEXT_BALANCES
391+
normalization_context:
392+
groups: [
393+
!php/const Ivoz\Provider\Domain\Model\Company\CompanyDto::CONTEXT_BALANCES
394+
]
395+
get_company_daily_usage:
396+
method: 'GET'
397+
path: '/companies/{id}/dailyUsage'
398+
operation_normalization_context: !php/const Ivoz\Provider\Domain\Model\Company\CompanyDto::CONTEXT_DAILY_USAGE
399+
normalization_context:
400+
groups: [
401+
!php/const Ivoz\Provider\Domain\Model\Company\CompanyDto::CONTEXT_DAILY_USAGE
402+
]
387403
post_company_modify_balance:
388404
method: 'POST'
389405
path: '/companies/{id}/modify_balance'

web/rest/brand/features/provider/company/getCompanies.feature

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Feature: Retrieve companies
1313
And the header "Content-Type" should be equal to "application/json; charset=utf-8"
1414
And the JSON should be equal to:
1515
"""
16-
[
16+
[
1717
{
1818
"type": "vpbx",
1919
"name": "DemoCompany",
@@ -27,7 +27,20 @@ Feature: Retrieve companies
2727
"nif": "12345678A"
2828
},
2929
"outgoingDdi": null,
30-
"domainName": "127.0.0.1"
30+
"corporation": 1,
31+
"applicationServerSet": 1,
32+
"mediaRelaySet": 0,
33+
"domainName": "127.0.0.1",
34+
"featureIds": [
35+
1,
36+
2,
37+
3,
38+
4,
39+
5
40+
],
41+
"geoIpAllowedCountries": [],
42+
"routingTagIds": [],
43+
"codecIds": []
3144
},
3245
{
3346
"type": "vpbx",
@@ -42,7 +55,14 @@ Feature: Retrieve companies
4255
"nif": "12345678-Z"
4356
},
4457
"outgoingDdi": null,
45-
"domainName": "test.irontec.com"
58+
"corporation": 1,
59+
"applicationServerSet": 1,
60+
"mediaRelaySet": 0,
61+
"domainName": "test.irontec.com",
62+
"featureIds": [],
63+
"geoIpAllowedCountries": [],
64+
"routingTagIds": [],
65+
"codecIds": []
4666
},
4767
{
4868
"type": "residential",
@@ -57,7 +77,14 @@ Feature: Retrieve companies
5777
"nif": "12345679-Z"
5878
},
5979
"outgoingDdi": null,
60-
"domainName": "retail.irontec.com"
80+
"corporation": null,
81+
"applicationServerSet": 1,
82+
"mediaRelaySet": 0,
83+
"domainName": "retail.irontec.com",
84+
"featureIds": [],
85+
"geoIpAllowedCountries": [],
86+
"routingTagIds": [],
87+
"codecIds": []
6188
},
6289
{
6390
"type": "retail",
@@ -72,7 +99,16 @@ Feature: Retrieve companies
7299
"nif": "12345679-Z"
73100
},
74101
"outgoingDdi": null,
75-
"domainName": "retail.irontec.com"
102+
"corporation": null,
103+
"applicationServerSet": 1,
104+
"mediaRelaySet": 0,
105+
"domainName": "retail.irontec.com",
106+
"featureIds": [],
107+
"geoIpAllowedCountries": [],
108+
"routingTagIds": [
109+
1
110+
],
111+
"codecIds": []
76112
},
77113
{
78114
"type": "wholesale",
@@ -87,7 +123,14 @@ Feature: Retrieve companies
87123
"nif": "12345689-Z"
88124
},
89125
"outgoingDdi": null,
90-
"domainName": null
126+
"corporation": null,
127+
"applicationServerSet": 0,
128+
"mediaRelaySet": 0,
129+
"domainName": null,
130+
"featureIds": [],
131+
"geoIpAllowedCountries": [],
132+
"routingTagIds": [],
133+
"codecIds": []
91134
}
92135
]
93136
"""
@@ -226,6 +269,16 @@ Feature: Retrieve companies
226269
"id": 1,
227270
"company": 1
228271
},
229-
"domainName": "127.0.0.1"
272+
"domainName": "127.0.0.1",
273+
"featureIds": [
274+
1,
275+
2,
276+
3,
277+
4,
278+
5
279+
],
280+
"geoIpAllowedCountries": [],
281+
"routingTagIds": [],
282+
"codecIds": []
230283
}
231284
"""

0 commit comments

Comments
 (0)