# 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
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

# TODO: [refactor] this import statement should specify some file instead of '*'

CACHE = {}


class storm_client (threading.Thread):

    def __init__(self, thread_id, name, experiment, component_id, max_results, cache_results):
        threading.Thread.__init__(self)
        self.threadID = thread_id
        self.name = name
        self.experiment = experiment
        self.comp_id = component_id
        self.result = {}
        self.max_results = max_results
        self.cache_results = cache_results
        print "Submitting topology to storm. End component", self.comp_id

# 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

#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

# TODO: [refactor] this import statement should specify some file instead of '*'

CACHE = {}


class storm_client (threading.Thread):

    def __init__(self, thread_id, name, experiment, component_id, max_results, cache_results):
        threading.Thread.__init__(self)
        self.threadID = thread_id
        self.name = name
        self.experiment = experiment
        self.comp_id = component_id
        self.result = {}
        self.max_results = max_results
        self.cache_results = cache_results
        print "Submitting topology to storm. End component", self.comp_id

        exp = Experiment.objects.get(pk=self.experiment)