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

Commit

Permalink
enh(api): update service graph endpoints (#8413)
Browse files Browse the repository at this point in the history
Refs: MON-4792
  • Loading branch information
kduret authored Mar 13, 2020
1 parent 6e5dc46 commit 6f0a2d1
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 8 deletions.
8 changes: 8 additions & 0 deletions config/packages/serializer/Centreon/Monitoring.Resource.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ Centreon\Domain\Monitoring\Resource:
type: string
groups:
- 'resource_main'
statusGraphEndpoint:
type: string
groups:
- 'resource_main'
performanceGraphEndpoint:
type: string
groups:
- 'resource_main'
severity:
type: Centreon\Domain\Monitoring\ResourceSeverity
groups:
Expand Down
18 changes: 18 additions & 0 deletions config/routes/Centreon/monitoring/metric.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,21 @@ monitoring.metric.getServiceStatus:
end: '\S+'
controller: 'Centreon\Application\Controller\Monitoring\MetricController::getServiceStatus'
condition: "request.attributes.get('version') >= 2.0"

monitoring.metric.getServicePerformanceMetrics:
methods: GET
path: /monitoring/hosts/{hostId}/services/{serviceId}/metrics/performance
requirements:
hostId: '\d+'
serviceId: '\d+'
controller: 'Centreon\Application\Controller\Monitoring\MetricController::getServicePerformanceMetrics'
condition: "request.attributes.get('version') >= 2.0"

monitoring.metric.getServiceStatusMetrics:
methods: GET
path: /monitoring/hosts/{hostId}/services/{serviceId}/metrics/status
requirements:
hostId: '\d+'
serviceId: '\d+'
controller: 'Centreon\Application\Controller\Monitoring\MetricController::getServiceStatusMetrics'
condition: "request.attributes.get('version') >= 2.0"
4 changes: 2 additions & 2 deletions doc/API/centreon-api-v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2489,9 +2489,9 @@ components:
type: string
description: "URL of the status chart linked to the resource"
example: "/monitoring/hosts/1/services/2/metrics/status"
metrics_graph_endpoint:
performance_graph_endpoint:
type: string
description: "URL of the metrics chart linked to the resource"
description: "URL of the performance chart linked to the resource"
example: "/monitoring/hosts/1/services/2/metrics/performance"
duration:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
use Centreon\Domain\Monitoring\Metric\Interfaces\MetricServiceInterface;
use Centreon\Domain\Monitoring\Interfaces\MonitoringServiceInterface;
use Centreon\Domain\Monitoring\Service;
use Centreon\Domain\RequestParameters\Interfaces\RequestParametersInterface;
use Centreon\Domain\Exception\EntityNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use FOS\RestBundle\View\View;

/**
Expand Down Expand Up @@ -88,6 +90,36 @@ private function findService(int $hostId, int $serviceId): Service
return $service;
}

/**
* Validate and extract start/end dates from request parameters
*
* @param RequestParametersInterface $requestParameters
* @return array
* @example [new \Datetime('yesterday'), new \Datetime('today')]
* @throws NotFoundHttpException
* @throws \LogicException
*/
private function extractDatesFromRequestParameters(RequestParametersInterface $requestParameters): array
{
$start = $requestParameters->getExtraParameter('start') ?: '1 day ago';
$end = $requestParameters->getExtraParameter('end') ?: 'now';

foreach (['start' => $start, 'end' => $end] as $param => $value) {
if (false === strtotime($value)) {
throw new NotFoundHttpException(sprintf('Invalid date given for parameter "%s".', $param));
}
}

$start = new \DateTime($start);
$end = new \DateTime($end);

if ($start >= $end) {
throw new \RangeException('End date must be greater than start date.');
}

return [$start, $end];
}

/**
* Entry point to get service metrics
*
Expand Down Expand Up @@ -151,4 +183,66 @@ public function getServiceStatus(

return $this->view($status);
}

/**
* Entry point to get service performance metrics
*
* @param int $hostId
* @param int $serviceId
* @return View
* @throws \Exception
*/
public function getServicePerformanceMetrics(
RequestParametersInterface $requestParameters,
int $hostId,
int $serviceId
): View {
$this->denyAccessUnlessGrantedForApiRealtime();

list($start, $end) = $this->extractDatesFromRequestParameters($requestParameters);

/**
* @var $contact Contact
*/
$contact = $this->getUser();

$service = $this->findService($hostId, $serviceId);

$metrics = $this->metricService
->filterByContact($contact)
->findMetricsByService($service, $start, $end);

return $this->view($metrics);
}

/**
* Entry point to get service status metrics
*
* @param int $hostId
* @param int $serviceId
* @return View
* @throws \Exception
*/
public function getServiceStatusMetrics(
RequestParametersInterface $requestParameters,
int $hostId,
int $serviceId
): View {
$this->denyAccessUnlessGrantedForApiRealtime();

list($start, $end) = $this->extractDatesFromRequestParameters($requestParameters);

/**
* @var $contact Contact
*/
$contact = $this->getUser();

$service = $this->findService($hostId, $serviceId);

$status = $this->metricService
->filterByContact($contact)
->findStatusByService($service, $start, $end);

return $this->view($status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Centreon\Domain\Monitoring\Serializer\ResourceExclusionStrategy;
use Centreon\Domain\Monitoring\Resource;
use Centreon\Domain\Monitoring\ResourceFilter;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* Resource APIs for the Unified View page
Expand Down Expand Up @@ -65,12 +66,19 @@ class MonitoringResourceController extends AbstractController
*/
protected $resource;

/**
* @var UrlGeneratorInterface
*/
protected $router;

/**
* @param ResourceServiceInterface $resource
* @param UrlGeneratorInterface $router
*/
public function __construct(ResourceServiceInterface $resource)
public function __construct(ResourceServiceInterface $resource, UrlGeneratorInterface $router)
{
$this->resource = $resource;
$this->router = $router;
}

/**
Expand Down Expand Up @@ -132,9 +140,36 @@ public function list(

$context->addExclusionStrategy(new ResourceExclusionStrategy());

$resources = $this->resource->filterByContact($this->getUser())
->findResources($filter);

foreach ($resources as $resource) {
if ($resource->getParent() != null) {
$parameters = [
'hostId' => $resource->getParent()->getId(),
'serviceId' => $resource->getId(),
];

// set service performance graph endpoint from metrics controller
$resource->setPerformanceGraphEndpoint(
$this->router->generate(
'monitoring.metric.getServicePerformanceMetrics',
$parameters
)
);

// set service status graph endpoint from metrics controller
$resource->setStatusGraphEndpoint(
$this->router->generate(
'monitoring.metric.getServiceStatusMetrics',
$parameters
)
);
}
}

return $this->view([
'result' => $this->resource->filterByContact($this->getUser())
->findResources($filter),
'result' => $resources,
'meta' => $requestParameters->toArray(),
])->setContext($context);
}
Expand Down
48 changes: 48 additions & 0 deletions src/Centreon/Domain/Monitoring/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ class Resource
*/
private $acknowledgementEndpoint;

/**
* @var string|null
*/
private $statusGraphEndpoint;

/**
* @var string|null
*/
private $performanceGraphEndpoint;

/**
* @var \Centreon\Domain\Monitoring\ResourceSeverity|null
*/
Expand Down Expand Up @@ -399,6 +409,44 @@ public function setAcknowledgementEndpoint(string $acknowledgementEndpoint): sel
return $this;
}

/**
* @return string|null
*/
public function getStatusGraphEndpoint(): ?string
{
return $this->statusGraphEndpoint;
}

/**
* @param string $statusGraphEndpoint
* @return \Centreon\Domain\Monitoring\Resource
*/
public function setStatusGraphEndpoint(string $statusGraphEndpoint): self
{
$this->statusGraphEndpoint = $statusGraphEndpoint;

return $this;
}

/**
* @return string|null
*/
public function getPerformanceGraphEndpoint(): ?string
{
return $this->performanceGraphEndpoint;
}

/**
* @param string $performanceGraphEndpoint
* @return \Centreon\Domain\Monitoring\Resource
*/
public function setPerformanceGraphEndpoint(string $performanceGraphEndpoint): self
{
$this->performanceGraphEndpoint = $performanceGraphEndpoint;

return $this;
}

/**
* @return \Centreon\Domain\Monitoring\ResourceSeverity|null
*/
Expand Down
Loading

0 comments on commit 6f0a2d1

Please sign in to comment.