Skip to content

Commit

Permalink
Add erros to stats
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian committed Nov 21, 2015
1 parent 40c60aa commit 6154e7e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
37 changes: 37 additions & 0 deletions lib/Transport/Statistics.php
Expand Up @@ -41,6 +41,21 @@ public function resource($path)
$this->count('stats:resources', $path, array('path' => $path));
}

public function error($e)
{
$exceptionClass = get_class($e);

if ($this->enabled) {
$date = date('Y-m-d');
$prefix = "stats:errors";
$key = "$prefix:$date";
$this->redis->sadd($prefix, $key);
$this->redis->incr($key);
}

$this->count('stats:exceptions', $exceptionClass, array('exception' => $exceptionClass));
}

protected function count($prefix, $id, $data)
{
if ($this->enabled) {
Expand Down Expand Up @@ -70,6 +85,23 @@ public function getCalls()
return $calls;
}

public function getErrors()
{
$result = $this->redis->sort("stats:errors", array(
'get' => array('#', '*'),
'sort' => 'ASC',
'alpha' => true
));

// regroup
$errors = array();
foreach (array_chunk($result, 2) as $values) {
$errors[substr($values[0], 13, 10)] = $values[1];
}

return $errors;
}

public function getTopResources()
{
return $this->top('stats:resources', array('path', 'calls'));
Expand All @@ -80,6 +112,11 @@ public function getTopStations()
return $this->top('stats:stations', array('name', 'x', 'y', 'calls'));
}

public function getTopExceptions()
{
return $this->top('stats:exceptions', array('exception', 'calls'));
}

protected function top($key, $fields)
{
$result = $this->redis->sort($key, array(
Expand Down
13 changes: 11 additions & 2 deletions views/stats.twig
Expand Up @@ -17,13 +17,15 @@

<script type="text/javascript">
$(function () {
var g = new Dygraph(document.getElementById("graphdiv"), "Date,API calls\n" + "{{ data }}", {
var g = new Dygraph(document.getElementById("graphdiv"), "Date,API calls,Errors\n" + "{{ data }}", {
width: 595,
fillGraph: true,
includeZero: true,
colors: ['#008000'],
colors: ['#008000', '#800000'],
yAxisLabelWidth: 45,
axisLabelFontSize: 13,
drawPoints: true,
pointSize: 1.5,
labelsDiv: document.getElementById("labels"),
axes: {
x: {
Expand Down Expand Up @@ -71,6 +73,13 @@
</div>
</div>
</article>

<h2 title="Top 5 since 22. November 2015">Errors</h2>
<ul class="unstyled">
{% for error in errors %}
<li><span class="value">{{ error.exception }}</span> <span class="count">{{ error.calls }}</span></li>
{% endfor %}
</ul>
</div>
</body>
</html>
2 changes: 2 additions & 0 deletions web/api.php
Expand Up @@ -53,6 +53,8 @@
// Exception handler
$app->error(function (\Exception $e, $code) use ($app) {

$app['stats']->error($e);

$errors = array(array('message' => $e->getMessage()));

$result = array('errors' => $errors);
Expand Down
21 changes: 11 additions & 10 deletions web/stats.php
Expand Up @@ -32,17 +32,24 @@
$app->get('/', function(Request $request) use ($app) {

$calls = $app['stats']->getCalls();
$errors = $app['stats']->getErrors();

// transform to comma and new line separated list
$data = array();
foreach (array_slice($calls, -30) as $date => $value) {
$data[] = $date . ',' . ($value ?: 0);
$data[$date] = $date . ',' . ($value ?: 0);
}
foreach (array_slice($errors, -30) as $date => $value) {
if (isset($data[$date])) {
$data[$date] .= ',' . ($value ?: 0);
}
}
$data = implode('\n', $data);

// get top resources and stations
// get top resources, stations and errors
$resources = $app['stats']->getTopResources();
$stations = $app['stats']->getTopStations();
$errors = $app['stats']->getTopExceptions();

// CSV response
if ($request->get('format') == 'csv') {
Expand All @@ -58,18 +65,12 @@
return $app->json(array('calls' => $calls));
}

// transform to comma and new line separated list
$data = array();
foreach (array_slice($calls, -30) as $date => $value) {
$data[] = $date . ',' . ($value ?: 0);
}
$data = implode('\n', $data);

return $app['twig']->render('stats.twig', array(
'data' => $data,
'calls' => $calls,
'resources' => $resources,
'stations' => $stations,
'errors' => $errors,
));
});
} else {
Expand All @@ -80,4 +81,4 @@


// run
$app->run();
$app->run();

0 comments on commit 6154e7e

Please sign in to comment.