<commit_before>''''
'''

##
#  Copyright 2007-2012 University Of Southern California
#
#  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.
##

# Revision : $Revision: 2012 $

__author__ = '<NAME>'

from netlogger.analysis.modules._base import SQLAlchemyInit
from netlogger.analysis.schema.schema_check import ErrorStrings, SchemaCheck, SchemaVersionError
from netlogger.analysis.schema.stampede_dashboard_schema import DashboardWorkflow, DashboardWorkflowstate, initializeToDashboardDB
from netlogger.analysis.schema.stampede_schema import *
from netlogger.nllog import DoesLogging

from Pegasus.dashboard.error.Errors import MasterDBNotFoundError
from netlogger.analysis.error.Error import StampedeDBNotFoundError

class MasterDatabase (SQLAlchemyInit, DoesLogging):
    
    def __init__(self, connString=None):
        if connString is None:
            raise ValueError('Connection string is required')
        DoesLogging.__init__(self)
        
        try:
            SQLAlchemyInit.__init__(self, connString, initializeToDashboardDB)
        except exceptions.OperationalError, e:
            self.log.error('init', msg='%s' % ErrorStrings.get_init_error(e))
            raise MasterDBNotFoundError
    
    def close (self):
        self.log.debug ('close')
        self.disconnect ()
    
    def get_wf_db_url (self, wf_id):
        '''
        Given a work-flow UUID, query the master database to get the connection URL for the work-flow's STAMPEDE database. 
        '''
        
        w = orm.aliased(DashboardWorkflow, name='w')
        
        q = self.session.query (w.db_url)
        q = q.filter (w.wf_id == wf_id)
        
        return q.one ().db_url
    
    def get_wf_id_url (self, root_wf_id):
        '''
        Given a work-flow UUID, query the master database to get the connection URL for the work-flow's STAMPEDE database. 
        '''
        
        w = orm.aliased(DashboardWorkflow, name='w')
        
        q = self.session.query (w.wf_id, w.wf_uuid, w.db_url)
        q = q.filter (w.wf_id == root_wf_id)
        
        q = q.one ()
        
        return q.wf_id, q.wf_uuid, q.db_url

    def get_all_workflows(self, **table_args):
        '''
        SELECT w.*, ws.*
         FROM   workflow w 
                     JOIN workflowstate ws ON w.wf_id = ws.wf_id 
                JOIN (SELECT wf_id, max(timestamp) time 
                            FROM   workflowstate 
                            GROUP  BY wf_id) t ON ws.wf_id = t.wf_id 
                           AND ws.timestamp = t.time 
         WHERE  w.wf_id = ws.wf_id 
        AND ws.wf_id = t.wf_id 
        AND ws.timestamp = t.time; 
        '''
        
        w = orm.aliased(DashboardWorkflow, name='w')
        ws = orm.aliased(DashboardWorkflowstate, name='ws')
        
        # Get last state change for each work-flow.
        qmax = self.session.query (DashboardWorkflowstate.wf_id, func.max (DashboardWorkflowstate.timestamp).label ('max_time'))
        qmax = qmax.group_by (DashboardWorkflowstate.wf_id)
        
        qmax = qmax.subquery ('max_timestamp')
        
        state = case([(ws.status == None, 'Running'), (ws.status == 0, 'Successful'), (ws.status != 0, 'Failed')], else_='Undefined').label ('state')
        
        q = self.session.query (w.wf_id, w.wf_uuid, w.timestamp,
                                w.dag_file_name, w.submit_hostname,
                                w.submit_dir, w.planner_arguments,
                                w.user, w.grid_dn, w.planner_version,
                                w.dax_label, w.dax_version, w.db_url,
                                state)
        
        q = q.filter (w.wf_id == ws.wf_id)
        q = q.filter (ws.wf_id == qmax.c.wf_id)
        q = q.filter (ws.timestamp == qmax.c.max_time)
        
        # Get Total Count. Need this to pass to jQuery Datatable.
        count = q.count ()
        if count == 0:
            return (0, 0, [])
        
        if 'filter' in table_args:
            filter_text = '%' + table_args ['filter'] + '%'
            q = q.filter (or_ (w.dax_label.like (filter_text), w.submit_dir.like (filter_text), state.like (filter_text)))
        
        # Get Total Count. Need this to pass to jQuery Datatable.
        filtered = q.count ()
        
        if filtered == 0:
            return (count, 0, [])
        
        if 'sort-col-count' in table_args:
            for i in range (table_args ['sort-col-count']):
                
                if 'iSortCol_' + str(i) in table_args:
                    if 'sSortDir_' + str(i) in table_args and table_args ['sSortDir_' + str(i)] == 'asc':
                        i = table_args ['iSortCol_' + str(i)]

                        if i == 0:
                            q = q.order_by (w.dax_label)
                        elif i == 1:
                            q = q.order_by (w.submit_dir)
                        elif i == 2:
                            q = q.order_by (state)
                        elif i == 3:
                            q = q.order_by (w.timestamp)
                        else:
                            raise ValueError, ('Invalid column (%s) in work-flow listing ' % i)
                    else:
                        i = table_args ['iSortCol_' + str(i)]

                        if i == 0:
                            q = q.order_by (desc (w.dax_label))
                        elif i == 1:
                            q = q.order_by (desc (w.submit_dir))
                        elif i == 2:
                            q = q.order_by (desc (state))
                        elif i == 3:
                            q = q.order_by (desc (w.timestamp))
                        else:
                            raise ValueError, ('Invalid column (%s) in work-flow listing ' % i)

        else:
            # Default sorting order
            q = q.order_by (desc (w.timestamp))  <commit_msg>Reduce line of code, by rewritting switch statement<commit_after>''''
'''

##
#  Copyright 2007-2012 University Of Southern California
#
#  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.
##

# Revision : $Revision: 2012 $

__author__ = '<NAME>'

from netlogger.analysis.modules._base import SQLAlchemyInit
from netlogger.analysis.schema.schema_check import ErrorStrings, SchemaCheck, SchemaVersionError
from netlogger.analysis.schema.stampede_dashboard_schema import DashboardWorkflow, DashboardWorkflowstate, initializeToDashboardDB
from netlogger.analysis.schema.stampede_schema import *
from netlogger.nllog import DoesLogging

from Pegasus.dashboard.error.Errors import MasterDBNotFoundError
from netlogger.analysis.error.Error import StampedeDBNotFoundError

class MasterDatabase (SQLAlchemyInit, DoesLogging):
    
    def __init__(self, connString=None):
        if connString is None:
            raise ValueError('Connection string is required')
        DoesLogging.__init__(self)
        
        try:
            SQLAlchemyInit.__init__(self, connString, initializeToDashboardDB)
        except exceptions.OperationalError, e:
            self.log.error('init', msg='%s' % ErrorStrings.get_init_error(e))
            raise MasterDBNotFoundError
    
    def close (self):
        self.log.debug ('close')
        self.disconnect ()
    
    def get_wf_db_url (self, wf_id):
        '''
        Given a work-flow UUID, query the master database to get the connection URL for the work-flow's STAMPEDE database. 
        '''
        
        w = orm.aliased(DashboardWorkflow, name='w')
        
        q = self.session.query (w.db_url)
        q = q.filter (w.wf_id == wf_id)
        
        return q.one ().db_url
    
    def get_wf_id_url (self, root_wf_id):
        '''
        Given a work-flow UUID, query the master database to get the connection URL for the work-flow's STAMPEDE database. 
        '''
        
        w = orm.aliased(DashboardWorkflow, name='w')
        
        q = self.session.query (w.wf_id, w.wf_uuid, w.db_url)
        q = q.filter (w.wf_id == root_wf_id)
        
        q = q.one ()
        
        return q.wf_id, q.wf_uuid, q.db_url

    def get_all_workflows(self, **table_args):
        '''
        SELECT w.*, ws.*
         FROM   workflow w 
                     JOIN workflowstate ws ON w.wf_id = ws.wf_id 
                JOIN (SELECT wf_id, max(timestamp) time 
                            FROM   workflowstate 
                            GROUP  BY wf_id) t ON ws.wf_id = t.wf_id 
                           AND ws.timestamp = t.time 
         WHERE  w.wf_id = ws.wf_id 
        AND ws.wf_id = t.wf_id 
        AND ws.timestamp = t.time; 
        '''
        
        w = orm.aliased(DashboardWorkflow, name='w')
        ws = orm.aliased(DashboardWorkflowstate, name='ws')
        
        # Get last state change for each work-flow.
        qmax = self.session.query (DashboardWorkflowstate.wf_id, func.max (DashboardWorkflowstate.timestamp).label ('max_time'))
        qmax = qmax.group_by (DashboardWorkflowstate.wf_id)
        
        qmax = qmax.subquery ('max_timestamp')
        
        state = case([(ws.status == None, 'Running'), (ws.status == 0, 'Successful'), (ws.status != 0, 'Failed')], else_='Undefined').label ('state')
        
        q = self.session.query (w.wf_id, w.wf_uuid, w.timestamp,
                                w.dag_file_name, w.submit_hostname,
                                w.submit_dir, w.planner_arguments,
                                w.user, w.grid_dn, w.planner_version,
                                w.dax_label, w.dax_version, w.db_url,
                                state)
        
        q = q.filter (w.wf_id == ws.wf_id)
        q = q.filter (ws.wf_id == qmax.c.wf_id)
        q = q.filter (ws.timestamp == qmax.c.max_time)
        
        # Get Total Count. Need this to pass to jQuery Datatable.
        count = q.count ()
        if count == 0:
            return (0, 0, [])
        
        if 'filter' in table_args:
            filter_text = '%' + table_args ['filter'] + '%'
            q = q.filter (or_ (w.dax_label.like (filter_text), w.submit_dir.like (filter_text), state.like (filter_text)))
        
        # Get Total Count. Need this to pass to jQuery Datatable.
        filtered = q.count ()
        
        if filtered == 0:
            return (count, 0, [])
        
        display_columns = [w.dax_label, w.submit_dir, state, w.timestamp]
        
        if 'sort-col-count' in table_args:
            for i in range (table_args ['sort-col-count']):
                
                if 'iSortCol_' + str(i) in table_args:
                    if 'sSortDir_' + str(i) in table_args and table_args ['sSortDir_' + str(i)] == 'asc':
                        i = table_args ['iSortCol_' + str(i)]

                        if i >= 0 and i < len(display_columns):
                            q = q.order_by (display_columns [i])
                        else:
                            raise ValueError, ('Invalid column (%s) in work-flow listing ' % i)
                    else:
                        i = table_args ['iSortCol_' + str(i)]

                        if i >= 0 and i < len(display_columns):
                            q = q.order_by (desc (display_columns [i]))
                        else:
                            raise ValueError, ('Invalid column (%s) in work-flow listing ' % i)

        else:
            # Default sorting order
            q = q.order_by (desc (w.timestamp))  
{'fraction_non_alphanumeric': 0.09911646586345381, 'fraction_numerical': 0.004497991967871486, 'mean_word_length': 1.7167794021383374, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5521713', 'n_tokens_mistral': 3810, 'n_tokens_neox': 3631, 'n_words': 987}