Skip to content

Commit

Permalink
Merge pull request #3 from thomask/master
Browse files Browse the repository at this point in the history
Add Timeseries type aggregations
  • Loading branch information
tmihalicka authored Aug 26, 2016
2 parents 30357e9 + 5b8bd78 commit 4cbbb10
Show file tree
Hide file tree
Showing 12 changed files with 627 additions and 236 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Put the following into your composer.json

## Current State

Currently this driver supports only **GroupBy** aggregation type which is tested on out production environment.
Currently this driver supports **GroupBy** and **Timeseries** aggregation types.
Everybody is welcome to create pull requests to implement some of the missing things.

Also, some unit tests are bound to running on our internal Druid instance, there is plan to change it to docker container
Expand Down Expand Up @@ -48,7 +48,7 @@ $druid = new Druid(
]
);

$queryBuilder = $druid->createQueryBuilder(AbstractQuery::TYPE_GROUP_BY);
$queryBuilder = $druid->createQueryBuilder(AbstractQuery::TYPE_GROUP_BY); // or AbstractQuery::TYPE_TIMESERIES

$queryBuilder->setDataSource('kpi_registrations_v1');
$queryBuilder->addInterval(new \DateTime('2000-01-01'), new \DateTime());
Expand All @@ -60,6 +60,7 @@ $queryBuilder->addAggregator($queryBuilder->aggregator()->count('count_rows'));
$queryBuilder->addAggregator($queryBuilder->aggregator()->doubleSum('sum_rows', 'event_count_metric'));
$queryBuilder->addAggregator($queryBuilder->aggregator()->hyperUnique('registrations', 'registrations'));

// Only include for GroupBy queries
$queryBuilder->addDimension('project', 'project');

$queryBuilder->addPostAggregator(
Expand Down Expand Up @@ -93,7 +94,6 @@ PHP Mess Detector [PHPMD](http://phpmd.org/)
1. **Query types**
* Aggregation queries
* TopN
* Timeseries
* Metadata Queries
* Time Boundary
* Segment Metadata
Expand Down
5 changes: 5 additions & 0 deletions src/Druid/Druid.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Druid\Query\QueryInterface;
use Druid\QueryBuilder\AbstractQueryBuilder;
use Druid\QueryBuilder\GroupByQueryBuilder;
use Druid\QueryBuilder\TimeseriesQueryBuilder;

/**
* Class Connection.
Expand Down Expand Up @@ -103,6 +104,10 @@ public function createQueryBuilder($queryType)
switch ($queryType) {
case AbstractQuery::TYPE_GROUP_BY:
return new GroupByQueryBuilder();
break;
case AbstractQuery::TYPE_TIMESERIES:
return new TimeseriesQueryBuilder();
break;
default:
throw new \RuntimeException(
sprintf('Invalid query type %s', $queryType)
Expand Down
3 changes: 2 additions & 1 deletion src/Druid/Query/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
/**
* Class AbstractQuery.
*/
abstract class AbstractQuery
abstract class AbstractQuery implements QueryInterface
{
const TYPE_GROUP_BY = 'groupBy';
const TYPE_TIMESERIES = 'timeseries';

/**
* @var string
Expand Down
198 changes: 198 additions & 0 deletions src/Druid/Query/Aggregation/AbstractAggregationQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

/*
* Copyright (c) 2016 PIXEL FEDERATION, s.r.o.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the PIXEL FEDERATION, s.r.o. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL PIXEL FEDERATION, s.r.o. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace Druid\Query\Aggregation;

use Druid\Query\AbstractQuery;
use Druid\Query\Component\AggregatorInterface;
use Druid\Query\Component\DataSourceInterface;
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 JMS\Serializer\Annotation as Serializer;

/**
* Class AbstractAggregationQuery.
*/
abstract class AbstractAggregationQuery extends AbstractQuery
{
/**
* @var DataSourceInterface
*/
private $dataSource;

/**
* @var GranularityInterface
*/
private $granularity;

/**
* @var array|AggregatorInterface[]
*/
private $aggregations;

/**
* @var array|PostAggregatorInterface[]
*/
private $postAggregations;

/**
* @var FilterInterface
*/
private $filter;

/**
* @var array|IntervalInterface[]
* @Serializer\Type("array<string>")
*/
private $intervals;

/**
* @return DataSourceInterface
*/
public function getDataSource()
{
return $this->dataSource;
}

/**
* @param DataSourceInterface $dataSource
*
* @return $this
*/
public function setDataSource(DataSourceInterface $dataSource)
{
$this->dataSource = $dataSource;

return $this;
}

/**
* @return GranularityInterface
*/
public function getGranularity()
{
return $this->granularity;
}

/**
* @param GranularityInterface $granularity
*
* @return $this
*/
public function setGranularity(GranularityInterface $granularity)
{
$this->granularity = $granularity;

return $this;
}

/**
* @return array|\Druid\Query\Component\AggregatorInterface[]
*/
public function getAggregations()
{
return $this->aggregations;
}

/**
* @param array|\Druid\Query\Component\AggregatorInterface[] $aggregations
*
* @return $this
*/
public function setAggregations(array $aggregations)
{
$this->aggregations = $aggregations;

return $this;
}

/**
* @return array|\Druid\Query\Component\PostAggregatorInterface[]
*/
public function getPostAggregations()
{
return $this->postAggregations;
}

/**
* @param array|\Druid\Query\Component\PostAggregatorInterface[] $postAggregations
*
* @return $this
*/
public function setPostAggregations(array $postAggregations)
{
$this->postAggregations = $postAggregations;

return $this;
}

/**
* @return array|\Druid\Query\Component\IntervalInterface[]
*/
public function getIntervals()
{
return $this->intervals;
}

/**
* @param array|\Druid\Query\Component\IntervalInterface[] $intervals
*
* @return $this
*/
public function setIntervals(array $intervals)
{
$this->intervals = $intervals;

return $this;
}

/**
* @return FilterInterface
*/
public function getFilter()
{
return $this->filter;
}

/**
* @param FilterInterface $filter
*
* @return $this
*/
public function setFilter(FilterInterface $filter)
{
$this->filter = $filter;

return $this;
}
}
Loading

0 comments on commit 4cbbb10

Please sign in to comment.