Example #1
0
    def query(self, session):
        """
        Queries the gbdx catalog and returns the results.
        Note that this object caches the query results,
        so that multiple calls to this method with no
        other changes will result in only one call to the
        network (unless the cache expiration period has elapsed).
        If you have changed the query parameters, such as
        by setting a new AOI value, you MUST call the
        refresh() method before performing a new query,
        or you will get the results from the old parameters.
        @param session: The gbdx session object
        """
        query_start = time.time()
        if self._last_query_results:
            if query_start < self._last_query_time + \
                             GBDXQuery.QUERY_CACHE_DURATION:
                #use cached results
                return GBDXQueryResult(self._last_query_results)

        payload = json.dumps(self.search_body)
        url = "/".join([GBDX_BASE_URL,'catalog','v1','search'])
        json_res = post_json(session, url, payload)

        self._last_query_results = json_res
        self._last_query_time = query_start

        query_results = GBDXQueryResult(json_res)

        return query_results
Example #2
0
def search_workflows(sess, state="all", owner=None, lookback_h=3, details=False, verbose=False):
    """
    Lists the workflows that are in a given state
    @param sess: The gbdx session object
    @param state: The state of the workflow. Must be one of the
    values defined by GBDX_WORKFLOW_STATES
    @param owner: If provided, will filter by the owner's id, otherwise
    will show the results for any owner.
    @param lookback_h: The number of hours from now to constrain
    the search. Default is 3, so you can list all the workflows in
    a given state within the past 3 hours.
    @param details: If True, this function will also retrieve summary details
    for each of the workflows that match the filter. If False, only the workflow
    ids will be returned. This will be slow if there are many workflows.
    @param verbose: If True and details is also true, then as the workflow details
    are returned, they are printed to standard out.
    @return: (workflow_ids, summary), where workflow_ids is a list of workflow ids,
    and summary contains the summary information, only if details=True, else None.
    """
    state = state.lower()
    assert state in GBDX_WORKFLOW_STATES
    url = "/".join([GBDX_BASE_URL,'workflows','v1','workflows','search'])
    search_filter = {"state":state, "lookback_h":lookback_h}
    if owner:
        search_filter["owner"]=owner
    payload = json.dumps(search_filter)
    ret = post_json(sess, url, payload)
    workflow_ids = ret['Workflows']
    summary = None
    if details:
        summary = ""
        for wf_id in workflow_ids:
            tmp = get_workflow_status(sess, wf_id)
            this_task = "Workflow {id} ({owner})\n".format(**tmp)
            this_task += summarize_workflow_tasks(tmp)
            this_task += "\n"
            if verbose: 
                print this_task,  #incrementally print results
                sys.stdout.flush()
            summary += this_task
        print ""
    return (workflow_ids, summary)