start api && upadte container list page
This commit is contained in:
parent
80742dd6f4
commit
03b0f7fc8d
5 changed files with 54 additions and 64 deletions
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
[submodule "kb/kb"]
|
||||
path = kb/kb
|
||||
url = http://github.com/apescale/kb
|
|
@ -29,21 +29,28 @@ class ControllerContainers:
|
|||
return True
|
||||
|
||||
def get_items(self):
|
||||
state = ControllerContainersStatisticsState()
|
||||
# state = ControllerContainersStatisticsState()
|
||||
query = models.Containers.select(
|
||||
models.Containers.id,
|
||||
models.Containers.ipv4,
|
||||
models.Containers.ipv6,
|
||||
models.Containers.status,
|
||||
models.ContainersStatisticsState.size,
|
||||
).join(models.ContainersStatisticsState).where(models.Containers.user == self.user_id)
|
||||
print query
|
||||
).join(models.ContainersStatisticsState).where(
|
||||
models.Containers.user == self.user_id
|
||||
)
|
||||
#
|
||||
containers = dict()
|
||||
containers["total"] = query.count()
|
||||
containers["items"] = []
|
||||
for item in query.execute():
|
||||
print (item.containersstatisticsstate.size)
|
||||
containers['items'].append(item)
|
||||
containers['items'].append({
|
||||
'id': str(item.id),
|
||||
'ipv4': item.ipv4,
|
||||
'ipv6': item.ipv6,
|
||||
'status': item.status,
|
||||
'size': item.containersstatisticsstate.size,
|
||||
})
|
||||
return containers
|
||||
|
||||
def get_item(self, container_id):
|
||||
|
|
|
@ -186,6 +186,14 @@ class ControllerSSHKey:
|
|||
|
||||
|
||||
class ControllerAPI:
|
||||
def auth(self, email, secret):
|
||||
if models.UsersSecrets.select().join(models.Users).where(
|
||||
models.Users.email == email,
|
||||
models.UsersSecrets.secret == secret
|
||||
).count() == 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
def get(self, user_id):
|
||||
secret = models.UsersSecrets.select().where(
|
||||
models.UsersSecrets.user == user_id
|
||||
|
|
|
@ -15,6 +15,7 @@ from flask import Blueprint
|
|||
from app import models
|
||||
from app.settings import settings
|
||||
from app.cloud.controllers.users import ControllerUsers
|
||||
from app.cloud.controllers.users import ControllerAPI
|
||||
from app.cloud.controllers.containers import ControllerContainers
|
||||
from cloudnsru import CloudnsClient
|
||||
|
||||
|
@ -25,6 +26,8 @@ viewAPI = Blueprint('api', __name__, url_prefix='/api')
|
|||
@viewAPI.route('/auth', methods=['POST'])
|
||||
def auth():
|
||||
"""
|
||||
curl -X POST http://localhost:5000/api/auth --data 'email=<email>&secret=<secret>'
|
||||
TODO:
|
||||
Процесс авторизации
|
||||
- получаем емейл и секретный ключ
|
||||
- создаём временный токен
|
||||
|
@ -33,68 +36,37 @@ def auth():
|
|||
"""
|
||||
email = request.form['email']
|
||||
secret = request.form['secret']
|
||||
expire = request.form['expire']
|
||||
token = ''
|
||||
return jsonify(token=token)
|
||||
# expire = request.form['expire']
|
||||
|
||||
if not ControllerAPI().auth(email, secret):
|
||||
return jsonify(status=1)
|
||||
|
||||
user_id = ControllerUsers().get_id_by_email(email)
|
||||
|
||||
return jsonify(user_id=user_id)
|
||||
# /container/create
|
||||
# /container/details/<uuid:container_id>
|
||||
|
||||
|
||||
@viewAPI.route('/container/list', methods=['GET', 'POST', 'PUT', 'DELETE'])
|
||||
def container_list():
|
||||
"""
|
||||
curl -X POST http://localhost:5000/api/container/list --data 'email=<email>&secret=<secret>'
|
||||
:return:
|
||||
"""
|
||||
email = request.form['email']
|
||||
secret = request.form['secret']
|
||||
# expire = request.form['expire']
|
||||
#
|
||||
if not ControllerAPI().auth(email, secret):
|
||||
return jsonify(status=1)
|
||||
#
|
||||
user_id = ControllerUsers().get_id_by_email(email)
|
||||
# get containers list
|
||||
containers = ControllerContainers(session['user_id']).get_items()
|
||||
|
||||
return jsonify(status=0, message='ok', containers=containers)
|
||||
|
||||
|
||||
@viewAPI.route('/stats/<uuid:rule_id>')
|
||||
def stats(rule_id):
|
||||
# auth user
|
||||
if not ControllerUsers().auth(session['email'], session['password']):
|
||||
return redirect(url_for("app_id_logout"))
|
||||
|
||||
rules = ControllerRules(session['user_id'])
|
||||
|
||||
# check the user have a selected rule
|
||||
# if user not have a container then redirect to the container list
|
||||
if not rules.check_exists_item(rule_id):
|
||||
return redirect(url_for('rules.index'))
|
||||
|
||||
rules_statistics = ControllerRulesStatistics(rule_id)
|
||||
|
||||
errors = {'total': 0, 'items': []}
|
||||
stats_string = {'requests': '', 'traffic': ''}
|
||||
last_days = datetime.today() - timedelta(days=30)
|
||||
|
||||
# get rule details
|
||||
rule_details = rules.get_item(rule_id)
|
||||
|
||||
stats_string['traffic'] = rules_statistics.traffic_total_get(last_days)
|
||||
stats_string['requests'] = rules_statistics.requests_total_get(last_days)
|
||||
|
||||
traffic_by_country = rules_statistics.traffic_by_country_get(last_days)
|
||||
|
||||
if stats_string['traffic'] > 0:
|
||||
stats_string['cost'] = 0
|
||||
elif stats_string['traffic'] > 50:
|
||||
stats_string['cost'] = 4
|
||||
elif stats_string['traffic'] > 1000:
|
||||
stats_string['cost'] = 3
|
||||
elif stats_string['traffic'] > 2000:
|
||||
stats_string['cost'] = 2.7
|
||||
elif stats_string['traffic'] > 3000:
|
||||
stats_string['cost'] = 2.6
|
||||
elif stats_string['traffic'] > 4000:
|
||||
stats_string['cost'] = 2.5
|
||||
elif stats_string['traffic'] > 5000:
|
||||
stats_string['cost'] = 2.4
|
||||
|
||||
# for data_item in data:
|
||||
# print data_item.timestamp, data_item.body_bytes_sent
|
||||
|
||||
return render_template('rules/stats.twig',
|
||||
rule=rule_details,
|
||||
stats_string=stats_string,
|
||||
traffic_by_country=traffic_by_country,
|
||||
errors=errors)
|
||||
containers = ControllerContainers(user_id).get_items()
|
||||
#
|
||||
return jsonify(
|
||||
status=0,
|
||||
total=containers['total'],
|
||||
items=containers['items']
|
||||
)
|
||||
|
|
|
@ -40,7 +40,7 @@
|
|||
{% endif %}
|
||||
</td>
|
||||
<td>
|
||||
{% set size_gb = (container.containersstatisticsstate.size / 1024 / 1024) %}
|
||||
{% set size_gb = (container.size / 1024 / 1024) %}
|
||||
{{ '%0.2f' | format(size_gb|float) }}GB
|
||||
</td>
|
||||
<td>
|
||||
|
|
Loading…
Add table
Reference in a new issue