# Path: cognitive/app/models.py
# class Experiment(models.Model):
#     EXPERIMENT_STATUS = (
#         ('Stop', ' Saved as stop'),
#         ('InComplete', 'More information required'),
#         ('InExecution', 'Execution started'),
#         ('Error', ' Error when executed'),
#         ('Completed', 'Executed Successfully'),
#     )
#     user = models.ForeignKey(User)
#     name = models.CharField(max_length=50)
#     status = models.CharField(max_length=50, choices=EXPERIMENT_STATUS, default='Stop')
#     created_time = models.DateTimeField(auto_now=True)
#     modified_time = models.DateTimeField(auto_now=True)
#     execution_start_time = models.DateTimeField(blank=True, null=True)
#     execution_end_time = models.DateTimeField(blank=True, null=True)
#     component_start_id = models.IntegerField(blank=True, null=True)
# 
#     class Meta:
#         ordering = ['-modified_time']
# 
# class Component(models.Model):
#     COMPONENT_STATUS = (
#         ('Stop', ' Saved as stop'),
#         ('InComplete', 'More information required'),
#         ('InExecution', 'Execution started'),
#         ('Error', ' Error when executed'),
#         ('Completed', 'Executed Successfully'),
#     )
# 
#     experiment = models.ForeignKey(Experiment)
#     status = models.CharField(max_length=50, choices=COMPONENT_STATUS, default='Stop')
#     operation_type = models.OneToOneField(DataOperationType, blank=True, null=True)
#     created_time = models.DateTimeField(auto_now=True)
#     modified_time = models.DateTimeField(auto_now=True)
#     execution_start_time = models.DateTimeField(blank=True, null=True)
#     execution_end_time = models.DateTimeField(blank=True, null=True)
#     data_location = models.CharField(max_length=50, blank=True, null=True)
#     preferred_data_location = models.CharField(max_length=50, blank=True, null=True)
#     # component_id = models.IntegerField(blank=True, null=True)


from ..models import Experiment, Component
from toposort import toposort_flatten
from rest_framework import viewsets
from django.http import HttpResponse
from django.conf import settings
from django.core import serializers
from collections import defaultdict
from pandas import *
import redis
import threading
import json
                depend_nodes.add(first_node)
            else:
                graph_data[second_node] = set()
                graph_data[second_node].add(first_node)
        topological_graph = toposort_flatten(graph_data)
        print "Graph after topological sort", topological_graph
        message = {
            'exp_id': self.experiment, 'result': self.comp_id,
            'graph': topological_graph, 'components': defaultdict()}

        for data in topological_graph:
            component_id = int(data)
            comp = Component.objects.get(pk=component_id)
            if comp.operation_type.function_type == 'Create':
                if comp.operation_type.function_arg == 'Table':
                        filename = comp.operation_type.function_subtype_arg
                        input_data = read_csv(filename)
                        message['input'] = {}
                        for elem in list(input_data.columns):
                            message['input'][elem] = list(input_data[elem])
                        message['cols'] = list(input_data.columns)
                        # message['input'] = input_data.to_dict()

            serialized_obj = serializers.serialize('json', [comp.operation_type, ])
            print "Component_id", component_id, " ", comp.operation_type
            message['components'][data] = serialized_obj

        print "Message ", message
        r = redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=0)
        self.pubsub = r.pubsub(ignore_subscribe_messages=True)

# Path: cognitive/app/models.py
# class Experiment(models.Model):
#     EXPERIMENT_STATUS = (
#         ('Stop', ' Saved as stop'),
#         ('InComplete', 'More information required'),
#         ('InExecution', 'Execution started'),
#         ('Error', ' Error when executed'),
#         ('Completed', 'Executed Successfully'),
#     )
#     user = models.ForeignKey(User)
#     name = models.CharField(max_length=50)
#     status = models.CharField(max_length=50, choices=EXPERIMENT_STATUS, default='Stop')
#     created_time = models.DateTimeField(auto_now=True)
#     modified_time = models.DateTimeField(auto_now=True)
#     execution_start_time = models.DateTimeField(blank=True, null=True)
#     execution_end_time = models.DateTimeField(blank=True, null=True)
#     component_start_id = models.IntegerField(blank=True, null=True)
# 
#     class Meta:
#         ordering = ['-modified_time']
# 
# class Component(models.Model):
#     COMPONENT_STATUS = (
#         ('Stop', ' Saved as stop'),
#         ('InComplete', 'More information required'),
#         ('InExecution', 'Execution started'),
#         ('Error', ' Error when executed'),
#         ('Completed', 'Executed Successfully'),
#     )
# 
#     experiment = models.ForeignKey(Experiment)
#     status = models.CharField(max_length=50, choices=COMPONENT_STATUS, default='Stop')
#     operation_type = models.OneToOneField(DataOperationType, blank=True, null=True)
#     created_time = models.DateTimeField(auto_now=True)
#     modified_time = models.DateTimeField(auto_now=True)
#     execution_start_time = models.DateTimeField(blank=True, null=True)
#     execution_end_time = models.DateTimeField(blank=True, null=True)
#     data_location = models.CharField(max_length=50, blank=True, null=True)
#     preferred_data_location = models.CharField(max_length=50, blank=True, null=True)
#     # component_id = models.IntegerField(blank=True, null=True)


# Path: cognitive/app/api/results_storm.py
from ..models import Experiment, Component
from toposort import toposort_flatten
from rest_framework import viewsets
from django.http import HttpResponse
from django.conf import settings
from django.core import serializers
from collections import defaultdict
from pandas import *
import redis
import threading
import json

                depend_nodes.add(first_node)
            else:
                graph_data[second_node] = set()
                graph_data[second_node].add(first_node)
        topological_graph = toposort_flatten(graph_data)
        print "Graph after topological sort", topological_graph
        message = {
            'exp_id': self.experiment, 'result': self.comp_id,
            'graph': topological_graph, 'components': defaultdict()}

        for data in topological_graph:
            component_id = int(data)
            comp = Component.objects.get(pk=component_id)
            if comp.operation_type.function_type == 'Create':
                if comp.operation_type.function_arg == 'Table':
                        filename = comp.operation_type.function_subtype_arg
                        input_data = read_csv(filename)
                        message['input'] = {}
                        for elem in list(input_data.columns):
                            message['input'][elem] = list(input_data[elem])
                        message['cols'] = list(input_data.columns)
                        # message['input'] = input_data.to_dict()

            serialized_obj = serializers.serialize('json', [comp.operation_type, ])
            print "Component_id", component_id, " ", comp.operation_type
            message['components'][data] = serialized_obj

        print "Message ", message
        r = redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=0)
        self.pubsub = r.pubsub(ignore_subscribe_messages=True)

        self.pubsub.subscribe("Exp " + str(self.experiment))