## ------------------------------------------------------------------------
## iSiLK - A python-based graphical user interface for SiLK
## ------------------------------------------------------------------------
## Copyright (c) 2007-2008 Carnegie Mellon University. All Rights Reserved.
## ------------------------------------------------------------------------
## Authors: Andrew Kompanek (ajk@cert.org)
## ------------------------------------------------------------------------
## GNU General Public License (GPL) Rights pursuant to Version 2, June 1991
## Government Purpose License Rights (GPLR) pursuant to DFARS 252.225-7013
## ------------------------------------------------------------------------
## See the LICENSE file included in this distribution
## ------------------------------------------------------------------------

import os, os.path, sys, string, tempfile, copy, time, shutil
import pdb
import config.silk as config
from analysisresult import ProblemSet
from analysisresult import change_event_trigger
from analysisresult import SimpleDataCache
from analysisresult import AnalysisResult
import silk

# Fixed set of fields -- consider changing this
DEFAULT_FILTER_FIELDS = [ "nhip", "sensor", "sip", "dip", "sport", "dport",
    "proto", "packets", "bytes", "flags", "stime", "dur", "etime" ]


class QueryResult(AnalysisResult):
    '''A data set encapsulates an rwfilter pull and the associated data'''
    def __init__(self, parent, name=None, **kargs):
        if not isinstance(parent, ProblemSet):
            parent = parent.GetProblemSet()
        print "In QueryResult.__init__: kargs = %s" % kargs
        opts = copy.copy(kargs)
        print "opts in QueryResult = %s" % opts
        AnalysisResult.__init__(self, AnalysisResult.FILTER_RESULT,
                 name, "rwf", parent, opts=opts)
                 
        # Don't hide sval, dval if opts contain pmap file
        if "pmap-file" in opts:
            self.viewOptions["RECVIEW_HIDE_COLUMNS"].remove("sval")
            self.viewOptions["RECVIEW_HIDE_COLUMNS"].remove("dval")
                 
            #raise Exception, "QueryResult must be added to a ProblemSet"
        # Display a fixed set of fields for binary data
        self.fields = copy.copy(DEFAULT_FILTER_FIELDS)
        # For xmlutils -- what to export
        # FIXME: Move more of this into AnalysisResult?
        self._xmlChildren =["availableLocal", "availableRemote",
            "uniqueId", "name", "outputFileName",
            "location","status", "opts", "viewOptions", "fileinfo",
            "recCount", "asc_file_size", "children"]
    def IsQueryResult(self):
        return True


# Result of filtering some other filter-data result
class FilterResult(AnalysisResult):
    def __init__(self, parent, name=None, **kargs):
        AnalysisResult.__init__(self, AnalysisResult.FILTER_RESULT,
                    name, "rwf", parent, kargs)
        self.fields = copy.copy(DEFAULT_FILTER_FIELDS)
        self._xmlChildren =["availableLocal", "availableRemote",
          "uniqueId", "name", "outputFileName",
          "location", "status", "opts", "viewOptions", "fileinfo",
          "recCount", "asc_file_size", "children"]
        #self.labels = parent.GetLabels()


class SimpleSplitResult(AnalysisResult):
     def __init__(self, parent, name=None, **kargs):
         AnalysisResult.__init__(self, AnalysisResult.FILTER_RESULT,
                     name, "rwf", parent, kargs)
         self.fields = copy.copy(DEFAULT_FILTER_FIELDS)
         self._xmlChildren =["availableLocal", "availableRemote",
             "uniqueId", "name", "outputFileName",
             "location", "status", "opts", "viewOptions", "fileinfo",
             "recCount", "asc_file_size", "children", "labels"]

class UniqResult(AnalysisResult):
    def __init__(self, parent, name=None, **kargs):
        AnalysisResult.__init__(self, AnalysisResult.COUNT_RESULT, 
            name, "asc", parent, kargs)
            
        # By default display everything
        self.viewOptions["RECVIEW_HIDE_COLUMNS"] = []
            
        self._xmlChildren =["availableLocal", "availableRemote",
            "uniqueId", "name", "outputFileName",
            "location", "status", "opts", "viewOptions", "fileinfo",
            "recCount", "asc_file_size", "children", "labels"]
        self._xmlChildren.append("fields")


class SetResult(AnalysisResult):
    def __init__(self, parent, name=None, **kargs):
        AnalysisResult.__init__(self, AnalysisResult.SET_RESULT, 
            name, "set", parent, kargs)
        self._xmlChildren =["availableLocal", "availableRemote",
            "uniqueId", "name", "outputFileName",
            "location", "status", "opts", "viewOptions", "fileinfo",
            "recCount", "asc_file_size", "children"]
        self._xmlChildren.append("fields")
        

class CountResult(AnalysisResult):
    def __init__(self, parent, name=None, **kargs):
        AnalysisResult.__init__(self, AnalysisResult.TREND_RESULT, 
            name, "asc", parent, kargs)
        self._xmlChildren =["availableLocal", "availableRemote",
            "uniqueId", "name", "outputFileName",
            "location", "status", "opts", "viewOptions", "fileinfo",
            "recCount", "asc_file_size", "children", "labels"]
        self._xmlChildren.append("fields")
        
# Generic tool result (neither count nor unique like)
class ToolResult(AnalysisResult):
    def __init__(self, parent, name=None, **kargs):
        AnalysisResult.__init__(self, AnalysisResult.TREND_RESULT, 
            name, "asc", parent, kargs)
        self._xmlChildren =["availableLocal", "availableRemote",
            "uniqueId", "name", "outputFileName",
            "location", "status", "opts", "viewOptions", "fileinfo",
            "recCount", "asc_file_size", "children", "labels"]
        self._xmlChildren.append("fields")
                
                
        


