diff --git a/ceagle/api/client.py b/ceagle/api/client.py index b2981b9..e51a639 100644 --- a/ceagle/api/client.py +++ b/ceagle/api/client.py @@ -17,10 +17,12 @@ from ceagle.api import base from ceagle.api_fake_data import security +from ceagle.api_fake_data import availability from ceagle import config FAKE_CLIENT_MAP = { "security": security.Client, + "availability": availability.Client, } diff --git a/ceagle/api/v1/status.py b/ceagle/api/v1/status.py index db77ed1..d701129 100644 --- a/ceagle/api/v1/status.py +++ b/ceagle/api/v1/status.py @@ -78,7 +78,6 @@ def get_status_performance(period): @bp_status.route("/availability/") -@fake_status.get_status_availability def get_status_availability(period): ct = client.get_client("availability") api_endpoint = "/api/v1/availability/{period}".format(period=period) diff --git a/ceagle/api_fake_data/availability.py b/ceagle/api_fake_data/availability.py new file mode 100644 index 0000000..240225c --- /dev/null +++ b/ceagle/api_fake_data/availability.py @@ -0,0 +1,36 @@ +# 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. + +from ceagle.api_fake_data import base + +import random + + +class Client(base.FakeClient): + + @base.route("/api/v1/availability/(?Pday|week|month)$") + def availability(self, query, period): + data = list(base.generate_data(period, + lambda x: round(random.random(), 2))) + avg = round(sum([i[1] for i in data]) / len(data), 2) + return { + "period": period, + "availability": { + "region1": { + "availability": avg, + "availability_data": data, + } + } + }, 200 diff --git a/ceagle/api_fake_data/base.py b/ceagle/api_fake_data/base.py index 9819966..c912c03 100644 --- a/ceagle/api_fake_data/base.py +++ b/ceagle/api_fake_data/base.py @@ -13,14 +13,41 @@ # License for the specific language governing permissions and limitations # under the License. +import datetime import functools import random import re from ceagle import config +import flask + USE_FAKE_DATA = config.get_config().get("use_fake_api_data", True) +PERIOD_DAYS = { + "day": 1, + "week": 7, + "month": 30, + "year": 365, +} + +REGIONS = [ + "region1", + "region2", + "far-away.example.net", +] + + +def generate_data(period, method, chunks=40): + days = PERIOD_DAYS.get(period) + if days is None: + flask.abort(404) + now = datetime.datetime.utcnow() + step = datetime.timedelta(days=days) / chunks + for i in range(chunks): + ts = now - step * (chunks - i) + yield [ts.isoformat()[:16], method(ts)] + def route(reg): def decorator(method): diff --git a/ceagle/api_fake_data/health.py b/ceagle/api_fake_data/health.py new file mode 100644 index 0000000..a29e2ed --- /dev/null +++ b/ceagle/api_fake_data/health.py @@ -0,0 +1,23 @@ +# 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. + +from ceagle.api_fake_data import base + + +class Client(base.FakeClient): + + @base.route("/api/v1/health/(?Pday|week|month)$") + def health(self, query, period): + return {"health": {}}, 200 diff --git a/tests/unit/api_fake_data/test_base.py b/tests/unit/api_fake_data/test_base.py index 5448281..7a3dd50 100644 --- a/tests/unit/api_fake_data/test_base.py +++ b/tests/unit/api_fake_data/test_base.py @@ -46,3 +46,14 @@ def test_default(self): c = base.FakeClient("name", "endpoint") res = c.get("/none") self.assertEqual(404, res[1]) + + +class GenerateDataTestCase(test.TestCase): + + def test_gen(self): + def gen(ts): + return "sample data for %s" % ts + gen = base.generate_data("day", gen) + for i in gen: + print i + self.assertEqual(1, 2)