def __init__(self, device_name, device_id, rabbitmq_address, metric): self.subscriber = PikaSubscriber(device_name=device_name, device_id=device_id, rabbitmq_address=rabbitmq_address, metric_name=metric) self.metric = metric self.device_name = device_name self.device_id = device_id self.num_channels = get_num_channels(device_name, metric) self.cassandra_dao = CassandraDAL()
class CassandraSubscriber(object): """ Subscribes and writes data to a file """ def __init__(self, device_name, device_id, rabbitmq_address, metric): self.subscriber = PikaSubscriber(device_name=device_name, device_id=device_id, rabbitmq_address=rabbitmq_address, metric_name=metric) self.metric = metric self.device_name = device_name self.device_id = device_id self.num_channels = get_num_channels(device_name, metric) self.cassandra_dao = CassandraDAL() def start(self): """ Consume and write data to file :return: """ self.cassandra_dao.connect() self.subscriber.connect() self.subscriber.consume_messages(self.write_to_cassandra) def stop(self): """ Unsubscribe and close file :return: """ self.subscriber.disconnect() self.file.close_file(self.write_to_cassandra) def write_to_cassandra(self, ch, method, properties, body): buffer_content = json.loads(body) for record in buffer_content: timestamp = record["timestamp"] channel_data = [ record["channel_%s" % i] for i in xrange(self.num_channels) ] self.cassandra_dao.store_data(timestamp, self.device_id, self.device_name, self.metric, channel_data)
class CassandraSubscriber(object): """ Subscribes and writes data to a file """ def __init__(self, device_name, device_id, rabbitmq_address, metric): self.subscriber = PikaSubscriber(device_name=device_name, device_id=device_id, rabbitmq_address=rabbitmq_address, metric_name=metric) self.metric = metric self.device_name = device_name self.device_id = device_id self.num_channels = get_num_channels(device_name, metric) self.cassandra_dao = CassandraDAL() def start(self): """ Consume and write data to file :return: """ self.cassandra_dao.connect() self.subscriber.connect() self.subscriber.consume_messages(self.write_to_cassandra) def stop(self): """ Unsubscribe and close file :return: """ self.subscriber.disconnect() self.file.close_file(self.write_to_cassandra) def write_to_cassandra(self, ch, method, properties, body): buffer_content = json.loads(body) for record in buffer_content: timestamp = record["timestamp"] channel_data = [record["channel_%s" % i] for i in xrange(self.num_channels)] self.cassandra_dao.store_data(timestamp, self.device_id, self.device_name, self.metric, channel_data)
import json import random from flask import Flask, request, current_app from functools import wraps from cloudbrain.utils.metadata_info import map_metric_name_to_num_channels, get_supported_devices from cloudbrain.settings import WEBSERVER_PORT _MOCK_ENABLED = True app = Flask(__name__) app.config['PROPAGATE_EXCEPTIONS'] = True if not _MOCK_ENABLED: from cloudbrain.datastore.CassandraDAL import CassandraDAL dao = CassandraDAL() dao.connect() def support_jsonp(f): """Wraps JSONified output for JSONP""" @wraps(f) def decorated_function(*args, **kwargs): callback = request.args.get('callback', False) if callback: content = str(callback) + '(' + str(f()) + ')' return current_app.response_class(content, mimetype='application/json') else: return f(*args, **kwargs)