def test_greater_than_fill_last_no_list(): start_time = datetime.utcnow() l_event = Event(start_time, 4) r_event = Event(start_time, 3) fill = FillLast(0) gte = GreaterThan(None, fill) events = gte.execute(l_event, r_event) expects = [1, 1, 1, 4] for i, e in enumerate(events): assert e.timestamp == start_time + timedelta(seconds=len(events) - i - 1) assert e.sample == expects[i]
def execute(self, left, right): if type(left) == list and type(right) == list: return [ Event(l.timestamp, l.sample / r.sample) for l, r in zip(left, right) ] elif type(left) == list: return [Event(e.timestamp, e.sample / right) for e in left] elif type(right) == list: return [Event(e.timestamp, left / e.sample) for e in right] msg = f'One side of operand must a list of Event\n' \ ' Left side: {type(left)}\n' \ ' Right side: {type(right}\n' raise SyntaxError(msg)
def __call__(self, left, right): if type(left) == list and type(right) == list: events = [] last = self.last_value for l, r in zip(left, right): if self.operator_func(l.sample, r.sample): last = l.sample events.append(l) else: events.append(Event(l.timestamp, last)) return events elif type(left) == list: events = [] last = self.last_value for l in left: if self.operator_func(l.sample, right.sample): last = l.sample events.append(l) else: events.append(Event(l.timestamp, last)) return events elif type(right) == list: events = [] last = self.last_value for r in right: if self.operator_func(left.sample, r.sample): last = left.sample events.append(left) else: events.append(Event(left.timestamp, last)) return events msg = f'One side of operand must a list of Event\n' \ ' Left side: {type(left)}\n' \ ' Right side: {type(right}\n' raise SyntaxError(msg)
def __call__(self, streams): output = [] num_samples = len(streams[0]) for i in range(num_samples): timestamp = streams[0][i].timestamp values = [s[i].sample for s in streams] out = reduce(lambda x, y: x/y, values) output.append(Event(timestamp, out)) return output
def __call__(self, streams): output = [] num_samples = len(streams[0]) for i in range(num_samples): timestamp = streams[0][i].timestamp val = streams[0][i].sample for j, s in enumerate(streams[1:]): val = val - s[i].sample output.append(Event(timestamp, val)) return output
def handle_msg(self): msg = self.queue.get() print(f'Received: {msg["topic"]}::{msg["msg"]}') data = super().handle_msg(msg) for k, s in self.streams.items(): events = [] for d in data: timestamp = d.timestamp sample = getattr(d, s.handle) events.append(Event(timestamp, sample)) s.handle_msg(events)
def __call__(self, streams, other): num_samples = len(streams[0]) if type(other) != list: other = [other] * num_samples else: other = [o.sample for o in other] for i in range(num_samples): timestamp = streams[0][i].timestamp for j, s in enumerate(streams): s[i] = Event(timestamp, s[i].sample + other[i]) return streams
def generate_events(start_time, events, in_order=True): if start_time: timestamp = start_time else: timestamp = datetime.utcnow() end_time = len(events) timestamps = [ timestamp + timedelta(seconds=i) for i in range(end_time, 0, -1) ] if not in_order: rand.shuffle(timestamps) gen_events = [Event(t, s) for t, s in zip(timestamps, events)] return gen_events
def data_to_stream(name, data, offset_sec, start_time=None, in_order=True): if start_time: timestamp = start_time else: timestamp = datetime.utcnow() num = len(data) end_time = num * offset_sec timestamps = [ timestamp + timedelta(seconds=i) for i in range(end_time, 0, -offset_sec) ] if not in_order: rand.shuffle(timestamps) events = [Event(t, s) for t, s in zip(timestamps, data)] return Stream(name, events)
def __call__(self, stream): values = [e.sample for e in stream] return [Event(stream[-1].timestamp, mean(values))]
def execute(self, stream): values = [e.sample for e in stream] return [Event(stream[0].timestamp, mean(values))]
def execute(self, stream): values = [e.sample for e in stream] return [Event(stream[0].timestamp, percentile(values, self.pct))]