design question


A

Andrea Crotti

I've been wondering for weeks now how to do but I still didn't get a
satisfying answer, so I hope someone can give a hint...

I have some logs which I extract from simulation results.
These logs are in the form

timestamp, nodeid, eventname, event_argument

and now I have to analyze the data.
I don't have so many metrics to analyze but for example I might look for
"symmetric events", in the form

0.0, 0: DATA_SENT(1)
0.1, 1: DATA_RECEIVED(0)
(so in short the nodeid and the argument are swapped)

After many changes now the timeline is something as below, where
everything almost all the functions are static methods and return a new
list of events.

But now I don't get anything I like for the metrics.

Every metric only has to filter and return a result, but the problem is
that maybe for some metrics I want the result on all the nodes.
Some only the average, sometimes I want to correlated with the time (or
time slots) and so on.

So it must be very flexible, but at the same time not a pain to write...
I first wrote a class for every metric subclassing a Metric, but it
wasn't nice.
Then I tried many other ways but actually I'm a bit stuck...

Any suggestions from someone that maybe had a similar problem?

--8<---------------cut here---------------start------------->8---
class Timeline(object):
"""
A timeline is generated by the list of events happened during the
simulation and it's the main point to analyze the data.
"""

def __init__(self, events=None):
if events is None:
self.events = []
else:
self.events = events

def __str__(self):
res = []
for evt_tuple in iter(self):
res.append(str(evt_tuple))

return "\n".join(res)

def __eq__(self, other):
return self.events == other.events

def __iter__(self):
return iter(sorted(self.events, key=lambda x: x.time))

def add(self, timestamp, nodeid, evt):
"Add one line for the given nodeid"
tup = EventTuple(timestamp, nodeid, evt)
self.events.append(tup)

def add_tuple(self, tup):
self.events.append(tup)

def first_event(self, _evt):
first_evt = Timeline.filt(self.events, evt=_evt)[0]
return pretty_time(first_evt.time)

def sort(self):
Timeline.sort_by(self.events)

@staticmethod
def sort_by(events, par='time'):
# side effecting method
events.sort(key=lambda x: x.__getattribute__(par))

@staticmethod
def filt(events, node=None, evt=None, arg=None):
"Takes a node, an event and a function to further filter the time"
# copy locally the events and filter them
res = events[:]
if node is not None:
res = filter(lambda x: x.node == node, res)

if evt is not None:
res = filter(lambda x: x.evt.name == evt, res)

if arg is not None:
res = filter(lambda x: x.evt.arg == arg, res)

return res
....

--8<---------------cut here---------------end--------------->8---
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top