-
Notifications
You must be signed in to change notification settings - Fork 1
/
fake_status.py
239 lines (190 loc) · 7.93 KB
/
fake_status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# 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 datetime
import random
import flask
from ceagle.api_fake_data import base
from ceagle.api_fake_data import fake_regions
def period_is_valid(period):
return period in ("day", "week", "month", "year")
def generate_timestamps(period):
today = datetime.date.today()
stamps = []
if period == "day":
day = (today - datetime.timedelta(days=1)).strftime("%Y-%m-%d")
stamps = []
for hour in range(24):
for minutes in ["00", "10", "20", "30", "40", "50"]:
stamps.append("%sT%.2i:%s" % (day, hour, minutes))
elif period == "week":
days = 7
for day in [(today - datetime.timedelta(days=(days - d)))
for d in range(days)]:
day = day.strftime("%Y-%m-%d")
for hour in range(24):
stamps.append("%sT%.2i:00" % (day, hour))
elif period == "month":
days = 30
for day in [(today - datetime.timedelta(days=(days - d)))
for d in range(days)]:
day = day.strftime("%Y-%m-%d")
for hour in [0, 4, 8, 12, 16, 20]:
stamps.append("%sT%.2i:00" % (day, hour))
elif period == "year":
days = 365
for day in [(today - datetime.timedelta(days=(days - d)))
for d in range(days)]:
day = day.strftime("%Y-%m-%d")
for hour in [0, 8, 16]:
stamps.append("%sT%.2i:00" % (day, hour))
return stamps
def generate_values(period_or_timestamps,
ratio=1, null_result_probability=20, integer=False):
if type(period_or_timestamps) == list:
timestamps = period_or_timestamps
else:
timestamps = generate_timestamps(period_or_timestamps)
if timestamps:
data = []
data_avg = []
for ts in timestamps:
if random.randint(1, 100) <= null_result_probability:
data.append([ts, None])
else:
if not integer:
value = base.randnum(.7, 1) * ratio
else:
value = random.randint(1, 1000)
data.append([ts, value])
data_avg.append(value)
avg = round(sum(data_avg) / len(data_avg), 3)
return (data, avg)
def generate_service_data(period, service):
if service == "health":
fci_data = generate_values(period)[0]
time_data = generate_values(period)[0]
size_data = generate_values(period, 50, integer=True)[0]
count_data = generate_values(period, 50, integer=True)[0]
return {
"fci": random.randint(0, 100) * 0.01,
"response_time": base.randnum(.1, 2.5),
"response_size": random.randint(2000, 5000),
"api_calls_count": random.randint(200, 500),
"fci_data": fci_data,
"response_time_data": time_data,
"repsonse_size_data": size_data,
"api_calls_count_data": count_data
}
elif service == "performance":
data, avg = generate_values(period)
return {"data": data, "duration": avg}
elif service == "availability":
data, avg = generate_values(period)
return {"availability_data": data, "availability": avg}
def generate_region_data(region, period, service=None):
data = fake_regions.regions(detailed=True).get(region)
if not data:
return ({"error": "Region '%s' not found" % region}, 404)
if service:
if service not in data["services"]:
error = ("Service '%s' is not available for region '%s'"
% (service, region))
return ({"error": error}, 404)
result = generate_service_data(period, service)
return ({service: {region: result}, "period": period}, 200)
result = {"sla": base.randnum(.7, 1)}
for service in ("availability", "performance", "health"):
if service in data["services"]:
result[service] = generate_service_data(period, service)
return ({"period": period, "status": {region: result}}, 200)
def generate_status_data(period, service):
if not period_is_valid(period):
return flask.jsonify({"error": "Not Found"}), 404
data = {}
for region in fake_regions.regions():
result, code = generate_region_data(region, period, service)
if code == 200:
data.update(result[service])
return flask.jsonify({"period": period, service: data})
def generate_region_data_response(region, period, service=None):
if not period_is_valid(period):
return flask.jsonify({"error": "Not Found"}), 404
result, code = generate_region_data(region, period, service)
return (flask.jsonify(result), code)
@base.api_handler
def get_status(period):
if not period_is_valid(period):
flask.abort(404)
status = {}
rand = random.random
for region in fake_regions.regions():
status[region] = {
"sla": rand() if rand() > 0.4 else None,
"performance": random.randint(1, 10),
"availability": rand() if rand() > 0.4 else None,
"health": rand() if rand() > 0.4 else None
}
return flask.jsonify({"status": status, "period": period})
@base.api_handler
def get_status_health(period):
return generate_status_data(period, "health")
@base.api_handler
def get_status_performance(period):
return generate_status_data(period, "performance")
@base.api_handler
def get_status_availability(period):
return generate_status_data(period, "availability")
@base.api_handler
def get_region_status(region, period):
if not period_is_valid(period):
flask.abort(404)
data = fake_regions.regions(detailed=True).get(region)
if not data:
return ({"error": "Region '%s' not found" % region}, 404)
status = {}
rand = random.random
for service in ("keystone", "nova", "cinder"):
status[service] = {
"sla": rand() if rand() > 0.4 else None,
"performance": random.randint(1, 10),
"availability": rand() if rand() > 0.4 else None,
"health": rand() if rand() > 0.4 else None
}
return flask.jsonify({"status": status, "period": period})
@base.api_handler
def get_region_status_health(region, period):
return generate_region_data_response(region, period, "health")
@base.api_handler
def get_region_status_performance(region, period):
return generate_region_data_response(region, period, "performance")
@base.api_handler
def get_region_status_availability(region, period):
if not period_is_valid(period):
return flask.jsonify({"error": "Not Found"}), 404
region_data = fake_regions.regions(detailed=True).get(region)
if not region_data:
return flask.jsonify({"error": "Region '%s' not found" % region}), 404
if "availability" not in region_data["services"]:
error = ("Service '%s' is not available for region '%s'"
% ("availability", region))
return flask.jsonify({"error": error}), 404
availability = {}
for svc in ["foo_service", "bar_service", "spam_service"]:
data, avg = generate_values(period)
availability[svc] = {"availability_data": data,
"availability": avg}
return flask.jsonify({"availability": availability,
"region": region,
"period": period})