def __init__(self, device_type, device_id, rabbitmq_address): self.subscribers = {} self.rabbitmq_address = rabbitmq_address self.device_type = device_type self.device_id = device_id self.cassandra_dao = CassandraDAO() self.threads = []
class CassandraSubscriber(object): """ Subscribes and writes data to a file """ def __init__(self, device_type, device_id, rabbitmq_address): self.subscribers = {} self.rabbitmq_address = rabbitmq_address self.device_type = device_type self.device_id = device_id self.cassandra_dao = CassandraDAO() self.threads = [] def start(self): self.cassandra_dao.connect() metrics = get_metrics_names(self.device_type) for metric in metrics: self.subscribers[metric] = PikaSubscriber( device_name=self.device_type, device_id=self.device_id, rabbitmq_address=self.rabbitmq_address, metric_name=metric) self.subscribers[metric].connect() t = threading.Thread(target=self.subscribers[metric].consume_messages, args=(self.write_to_cassandra_factory(metric),)) self.threads.append(t) t.start() def stop(self): for subscriber in self.subscribers: subscriber.disconnect() def write_to_cassandra_factory(self, metric): num_channels = get_num_channels(self.device_type, metric) def write_to_cassandra(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(num_channels)] self.cassandra_dao.store_data(timestamp, self.device_id, self.device_type, metric, channel_data) return write_to_cassandra
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 = CassandraDAO() 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)
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 = CassandraDAO()
get_supported_devices, get_metrics_names) from cloudbrain.settings import WEBSERVER_PORT _API_VERSION = "v1.0" app = Flask(__name__) app.config['PROPAGATE_EXCEPTIONS'] = True from cloudbrain.datastore.CassandraDAO import CassandraDAO mock_data_enabled = False try: dao = CassandraDAO() dao.connect() print "INFO: cassandra is running." except NoHostAvailable: print "WARNING: cassandra is not running. Using mock data." mock_data_enabled = True def support_jsonp(f): """Wraps JSONified output for JSONP""" @wraps(f) def decorated_function(*args, **kwargs): callback = request.args.get('callback', False)
from cloudbrain.utils.metadata_info import (map_metric_name_to_num_channels, get_supported_devices, get_metrics_names) from cloudbrain.settings import WEBSERVER_PORT _API_VERSION = "v1.0" app = Flask(__name__) app.config['PROPAGATE_EXCEPTIONS'] = True from cloudbrain.datastore.CassandraDAO import CassandraDAO mock_data_enabled = False try: dao = CassandraDAO() dao.connect() print "INFO: cassandra is running." except NoHostAvailable: print "WARNING: cassandra is not running. Using mock data." mock_data_enabled = True 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,