@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 98 lines 2.2 kB view raw
1<?php 2 3final class PhabricatorChartDataQuery 4 extends Phobject { 5 6 private $limit; 7 private $minimumValue; 8 private $maximumValue; 9 10 /** 11 * @param int $minimum_value Epoch timestamp of the first input data 12 */ 13 public function setMinimumValue($minimum_value) { 14 $this->minimumValue = $minimum_value; 15 return $this; 16 } 17 18 public function getMinimumValue() { 19 return $this->minimumValue; 20 } 21 22 /** 23 * @param int $maximum_value Epoch timestamp of the last input data 24 */ 25 public function setMaximumValue($maximum_value) { 26 $this->maximumValue = $maximum_value; 27 return $this; 28 } 29 30 public function getMaximumValue() { 31 return $this->maximumValue; 32 } 33 34 /** 35 * @param int $limit Maximum amount of data points to handle. If there are 36 * more data points, some in-between data will be ignored. 37 */ 38 public function setLimit($limit) { 39 $this->limit = $limit; 40 return $this; 41 } 42 43 public function getLimit() { 44 return $this->limit; 45 } 46 47 /** 48 * Filter input data: Remove values lower resp. higher than the minimum 49 * resp. maximum values; remove some data if amount of data above the limit. 50 * 51 * @param array<int> $xv Epoch timestamps of unfitlered input data 52 * @return array<int> Epoch timestamps of fitlered input data 53 */ 54 public function selectInputValues(array $xv) { 55 $result = array(); 56 57 $x_min = $this->getMinimumValue(); 58 $x_max = $this->getMaximumValue(); 59 $limit = $this->getLimit(); 60 61 if ($x_min !== null) { 62 foreach ($xv as $key => $x) { 63 if ($x < $x_min) { 64 unset($xv[$key]); 65 } 66 } 67 } 68 69 if ($x_max !== null) { 70 foreach ($xv as $key => $x) { 71 if ($x > $x_max) { 72 unset($xv[$key]); 73 } 74 } 75 } 76 77 // If we have too many data points, throw away some of the data. 78 79 // TODO: This doesn't work especially well right now. 80 81 if ($limit !== null) { 82 $count = count($xv); 83 if ($count > $limit) { 84 $ii = 0; 85 $every = ceil($count / $limit); 86 foreach ($xv as $key => $x) { 87 $ii++; 88 if (($ii % $every) && ($ii != $count)) { 89 unset($xv[$key]); 90 } 91 } 92 } 93 } 94 95 return array_values($xv); 96 } 97 98}