Permalink
Browse files

Issue #154: Add `source_id` field to allow linking back to source doc…

…ument.
  • Loading branch information...
kiwiz committed Oct 30, 2017
1 parent f3dc811 commit bd4394c0b9b6935e756c86933848d18d61a2c0b2
View
@@ -81,6 +81,10 @@ function ver_cmp($a, $b) {
FOO\SiteFinder::setSite(null);
}
if(ver_cmp($old_ver, '1.5.0') < 0) {
FOO\DB::query('ALTER TABLE `alerts` ADD COLUMN `source_id` VARCHAR(64) NOT NULL DEFAULT ""');
}
/**
* Migration logic
*/
View
1 db.sql
@@ -86,6 +86,7 @@ CREATE TABLE `alerts` (
`alert_date` UNSIGNED INTEGER NOT NULL,
`content` TEXT NOT NULL,
`content_hash` VARCHAR(64) NOT NULL,
`source_id` TEXT NOT NULL,
`renderer_data` TEXT NOT NULL, /* unused */
`assignee_type` INTEGER NOT NULL,
`assignee` INTEGER NOT NULL,
View
@@ -29,6 +29,7 @@ CREATE TABLE `alerts` (
`alert_date` bigint(20) unsigned NOT NULL,
`content` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`content_hash` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
`source_id` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL,
`assignee_type` tinyint(4) NOT NULL,
`assignee` bigint(20) unsigned NOT NULL,
`search_id` bigint(20) unsigned NOT NULL,
View
@@ -44,6 +44,7 @@ protected static function generateSchema() {
'assignee_type' => [static::T_ENUM, Assignee::$TYPES, Assignee::T_USER],
'assignee' => [static::T_NUM, null, User::NONE],
'content' => [static::T_OBJ, null, []],
'source_id' => [static::T_STR, null, ''],
'search_id' => [static::T_NUM, null, 0],
'state' => [static::T_ENUM, static::$STATES, self::ST_NEW],
'resolution' => [static::T_ENUM, static::$RESOLUTIONS, self::RES_NOT],
View
@@ -81,12 +81,17 @@ public function initializeIndex() {
'assignee' => ['type' => 'long'],
'content' => ['type' => 'object'],
'source' => ['type' => 'string'],
'source_id' => ['type' => 'string'],
'search_id' => ['type' => 'long'],
'state' => ['type' => 'long'],
'resolution' => ['type' => 'long'],
'escalated' => ['type' => 'boolean'],
'content_hash' => ['type' => 'string'],
'notes' => ['type' => 'string'],
'tags' => ['type' => 'string'],
'priority' => ['type' => 'long'],
'category' => ['type' => 'string'],
'owner' => ['type' => 'long'],
'create_date' => ['type' => 'date', 'format' => 'epoch_second'],
'update_date' => ['type' => 'date', 'format' => 'epoch_second'],
]
View
@@ -32,6 +32,7 @@ protected function constructQuery() {
'assignee',
'content',
'source',
'source_id',
'search_id',
'state',
'resolution',
@@ -41,4 +42,22 @@ protected function constructQuery() {
}
return [$settings, $query_list, $fields, $date_field, $result_type, $filter_range];
}
protected function _getLink(Alert $alert) {
if($alert['source_id']) {
return null;
}
$parts = explode('/', $alert['source_id'], 3);
if(count($parts) != 3) {
return null;
}
$this->generateLink($parts[2]);
}
public function generateLink($alert_id) {
$site = SiteFinder::getCurrent();
return $site->urlFor(sprintf('alert/%d', $alert_id));
}
}
View
@@ -75,11 +75,14 @@ protected function _execute($date, $constructed_qdata) {
}
}
if ($has_date_field) {
if($has_date_field) {
// Extract the date field.
$alert_date = Util::parseDates($date_type, [$entry[$date_field]])[0] / 1000;
unset($entry[$date_field]);
}
if(array_key_exists('_index', $entry) && array_key_exists('_type', $entry) && array_key_exists('_id', $entry)) {
$alert['source_id'] = sprintf('%s/%s/%s', $entry['_index'], $entry['_type'], $entry['_id']);
}
$alert['alert_date'] = $alert_date;
$alert['content'] = $entry;
$alerts[] = $alert;
@@ -21,23 +21,36 @@
const R_NO_RESULTS = 2;
protected function _getLink(Alert $alert) {
if($alert['source_id']) {
$parts = explode('/', $alert['source_id'], 3);
if(count($parts) == 3) {
return $this->generateAlertLink($parts[0], $parts[1], $parts[2]);
}
}
return $this->generateLink(
Util::get($this->obj['query_data'], 'query'),
$alert['alert_date'] - ($this->obj['range'] * 60),
$alert['alert_date']
);
}
public function generateAlertLink($index, $type, $id) {
$cfg = $this->getConfig();
$index_pattern = $cfg['date_based'] ? \ESQuery\Util::generateKibanaPattern($cfg['index']):$cfg['index'];
return sprintf('%s/app/kibana#/doc/%s/%s/%s?%s', $cfg['src_url'], $index_pattern, $index, $type, http_build_query(['id' => $id]));
}
public function generateLink($query, $start, $end) {
$cfg = $this->getConfig();
if(is_null($cfg['src_url'])) {
return null;
}
$index = $cfg['date_based'] ? \ESQuery\Util::generateKibanaPattern($cfg['index']):$cfg['index'];
$index_pattern = $cfg['date_based'] ? \ESQuery\Util::generateKibanaPattern($cfg['index']):$cfg['index'];
$parser = new \ESQuery\Parser;
try {
return $parser->generateUrl($query, $start, $end, $cfg['src_url'], $index);
return $parser->generateUrl($query, $start, $end, $cfg['src_url'], $index_pattern);
} catch(\ESQuery\Exception $e) {
return null;
}
@@ -239,7 +252,6 @@ public function search($date, $settings, $query_list, $fields, $date_field, $dat
$ok = $count <= $filter_range[1];
}
// _index, _type, _id and _score always show up. If they aren't explicitly included in the field list, just exclude them!
$underscore_fields = ['_index', '_type', '_id', '_score'];
$field_specified = [];
foreach($underscore_fields as $field) {
@@ -266,6 +278,7 @@ public function search($date, $settings, $query_list, $fields, $date_field, $dat
}
$alert['alert_date'] = $alert_date;
}
$alert['source_id'] = sprintf('%s/%s/%s', $row['_index'], $row['_type'], $row['_id']);
foreach($underscore_fields as $field) {
if(!array_key_exists($field, $field_specified)) {
unset($row[$field]);
View
@@ -60,31 +60,27 @@ protected function _execute($date, $constructed_qdata) {
return [$alert];
}
private function isStatusCodeValid($expectedData, $curl)
{
private function isStatusCodeValid($expectedData, $curl) {
return $expectedData['code'] === $curl->httpStatusCode;
}
private function isContentValid($expectedData, $curl)
{
private function isContentValid($expectedData, $curl) {
if (!isset($expectedData['content_match']) && !empty($expectedData['content_match'])) {
return true;
}
return preg_match("/" . $expectedData['content_match'] . "/", $curl->rawResponse) === 1;
}
private function getHttpClient()
{
private function getHttpClient() {
if ($this->httpClient === null) {
$this->httpClient = new Curl;
}
return $this->httpClient;
}
public function setHttpClient(Curl $httpClient)
{
public function setHttpClient(Curl $httpClient) {
$this->httpClient = $httpClient;
}
}
View
@@ -30,6 +30,7 @@ protected function _execute($date, $constructed_qdata) {
foreach($this->results as $result) {
$alert = new Alert;
$alert['source_id'] = Util::get($result, 'source_link', '');
$alert['alert_date'] = (int) Util::get($result, 'alert_date', $_SERVER['REQUEST_TIME']);
$alert['content'] = (array) Util::get($result, 'content', []);
@@ -39,4 +40,8 @@ protected function _execute($date, $constructed_qdata) {
return $alerts;
}
protected function _getLink(Alert $alert) {
return $alert['source_id'] ?: null;
}
}

0 comments on commit bd4394c

Please sign in to comment.