<commit_before>__author__ = 'marion'

from cassandra.cluster import Cluster

# add the shared settings file to namespace
import sys
from os.path import dirname, abspath
sys.path.insert(0, dirname(dirname(abspath(__file__))))
from settings import EXPLO_BRAINSERVER_IP
from settings import CASSANDRA_METRICS
from settings import MUSE_PORTS
from settings import SPACEBREW_CASSANDRA_NAME
from settings import SPACEBREW_CASSANDRA_IP
from spacebrew.spacebrew import SpacebrewApp
from settings import SPACEBREW_BRAINSERVER_IP
from router.spacebrew_router import SpacebrewRouter

from spacebrew_utils import calculate_spacebrew_name
import time


class CassandraSpacebrewClient(object):
    def __init__(self, name, server):

        start = time.time()

        # spacebrew router
        self.sp_router = SpacebrewRouter(server=server)

        # configure cassandra cluster
        self.cluster = Cluster()
        self.session = self.cluster.connect('cloudbrain')


        # configure the spacebrew client
        self.brew = SpacebrewApp(name, server=server)
        for path in CASSANDRA_METRICS:
            publisher_metric_name = calculate_spacebrew_name(path)
            for muse_port in MUSE_PORTS:

                # add subscriber
                subscriber_metric_name = '%s-muse-%s' % (publisher_metric_name, muse_port)
                self.brew.add_subscriber(subscriber_metric_name, "string")

                # handle value
                handle_value = self.handle_value_factory(publisher_metric_name, muse_port)
                self.brew.subscribe(subscriber_metric_name, handle_value)

                # route data
                publisher_name = 'muse-%s' % muse_port
                subscriber_name = SPACEBREW_CASSANDRA_NAME
                self.sp_router.link(publisher_metric_name, subscriber_metric_name, publisher_name, subscriber_name,
                                    SPACEBREW_BRAINSERVER_IP, SPACEBREW_CASSANDRA_IP)


        end = time.time()

        print 'configuration took %s s' % (end - start)

    def handle_value_factory(self, metric, muse_port):

        def handle_value(csv_string):

            values = csv_string.split(',')

            # numerical values
            num_arguments = len(values)
            numerical_columns = ''.join([", value_%s" % i for i in range(num_arguments)])

            # column values
            timestamp = int(time.time() * 1000)
            column_values = "'%s', '%s', %s" % (muse_port, timestamp, values[0])
            if len(values) > 1:
                for value in values[1:]:<commit_msg>unlinking before linking<commit_after>__author__ = 'marion'

from cassandra.cluster import Cluster

# add the shared settings file to namespace
import sys
from os.path import dirname, abspath
sys.path.insert(0, dirname(dirname(abspath(__file__))))
from settings import EXPLO_BRAINSERVER_IP
from settings import CASSANDRA_METRICS
from settings import MUSE_PORTS
from settings import SPACEBREW_CASSANDRA_NAME
from settings import SPACEBREW_CASSANDRA_IP
from spacebrew.spacebrew import SpacebrewApp
from settings import SPACEBREW_BRAINSERVER_IP
from router.spacebrew_router import SpacebrewRouter

from spacebrew_utils import calculate_spacebrew_name
import time


class CassandraSpacebrewClient(object):
    def __init__(self, name, server):

        start = time.time()

        # spacebrew router
        self.sp_router = SpacebrewRouter(server=server)

        # configure cassandra cluster
        self.cluster = Cluster()
        self.session = self.cluster.connect('cloudbrain')


        # configure the spacebrew client
        self.brew = SpacebrewApp(name, server=server)
        for path in CASSANDRA_METRICS:
            publisher_metric_name = calculate_spacebrew_name(path)
            for muse_port in MUSE_PORTS:

                # add subscriber
                subscriber_metric_name = '%s-muse-%s' % (publisher_metric_name, muse_port)
                self.brew.add_subscriber(subscriber_metric_name, "string")

                # handle value
                handle_value = self.handle_value_factory(publisher_metric_name, muse_port)
                self.brew.subscribe(subscriber_metric_name, handle_value)

                # route data
                publisher_name = 'muse-%s' % muse_port
                subscriber_name = SPACEBREW_CASSANDRA_NAME
                self.sp_router.unlink(publisher_metric_name, subscriber_metric_name, publisher_name, subscriber_name,
                                    SPACEBREW_BRAINSERVER_IP, SPACEBREW_CASSANDRA_IP)

                time.sleep(1)
                self.sp_router.link(publisher_metric_name, subscriber_metric_name, publisher_name, subscriber_name,
                                    SPACEBREW_BRAINSERVER_IP, SPACEBREW_CASSANDRA_IP)


        end = time.time()

        print 'configuration took %s s' % (end - start)

    def handle_value_factory(self, metric, muse_port):

        def handle_value(csv_string):

            values = csv_string.split(',')

            # numerical values
            num_arguments = len(values)
            numerical_columns = ''.join([", value_%s" % i for i in range(num_arguments)])

            # column values
            timestamp = int(time.time() * 1000)
            column_values = "'%s', '%s', %s" % (muse_port, timestamp, values[0])
            if len(values) > 1:
                for value in values[1:]:
{'fraction_non_alphanumeric': 0.0737583395107487, 'fraction_numerical': 0.003150481838398814, 'mean_word_length': 2.6915184678522572, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3504154', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1532, 'n_words': 352}