Skip to content

Instantly share code, notes, and snippets.

@jakubkulhan
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save jakubkulhan/3f09a719257882173ec2 to your computer and use it in GitHub Desktop.

Select an option

Save jakubkulhan/3f09a719257882173ec2 to your computer and use it in GitHub Desktop.
<?php
$q = [
"query" => ["filtered" => [
"filter" => ["range" => [
"created" => ["gte" => "-31 days"],
]],
]],
"aggs" => [
"by_date" => ["date_histogram" => [
"field" => "created",
"interval" => "1d",
"aggs" => [
"by_severity" => ["filters" => [
"filters" => [
"error" => ["term" => ["severity" => 1]],
"warning" => ["term" => ["severity" => 2]],
// no need for total, by_date buckets already have doc_count == total
// "total" => ["match_all" => []],
],
]],
],
]]
]
];
SELECT
DATE(created),
COUNT(*) total,
SUM(IF(severity = 1, 1, 0)) errors,
SUM(IF(severity = 2, 1, 0)) warnings
FROM t
WHERE created >= NOW()-INTERVAL 31 DAY
GROUP BY DATE(created)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment