API call for DB summary added
API call for fetching fuel-stats DB summary information added. Change-Id: Ia4d0efc1da720a601854f56de76488a89fb54f35 Closes-Bug: #1470455
This commit is contained in:
parent
ae9fa294be
commit
7e3fa258e2
|
@ -14,11 +14,12 @@
|
|||
|
||||
import datetime
|
||||
import json
|
||||
from sqlalchemy import and_
|
||||
from sqlalchemy import func
|
||||
|
||||
from flask import Blueprint
|
||||
from flask import request
|
||||
from flask import Response
|
||||
from sqlalchemy import and_
|
||||
|
||||
from fuel_analytics.api.app import app
|
||||
from fuel_analytics.api.app import db
|
||||
|
@ -182,3 +183,28 @@ def get_filtered_installation_infos():
|
|||
inst_infos_total)
|
||||
app.logger.debug("Filtered installation_info: %s fetched", paging_params)
|
||||
return Response(jsons_data, mimetype='application/json')
|
||||
|
||||
|
||||
@bp.route('/summary', methods=['GET'])
|
||||
def get_db_summary():
|
||||
app.logger.debug("Getting db summary")
|
||||
summary = {}
|
||||
for model in (IS, AL, OSWL):
|
||||
count = db.session.query(model).count()
|
||||
summary[model.__tablename__] = {'total': count}
|
||||
|
||||
# Counting filtered installation info
|
||||
filtered_summary = db.session.query(
|
||||
IS.is_filtered, func.count(IS.id)).group_by(IS.is_filtered).all()
|
||||
filtered_num = 0
|
||||
not_filtered_num = 0
|
||||
for is_filtered, count in filtered_summary:
|
||||
if is_filtered is False:
|
||||
not_filtered_num += count
|
||||
else:
|
||||
filtered_num += count
|
||||
summary[IS.__tablename__]['not_filtered'] = not_filtered_num
|
||||
summary[IS.__tablename__]['filtered'] = filtered_num
|
||||
|
||||
app.logger.debug("Db summary got")
|
||||
return Response(json.dumps(summary), mimetype='application/json')
|
||||
|
|
|
@ -26,6 +26,7 @@ from fuel_analytics.test.base import DbTest
|
|||
|
||||
from fuel_analytics.api.app import app
|
||||
from fuel_analytics.api.app import db
|
||||
from fuel_analytics.api.common import consts
|
||||
from fuel_analytics.api.db import model
|
||||
from fuel_analytics.api.resources import json_exporter
|
||||
|
||||
|
@ -194,3 +195,27 @@ class JsonExporterTest(InstStructureTest, OswlTest, DbTest):
|
|||
self.assertEquals(filtered_num, result['paging_params']['total'])
|
||||
for struct in result['objs']:
|
||||
self.assertTrue(struct['is_filtered'])
|
||||
|
||||
def test_get_db_summary(self):
|
||||
oswls = self.get_saved_oswls(100, consts.OSWL_RESOURCE_TYPES.volume)
|
||||
inst_infos = self.get_saved_inst_structs(
|
||||
oswls, is_filtered_values=(True, False, None))
|
||||
not_filtered_num = len(filter(lambda x: x.is_filtered is False,
|
||||
inst_infos))
|
||||
filtered_num = len(inst_infos) - not_filtered_num
|
||||
action_logs = self.get_saved_action_logs(inst_infos)
|
||||
expected = {
|
||||
'oswl_stats': {'total': len(oswls)},
|
||||
'installation_structures': {
|
||||
'total': len(inst_infos),
|
||||
'filtered': filtered_num,
|
||||
'not_filtered': not_filtered_num
|
||||
},
|
||||
'action_logs': {'total': len(action_logs)}
|
||||
}
|
||||
with app.test_request_context():
|
||||
url = '/api/v1/json/summary'
|
||||
resp = self.client.get(url)
|
||||
self.check_response_ok(resp)
|
||||
actual = json.loads(resp.data)
|
||||
self.assertEqual(expected, actual)
|
||||
|
|
Loading…
Reference in New Issue