Skip to content

Commit

Permalink
REV-18633 Adding support to query multiple indices, as well as some h… (
Browse files Browse the repository at this point in the history
#11)

* REV-18636 Adding support to query multiple indices, as well as some helper methods for dates.
  • Loading branch information
revinatemarcos authored Oct 25, 2019
1 parent 54156b9 commit c1eab12
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
56 changes: 56 additions & 0 deletions Lib/DateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,60 @@ public static function getIntervalTimestampsForES($period, $scale) {
}
return $timestamps;
}

/**
* Extracts start and end date for ES date filters
*
* @param array $period Supports a range in this format: array(0 => StartDate, 1 => EndDate)
* @throws \Exception if no valid start and dates are given
* @return array(start date, end date)
*/
public static function extractStartAndEndDates($period) {
$origTz = date_default_timezone_get();
date_default_timezone_set("UTC");
$isStartDateInvalid = !isset($period[0]) || strtotime($period[0]) === false;
$isEndDateInvalid = !isset($period[1]) || strtotime($period[1]) === false;
if ($isStartDateInvalid || $isEndDateInvalid) {
throw new \Exception("Missing start date or end date in passed period: " . json_encode($period));
}
$startDate = date('c', strtotime(date('Y-m-d 00:00:00', strtotime($period[0]))));
$endDate = date('c', strtotime(date('Y-m-d 23:59:59', strtotime($period[1]))));
date_default_timezone_set($origTz);
return array($startDate, $endDate);
}

/**
* Returns Date in UTC (or whatever other timezone is provided)
*
* @static
* @param string $dateFormat Format of the date as supported by date()
* @param int $timestamp Timestamp
* @param string $timeZone Timezone
*
* @return string Formatted date/time string
*/
public static function getUTCDate($dateFormat, $timestamp, $timeZone = 'UTC') {
$origTz = date_default_timezone_get();
date_default_timezone_set($timeZone);
$date = date($dateFormat, $timestamp);
date_default_timezone_set($origTz);
return $date;
}

/**
* Returns timestamp in UTC (or whatever other timezone is provided)
*
* @static
* @param string $date date in string format
* @param string $timeZone the timezone, defaults to utc
* @param string $now the reference timestamp to base the calculation off, defaults to null (i.e. now)
* @return int
*/
public static function getUTCTimestamp($date, $timeZone = 'UTC', $now = null) {
$origTz = date_default_timezone_get();
date_default_timezone_set($timeZone);
$ts = is_null($now) ? strtotime($date) : strtotime($date, $now);
date_default_timezone_set($origTz);
return $ts;
}
}
9 changes: 7 additions & 2 deletions Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,13 @@ public function execute($query = null) {
$query = $this->getQuery();
}

// Ensure we set an array of indices, always.
$indices = $this->analytics->getIndex();
if (! is_array($indices)) {
$indices = array($indices);
}
$search = new \Elastica\Search($this->elasticaClient);
$search->addIndex($this->analytics->getIndex());
$search->addIndices($indices);
$search->addType($this->analytics->getType());
$search->setQuery($query);

Expand Down Expand Up @@ -591,4 +596,4 @@ public function setEnableInfo($enableInfo) {
$this->enableInfo = $enableInfo;
return $this;
}
}
}
4 changes: 4 additions & 0 deletions Test/TestCase/Helper/DateHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,9 @@ public function testGetPeriodInfo() {
);

$this->assertEquals($customExpectedResults, DateHelper::getPeriodInfo('01/01/2014-12/15/2014'));

$period = array('01/01/2014', '12/15/2014');
$customExpectedResults = array('2014-01-01T00:00:00+00:00', '2014-12-15T23:59:59+00:00');
$this->assertEquals($customExpectedResults, DateHelper::extractStartAndEndDates($period));
}
}

0 comments on commit c1eab12

Please sign in to comment.