Skip to content

Commit 049c163

Browse files
authored
Merge pull request #7 from EdisonLabs/summary
Added base date and config as constructor parameters
2 parents fb22400 + 8da1bee commit 049c163

11 files changed

+192
-192
lines changed

README.md

Lines changed: 13 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,16 @@ The Metrics Collector is a simple library that provides base classes to easily e
77

88
## Usage
99

10-
This library does not provide any metrics by default. To create your own metrics, [create a Composer package](https://getcomposer.org/doc/01-basic-usage.md) and then add a dependency to this package:
10+
This library does not provide any metrics by default. To create new metrics, [create a Composer package](https://getcomposer.org/doc/01-basic-usage.md) and then add a dependency to this package:
1111

1212
```
1313
composer require edisonlabs/metrics
1414
```
1515

16-
Now create your metrics classes extending `edisonlabs/metrics` classes.
16+
Now create the metrics classes extending `edisonlabs/metrics` classes.
1717

18-
In the following example, we will create three metrics:
19-
- Total number of files that a location has
20-
- Total number of PHP files
21-
- Percentage of PHP files
18+
Example: Number of PHP files.
2219

23-
Create number of files metric:
24-
```php
25-
// src/EdisonLabs/Metric/NumberOfFiles.php
26-
27-
namespace EdisonLabs\Metric;
28-
29-
use EdisonLabs\Metrics\Metric\AbstractMetricBase;
30-
31-
class NumberOfFiles extends AbstractMetricBase
32-
{
33-
public function getName()
34-
{
35-
return 'Number of files';
36-
}
37-
38-
public function getDescription()
39-
{
40-
return 'The total number of files';
41-
}
42-
43-
public function getMetric()
44-
{
45-
// Put the logic to calculate the total here.
46-
// ..
47-
48-
// Random example.
49-
return rand(10, 100);
50-
}
51-
}
52-
```
53-
54-
Create number of PHP files metric:
5520
```php
5621
// src/EdisonLabs/Metric/NumberOfPhpFiles.php
5722

@@ -82,34 +47,7 @@ class NumberOfPhpFiles extends AbstractMetricBase
8247
}
8348
```
8449

85-
Create the percentage of PHP files metric:
86-
```php
87-
// src/EdisonLabs/Metric/PercentageOfPhpFiles.php
88-
89-
namespace EdisonLabs\Metric;
90-
91-
use EdisonLabs\Metrics\Metric\AbstractMetricPercentage;
92-
93-
class PercentageOfPhpFiles extends AbstractMetricPercentage
94-
{
95-
public function __construct(NumberOfFiles $total, NumberOfPhpFiles $count)
96-
{
97-
parent::__construct($total, $count);
98-
}
99-
100-
public function getName()
101-
{
102-
return 'PHP files (%)';
103-
}
104-
105-
public function getDescription()
106-
{
107-
return 'Percentage of PHP files';
108-
}
109-
}
110-
```
111-
112-
Configure the autoload on the `composer.json` file:
50+
Configure the autoload in `composer.json`:
11351
```json
11452
"autoload": {
11553
"psr-4": {
@@ -134,7 +72,10 @@ There are two ways to collect the metrics: programmatically and by command-line.
13472

13573
use EdisonLabs\Metrics\Collector;
13674

137-
$collector = new Collector();
75+
$date = strtotime('now');
76+
$config = array();
77+
78+
$collector = new Collector($date, $config);
13879
$metrics = $collector->getMetrics();
13980
```
14081

@@ -183,10 +124,13 @@ class SqLite extends AbstractMetricDatastore
183124
use EdisonLabs\Metrics\Collector;
184125
use EdisonLabs\Metrics\DatastoreHandler;
185126

186-
$collector = new Collector();
127+
$date = strtotime('now');
128+
$config = array();
129+
130+
$collector = new Collector($date, $config);
187131
$metrics = $collector->getMetrics();
188132

189-
$datastoreHandler = new DatastoreHandler();
133+
$datastoreHandler = new DatastoreHandler($date, $config);
190134
$datastore = $datastoreHandler->getDatastoreByName('SQLite');
191135
$datastore->setMetrics($metrics);
192136
$datastore->save();

config/services.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
if ($finder->count() !== 0) {
1818
$definition = new Definition();
1919
$definition
20+
->addArgument('%metrics.date%')
21+
->addArgument('%metrics.config%')
2022
->setAutowired(true)
2123
->setAutoconfigured(true)
2224
->setPublic(true);

src/Collector.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ class Collector
1212

1313
const METRICS_NAMESPACE = 'EdisonLabs\Metric';
1414

15+
/**
16+
* @var string
17+
*/
18+
protected $date;
19+
1520
/**
1621
* @var array
1722
*/
18-
protected $metrics = array();
23+
protected $config;
1924

2025
/**
2126
* @var array
@@ -25,30 +30,29 @@ class Collector
2530
/**
2631
* @var array
2732
*/
28-
protected $config;
33+
protected $metrics = array();
2934

3035
/**
3136
* Collector constructor.
3237
*
33-
* @param array $groups
34-
* A list containing the groups to filter for.
35-
* @param array $config
36-
* The custom config array.
38+
* @param string $date A date timestamp to collect for.
39+
* @param array $config The custom config array.
40+
* @param array $groups A list containing the groups to filter for.
3741
*
3842
* @throws \Exception
3943
*/
40-
public function __construct(array $groups = array(), array $config = array())
44+
public function __construct($date, array $config = array(), array $groups = array())
4145
{
4246
$this->groups = $groups;
4347
$this->config = $config;
48+
$this->date = $date;
4449
$this->setMetrics();
4550
}
4651

4752
/**
4853
* Register a metric to be collected.
4954
*
50-
* @param AbstractMetricBase $metric
51-
* The Metric object.
55+
* @param AbstractMetricBase $metric The Metric object.
5256
*/
5357
public function setMetric(AbstractMetricBase $metric)
5458
{
@@ -58,8 +62,7 @@ public function setMetric(AbstractMetricBase $metric)
5862
/**
5963
* Returns the available metrics.
6064
*
61-
* @return array
62-
* An array containing the metrics objects.
65+
* @return array An array containing the metrics objects.
6366
*/
6467
public function getMetrics()
6568
{
@@ -73,7 +76,7 @@ public function getMetrics()
7376
*/
7477
protected function setMetrics()
7578
{
76-
$containerBuilder = new ContainerBuilder();
79+
$containerBuilder = new ContainerBuilder($this->date, $this->config);
7780
$containerBuilder = $containerBuilder->getContainerBuilder();
7881

7982
$services = $containerBuilder->getServiceIds();
@@ -83,6 +86,7 @@ protected function setMetrics()
8386
continue;
8487
}
8588

89+
/** @var \EdisonLabs\Metrics\Metric\MetricInterface $metric */
8690
$metric = $containerBuilder->get($serviceName);
8791

8892
// Sanity check by class type.
@@ -95,7 +99,6 @@ protected function setMetrics()
9599
continue;
96100
}
97101

98-
$metric->setConfig($this->config);
99102
$this->setMetric($metric);
100103
}
101104
}

src/Command/MetricsCommand.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@ protected function initialize(InputInterface $input, OutputInterface $output)
5151

5252
$this->config = $config;
5353

54+
// Gets the date.
55+
$date = $input->getOption('date');
56+
if (!$date = strtotime($date)) {
57+
throw new RuntimeException('Invalid date string');
58+
}
59+
5460
// Sets datastore handler.
55-
$this->datastoreHandler = new DatastoreHandler($this->config);
61+
$this->datastoreHandler = new DatastoreHandler($date, $this->config);
5662

5763
if ($input->getOption('list-datastores')) {
5864
return;
@@ -65,7 +71,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
6571
}
6672

6773
// Gets metrics.
68-
$collector = new Collector($groups, $this->config);
74+
$collector = new Collector($date, $this->config, $groups);
6975
$this->metrics = $collector->getMetrics();
7076
}
7177

@@ -83,18 +89,17 @@ protected function configure()
8389
->addOption('save', null, InputOption::VALUE_REQUIRED, 'Save the metrics to target datastores')
8490
->addOption('no-messages', null, InputOption::VALUE_NONE, 'Do not output messages')
8591
->addOption('groups', null, InputOption::VALUE_REQUIRED, 'Collect metrics from specific groups only', array())
86-
->addOption('config', null, InputOption::VALUE_REQUIRED, 'Pass custom config to the metrics, which can be a file or a string containing JSON format');
92+
->addOption('config', null, InputOption::VALUE_REQUIRED, 'Pass custom config to the metrics, which can be a file or a string containing JSON format')
93+
->addOption('date', null, InputOption::VALUE_REQUIRED, 'Collect metrics from a specific date. Pass a string supported by strtotime()', 'now')
8794
;
8895
}
8996

9097
/**
9198
* Converts and returns the config parameter value to array.
9299
*
93-
* @param string $config
94-
* The config string.
100+
* @param string $config The config string.
95101
*
96-
* @return array
97-
* The config array.
102+
* @return array The config array.
98103
*/
99104
protected function getConfigArray($config)
100105
{
@@ -120,8 +125,7 @@ protected function getConfigArray($config)
120125
/**
121126
* Outputs the metrics on Json format.
122127
*
123-
* @param OutputInterface $output
124-
* Console output object.
128+
* @param OutputInterface $output Console output object.
125129
*/
126130
protected function outputMetricsJson(OutputInterface $output)
127131
{
@@ -145,12 +149,9 @@ protected function outputMetricsJson(OutputInterface $output)
145149
/**
146150
* Outputs a table.
147151
*
148-
* @param array $header
149-
* An array containing the header columns.
150-
* @param array $rows
151-
* An array containing the table rows.
152-
* @param OutputInterface $output
153-
* Console output object.
152+
* @param array $header An array containing the header columns.
153+
* @param array $rows An array containing the table rows.
154+
* @param OutputInterface $output Console output object.
154155
*/
155156
protected function outputTable(array $header, array $rows, OutputInterface $output)
156157
{
@@ -166,8 +167,7 @@ protected function outputTable(array $header, array $rows, OutputInterface $outp
166167
/**
167168
* Outputs the available datastores on table format.
168169
*
169-
* @param OutputInterface $output
170-
* Console output object.
170+
* @param OutputInterface $output Console output object.
171171
*/
172172
protected function outputDatastoresTable(OutputInterface $output)
173173
{
@@ -188,8 +188,7 @@ protected function outputDatastoresTable(OutputInterface $output)
188188
/**
189189
* Outputs the metrics on table format.
190190
*
191-
* @param OutputInterface $output
192-
* Console output object.
191+
* @param OutputInterface $output Console output object.
193192
*/
194193
protected function outputMetricsTable(OutputInterface $output)
195194
{

src/ContainerBuilder.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ class ContainerBuilder
1818

1919
/**
2020
* ContainerBuilder constructor.
21+
*
22+
* @param int $date The date (timestamp) of the metrics.
23+
* @param array $config The custom config array.
24+
*
25+
* @throws \Exception
2126
*/
22-
public function __construct()
27+
public function __construct($date = null, array $config = array())
2328
{
2429
$containerBuilder = new SymfonyContainerBuilder();
2530
$loader = new PhpFileLoader($containerBuilder, new FileLocator(__DIR__));
2631
$loader->load(self::SERVICES_PHP_FILE);
32+
33+
$containerBuilder->setParameter('metrics.date', $date);
34+
$containerBuilder->setParameter('metrics.config', $config);
2735
$containerBuilder->compile();
2836

2937
$this->containerBuilder = $containerBuilder;
@@ -32,8 +40,7 @@ public function __construct()
3240
/**
3341
* Returns the container builder instance.
3442
*
35-
* @return SymfonyContainerBuilder
36-
* Container builder instance.
43+
* @return SymfonyContainerBuilder Container builder instance.
3744
*/
3845
public function getContainerBuilder()
3946
{

0 commit comments

Comments
 (0)