-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ES queries are far from perfect * missed unit tests
- Loading branch information
Alexander Maretskiy
committed
Dec 3, 2016
1 parent
5fb6860
commit 3fb4bb0
Showing
6 changed files
with
188 additions
and
4 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
# Copyright 2016: Mirantis Inc. | ||
# All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import logging | ||
|
||
import flask | ||
|
||
from availability import config | ||
from availability import storage | ||
|
||
|
||
LOG = logging.getLogger("api") | ||
LOG.setLevel(config.get_config().get("logging", {}).get("level", "INFO")) | ||
|
||
# NOTE(maretskiy): Cache names and services for validation and performance | ||
REGIONS = {r["name"]: r["services"] | ||
for r in config.get_config().get("regions")} | ||
|
||
PERIODS = { | ||
"day": ("now-1d", "1h"), | ||
"week": ("now-1w", "1d"), | ||
"month": ("now-1M", "1d") | ||
} | ||
|
||
|
||
def process_buckets(buckets, round_to=3): | ||
"""Convert ES buckets into API format, calculate average. | ||
:param buckets: ES buckets list | ||
:param round_to: int round depth | ||
:returns: {"availability_data": <list>, "availability": <float>} | ||
""" | ||
data = [] | ||
status_sum = 0 | ||
|
||
for b in buckets: | ||
status = b["status"]["value"] or 0 | ||
status = round(status, round_to) | ||
status_sum += status | ||
data.append([b["key_as_string"], status]) | ||
|
||
# TODO(maretskiy): get AVG from ES, do not calculate it here | ||
availability = round((status_sum / len(data)), round_to) | ||
return {"availability_data": data, | ||
"availability": availability} | ||
|
||
|
||
def query_region(region, period): | ||
gte, interval = PERIODS[period] | ||
query = { | ||
"size": 0, | ||
"query": {"range": {"time": {"lte": "now", | ||
"gte": gte, | ||
"format": "date_optional_time"}}}, | ||
"aggs": { | ||
"availability": { | ||
"date_histogram": {"field": "time", | ||
"interval": interval, | ||
"format": "yyyy-MM-dd'T'HH:mm", | ||
"min_doc_count": 0}, | ||
"aggs": {"status": {"avg": {"field": "status"}}} | ||
} | ||
} | ||
} | ||
result = storage.es_search(region, body=query) | ||
if not result: | ||
return None | ||
buckets = result["aggregations"]["availability"]["buckets"] | ||
return process_buckets(buckets) | ||
|
||
|
||
def query_region_by_services(region, period): | ||
gte, interval = PERIODS[period] | ||
services_av = {} | ||
for service in REGIONS[region]: | ||
service_name = service["name"] | ||
query = { | ||
"size": 0, | ||
"query": { | ||
"bool": { | ||
"must": [ | ||
{ | ||
"range": { | ||
"time": {"lte": "now", | ||
"gte": gte, | ||
"format": "date_optional_time"}} | ||
}, | ||
{ | ||
"term": {"name": service_name} | ||
} | ||
] | ||
} | ||
}, | ||
"aggs": { | ||
"availability": { | ||
"date_histogram": {"field": "time", | ||
"interval": interval, | ||
"format": "yyyy-MM-dd'T'HH:mm", | ||
"min_doc_count": 0}, | ||
"aggs": {"status": {"avg": {"field": "status"}}} | ||
} | ||
} | ||
} | ||
result = storage.es_search(region, body=query) | ||
if not result: | ||
# NOTE(maretskiy): Skip if some issues with Elastic. Ideas? | ||
continue | ||
buckets = result["aggregations"]["availability"]["buckets"] | ||
services_av[service_name] = process_buckets(buckets) | ||
return services_av | ||
|
||
|
||
bp = flask.Blueprint("availability", __name__) | ||
|
||
|
||
@bp.route("/availability/<period>") | ||
def get_availability(period): | ||
if period not in PERIODS: | ||
return flask.jsonify({"error": "Not found"}), 404 | ||
|
||
regions_av = {} | ||
for region in REGIONS: | ||
availability = query_region(region, period) | ||
if not availability: | ||
# NOTE(maretskiy): Skip if some issues with Elastic. Ideas? | ||
continue | ||
regions_av[region] = availability | ||
|
||
return flask.jsonify({"availability": regions_av, "period": period}) | ||
|
||
|
||
@bp.route("/region/<region>/availability/<period>") | ||
def get_region_availability(region, period): | ||
if (period not in PERIODS) or (region not in REGIONS): | ||
return flask.jsonify({"error": "Not found"}), 404 | ||
|
||
services_av = query_region_by_services(region, period) | ||
result = {"availability": services_av, | ||
"region": region, | ||
"period": period} | ||
|
||
return flask.jsonify(result) | ||
|
||
|
||
def get_blueprints(): | ||
return [["", bp]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters