-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nicolas Debrigode
committed
Jan 26, 2024
1 parent
e81b652
commit fdc24a0
Showing
21 changed files
with
438 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ parameters: | |
|
||
ignoreErrors: | ||
- '#Only booleans are allowed#' | ||
- '#Cannot cast mixed to int.#' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Client; | ||
|
||
use App\Util\EndWithFunction; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
class PeeringDbClient extends AbstractController | ||
{ | ||
public function __construct( | ||
readonly string $host, | ||
readonly string $uri, | ||
private HttpClientInterface $client, | ||
) { | ||
if (!EndWithFunction::endsWith($uri, '/')) { | ||
$uri = \sprintf('%s/', $uri); | ||
} | ||
|
||
$this->client = $client->withOptions([ | ||
'base_uri' => \sprintf('%s%s', $host, $uri), | ||
'verify_host' => false, | ||
'verify_peer' => false, | ||
'timeout' => 30, | ||
'max_duration' => 30, | ||
]); | ||
} | ||
|
||
/** | ||
* @throws TransportExceptionInterface | ||
*/ | ||
public function get(string $url): array | ||
{ | ||
$response = $this->client->request('GET', $url, [ | ||
'headers' => [ | ||
'Content-Type' => 'application/json', | ||
], | ||
]); | ||
|
||
try { | ||
return [ | ||
'status_code' => $response->getStatusCode(), | ||
'response' => $response->toArray(), | ||
]; | ||
} catch (ClientExceptionInterface|TransportExceptionInterface|DecodingExceptionInterface|ServerExceptionInterface|RedirectionExceptionInterface $e) { | ||
if (429 === $e->getCode()) { | ||
$this->addFlash('warning', 'Request was throttled by peeringdb.com API server.'); | ||
} | ||
|
||
return [ | ||
'status_code' => $e->getCode(), | ||
'message' => $e->getMessage(), | ||
'response' => [], | ||
]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controller; | ||
|
||
use App\Application\ConfigApplication; | ||
use App\Exception\ConfigErrorException; | ||
use App\Exception\DbErrorException; | ||
use App\Form\SelectMyIXForm; | ||
use App\Repository\GetAsDataRepository; | ||
use App\Repository\KnowlinksRepository; | ||
use App\Repository\PeeringDBRepository; | ||
use App\Util\Annotation\Menu; | ||
use Doctrine\DBAL\Exception; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
#[Route( | ||
path: '/ix', | ||
)] | ||
#[Menu('view_ix')] | ||
class IXStatsController extends BaseController | ||
{ | ||
protected array $data = []; | ||
|
||
/** | ||
* @throws ConfigErrorException | ||
* @throws DbErrorException | ||
* @throws Exception | ||
*/ | ||
#[Route( | ||
path: '/my-ix', | ||
name: 'my_ix', | ||
methods: ['GET|POST'], | ||
)] | ||
public function history( | ||
Request $request, | ||
PeeringDBRepository $peeringDBRepository, | ||
GetAsDataRepository $asDataRepository, | ||
ConfigApplication $Config, | ||
): Response { | ||
$this->base_data['content_wrapper']['titre'] = 'My IX Stats'; | ||
|
||
$form = $this->createForm(SelectMyIXForm::class); | ||
$form->handleRequest($request); | ||
|
||
if ($form->isSubmitted() && $form->isValid()) { | ||
$ixInfo = $peeringDBRepository->getIXInfo((int) $form->get('myix')->getData()); | ||
|
||
$this->base_data['content_wrapper']['titre'] = \sprintf( | ||
'Top %s (%s)', | ||
$this->base_data['top'], | ||
'24 hours' | ||
); | ||
|
||
$this->base_data['content_wrapper']['small'] = $ixInfo['name']; | ||
$this->data['data'] = $asDataRepository::get( | ||
$this->base_data['top'], | ||
'', | ||
[], | ||
$peeringDBRepository->getIXMembers((int) $form->get('myix')->getData()), | ||
); | ||
|
||
$this->data['start'] = time() - 24 * 3600; | ||
$this->data['end'] = time(); | ||
$this->data['graph_size'] = [ | ||
'width' => $Config::getAsStatsConfigGraph()['top_graph_width'], | ||
'height' => $Config::getAsStatsConfigGraph()['top_graph_height'], | ||
]; | ||
$this->data['selectedLinks'] = []; | ||
|
||
return $this->render('pages/ix/my_ix/show.html.twig', [ | ||
'base_data' => $this->base_data, | ||
'data' => $this->data, | ||
'knownlinks' => KnowlinksRepository::get(), | ||
'form' => $form->createView(), | ||
]); | ||
} | ||
|
||
return $this->render('pages/ix/my_ix/index.html.twig', [ | ||
'base_data' => $this->base_data, | ||
'form' => $form->createView(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Form; | ||
|
||
use App\Application\ConfigApplication; | ||
use App\Client\PeeringDbClient; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class SelectMyIXForm extends AbstractType | ||
{ | ||
public function __construct(private PeeringDbClient $peeringDbClient) | ||
{ | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$peering_data = $this->peeringDbClient->get(\sprintf('netixlan?asn=%s', ConfigApplication::getAsStatsConfigMyAsn())); | ||
|
||
$data = []; | ||
if (200 === $peering_data['status_code']) { | ||
foreach ($peering_data['response']['data'] as $myix) { | ||
$data[$myix['name']] = $myix['ix_id']; | ||
} | ||
} | ||
|
||
ksort($data); | ||
|
||
$builder | ||
->add('myix', ChoiceType::class, [ | ||
'label' => false, | ||
'choices' => $data, | ||
'choice_translation_domain' => false, | ||
]); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->setDefaults([ | ||
'csrf_protection' => false, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Repository; | ||
|
||
use App\Client\PeeringDbClient; | ||
|
||
class PeeringDBRepository | ||
{ | ||
private PeeringDbClient $peeringDbClient; | ||
|
||
public function __construct(PeeringDbClient $peeringDbClient) | ||
{ | ||
$this->peeringDbClient = $peeringDbClient; | ||
} | ||
|
||
public function getIXMembers(int $ix_id): array | ||
{ | ||
$data = $this->peeringDbClient->get(\sprintf('net?ix_id=%s', $ix_id)); | ||
|
||
if (200 !== $data['status_code']) { | ||
return []; | ||
} | ||
|
||
$asn = []; | ||
foreach ($data['response']['data'] as $list) { | ||
$asn[] = $list['asn']; | ||
} | ||
|
||
return $asn; | ||
} | ||
|
||
public function getIXInfo(int $ix_id): array | ||
{ | ||
$data = $this->peeringDbClient->get(\sprintf('ix?id=%s', $ix_id)); | ||
|
||
if (200 !== $data['status_code']) { | ||
return []; | ||
} | ||
|
||
return $data['response']['data'][0]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Util; | ||
|
||
class EndWithFunction | ||
{ | ||
public static function endsWith(string $FullStr, string $needle): bool | ||
{ | ||
$StrLen = strlen($needle); | ||
$FullStrEnd = substr($FullStr, strlen($FullStr) - $StrLen); | ||
|
||
return $FullStrEnd === $needle; | ||
} | ||
} |
Oops, something went wrong.