Example #1
0
def removeProc(server, databaseName, procname, **kwargs):
  '''
    This script will remove a "procname" key from database run document 
    and set the doc['status'] = 'good'. If "procname" == "proc0", then 
    doc['status'] will be set to 'closed' 


    You need to set the following arguments: 
    procname = the name of the "procname" object that will be removed  
    from the database documents (proc0, proc1, etc...) 

    server = the full URL to the main database server 
          (https://<username>:<password>@edelweiss.cloudant.com)
    databaseName = the name of the database (datadb) 


    This script will remove the "procname" object from a subset of the  
    database documents. To specify the particular subset of documents, 
    you MUST specify the following kwargs: 
    
    startkey = first run name (ex. "ma22a003_003")
    endkey = final run name (ex. "mb24b001_010")
    viewname = the DB View function that you use to get the list 
      of documents. 
      The most general view is "proc/daqdoc". You can use any of the 
      "Views" that return the run_name + file_name as a key. These are 
      "proc/bad", "proc/daqdoc", "proc/failed", "proc/inprogress", 
      "proc/inqueue", "proc/procX",  "proc/procXinprogress" (X = 1,2,3), 
      and "proc/raw"

    All documents that are returned by the viewname within the given 
    startkey/endkey range will be affected.

    Optional kwargs:

    status = if you set kwargs['status'] then only database documents 
    with this status will be affected
    
    test = if this is set to True, then no documents will be saved 
      back to the database. All other operations will be performed so that
      you may test the use of this function before affecting docs in 
      the database. 


    example: 
    
    import removeProc
    
    options = {};
    options['startkey'] = 'mj00a000_000'
    options['endkey'] = 'zz99z999';
    options['test'] = True;  #this will just be a test
    options['viewname'] = 'proc/failed'
    
    removeProc.removeProc('https://*****:*****@edelweiss.cloudant.com', 
      'datadb', 'proc1', options)

   '''

  db = Server(server)[databaseName]
  
  if kwargs['startkey'] == None or kwargs['endkey'] == None:
    print 'you must supply a range of runs to use. Set **kwargs["startkey"] and kwargs["endkey"]'
    return
  print 'using view', kwargs['viewname']

  new_status = 'good'
  if procname == 'proc0':
    new_status = 'closed'

    
  vr = db.view(kwargs['viewname'],  reduce = False, startkey=kwargs['startkey'], endkey=kwargs['endkey'])
  print len(vr), 'rows returned by the view'

  for row in vr:
    print 'checking', row['id']

    reqCom = '%s/%s/_design/proc/_update/removeproc/%s' % (server, databaseName, row['id'] )

    reqComDic = {'procname':procname, 'new_status':new_status}
    
    if kwargs.has_key('status') and kwargs['status'] != '':
      reqComDic['old_status'] = kwargs['status']

    if kwargs.has_key('test') and kwargs['test'] != '' and  kwargs['test'] is not False:
      reqComDic['test'] = kwargs['test']

    reqCom += '?' + urllib.urlencode(reqComDic)

    print 'HTTP PUT', reqCom
    resp = request(reqCom, method='PUT')
    respj = json.loads(resp.body_string())
    print json.dumps(respj, indent=1)
    print ''
Example #2
0
def resetStatus(server, databaseName, newstatus, **kwargs):
  '''
    This script will reset the "status" key from a database run document.  You must give a range of runs numbers
    by using the kwargs['startkey'] and kwargs['endkey']. You can change the status of a single run by specifing that
    run name for both 'startkey' and 'endkey'
    It will set the 'status' keyword for all documents unless you specify 'status'=='oldvalue' in the kwargs. If kwargs['status']
    is given, then only documents with 'status' = 'oldvalue' will be updated to the new status.
    (For now, it will change the status key of ALL partition files within a single run.)

    This script will reset the "status" key from a run document in the database.

    There are three options explicitly specified by this function. They are:
    
    newstatus = the value of doc['status']
    server = the full URL to the main database server (https://<username>:<password>@edelweiss.cloudant.com)
    databaseName = the name of the database (datadb)


    This script will affect only a subset of the database documents. 
    To specify this particular subset of documents, you MUST specify the following kwargs:
    
    startkey = first run name (ex. "ma22a003_003")
    endkey = final run name (ex. "mb24b001_010")
    viewname = the DB View function that you use to get the list of documents. The most general view is "proc/daqdoc". 
      You can use any of the "Views" that return the run_name_file_name as a key (i.e. ma22a003_003). 
      These are "proc/bad", "proc/daqdoc", "proc/failed", "proc/inprogress", "proc/inqueue", "proc/procX", 
      "proc/procXinprogress" (X = 1,2,3), and "proc/raw"

    All documents that are returned by the viewname within the given startkey/endkey range will be affected.

    Optional kwargs:

    status = if you set kwargs['status'] then only database documents with this status will have their status changed.

    test = if this is set to True, then no documents will be saved back to the database. All other operations will
      be performed so that you may test the use of this function before affecting docs in the database. 


    example: 
    
    import resetStatus
    
    options = {};
    options['startkey'] = 'mj00a000_000'
    options['endkey'] = 'zz99z999';
    options['test'] = True;  #this will just be a test
    options['viewname'] = 'proc/inqueue'
    options['status'] = 'proc1 queued'

    #reset these status values to 'good'
    resetStatus.resetStatus('https://*****:*****@edelweiss.cloudant.com', 'datadb', 'good', options)

  '''

  db = Server(server)[databaseName]

  print 'kwargs:' , kwargs

  if kwargs['startkey'] == None or kwargs['endkey'] == None:
    print 'you should supply a range of runs to use with **kwargs["startkey"] and kwargs["endkey"]'
    return

  vr = db.view(kwargs['viewname'],  reduce = False, startkey=kwargs['startkey'], endkey=kwargs['endkey'])

  for row in vr:
    print 'checking', row['id']

    reqCom = '%s/%s/_design/proc/_update/resetstatus/%s' % (server, databaseName, row['id'] )

    reqComDic = {'new_status':newstatus}

    if kwargs.has_key('status') and kwargs['status'] != '':
      reqComDic['old_status'] = kwargs['status']

    if kwargs.has_key('test') and kwargs['test'] != '' and  kwargs['test'] is not False:
      reqComDic['test'] = kwargs['test']

    reqCom += '?' + urllib.urlencode(reqComDic)

    print 'HTTP PUT', reqCom
    resp = request(reqCom, method='PUT')
    respj = json.loads(resp.body_string())
    print json.dumps(respj, indent=1)
    print ''