diff --git a/src/Druid/Query/Aggregation/AbstractAggregationQuery.php b/src/Druid/Query/Aggregation/AbstractAggregationQuery.php index b1e650f..30a0689 100644 --- a/src/Druid/Query/Aggregation/AbstractAggregationQuery.php +++ b/src/Druid/Query/Aggregation/AbstractAggregationQuery.php @@ -35,9 +35,8 @@ use Druid\Query\Component\FilterInterface; use Druid\Query\Component\GranularityInterface; use Druid\Query\Component\IntervalInterface; -use Druid\Query\Component\LimitSpecInterface; use Druid\Query\Component\PostAggregatorInterface; -use Druid\Query\QueryInterface; +use Druid\Query\Component\StringComponentInterface; use JMS\Serializer\Annotation as Serializer; /** @@ -55,6 +54,12 @@ abstract class AbstractAggregationQuery extends AbstractQuery */ private $granularity; + /** + * @var GranularityInterface + * @Serializer\Exclude + */ + private $granularitySafe; + /** * @var array|AggregatorInterface[] */ @@ -104,6 +109,28 @@ public function getGranularity() return $this->granularity; } + /** + * @Serializer\PreSerialize + */ + public function preSerialize() + { + if ($this->granularity instanceof StringComponentInterface) { + $this->granularitySafe = $this->granularity; + $this->granularity = (string)$this->granularity; + } + } + + /** + * @Serializer\PostSerialize + */ + public function postSerialize() + { + if ($this->granularitySafe) { + $this->granularity = $this->granularitySafe; + $this->granularitySafe = null; + } + } + /** * @param GranularityInterface $granularity * diff --git a/src/Druid/Query/Aggregation/GroupBy.php b/src/Druid/Query/Aggregation/GroupBy.php index e40c69d..fe3590f 100644 --- a/src/Druid/Query/Aggregation/GroupBy.php +++ b/src/Druid/Query/Aggregation/GroupBy.php @@ -29,8 +29,8 @@ namespace Druid\Query\Aggregation; - use Druid\Query\Component\DimensionSpecInterface; +use Druid\Query\Component\Granularity\SimpleGranularity; use Druid\Query\Component\HavingInterface; use Druid\Query\Component\LimitSpecInterface; use Druid\Query\Exception\RequiredArgumentException; @@ -135,6 +135,9 @@ public function validate() if (!$this->getGranularity()) { throw new RequiredArgumentException('\'granularity\' is a required parameter'); } + if ($this->getGranularity() instanceof SimpleGranularity && !$this->getGranularity()->getGranularity()) { + throw new RequiredArgumentException('invalid \'granularity\' parameter'); + } if (!$this->getIntervals()) { throw new RequiredArgumentException('\'intervals\' is a required parameter'); } diff --git a/src/Druid/Query/Component/Granularity/SimpleGranularity.php b/src/Druid/Query/Component/Granularity/SimpleGranularity.php index be9c10a..c9cbceb 100644 --- a/src/Druid/Query/Component/Granularity/SimpleGranularity.php +++ b/src/Druid/Query/Component/Granularity/SimpleGranularity.php @@ -31,12 +31,27 @@ use Druid\Query\Component\AbstractTypedComponent; use Druid\Query\Component\GranularityInterface; +use Druid\Query\Component\StringComponentInterface; /** * Class SimpleGranularity. + * @link http://druid.io/docs/latest/querying/granularities.html */ -class SimpleGranularity extends AbstractTypedComponent implements GranularityInterface +class SimpleGranularity extends AbstractTypedComponent implements GranularityInterface, StringComponentInterface { + const ALL = "all"; + const NONE = "none"; + const SECOND = "second"; + const MINUTE = "minute"; + const FIFTEEN_MINUTE = "fifteen_minute"; + const THIRTY_MINUTE = "thirty_minute"; + const HOUR = "hour"; + const DAY = "day"; + const WEEK = "week"; + const MONTH = "month"; + const QUARTER = "quarter"; + const YEAR = "year"; + /** * @var string */ @@ -49,9 +64,10 @@ class SimpleGranularity extends AbstractTypedComponent implements GranularityInt */ public function __construct($granularity) { - $this->granularity = $granularity; - - parent::__construct($granularity); + if (defined('self::' . strtoupper($granularity))) { + $this->granularity = (string)$granularity; + } + parent::__construct(self::TYPE_SIMPLE); } /** @@ -61,4 +77,12 @@ public function getGranularity() { return $this->granularity; } + + /** + * @return string + */ + public function __toString() + { + return (string)$this->granularity; + } } diff --git a/src/Druid/Query/Component/GranularityInterface.php b/src/Druid/Query/Component/GranularityInterface.php index a1b9f50..4803251 100644 --- a/src/Druid/Query/Component/GranularityInterface.php +++ b/src/Druid/Query/Component/GranularityInterface.php @@ -34,6 +34,7 @@ */ interface GranularityInterface extends TypedInterface, ComponentInterface { + const TYPE_SIMPLE = 'simple'; const TYPE_PERIOD = 'period'; const TYPE_ALL = 'all'; const TYPE_NONE = 'none'; diff --git a/src/Druid/Query/Component/StringComponentInterface.php b/src/Druid/Query/Component/StringComponentInterface.php new file mode 100644 index 0000000..9da7b95 --- /dev/null +++ b/src/Druid/Query/Component/StringComponentInterface.php @@ -0,0 +1,11 @@ +addComponent('granularity', $granularity); + if ($granularity instanceof GranularityInterface) { + return $this->addComponent('granularity', $granularity); + } elseif (is_string($granularity)) { + return $this->addComponent('granularity', new SimpleGranularity($granularity)); + } + return $this; } /** diff --git a/src/Druid/QueryBuilder/TopNQueryBuilder.php b/src/Druid/QueryBuilder/TopNQueryBuilder.php index 4ca4bc0..529ec93 100644 --- a/src/Druid/QueryBuilder/TopNQueryBuilder.php +++ b/src/Druid/QueryBuilder/TopNQueryBuilder.php @@ -80,7 +80,10 @@ public function setMetric(MetricInterface $metric) */ public function setThreshold($threshold) { - return $this->addComponent('threshold', $threshold instanceof ThresholdInterface ? $threshold : new Threshold((int)$threshold)); + return $this->addComponent( + 'threshold', + $threshold instanceof ThresholdInterface ? $threshold : new Threshold((int)$threshold) + ); } /**