Skip to content

Commit

Permalink
Merge pull request #1 from muhrin/workflows
Browse files Browse the repository at this point in the history
API changes to log collection and entries
  • Loading branch information
sphuber authored Mar 15, 2017
2 parents 9ed8ae1 + 447799b commit f291b4a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 84 deletions.
6 changes: 3 additions & 3 deletions aiida/backends/tests/orm/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ def tearDown(self):
Delete all the created log entries
"""
super(TestBackendLog, self).tearDown()
self._backend.log.delete_all()
self._backend.log.delete_many({})

def test_create_backend(self):
"""
Test creating the backend specific backend instance
"""
backend = construct()

def test_delete_all(self):
def test_delete_many(self):
"""
Test deleting all log entries
Bit superfluous, given that other tests most likely would fail
Expand All @@ -46,7 +46,7 @@ def test_delete_all(self):
self._backend.log.create_entry(**self._record)

self.assertEquals(len(self._backend.log.find()), count)
self._backend.log.delete_all()
self._backend.log.delete_many({})
self.assertEquals(len(self._backend.log.find()), 0)

def test_create_log_message(self):
Expand Down
41 changes: 9 additions & 32 deletions aiida/orm/implementation/django/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,7 @@ def create_entry(self, time, loggername, levelname, objname,
metadata=json.dumps(metadata)
)
)
entry.persist()

return entry

def create_entry_from_record(self, record):
"""
Create a log entry from a record created by the python logging
"""
from datetime import datetime

objpk = record.__dict__.get('objpk', None)
objname = record.__dict__.get('objname', None)

# Do not store if objpk and objname are not set
if objpk is None or objname is None:
return None

entry = DjangoLogEntry(
DbLog(
time=timezone.make_aware(datetime.fromtimestamp(record.created)),
loggername=record.name,
levelname=record.levelname,
objname=objname,
objpk=objpk,
message=record.getMessage(),
metadata=json.dumps(record.__dict__)
)
)
entry.persist()
entry.save()

return entry

Expand Down Expand Up @@ -88,11 +60,16 @@ def find(self, filter_by=None, order_by=None, limit=None):

return [DjangoLogEntry(entry) for entry in entries]

def delete_all(self):
def delete_many(self, filter):
"""
Delete all log entries in the table
"""
DbLog.objects.all().delete()
if not filter:
DbLog.objects.all().delete()
else:
raise NotImplemented(
"Only deleting all by passing an empty filer dictionary is "
"currently supported")


class DjangoLogEntry(LogEntry):
Expand Down Expand Up @@ -158,7 +135,7 @@ def metadata(self):
"""
return json.loads(self._model.metadata)

def persist(self):
def save(self):
"""
Persist the log entry to the database
"""
Expand Down
52 changes: 12 additions & 40 deletions aiida/orm/implementation/sqlalchemy/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class SqlaLog(Log):

def create_entry(self, time, loggername, levelname, objname,
objpk=None, message="", metadata=None):
"""
Expand All @@ -26,46 +25,16 @@ def create_entry(self, time, loggername, levelname, objname,
metadata=metadata
)
)
entry.persist()
entry.save()

return entry


def create_entry_from_record(self, record):
"""
Create a log entry from a record created by the python logging
"""
from datetime import datetime

objpk = record.__dict__.get('objpk', None)
objname = record.__dict__.get('objname', None)

# Do not store if objpk and objname are not set
if objpk is None or objname is None:
return None

entry = SqlaLogEntry(
DbLog(
time=timezone.make_aware(datetime.fromtimestamp(record.created)),
loggername=record.name,
levelname=record.levelname,
objname=objname,
objpk=objpk,
message=record.getMessage(),
metadata=record.__dict__
)
)
entry.persist()

return entry


def find(self, filter_by=None, order_by=None, limit=None):
"""
Find all entries in the Log collection that confirm to the filter and
optionally sort and/or apply a limit.
"""
order = []
order = []
filters = {}

if not filter_by:
Expand Down Expand Up @@ -96,15 +65,18 @@ def find(self, filter_by=None, order_by=None, limit=None):

return [SqlaLogEntry(entry) for entry in entries]


def delete_all(self):
def delete_many(self, filter):
"""
Delete all log entries in the table
"""
for entry in DbLog.query.all():
entry.delete()
session.commit()

if not filter:
for entry in DbLog.query.all():
entry.delete()
session.commit()
else:
raise NotImplemented(
"Only deleting all by passing an empty filer dictionary is "
"currently supported")


class SqlaLogEntry(LogEntry):
Expand Down Expand Up @@ -170,7 +142,7 @@ def metadata(self):
"""
return self._model._metadata

def persist(self):
def save(self):
"""
Persist the log entry to the database
"""
Expand Down
35 changes: 26 additions & 9 deletions aiida/orm/log.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from abc import abstractmethod, abstractproperty, ABCMeta
from collections import namedtuple
from aiida.utils import timezone

ASCENDING = 1
DESCENDING = -1
Expand Down Expand Up @@ -35,22 +36,39 @@ def create_entry(self, time, loggername, levelname, objname,
"""
pass

@abstractmethod
def create_entry_from_record(self, record):
"""
Create a log entry from a record created by the python logging
Helper function to create a log entry from a record created as by the
python logging lobrary
:param record: The record created by the logging module
:type record: :class:`logging.record`
:return: An object implementing the log entry interface
:rtype: :class:`aiida.orm.log.LogEntry`
"""
pass
from datetime import datetime

objpk = record.__dict__.get('objpk', None)
objname = record.__dict__.get('objname', None)

# Do not store if objpk and objname are not set
if objpk is None or objname is None:
return None

return self.create_entry(
time=timezone.make_aware(datetime.fromtimestamp(record.created)),
loggername=record.name,
levelname=record.levelname,
objname=objname,
objpk=objpk,
message=record.getMessage(),
metadata=record.__dict__
)

@abstractmethod
def find(self, filter_by=None, order_by=None, limit=None):
"""
Find all entries in the Log collection that confirm to the filter and
Find all entries in the Log collection that conforms to the filter and
optionally sort and/or apply a limit.
:param filter_by: A dictionary of key value pairs where the entries have
Expand All @@ -64,10 +82,9 @@ def find(self, filter_by=None, order_by=None, limit=None):
pass

@abstractmethod
def delete_all(self):
def delete_many(self, filter):
"""
This is temporarily added to be used only in tests
Deletes all log entries in the table
Delete all the log entries matching the given filter
"""
pass

Expand Down Expand Up @@ -156,11 +173,11 @@ def metadata(self):
pass

@abstractmethod
def persist(self):
def save(self):
"""
Persist the log entry to the database
:return: reference of self
:rtype: :class: LogEntry
"""
pass
pass

0 comments on commit f291b4a

Please sign in to comment.