API Example
Added a /log api with example file.
This commit is contained in:
parent
d48db67a82
commit
e3e7ff0acb
@ -11,11 +11,11 @@
|
||||
:copyright: (c) 2013 by Jarret Raim
|
||||
:license: Apache 2.0, see LICENSE for details
|
||||
"""
|
||||
import json
|
||||
import uuid
|
||||
import datetime
|
||||
from dateutil.parser import parse
|
||||
from flask import Blueprint, request, jsonify
|
||||
from models import Event
|
||||
from flask import Blueprint, request, jsonify, Response, json
|
||||
from models import Event, Tenant, Key
|
||||
from database import db_session
|
||||
|
||||
api = Blueprint('api', __name__, url_prefix="/api")
|
||||
@ -31,20 +31,32 @@ def log(tenant_id):
|
||||
if request.method == 'POST':
|
||||
agent_id = uuid.UUID(request.json['agent_id'])
|
||||
received_on = parse(request.json['received_on'])
|
||||
key_id = uuid.UUID(request.json['key_id'])
|
||||
|
||||
if request.json['severity'] in ['DEBUG', 'INFO', 'WARN', 'FATAL']:
|
||||
severity = request.json['severity']
|
||||
else:
|
||||
severity = 'UNKNOWN'
|
||||
|
||||
# Load the key and tenant
|
||||
tenant = Tenant.query.get(tenant_id)
|
||||
key = Key.query.filter_by(uuid=str(key_id)).first()
|
||||
|
||||
ev = Event(tenant_id=tenant_id, agent_id=str(agent_id), received_on=received_on,
|
||||
severity=severity, message=request.json['message'])
|
||||
severity=severity, message=request.json['message'], tenant=tenant, key=key)
|
||||
db_session.add(ev)
|
||||
db_session.commit()
|
||||
|
||||
return jsonify(ev.as_dict())
|
||||
return Response(json.dumps(ev.as_dict(), cls=DateTimeJsonEncoder), mimetype='application/json')
|
||||
else:
|
||||
events = Event.query.filter_by(tenant_id=tenant_id).order_by(Event.received_on)
|
||||
return jsonify(json_list=events.all())
|
||||
events_dicts = map(Event.as_dict, events.all())
|
||||
return Response(json.dumps(events_dicts, cls=DateTimeJsonEncoder), mimetype='application/json')
|
||||
|
||||
|
||||
class DateTimeJsonEncoder(json.JSONEncoder):
|
||||
def default(self, obj):
|
||||
if isinstance(obj, datetime.datetime):
|
||||
return obj.isoformat()
|
||||
else:
|
||||
return super(DateTimeJsonEncoder, self).default(obj)
|
7
examples/log-event.json
Normal file
7
examples/log-event.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"agent_id": "869b81e0-6181-4afc-bf5b-3f785f2d503f",
|
||||
"received_on": "2013-02-26T19:53:14+18:00",
|
||||
"severity": "INFO",
|
||||
"key_id": "331d341c-b171-47f9-9a7b-7a15e8d85893",
|
||||
"message": "Key accessed by user 'apache'."
|
||||
}
|
20
models.py
20
models.py
@ -47,7 +47,6 @@ class Tenant(Base):
|
||||
__tablename__ = 'tenants'
|
||||
id = Column(Integer, primary_key=True)
|
||||
uuid = Column(String(36), unique=True)
|
||||
#keys = relationship('Key', backref='tenant', lazy='dynamic')
|
||||
|
||||
def __init__(self, uuid=None):
|
||||
if uuid is None:
|
||||
@ -104,15 +103,30 @@ class Event(Base):
|
||||
tenant_id = Column(Integer, ForeignKey('tenants.id'))
|
||||
tenant = relationship("Tenant", backref=backref('events', order_by=id))
|
||||
|
||||
def __init__(self, tenant_id=None, agent_id=None, received_on=None, severity=None, message=None):
|
||||
key_id = Column(Integer, ForeignKey('keys.id'))
|
||||
key = relationship("Key", backref=backref('events', order_by=id))
|
||||
|
||||
def __init__(self, tenant_id=None, agent_id=None, received_on=None, severity=None,
|
||||
message=None, tenant=None, key=None):
|
||||
self.tenant_id = tenant_id
|
||||
self.agent_id = agent_id
|
||||
self.received_on = received_on
|
||||
self.severity = severity
|
||||
self.message = message
|
||||
self.key = key
|
||||
self.tenant = tenant
|
||||
|
||||
def __repr__(self):
|
||||
return '<Event %s [%s] - %s >' % (self.received_on, self.severity, self.message[:25])
|
||||
|
||||
def as_dict(self):
|
||||
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
|
||||
json = {
|
||||
'id': self.id,
|
||||
'agent_id': self.agent_id,
|
||||
'received_on': self.received_on.isoformat(),
|
||||
'severity': self.severity,
|
||||
'tenant_id': self.tenant_id,
|
||||
'key_id': self.key_id,
|
||||
'message': self.message
|
||||
}
|
||||
return json
|
||||
|
Loading…
Reference in New Issue
Block a user