Action logs payload collection added

Field 'body' is added to 'action_logs' table with its processing in handler.
'body' contains action log record, its structure is checked with json scheme.

Blueprint: send-anon-usage

Change-Id: I2ec40a9904899a72f62c1f29b58ccbe0a1d6a02d
This commit is contained in:
Aleksey Kasatkin 2014-10-14 12:01:25 +03:00
parent 21c6679048
commit 96b6bc7c0b
5 changed files with 79 additions and 10 deletions

View File

@ -35,6 +35,7 @@ def upgrade():
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('node_aid', sa.String(), nullable=False),
sa.Column('external_id', sa.Integer(), nullable=False),
sa.Column('body', sa.Text(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('node_aid', 'external_id')
)

View File

@ -24,6 +24,7 @@ class ActionLog(db.Model):
id = db.Column(db.Integer, primary_key=True)
node_aid = db.Column(db.String, nullable=False)
external_id = db.Column(db.Integer, nullable=False)
body = db.Column(db.Text, nullable=False)
class InstallationStruct(db.Model):

View File

@ -13,6 +13,7 @@
# under the License.
from flask import Blueprint
from flask import json
from flask import request
from flask_jsonschema import validate as validate_request
import six
@ -45,6 +46,8 @@ def post():
for chunk in util.split_collection(action_logs, chunk_size=1000):
existed_objs, action_logs_to_add = _separate_action_logs(chunk)
objects_info.extend(_extract_objects_info(existed_objs))
for obj in action_logs_to_add:
obj['body'] = json.dumps(obj['body'])
objects_info.extend(_save_action_logs(action_logs_to_add))
return {'status': 'ok', 'action_logs': list(objects_info)}

View File

@ -12,9 +12,25 @@
"type": "object",
"properties": {
"node_aid": {"type": "string"},
"external_id": {"type": "integer"}
"external_id": {"type": "integer"},
"body": {
"type": "object",
"properties": {
"id": {"type": "number"},
"actor_id": {"type": ["string", "null"]},
"action_group": {"type": "string"},
"action_name": {"type": "string"},
"action_type": {"type": "string"},
"start_timestamp": {"type": "string"},
"end_timestamp": {"type": "string"},
"additional_info": {"type": "object"},
"is_sent": {"type": "boolean"},
"cluster_id": {"type": ["number", "null"]},
"task_uuid": {"type": ["string", "null"]}
}
}
},
"required": ["node_aid", "external_id"]
"required": ["node_aid", "external_id", "body"]
}
}
},

View File

@ -40,10 +40,24 @@ class TestActionLogs(DbTest):
def test_post(self):
node_aid = 'x'
expected_logs = [
{'node_aid': node_aid, 'external_id': 1},
{'node_aid': node_aid, 'external_id': 2},
{'node_aid': node_aid, 'external_id': 3}
]
{
'node_aid': node_aid,
'external_id': i,
'body': {
"id": i,
"actor_id": "",
"action_group": "",
"action_name": "",
"action_type": "",
"start_timestamp": "",
"end_timestamp": "",
"additional_info": {},
"is_sent": False,
"cluster_id": 5,
"task_uuid": None
}
}
for i in xrange(3)]
resp = self.post(
'/api/v1/action_logs/',
{'action_logs': expected_logs}
@ -66,8 +80,25 @@ class TestActionLogs(DbTest):
def test_post_duplication(self):
node_aid = 'x'
action_logs = [{'node_aid': node_aid, 'external_id': i}
for i in xrange(100)]
action_logs = [
{
'node_aid': node_aid,
'external_id': i,
'body': {
"id": i,
"actor_id": "",
"action_group": "",
"action_name": "",
"action_type": "",
"start_timestamp": "",
"end_timestamp": "",
"additional_info": {},
"is_sent": False,
"cluster_id": 5,
"task_uuid": None
}
}
for i in xrange(100)]
resp = self.post(
'/api/v1/action_logs/',
{'action_logs': action_logs}
@ -84,8 +115,25 @@ class TestActionLogs(DbTest):
self.assertEquals(len(action_logs), count_actual)
# Checking duplications is not added
new_action_logs = [{'node_aid': node_aid, 'external_id': i}
for i in xrange(len(action_logs) + 50)]
new_action_logs = [
{
'node_aid': node_aid,
'external_id': i,
'body': {
"id": i,
"actor_id": "",
"action_group": "",
"action_name": "",
"action_type": "",
"start_timestamp": "",
"end_timestamp": "",
"additional_info": {},
"is_sent": False,
"cluster_id": 5,
"task_uuid": None
}
}
for i in xrange(len(action_logs) + 50)]
resp = self.post(
'/api/v1/action_logs/',
{'action_logs': action_logs + new_action_logs}