Skip to content

Commit

Permalink
added method-used-apps.json
Browse files Browse the repository at this point in the history
  • Loading branch information
shagtv committed Jan 29, 2020
1 parent f94778f commit 2afcdb8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

There are next changes:

## 1.2.14

There are next changes:

- added method-used-apps.json

## 1.2.13

There are next changes:
Expand Down
67 changes: 67 additions & 0 deletions src/Badoo/LiveProfilerUI/Pages/AjaxPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,46 @@
use Badoo\LiveProfilerUI\DataProviders\Interfaces\SourceInterface;
use Badoo\LiveProfilerUI\DataProviders\Interfaces\JobInterface;
use Badoo\LiveProfilerUI\DataProviders\Interfaces\MethodInterface;
use Badoo\LiveProfilerUI\DataProviders\Interfaces\MethodDataInterface;
use Badoo\LiveProfilerUI\DataProviders\Interfaces\SnapshotInterface;
use Badoo\LiveProfilerUI\FieldList;

class AjaxPages
{
/** @var SnapshotInterface */
protected $Snapshot;
/** @var MethodInterface */
protected $Method;
/** @var MethodDataInterface */
protected $MethodData;
/** @var JobInterface */
protected $Job;
/** @var Aggregator */
protected $Aggregator;
/** @var SourceInterface */
protected $Source;
/** @var FieldList */
protected $FieldList;
/** @var bool */
protected $use_jobs;

public function __construct(
SnapshotInterface $Snapshot,
MethodInterface $Method,
MethodDataInterface $MethodData,
JobInterface $Job,
Aggregator $Aggregator,
SourceInterface $Source,
FieldList $FieldList,
bool $use_jobs = false
) {
$this->Snapshot = $Snapshot;
$this->Method = $Method;
$this->MethodData = $MethodData;
$this->Job = $Job;
$this->Aggregator = $Aggregator;
$this->Source = $Source;
$this->FieldList = $FieldList;
$this->use_jobs = $use_jobs;
}

Expand Down Expand Up @@ -160,6 +170,63 @@ public function searchMethods(string $term) : array
}
}

public function getMethodUsedApps(string $method_name) : array
{
$method_name = ltrim($method_name, '\\');
try {
$methods = $this->Method->findByName($method_name, true);
if (!$methods) {
return [];
}

$method = current($methods);

$last_two_days = \Badoo\LiveProfilerUI\DateGenerator::getDatesArray(date('Y-m-d'), 2, 2);
$start_snapshot_id = in_array(current($methods)['date'], $last_two_days, true)
? $this->Snapshot->getMinSnapshotIdByDates($last_two_days)
: 0;

$method_data = $this->MethodData->getDataByMethodIdsAndSnapshotIds(
[],
[$method['id']],
100,
$start_snapshot_id
);

$snapshot_ids = [];
foreach ($method_data as $Row) {
$snapshot_id = $Row->getSnapshotId();
$snapshot_ids[$snapshot_id] = $snapshot_id;
}
$snapshots = $this->Snapshot->getListByIds($snapshot_ids);

$fields = $this->FieldList->getFields();

$results = [];
foreach ($method_data as $Row) {
$result = [];
$result['app'] = $snapshots[$Row->getSnapshotId()]['app'];
$result['label'] = $snapshots[$Row->getSnapshotId()]['label'];

$uniq_key = $result['app'] . '_' . $result['label'];
if (!empty($results[$uniq_key])) {
continue;
}

$values = $Row->getFormattedValues();
foreach ($fields as $field) {
$result['fields'][$field] = $values[$field];
}
$result['fields']['calls_count'] = $snapshots[$Row->getSnapshotId()]['calls_count'];

$results[$uniq_key] = $result;
}
return array_values($results);
} catch (\Throwable $Ex) {
return [];
}
}

public function allMethods() : array
{
try {
Expand Down
2 changes: 1 addition & 1 deletion src/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ services:

ajax_pages:
class: \Badoo\LiveProfilerUI\Pages\AjaxPages
arguments: ['@snapshot', '@method', '@job', '@aggregator', '@source', '%aggregator.use_jobs_in_aggregation%']
arguments: ['@snapshot', '@method', '@method_data', '@job', '@aggregator', '@source', '@fields', '%aggregator.use_jobs_in_aggregation%']

# services for data providers
snapshot:
Expand Down
10 changes: 10 additions & 0 deletions src/www/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@
echo json_encode($result);
break;

case '/profiler/method-used-apps.json':
$method = isset($_GET['method']) ? trim($_GET['method']) : '';
header('Content-Type: application/json;charset=UTF-8');

/** @var \Badoo\LiveProfilerUI\Pages\AjaxPages $Page */
$Page = $App->getPage('ajax_pages');

echo json_encode($Page->getMethodUsedApps($method));
break;

case '/profiler/all-methods.json':
header('Content-Type: application/json;charset=UTF-8');

Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Badoo/LiveProfilerUI/Pages/AjaxPagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@ public function testConstruct()
->setMethods()
->getMock();

/** @var \Badoo\LiveProfilerUI\DataProviders\MethodData $MethodDataMock */
$MethodDataMock = $this->getMockBuilder(\Badoo\LiveProfilerUI\DataProviders\MethodData::class)
->disableOriginalConstructor()
->setMethods([])
->getMock();

/** @var \Badoo\LiveProfilerUI\DataProviders\Job $JobMock */
$JobMock = $this->getMockBuilder(\Badoo\LiveProfilerUI\DataProviders\Job::class)
->disableOriginalConstructor()
Expand All @@ -494,14 +500,18 @@ public function testConstruct()
->setMethods()
->getMock();

$FieldList = new \Badoo\LiveProfilerUI\FieldList(['wt'], ['min', 'max', 'percent'], []);

$use_jobs = true;

$Page = new \Badoo\LiveProfilerUI\Pages\AjaxPages(
$SnapshotMock,
$MethodMock,
$MethodDataMock,
$JobMock,
$AggregatorMock,
$SourceMock,
$FieldList,
$use_jobs
);

Expand Down

0 comments on commit 2afcdb8

Please sign in to comment.