def f(): # Create the api object. api = HttpAPI(namespace='ns', apikey='apikey', hostname='localhost', port=2000, timeout=20) # For n (100) times insert some data. start = time.time() - 1000 for i in range(1000): # timestamp denotes when the event occured. print api.store(timestamp=start + i, **{ 'amount': 11000 + i, 'type': random.choice(['A', 'B']), # 'type': randint(0, 2) and 'A' or 'B', })
class LinuxData(object): def __init__(self, ns, apikey, url): self.ns = ns self.apikey = apikey self.url = url self.api = HttpAPI(namespace=self.ns, apikey=self.apikey, url=self.url) def store(self, count, timestamp): #logging.debug('store: count=%s timestamp=%s', count, timestamp) resp = self.api.store(count=count,timestamp=float(timestamp)) if not resp['code'] == 201: raise Exception('Endpoint error.')
def store(self): api = HttpAPI(namespace=self.ns, apikey=self.apikey, url=self.url) for timestamp, data in self.parselines(): # A successful read last, pos = self.ld.get() if last: logging.info('Last update: %.6f' % last) if last is None or timestamp > last: for host, domain in data: # Upload data. logging.debug('store: hostname=%s domain=%s timestamp=%s', host.lower(), domain, timestamp) resp = api.store(hostname=host.lower(), domain=domain, timestamp=timestamp) if not resp['code'] == 201: raise Exception('Endpoint error.') # TODO get seek pos. self.ld.set(str(timestamp), pos)