Commit acfae547 authored by Cristiano Urban's avatar Cristiano Urban
Browse files

Added 'search' method to 'vos_job' command.

parent 4140093a
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -10,9 +10,9 @@ _vos_job()
  COMPREPLY=()
  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"
  opts="info list results"
  opts="info list search results"

  if [[ ${cur} == info || ${cur} == results || ${COMP_CWORD} -eq 1 ]] ; then
  if [[ ${cur} == info || ${cur} == search || ${cur} == results || ${COMP_CWORD} -eq 1 ]] ; then
    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    return 0
  fi
+26 −0
Original line number Diff line number Diff line
@@ -72,6 +72,24 @@ class VOSJob(RedisRPCClient):
        else:
            sys.exit("\nFATAL: Unknown response type.\n")
            
    def search(self, searchStr):
        jobRequest = { "requestType": "JOB_SEARCH", "searchStr": searchStr }
        jobResponse = self.call(jobRequest)
        if "responseType" not in jobResponse:
            sys.exit("FATAL: Malformed response.\n")
        elif jobResponse["responseType"] == "SEARCH_DONE":
            jobSearch = jobResponse["jobSearch"]
            if jobSearch:
                print("\n" + tabulate(jobResponse["jobSearch"], headers = "keys", tablefmt = "pretty") + "\n")
            else:
                sys.exit(f"\nThe search did not return any results.\n")
        elif jobResponse["responseType"] == "ERROR":
            errorCode = jobResponse["errorCode"]
            errorMsg = jobResponse["errorMsg"]
            sys.exit(f"\nError code: {errorCode}\nError message: {errorMsg}\n")
        else:
            sys.exit("\nFATAL: Unknown response type.\n")

    def results(self, jobId):
        jobRequest = { "requestType": "JOB_RESULTS", "jobId": jobId }
        jobResponse = self.call(jobRequest)
@@ -117,6 +135,12 @@ DESCRIPTION
        prints a JSON object containing the job info according to the UWS specification.
        A job ID is required as argument.
        
    search
        performs a search on jobs and returns those having a match between the search string
        passed by the user and one of the following fields: 
        
        'job_id', 'job_type', 'ownder_id', 'user_name'

    results
        prints a JSON object containing the job results according to the UWS specification.
        A job ID is required as argument.
@@ -141,6 +165,8 @@ elif len(sys.argv) == 3:
            vosJobCli.help()
    elif cmd == "info":
        vosJobCli.info(arg)
    elif cmd == "search":
        vosJobCli.search(arg)
    elif cmd == "results":
        vosJobCli.results(arg)
    else:
+47 −0
Original line number Diff line number Diff line
@@ -509,6 +509,53 @@ class DbConnector(object):
        finally:
            self.connPool.putconn(conn, close = False)
            
    def searchJobs(self, searchStr):
        "Performs a search on jobs."
        conn = self.getConnection()
        try:
            cursor = conn.cursor(cursor_factory = RealDictCursor)
            cursor.execute("""
                SELECT job_id,
                       job_type,
                       phase,
                       start_time,
                       end_time,
                       owner_id
                FROM job
                JOIN users
                ON job.owner_id = users.user_id
                WHERE job_type IN ('pullFromVoSpace',
                                   'pullToVoSpace',
                                   'pushToVoSpace',
                                   'vos_data',
                                   'vos_group',
                                   'vos_import')
                AND (job_id ~ %s
                     OR job_type ~ %s
                     OR owner_id ~ %s
                     OR user_name ~ %s)
                ORDER BY creation_time DESC;
                """,
                (searchStr,
                 searchStr,
                 searchStr,
                 searchStr,))
            result = cursor.fetchall()
            cursor.close()
        except Exception:
            if not conn.closed:
                conn.rollback()
            raise
        else:
            for row in result:
                for idx in row:
                    el = row[idx]
                    if isinstance(el, datetime.datetime):
                        row[idx] = el.isoformat()
            return result
        finally:
            self.connPool.putconn(conn, close = False)

    ##### User #####

    def userExists(self, username):
+14 −0
Original line number Diff line number Diff line
@@ -113,6 +113,20 @@ class JobRPCServer(RedisRPCServer):
                             "errorCode": 2,
                             "errorMsg": errorMsg }
                return response
        elif requestBody["requestType"] == "JOB_SEARCH":
            searchStr = requestBody["searchStr"]
            try:
                result = self.dbConn.searchJobs(searchStr)
            except Exception:
                errorMsg = "Database error."
                self.logger.exception(errorMsg)
                response = { "responseType": "ERROR",
                             "errorCode": 2,
                             "errorMsg": errorMsg }
                return response
            else:
                response = { "responseType": "SEARCH_DONE", 
                             "jobSearch": result }
        else:
            errorMsg = "Unkown request type."
            self.logger.error(errorMsg)