Exemplo n.º 1
0
def record_session_start(model, version, experiment, run_id, timestamp):
    """
    Records the start of a session

    When you start a new run, this method gets called to record the start of the session.
    After you've started a session you can record its final status and completion time with record_session_end.

    Parameters
    ----------
    model : string
        The name of the model
    version : int
        The version number of the model
    experiment : string
        The name of the experiment
    run_id : string
        The ID of the run
    timestamp : int
        The timestamp
    """
    global es

    ensure_model(model, timestamp)
    ensure_version(model, version, timestamp)
    ensure_experiment(model, version, experiment, timestamp)

    identifier = '{}-{}-{}-{}'.format(model, version, experiment, run_id)

    if not es.exists(index=index_name('run'), doc_type='run', id=identifier):
        run_data = {'model': model, 'version': version, 'run_id': run_id}

        es.index(index=index_name('run'),
                 doc_type='run',
                 id=identifier,
                 body=run_data)
Exemplo n.º 2
0
def ensure_index(name, mapping):
    """
    Ensures that the specified index exists with the correct mapping.

    If the index exists, nothing is done.
    When the index doesn't exist, it will be created with the provided mapping.

    Parameters
    ----------
    name : str
        Name of the index
    mapping: object
        The mapping for the elastic search index
    """

    global es

    if not es.indices.exists(index_name(name)):
        index_settings = {
            'settings': {
                'number_of_shards': 3,
                'number_of_replicas': 2
            },
            'mappings': {
                name: mapping
            }
        }

        es.indices.create(index_name(name), body=index_settings)
Exemplo n.º 3
0
def ensure_version(model, version, timestamp):
    """
    Ensures that model metadata is recorded.

    If the version does not exist, its metadata is recorded in the index.
    Otherwise, nothing is done.

    Parameters
    ----------
    model : str
        The name of the model
    version : int
        The version number
    timestamp: int
        The timestamp to use for recording creation date of the version
    """
    global es

    ensure_version_index()

    identifier = '{}-{}'.format(model, version)

    if not es.exists(
            index=index_name('version'), doc_type='version', id=identifier):
        version_data = {
            'model': model,
            'version_number': version,
            'date_created': timestamp
        }

        es.index(index=index_name('version'),
                 doc_type='version',
                 id=identifier,
                 body=version_data)
Exemplo n.º 4
0
def ensure_model(model, timestamp):
    """
    Ensures that model metadata is recorded

    If the model does not exist, its metadata is recorded in the index.
    Otherwise, nothing is done.

    Parameters
    ----------
    model : str
        The name of the model
    timestamp : int
        The timestamp to use as creation date when the model doesn't exist.
    """
    global es

    ensure_model_index()

    if not es.exists(index=index_name('model'), doc_type='model', id=model):
        model_data = {'date_created': timestamp}

        es.index(index=index_name('model'),
                 doc_type='model',
                 id=model,
                 body=model_data)
Exemplo n.º 5
0
def record_session_end(model, version, experiment, run_id, status, timestamp):
    """
    Records the end of a tracking session

    When you've started tracking a run with record_session_start you can call this method to signal
    the completion of the run. This updates the existing run document in the index with the completion time
    and status of the run.

    Please note that this function raises an error when you try to complete a run that wasn't started earlier.
    This is done to prevent the tool from recording "empty" sessions.

    Parameters
    ----------
    model : str
        The name of the model
    version : int
        The version number
    experiment : str
        The name of the experiment
    run_id : str
        The ID of the run
    status : str
        The status of the run (completed, failed)
    timestamp : int
        Timestamp indicating when the session was finished
    """
    global es

    ensure_model(model, timestamp)
    ensure_version(model, version, timestamp)
    ensure_experiment(model, version, experiment, timestamp)

    identifier = '{}-{}-{}-{}'.format(model, version, experiment, run_id)

    if not es.exists(index=index_name('run'), doc_type='run', id=identifier):
        raise AssertionError(
            'There was no tracking session start recorded so a session end cannot be stored.'
        )
    else:
        run_data = es.get(index=index_name('run'),
                          doc_type='run',
                          id=identifier)['_source']
        run_data['completed'] = timestamp
        run_data['status'] = status

        es.index(index='observatory-meta',
                 doc_type='run',
                 id=identifier,
                 body=run_data)
Exemplo n.º 6
0
def ensure_experiment(model, version, experiment, timestamp):
    """
    Ensures that model metadata is recorded

    If the experiment does not exist, its metadata is recorded in the index.
    Otherwise, nothing is done.

    Parameters
    ----------
    model : str
        The name of the model
    version : int
        The version number
    experiment : str
        The name of the experiment
    timestamp : int
        Timestamp to use for the creation date of the experiment if it doesn't exist
    """
    global es

    ensure_experiment_index()

    identifier = '{}-{}-{}'.format(model, version, experiment)

    if not es.exists(index=index_name('experiment'),
                     doc_type='experiment',
                     id=identifier):
        experiment_data = {
            'model': model,
            'version': version,
            'experiment': experiment,
            'date_created': timestamp
        }

        es.index(index=index_name('experiment'),
                 doc_type='experiment',
                 id=identifier,
                 body=experiment_data)
Exemplo n.º 7
0
def find_items(type_name, query, page_index):
    """
    Executes a search query for the specified item type and retrieves the specified search results page.

    This function assumes you're looking model, version, experiment or run.
    The index name is derived from the type parameter, if it doesn't exist you will receive a HTTP error.

    Parameters
    ----------
    type_name : str
        The type of item to find
    query : object
        The query DSL object to use for executing the query
    page_index : int
        The page index to retrieve

    Returns
    -------
    dictionary
        The results, this will contain a list of max. 20 items.
    """
    global es

    return es.search(index=index_name(type_name), body=query, from_=page_index * PAGE_SIZE, size=PAGE_SIZE)