forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhabricatorFactChartController.php
90 lines (74 loc) · 2.22 KB
/
PhabricatorFactChartController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
final class PhabricatorFactChartController extends PhabricatorFactController {
public function processRequest() {
$request = $this->getRequest();
$user = $request->getUser();
$table = new PhabricatorFactRaw();
$conn_r = $table->establishConnection('r');
$table_name = $table->getTableName();
$series = $request->getStr('y1');
$specs = PhabricatorFactSpec::newSpecsForFactTypes(
PhabricatorFactEngine::loadAllEngines(),
array($series));
$spec = idx($specs, $series);
$data = queryfx_all(
$conn_r,
'SELECT valueX, epoch FROM %T WHERE factType = %s ORDER BY epoch ASC',
$table_name,
$series);
$points = array();
$sum = 0;
foreach ($data as $key => $row) {
$sum += (int)$row['valueX'];
$points[(int)$row['epoch']] = $sum;
}
if (!$points) {
// NOTE: Raphael crashes Safari if you hand it series with no points.
throw new Exception("No data to show!");
}
// Limit amount of data passed to browser.
$count = count($points);
$limit = 2000;
if ($count > $limit) {
$i = 0;
$every = ceil($count / $limit);
foreach ($points as $epoch => $sum) {
$i++;
if ($i % $every && $i != $count) {
unset($points[$epoch]);
}
}
}
$x = array_keys($points);
$y = array_values($points);
$id = celerity_generate_unique_node_id();
$chart = phutil_tag(
'div',
array(
'id' => $id,
'style' => 'border: 1px solid #6f6f6f; '.
'margin: 1em 2em; '.
'background: #ffffff; '.
'height: 400px; ',
),
'');
require_celerity_resource('raphael-core');
require_celerity_resource('raphael-g');
require_celerity_resource('raphael-g-line');
Javelin::initBehavior('line-chart', array(
'hardpoint' => $id,
'x' => array($x),
'y' => array($y),
'xformat' => 'epoch',
'colors' => array('#0000ff'),
));
$panel = new AphrontPanelView();
$panel->setHeader('Count of '.$spec->getName());
$panel->appendChild($chart);
return $this->buildStandardPageResponse(
$panel,
array(
'title' => 'Chart',
));
}
}