Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
feat(graph): Create new API endpoint metrics/download (#11367)
Browse files Browse the repository at this point in the history
* Add performance metric download API

* Remove unused classes

* Add OpenAPI documentation for metrics/download API

* Fix coding style

* Add new CSV response class

* Enable usage of csv and json presenters

* Proposal from Laurent

* Allow to use custom file name in csv download response

* Remove unused methods. Fix coding style

* Renaming ReadDataBinRepositoryInterface to ReadPerformanceDataRepositoryInterface

* Add unit test for FindPerformanceMetricResponse class

* Fix Code style

* Add unit tests for FindPerformanceMetrics class

* Refactor FindPerformanceMetrics use case

* Refactor IndexDataRepository class methods

* Convert phpunit tests for Performance metric to pestphp

* Add phpdoc on Find Performance Metric use case methods

* Update

Co-authored-by: Laurent Calvet <lcalvet@centreon.com>
  • Loading branch information
TamazC and callapa authored Jul 28, 2022
1 parent 6ed076e commit 8fca7e8
Show file tree
Hide file tree
Showing 27 changed files with 1,640 additions and 101 deletions.
16 changes: 16 additions & 0 deletions config/packages/Centreon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ services:
class: Centreon\Infrastructure\Serializer\ObjectConstructor
public: false

json_presenter:
class: Core\Infrastructure\Common\Presenter\JsonPresenter
public: false

Core\Infrastructure\Common\Presenter\PresenterFormatterInterface:
class: Core\Infrastructure\Common\Presenter\JsonPresenter
public: false

# Encryption
Security\Interfaces\EncryptionInterface:
class: Security\Encryption
Expand Down Expand Up @@ -85,6 +93,14 @@ services:
class: Centreon\Domain\Contact\ContactProvider
public: true

presenter.download.csv:
class: Core\Infrastructure\Common\Presenter\DownloadPresenter
arguments: ['@Core\Infrastructure\Common\Presenter\CsvPresenter']

Core\Application\RealTime\UseCase\FindPerformanceMetrics\FindPerformanceMetricPresenterInterface:
class: Core\Infrastructure\RealTime\Api\DownloadPerformanceMetrics\DownloadPerformanceMetricsPresenter
arguments: ['@presenter.download.csv']

# Authentication
Core\Security\Application\Repository\ReadAccessGroupRepositoryInterface:
class: Core\Security\Infrastructure\Repository\DbReadAccessGroupRepository
Expand Down
9 changes: 9 additions & 0 deletions config/routes/Centreon/monitoring/metric.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ monitoring.metric.getServicePerformanceMetrics:
controller: 'Centreon\Application\Controller\Monitoring\MetricController::getServicePerformanceMetrics'
condition: "request.attributes.get('version') >= 21.10"

monitoring.metric.downloadPerformanceMetrics:
methods: GET
path: /monitoring/hosts/{hostId}/services/{serviceId}/metrics/download
requirements:
hostId: '\d+'
serviceId: '\d+'
controller: 'Core\Infrastructure\RealTime\Api\DownloadPerformanceMetrics\DownloadPerformanceMetricsController'
condition: "request.attributes.get('version') >= 22.10"

monitoring.metric.getServiceStatusMetrics:
methods: GET
path: /monitoring/hosts/{hostId}/services/{serviceId}/metrics/status
Expand Down
34 changes: 34 additions & 0 deletions doc/API/centreon-api-v22.10.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3400,6 +3400,40 @@ paths:
$ref: '#/components/responses/NotFoundHostOrService'
'500':
$ref: '#/components/responses/InternalServerError'
/monitoring/hosts/{host_id}/services/{service_id}/metrics/download:
get:
tags:
- Metrics
summary: 'Download performance data as csv file'
parameters:
- $ref: '#/components/parameters/HostId'
- $ref: '#/components/parameters/ServiceId'
- in: query
name: start_date
required: true
description: "Start date of metrics (date format should be in ISO 8601)"
schema:
type: string
format: date-time
example: '2022-01-01T00:00:22Z'
- in: query
name: end
required: true
description: "End date of metrics (date format should be in ISO 8601)"
schema:
type: string
format: date-time
example: '2023-01-01T00:00:00Z'
responses:
'200':
description: A CSV file containg performance data
content:
application/force-download:
schema:
type: string
format: string
'500':
$ref: '#/components/responses/InternalServerError'
/monitoring/hosts/{host_id}/services/{service_id}/metrics/status:
get:
tags:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/

declare(strict_types=1);

namespace Core\Application\RealTime\Repository;

use Core\Domain\RealTime\Model\IndexData;

interface ReadIndexDataRepositoryInterface
{
/**
* @param int $hostId
* @param int $serviceId
* @throw \Throwable
* @return int
*/
public function findIndexByHostIdAndServiceId(int $hostId, int $serviceId): int;

/**
* @param int $index
* @return IndexData|null
*/
public function findHostNameAndServiceDescriptionByIndex(int $index): ?IndexData;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/

declare(strict_types=1);

namespace Core\Application\RealTime\Repository;

use Core\Domain\RealTime\Model\Metric;

interface ReadMetricRepositoryInterface
{
/**
* @param int $indexId
* @return array<Metric>
*/
public function findMetricsByIndexId(int $indexId): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/

declare(strict_types=1);

namespace Core\Application\RealTime\Repository;

use DateTimeInterface;
use Core\Domain\RealTime\Model\Metric;
use Core\Domain\RealTime\Model\PerformanceMetric;

interface ReadPerformanceDataRepositoryInterface
{
/**
* @param array<Metric> $metrics
* @param DateTimeInterface $startDate
* @param DateTimeInterface $endDate
* @return iterable<PerformanceMetric>
*/
public function findDataByMetricsAndDates(
array $metrics,
DateTimeInterface $startDate,
DateTimeInterface $endDate
): iterable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/

declare(strict_types=1);

namespace Core\Application\RealTime\UseCase\FindPerformanceMetrics;

use Core\Application\Common\UseCase\PresenterInterface;
use Core\Infrastructure\Common\Presenter\DownloadInterface;

interface FindPerformanceMetricPresenterInterface extends PresenterInterface, DownloadInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/

declare(strict_types=1);

namespace Core\Application\RealTime\UseCase\FindPerformanceMetrics;

use DateTimeInterface;

class FindPerformanceMetricRequest
{
/**
* @param int $hostId
* @param int $serviceId
* @param DateTimeInterface $startDate
* @param DateTimeInterface $endDate
*/
public function __construct(
public int $hostId,
public int $serviceId,
public DateTimeInterface $startDate,
public DateTimeInterface $endDate
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/

declare(strict_types=1);

namespace Core\Application\RealTime\UseCase\FindPerformanceMetrics;

use Core\Domain\RealTime\Model\PerformanceMetric;

class FindPerformanceMetricResponse
{
/**
* @var iterable<mixed>
*/
public iterable $performanceMetrics = [];

/**
* @param iterable<PerformanceMetric> $performanceMetrics
*/
public function __construct(iterable $performanceMetrics)
{
$this->performanceMetrics = $this->performanceMetricToArray($performanceMetrics);
}

/**
* @param iterable<PerformanceMetric> $performanceMetrics
* @return iterable<mixed>
*/
private function performanceMetricToArray(iterable $performanceMetrics): iterable
{
foreach ($performanceMetrics as $performanceMetric) {
yield $this->formatPerformanceMetric($performanceMetric);
}
}

/**
* @param PerformanceMetric $performanceMetric
* @return array<string, mixed>
*/
private function formatPerformanceMetric(PerformanceMetric $performanceMetric): array
{
$formattedData = [
'time' => $performanceMetric->getDateValue()->getTimestamp(),
'humantime' => $performanceMetric->getDateValue()->format('Y-m-d H:i:s')
];

foreach ($performanceMetric->getMetricValues() as $metricValue) {
$formattedData[$metricValue->getName()] = sprintf('%f', $metricValue->getValue());
}

return $formattedData;
}
}
Loading

0 comments on commit 8fca7e8

Please sign in to comment.