#!/usr/bin/env python
"""
_FailedJobs_

MySQL implementation of Workflow.FailedJobs
"""




from WMCore.Database.DBFormatter import DBFormatter

class FailedJobs(DBFormatter):
    sql = """SELECT wmbs_users.cert_dn as owner, wmbs_workflow.task, wmbs_job.id
                    FROM wmbs_workflow
               INNER JOIN wmbs_users ON
                 wmbs_workflow.owner = wmbs_users.id
               INNER JOIN wmbs_subscription ON
                 wmbs_workflow.id = wmbs_subscription.workflow
               LEFT OUTER JOIN wmbs_jobgroup ON
                 wmbs_subscription.id = wmbs_jobgroup.subscription
               LEFT OUTER JOIN wmbs_job ON
                 wmbs_jobgroup.id = wmbs_job.jobgroup
               LEFT OUTER JOIN wmbs_job_state ON
                 wmbs_job.state = wmbs_job_state.id
             WHERE wmbs_job.outcome = 0 AND
                   (wmbs_job_state.name = 'exhausted' OR
                    wmbs_job_state.name = 'cleanout')"""

    def execute(self, conn = None, transaction = False):
        result = self.dbi.processData(self.sql, conn = conn, transaction = transaction)
        return self.formatDict(result)
