1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-22 23:02:42 +01:00

Wrap "<min, max>" chart domain pairs in an "Interval" class

Summary: Ref T13279. Slightly simplify domain handling by putting all the "[x, y]" stuff in an Interval class. I'm planning to do something similar for ranges next, so this should make that easierr.

Test Plan: Viewed chart, saw same chart as before.

Reviewers: amckinley

Reviewed By: amckinley

Subscribers: yelirekim

Maniphest Tasks: T13279

Differential Revision: https://secure.phabricator.com/D20502
This commit is contained in:
epriestley 2019-05-08 08:40:30 -07:00
parent a80426b339
commit e90360c289
5 changed files with 86 additions and 68 deletions

View file

@ -2669,6 +2669,7 @@ phutil_register_library_map(array(
'PhabricatorChartFunctionArgument' => 'applications/fact/chart/PhabricatorChartFunctionArgument.php',
'PhabricatorChartFunctionArgumentParser' => 'applications/fact/chart/PhabricatorChartFunctionArgumentParser.php',
'PhabricatorChartFunctionLabel' => 'applications/fact/chart/PhabricatorChartFunctionLabel.php',
'PhabricatorChartInterval' => 'applications/fact/chart/PhabricatorChartInterval.php',
'PhabricatorChartRenderingEngine' => 'applications/fact/engine/PhabricatorChartRenderingEngine.php',
'PhabricatorChartStackedAreaDataset' => 'applications/fact/chart/PhabricatorChartStackedAreaDataset.php',
'PhabricatorChatLogApplication' => 'applications/chatlog/application/PhabricatorChatLogApplication.php',
@ -8685,6 +8686,7 @@ phutil_register_library_map(array(
'PhabricatorChartFunctionArgument' => 'Phobject',
'PhabricatorChartFunctionArgumentParser' => 'Phobject',
'PhabricatorChartFunctionLabel' => 'Phobject',
'PhabricatorChartInterval' => 'Phobject',
'PhabricatorChartRenderingEngine' => 'Phobject',
'PhabricatorChartStackedAreaDataset' => 'PhabricatorChartDataset',
'PhabricatorChatLogApplication' => 'PhabricatorApplication',

View file

@ -0,0 +1,62 @@
<?php
final class PhabricatorChartInterval
extends Phobject {
private $min;
private $max;
public function __construct($min, $max) {
$this->min = $min;
$this->max = $max;
}
public static function newFromIntervalList(array $intervals) {
$min = null;
$max = null;
foreach ($intervals as $interval) {
if ($interval === null) {
continue;
}
$interval_min = $interval->getMin();
if ($interval_min !== null) {
if ($min === null) {
$min = $interval_min;
} else {
$min = min($min, $interval_min);
}
}
$interval_max = $interval->getMax();
if ($interval_max !== null) {
if ($max === null) {
$max = $interval_max;
} else {
$max = max($max, $interval_max);
}
}
}
return new self($min, $max);
}
public function setMin($min) {
$this->min = $min;
return $this;
}
public function getMin() {
return $this->min;
}
public function setMax($max) {
$this->max = $max;
return $this;
}
public function getMax() {
return $this->max;
}
}

View file

@ -73,10 +73,10 @@ final class PhabricatorFactChartFunction
}
public function getDomain() {
return array(
head_key($this->map),
last_key($this->map),
);
$min = head_key($this->map);
$max = last_key($this->map);
return new PhabricatorChartInterval($min, $max);
}
public function newInputValues(PhabricatorChartDataQuery $query) {

View file

@ -4,37 +4,12 @@ abstract class PhabricatorHigherOrderChartFunction
extends PhabricatorChartFunction {
public function getDomain() {
$minv = array();
$maxv = array();
$domains = array();
foreach ($this->getFunctionArguments() as $function) {
$domain = $function->getDomain();
if ($domain !== null) {
list($min, $max) = $domain;
if ($min !== null) {
$minv[] = $min;
}
if ($max !== null) {
$maxv[] = $max;
}
}
$domains[] = $function->getDomain();
}
if (!$minv && !$maxv) {
return null;
}
$min = null;
$max = null;
if ($minv) {
$min = min($minv);
}
if ($maxv) {
$max = max($maxv);
}
return array($min, $max);
return PhabricatorChartInterval::newFromIntervalList($domains);
}
public function newInputValues(PhabricatorChartDataQuery $query) {

View file

@ -133,15 +133,15 @@ final class PhabricatorChartRenderingEngine
$subfunction->loadData();
}
list($domain_min, $domain_max) = $this->getDomain($functions);
$domain = $this->getDomain($functions);
$axis = id(new PhabricatorChartAxis())
->setMinimumValue($domain_min)
->setMaximumValue($domain_max);
->setMinimumValue($domain->getMin())
->setMaximumValue($domain->getMax());
$data_query = id(new PhabricatorChartDataQuery())
->setMinimumValue($domain_min)
->setMaximumValue($domain_max)
->setMinimumValue($domain->getMin())
->setMaximumValue($domain->getMax())
->setLimit(2000);
$wire_datasets = array();
@ -155,8 +155,8 @@ final class PhabricatorChartRenderingEngine
$chart_data = array(
'datasets' => $wire_datasets,
'xMin' => $domain_min,
'xMax' => $domain_max,
'xMin' => $domain->getMin(),
'xMax' => $domain->getMax(),
'yMin' => $y_min,
'yMax' => $y_max,
);
@ -165,46 +165,25 @@ final class PhabricatorChartRenderingEngine
}
private function getDomain(array $functions) {
$domain_min_list = null;
$domain_max_list = null;
$domains = array();
foreach ($functions as $function) {
$domain = $function->getDomain();
list($function_min, $function_max) = $domain;
if ($function_min !== null) {
$domain_min_list[] = $function_min;
}
if ($function_max !== null) {
$domain_max_list[] = $function_max;
}
$domains[] = $function->getDomain();
}
$domain_min = null;
$domain_max = null;
if ($domain_min_list) {
$domain_min = min($domain_min_list);
}
if ($domain_max_list) {
$domain_max = max($domain_max_list);
}
$domain = PhabricatorChartInterval::newFromIntervalList($domains);
// If we don't have any domain data from the actual functions, pick a
// plausible domain automatically.
if ($domain_max === null) {
$domain_max = PhabricatorTime::getNow();
if ($domain->getMax() === null) {
$domain->setMax(PhabricatorTime::getNow());
}
if ($domain_min === null) {
$domain_min = $domain_max - phutil_units('365 days in seconds');
if ($domain->getMin() === null) {
$domain->setMin($domain->getMax() - phutil_units('365 days in seconds'));
}
return array($domain_min, $domain_max);
return $domain;
}
}