Skip to content

Commit

Permalink
Merge pull request #16 from alexbusu/simple-granularity
Browse files Browse the repository at this point in the history
SimpleGranularity: serialize as string
  • Loading branch information
dzoudzou authored Mar 30, 2017
2 parents 06bfd62 + 2d75abe commit 2f72d6e
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 12 deletions.
31 changes: 29 additions & 2 deletions src/Druid/Query/Aggregation/AbstractAggregationQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -55,6 +54,12 @@ abstract class AbstractAggregationQuery extends AbstractQuery
*/
private $granularity;

/**
* @var GranularityInterface
* @Serializer\Exclude
*/
private $granularitySafe;

/**
* @var array|AggregatorInterface[]
*/
Expand Down Expand Up @@ -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
*
Expand Down
5 changes: 4 additions & 1 deletion src/Druid/Query/Aggregation/GroupBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
Expand Down
32 changes: 28 additions & 4 deletions src/Druid/Query/Component/Granularity/SimpleGranularity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
}

/**
Expand All @@ -61,4 +77,12 @@ public function getGranularity()
{
return $this->granularity;
}

/**
* @return string
*/
public function __toString()
{
return (string)$this->granularity;
}
}
1 change: 1 addition & 0 deletions src/Druid/Query/Component/GranularityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
11 changes: 11 additions & 0 deletions src/Druid/Query/Component/StringComponentInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Druid\Query\Component;

interface StringComponentInterface
{
/**
* @return string
*/
public function __toString();
}
1 change: 0 additions & 1 deletion src/Druid/Query/QueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

namespace Druid\Query;


use Druid\Query\Exception\RequiredArgumentException;

/**
Expand Down
12 changes: 9 additions & 3 deletions src/Druid/QueryBuilder/AbstractAggregationQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

use Druid\Query\Component\DataSource\TableDataSource;
use Druid\Query\Component\DataSourceInterface;
use Druid\Query\Component\Granularity\SimpleGranularity;
use Druid\Query\Component\GranularityInterface;
use Druid\Query\Component\Interval\Interval;
use Druid\Query\Component\FilterInterface;
Expand Down Expand Up @@ -58,13 +59,18 @@ public function setDataSource($dataSource)
}

/**
* @param GranularityInterface $granularity
* @param string|GranularityInterface $granularity
*
* @return $this
*/
public function setGranularity(GranularityInterface $granularity)
public function setGranularity($granularity)
{
return $this->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;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Druid/QueryBuilder/TopNQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

/**
Expand Down

0 comments on commit 2f72d6e

Please sign in to comment.