1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-27 01: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', 'PhabricatorChartFunctionArgument' => 'applications/fact/chart/PhabricatorChartFunctionArgument.php',
'PhabricatorChartFunctionArgumentParser' => 'applications/fact/chart/PhabricatorChartFunctionArgumentParser.php', 'PhabricatorChartFunctionArgumentParser' => 'applications/fact/chart/PhabricatorChartFunctionArgumentParser.php',
'PhabricatorChartFunctionLabel' => 'applications/fact/chart/PhabricatorChartFunctionLabel.php', 'PhabricatorChartFunctionLabel' => 'applications/fact/chart/PhabricatorChartFunctionLabel.php',
'PhabricatorChartInterval' => 'applications/fact/chart/PhabricatorChartInterval.php',
'PhabricatorChartRenderingEngine' => 'applications/fact/engine/PhabricatorChartRenderingEngine.php', 'PhabricatorChartRenderingEngine' => 'applications/fact/engine/PhabricatorChartRenderingEngine.php',
'PhabricatorChartStackedAreaDataset' => 'applications/fact/chart/PhabricatorChartStackedAreaDataset.php', 'PhabricatorChartStackedAreaDataset' => 'applications/fact/chart/PhabricatorChartStackedAreaDataset.php',
'PhabricatorChatLogApplication' => 'applications/chatlog/application/PhabricatorChatLogApplication.php', 'PhabricatorChatLogApplication' => 'applications/chatlog/application/PhabricatorChatLogApplication.php',
@ -8685,6 +8686,7 @@ phutil_register_library_map(array(
'PhabricatorChartFunctionArgument' => 'Phobject', 'PhabricatorChartFunctionArgument' => 'Phobject',
'PhabricatorChartFunctionArgumentParser' => 'Phobject', 'PhabricatorChartFunctionArgumentParser' => 'Phobject',
'PhabricatorChartFunctionLabel' => 'Phobject', 'PhabricatorChartFunctionLabel' => 'Phobject',
'PhabricatorChartInterval' => 'Phobject',
'PhabricatorChartRenderingEngine' => 'Phobject', 'PhabricatorChartRenderingEngine' => 'Phobject',
'PhabricatorChartStackedAreaDataset' => 'PhabricatorChartDataset', 'PhabricatorChartStackedAreaDataset' => 'PhabricatorChartDataset',
'PhabricatorChatLogApplication' => 'PhabricatorApplication', '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() { public function getDomain() {
return array( $min = head_key($this->map);
head_key($this->map), $max = last_key($this->map);
last_key($this->map),
); return new PhabricatorChartInterval($min, $max);
} }
public function newInputValues(PhabricatorChartDataQuery $query) { public function newInputValues(PhabricatorChartDataQuery $query) {

View file

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

View file

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