Skip to content

Commit

Permalink
Replace coke and fix cs (#29)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Garifullin <[email protected]>
  • Loading branch information
bullder and Michael Garifullin authored Mar 2, 2022
1 parent f62b56f commit 212a61e
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 134 deletions.
11 changes: 0 additions & 11 deletions .coke

This file was deleted.

28 changes: 28 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Tests

on: [push]

jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
php-version:
- 7.1
- 7.2
- 7.3
- 7.4

steps:
- uses: actions/checkout@v2

- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: none
- uses: "ramsey/composer-install@v1"

- name: Code style check
run: vendor/bin/php-cs-fixer fix --dry-run --using-cache=no --verbose
- name: Unit tests
run: vendor/bin/atoum
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
.php-cs-fixer.cache
composer.lock
vendor/
13 changes: 13 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(
[
__DIR__.'/src',
]
);

$config = new M6Web\CS\Config\BedrockStreaming();
$config->setFinder($finder);

return $config;
19 changes: 0 additions & 19 deletions .travis.yml

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Stastd PHP Component [![Build Status](https://travis-ci.org/M6Web/Statsd.png?branch=master)](https://travis-ci.org/M6Web/Statsd)
# Stastd PHP Component [![Build Status](https://github.com/BedrockStreaming/Statsd/actions/workflows/tests.yml/badge.svg)](https://github.com/BedrockStreaming/Statsd/actions/workflows/tests.yml)

[usage](doc/usage.md)

See [StatsdBundle](https://github.com/M6Web/StatsdBundle) for an implementation on a symfony bundle.
See [StatsdBundle](https://github.com/M6Web/StatsdBundle) for an implementation on a symfony bundle.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
},
"require-dev": {
"atoum/atoum": "^2.8|^3.0",
"m6web/symfony2-coding-standard": "~1.2",
"m6web/coke": "~1.2"
"m6web/php-cs-fixer-config": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
56 changes: 29 additions & 27 deletions src/M6Web/Component/Statsd/Client.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* class sending udp packets to statsd
*
*/

namespace M6Web\Component\Statsd;

/**
Expand All @@ -12,30 +12,35 @@ class Client
{
/**
* commands to send
*
* @var \SplQueue
*/
protected $toSend;

/**
* statsd servers
*
* @var array
*/
protected $servers = array();
protected $servers = [];

/**
* number of servers
* @var integer
*
* @var int
*/
private $nbServers = 0;

/**
* list of server keys
*
* @var array
*/
private $serverKeys = array();
private $serverKeys = [];

/**
* contructeur
*
* @param array $servers les serveurs
*/
public function __construct(array $servers)
Expand All @@ -46,6 +51,7 @@ public function __construct(array $servers)

/**
* set the params from config
*
* @param array $servers les serveurs
*
* @throws Exception
Expand All @@ -55,7 +61,7 @@ public function __construct(array $servers)
protected function init(array $servers)
{
if (0 === count($servers)) {
throw new Exception("dont have any servers ?");
throw new Exception('dont have any servers ?');
}
// check server
foreach ($servers as $serName => $server) {
Expand All @@ -65,10 +71,10 @@ protected function init(array $servers)
}

if (!isset($server['address']) or !isset($server['port'])) {
throw new Exception($serName." : no address or port in the configuration ?!");
throw new Exception($serName.' : no address or port in the configuration ?!');
}
if (strpos($server['address'], 'udp://') !== 0) {
throw new Exception($serName." : address should begin with udp:// ?!");
throw new Exception($serName.' : address should begin with udp:// ?!');
}
// TODO : address format ?
}
Expand All @@ -87,6 +93,7 @@ protected function initQueue()

/**
* get servers
*
* @return array
*/
public function getServers()
Expand All @@ -96,6 +103,7 @@ public function getServers()

/**
* get commands to send
*
* @return array
*/
public function getToSend()
Expand All @@ -105,6 +113,7 @@ public function getToSend()

/**
* erase commands
*
* @return Client
*/
public function clearToSend()
Expand Down Expand Up @@ -139,14 +148,12 @@ public function getServerKey($stats)
*/
protected function addToSend($stats, $value, $sampleRate, $unit, $tags)
{

$message = new MessageEntity(
$message = new MessageEntity(
(string) $stats, (int) $value, (string) $unit, (float) $sampleRate, $tags
);

$queue = [
'server' => $this->getServerKey($stats)
, 'message' => $message
'server' => $this->getServerKey($stats), 'message' => $message,
];

$this->toSend->enqueue($queue);
Expand All @@ -164,7 +171,6 @@ protected function buildSampledData()
foreach ($this->getToSend() as $metric) {
$server = $metric['server'];
$sampledData[$server][] = $metric['message']->getStatsdMessage();

}

return $sampledData;
Expand All @@ -173,9 +179,9 @@ protected function buildSampledData()
/**
* Log timing information
*
* @param string $stats The metric to in log timing info for.
* @param string $stats the metric to in log timing info for
* @param int $time The ellapsed time (ms) to log
* @param float|int $sampleRate the rate (0-1) for sampling.
* @param float|int $sampleRate the rate (0-1) for sampling
* @param array $tags Tags key => value for influxDb
*
* @return Client
Expand All @@ -190,11 +196,11 @@ public function timing($stats, $time, $sampleRate = 1.0, $tags = [])
/**
* Increments one or more stats counters
*
* @param string $stats The metric(s) to increment.
* @param string $stats the metric(s) to increment
* @param float $sampleRate SamplingRate
* @param array $tags Tags key => value for influxDb
*
* @internal param $ float|1 $sampleRate the rate (0-1) for sampling.
* @internal param $ float|1 $sampleRate the rate (0-1) for sampling
*
* @return Client
*/
Expand All @@ -205,12 +211,11 @@ public function increment($stats, $sampleRate = 1.0, $tags = [])
return $this;
}


/**
* Decrements one or more stats counters.
*
* @param string $stats The metric(s) to decrement.
* @param float|int $sampleRate the rate (0-1) for sampling.
* @param string $stats the metric(s) to decrement
* @param float|int $sampleRate the rate (0-1) for sampling
* @param array $tags Tags key => value for influxDb
*
* @return Client
Expand All @@ -227,11 +232,9 @@ public function decrement($stats, $sampleRate = 1, $tags = [])
*
* @param string $stats The metric(s) to count
* @param int $value The count value
* @param float|int $sampleRate the rate (0-1) for sampling.
* @param float|int $sampleRate the rate (0-1) for sampling
* @param array $tags Tags key => value for influxDb
*
* @access public
*
* @return Client
*/
public function count($stats, $value, $sampleRate = 1, $tags = [])
Expand All @@ -246,10 +249,9 @@ public function count($stats, $value, $sampleRate = 1, $tags = [])
*
* @param string $stats The metric(s) to count
* @param int $value The value
* @param float|int $sampleRate the rate (0-1) for sampling.
* @param float|int $sampleRate the rate (0-1) for sampling
* @param array $tags Tags key => value for influxDb
*
* @access public
* @return Client
*/
public function gauge($stats, $value, $sampleRate = 1, $tags = [])
Expand All @@ -264,10 +266,9 @@ public function gauge($stats, $value, $sampleRate = 1, $tags = [])
*
* @param string $stats The metric(s) to count
* @param int $value The value
* @param float|int $sampleRate the rate (0-1) for sampling.
* @param float|int $sampleRate the rate (0-1) for sampling
* @param array $tags Tags key => value for influxDb
*
* @access public
* @return Client
*/
public function set($stats, $value, $sampleRate = 1, $tags = [])
Expand Down Expand Up @@ -311,12 +312,13 @@ public function send()
* @param array $datas array de data à env
*
* @throws Exception
*
* @return bool
*/
public function writeDatas($server, $datas)
{
if (!isset($this->getServers()[$server])) {
throw new Exception($server." undefined in the configuration");
throw new Exception($server.' undefined in the configuration');
}
$s = $this->getServers()[$server];
$fp = @fsockopen($s['address'], $s['port']);
Expand Down
1 change: 1 addition & 0 deletions src/M6Web/Component/Statsd/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/**
* Statsd exception
*/

namespace M6Web\Component\Statsd;

/**
Expand Down
30 changes: 9 additions & 21 deletions src/M6Web/Component/Statsd/MessageEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,22 @@

/**
* Class MessageEntity
*
* @package M6Web\Component\Statsd
*/
class MessageEntity
{
/**
* @var string
*/
/** @var string */
protected $node;

/**
* @var string
*/
/** @var string */
protected $value;

/**
* @var float
*/
/** @var float */
protected $sampleRate;

/**
* @var string
*/
/** @var string */
protected $unit;

/**
* @var array
*/
/** @var array */
protected $tags = [];

/**
Expand All @@ -45,7 +33,7 @@ class MessageEntity
*/
public function __construct($node, $value, $unit = '', $sampleRate = 1.0, $tags = [])
{
$this->node = $node;
$this->node = $node;
$this->value = $value;
if (!is_null($sampleRate)) {
$this->sampleRate = $sampleRate;
Expand Down Expand Up @@ -131,11 +119,11 @@ public function getUnit()

/**
* @return string Tags formatted for sending
* ex: "server=5,country=fr"
* ex: "server=5,country=fr"
*/
private function getTagsAsString()
{
$tags = array_map(function($k, $v) {
$tags = array_map(function ($k, $v) {
return $k.'='.$v;
}, array_keys($this->tags), $this->tags);

Expand All @@ -144,7 +132,7 @@ private function getTagsAsString()

/**
* @return string the node with tags as string
* ex : node "foo.bar" and tag ["country" => "fr" ] Into "foo.bar,country=fr"
* ex : node "foo.bar" and tag ["country" => "fr" ] Into "foo.bar,country=fr"
*/
private function getFullNode()
{
Expand Down
Loading

0 comments on commit 212a61e

Please sign in to comment.