Changeset View
Changeset View
Standalone View
Standalone View
src/applications/fact/chart/PhabricatorFactChartFunction.php
Show All 16 Lines | protected function newArguments() { | ||||
$parser->parseArgument($key_argument); | $parser->parseArgument($key_argument); | ||||
$fact = $this->getArgument('fact-key'); | $fact = $this->getArgument('fact-key'); | ||||
$this->fact = $fact; | $this->fact = $fact; | ||||
return $fact->getFunctionArguments(); | return $fact->getFunctionArguments(); | ||||
} | } | ||||
public function isImpulseFunction() { | |||||
return true; | |||||
} | |||||
public function loadData() { | public function loadData() { | ||||
$fact = $this->fact; | $fact = $this->fact; | ||||
$key_id = id(new PhabricatorFactKeyDimension()) | $key_id = id(new PhabricatorFactKeyDimension()) | ||||
->newDimensionID($fact->getKey()); | ->newDimensionID($fact->getKey()); | ||||
if (!$key_id) { | if (!$key_id) { | ||||
return; | return; | ||||
} | } | ||||
$table = $fact->newDatapoint(); | $table = $fact->newDatapoint(); | ||||
$conn = $table->establishConnection('r'); | $conn = $table->establishConnection('r'); | ||||
$table_name = $table->getTableName(); | $table_name = $table->getTableName(); | ||||
$data = queryfx_all( | $data = queryfx_all( | ||||
$conn, | $conn, | ||||
'SELECT value, epoch FROM %T WHERE keyID = %d ORDER BY epoch ASC', | 'SELECT value, epoch FROM %T WHERE keyID = %d ORDER BY epoch ASC', | ||||
$table_name, | $table_name, | ||||
$key_id); | $key_id); | ||||
if (!$data) { | if (!$data) { | ||||
return; | return; | ||||
} | } | ||||
$points = array(); | $map = array(); | ||||
foreach ($data as $row) { | |||||
$sum = 0; | $value = (int)$row['value']; | ||||
foreach ($data as $key => $row) { | $epoch = (int)$row['epoch']; | ||||
$sum += (int)$row['value']; | |||||
$points[] = array( | |||||
'x' => (int)$row['epoch'], | |||||
'y' => $sum, | |||||
); | |||||
} | |||||
$this->datapoints = $points; | |||||
} | |||||
public function getDatapoints(PhabricatorChartDataQuery $query) { | |||||
$points = $this->datapoints; | |||||
if (!$points) { | |||||
return array(); | |||||
} | |||||
$x_min = $query->getMinimumValue(); | |||||
$x_max = $query->getMaximumValue(); | |||||
$limit = $query->getLimit(); | |||||
if ($x_min !== null) { | if (!isset($map[$epoch])) { | ||||
foreach ($points as $key => $point) { | $map[$epoch] = 0; | ||||
if ($point['x'] < $x_min) { | |||||
unset($points[$key]); | |||||
} | |||||
} | |||||
} | } | ||||
if ($x_max !== null) { | $map[$epoch] += $value; | ||||
foreach ($points as $key => $point) { | |||||
if ($point['x'] > $x_max) { | |||||
unset($points[$key]); | |||||
} | |||||
} | |||||
} | } | ||||
// If we have too many data points, throw away some of the data. | $points = array(); | ||||
if ($limit !== null) { | foreach ($map as $x => $y) { | ||||
$count = count($points); | $points[] = array( | ||||
if ($count > $limit) { | 'x' => $x, | ||||
$ii = 0; | 'y' => $y, | ||||
$every = ceil($count / $limit); | ); | ||||
foreach ($points as $key => $point) { | |||||
$ii++; | |||||
if (($ii % $every) && ($ii != $count)) { | |||||
unset($points[$key]); | |||||
} | |||||
} | |||||
} | |||||
} | } | ||||
return $points; | $this->datapoints = $points; | ||||
} | } | ||||
public function hasDomain() { | protected function newDatapoints(PhabricatorChartDataQuery $query) { | ||||
return true; | return $query->selectDatapoints($this->datapoints); | ||||
} | } | ||||
public function getDomain() { | |||||
// TODO: We can examine the data to fit a better domain. | |||||
$now = PhabricatorTime::getNow(); | |||||
return array($now - phutil_units('90 days in seconds'), $now); | |||||
} | |||||
} | } |