Skip to content

Commit

Permalink
monitor support and event support
Browse files Browse the repository at this point in the history
  • Loading branch information
kouk committed Oct 27, 2016
1 parent 9eaf92e commit 3ecd9b5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
17 changes: 17 additions & 0 deletions mgship/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,29 @@ def to_timestamp(ctx, param, value):
@click.option('--begin', default=None, type=DateTime(),
callback=to_timestamp,
help='when to start archiving, as unix timestamp')
@click.option('--event', default=None, type=str,
help='event type')
@click.option('--recipient', default=None, type=Email(),
help='email address of recipient')
def archive(format, *args, **kwargs):
dest = csv.Destination() if format == 'csv' else json.Destination()
mgship.Archive(dest, *args, **kwargs).ship()


@main.command()
@click.option('--format', default='csv', type=click.Choice(['json', 'csv']),
help='the output format')
@click.option('--sleep', default=None, type=int,
help='how much to wait before repeating requests')
@click.option('--begin', default=None, type=DateTime(),
callback=to_timestamp,
help='when to start polling, as unix timestamp')
@click.option('--recipient', default=None, type=Email(),
help='email address of recipient')
def monitor(format, *args, **kwargs):
dest = csv.Destination() if format == 'csv' else json.Destination()
mgship.Monitor(dest, *args, **kwargs).ship()


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions mgship/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def mg_poll_events(client=None, max_age=1800, sleep=900, rewind=None,

if 'begin' not in kwargs:
begin = kwargs['begin'] = utctimestamp(timeago(seconds=rewind))
else:
begin = kwargs['begin']

url = events_url(ascending="yes", **kwargs)

Expand Down
26 changes: 25 additions & 1 deletion mgship/mgship.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from functools import wraps

from mgship.api import Client
from mgship.events import mg_past_events
from mgship.events import mg_past_events, mg_poll_events
from mgship.util import is_past
from mgship.email import is_email
from mgship.log import logger
Expand All @@ -30,6 +30,7 @@ class Archive(object):
"""Ship all existing events."""
dest = attr.ib()
begin = attr.ib(default=None, validator=mg_field_validator(is_past))
event = attr.ib(default=None)
recipient = attr.ib(default=None, validator=mg_field_validator(is_email))
_client = attr.ib(default=attr.Factory(Client), repr=False)
_filtered_params = ['dest', '_client']
Expand All @@ -55,3 +56,26 @@ def ship(self):
@attr.s
class Monitor(object):
"""Monitor current events."""
dest = attr.ib()
begin = attr.ib(default=None)
sleep = attr.ib(default=None)
recipient = attr.ib(default=None, validator=mg_field_validator(is_email))
_client = attr.ib(default=attr.Factory(Client), repr=False)
_filtered_params = ['dest', '_client']

@classmethod
def _filter_params(self, attr, value):
if attr.name in self._filtered_params or value is None:
return False
return True

def _iter_sink(self, sink):
kwargs = attr.asdict(self, filter=self._filter_params)
logger.info("starting monitoring ({})".format(kwargs))
for event in mg_poll_events(client=self._client, **kwargs):
yield sink.send(event)

def ship(self):
with self.dest as sink:
for _ in self._iter_sink(sink):
pass

0 comments on commit 3ecd9b5

Please sign in to comment.