Created
May 21, 2014 18:13
-
-
Save yunazuno/466004c500c6576c3644 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from norikraclient.client import Client | |
import pprint | |
norikra = Client() | |
def __read_data(): | |
# Actually, here we read data from a data source (e.g., csv file) | |
for i in range(61): | |
timestamp = 1396278000 + i * 60 | |
value = i | |
data_host1 = [{"timestamp": timestamp, "hostname": "host1", "metric1": value, "metric2": value*2}] | |
data_host2 = [{"timestamp": timestamp, "hostname": "host2", "metric1": value, "metric2": value*2}] | |
yield data_host1 | |
yield data_host2 | |
def calc(): | |
# Register queries with LOOPBACK mechanism | |
norikra.register("aggregate_metric1", "LOOPBACK(metric_aggregated)", """ | |
select | |
min(timestamp) as timestamp, | |
sum(metric1) as metric_sum | |
from | |
host_data.win:ext_timed_batch(timestamp * 1000, 1 min, 1396278000000L) | |
where hostname in ("host1", "host2") | |
""") | |
norikra.register("calc_avg_max", "", """ | |
select | |
min(timestamp) as timestamp, | |
avg(metric_sum) as metric_avg, | |
max(metric_sum) as matric_max | |
from | |
metric_aggregated.win:ext_timed_batch(timestamp * 1000, 10 min, 1396278000000L) | |
""") | |
# Send dummy data to a "head" target | |
for data in __read_data(): | |
norikra.send("host_data", data) | |
# Fetch events from a query with a "tail" target | |
result = norikra.event("calc_avg_max") | |
pp = pprint.PrettyPrinter() | |
pp.pprint(list(map(lambda e: e[1], result))) | |
if __name__ == '__main__': | |
calc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment