Ejemplo n.º 1
0
def create_job(calc_mode, description, user_name, datadir, hc_id=None):
    """
    Create job for the given user, return it.

    :param str calc_mode:
        Calculation mode, such as classical, event_based, etc
    :param user_name:
        User who owns/started this job.
    :param datadir:
        Data directory of the user who owns/started this job.
    :param description:
         Description of the calculation
    :param hc_id:
        If not None, then the created job is a risk job
    :returns:
        :class:`openquake.server.db.models.OqJob` instance.
    """
    calc_id = get_calc_id(datadir) + 1
    job = models.OqJob.objects.create(id=calc_id,
                                      calculation_mode=calc_mode,
                                      description=description,
                                      user_name=user_name,
                                      ds_calc_dir=os.path.join(
                                          '%s/calc_%s' % (datadir, calc_id)))
    if hc_id:
        job.hazard_calculation = models.get(models.OqJob, pk=hc_id)
    job.save()
    return job.id
Ejemplo n.º 2
0
def create_job(calc_mode, description, user_name, datadir, hc_id=None):
    """
    Create job for the given user, return it.

    :param str calc_mode:
        Calculation mode, such as classical, event_based, etc
    :param user_name:
        User who owns/started this job.
    :param datadir:
        Data directory of the user who owns/started this job.
    :param description:
         Description of the calculation
    :param hc_id:
        If not None, then the created job is a risk job
    :returns:
        :class:`openquake.server.db.models.OqJob` instance.
    """
    calc_id = get_calc_id(datadir) + 1
    job = models.OqJob.objects.create(
        id=calc_id,
        calculation_mode=calc_mode,
        description=description,
        user_name=user_name,
        ds_calc_dir=os.path.join('%s/calc_%s' % (datadir, calc_id)))
    if hc_id:
        job.hazard_calculation = models.get(models.OqJob, pk=hc_id)
    job.save()
    return job.id
Ejemplo n.º 3
0
def get_result(result_id):
    """
    :returns: (job_id, job_status, datadir, datastore_key)
    """
    output = models.get(models.Output, pk=result_id)
    job = output.oq_job
    return job.id, job.status, os.path.dirname(job.ds_calc_dir), output.ds_key
Ejemplo n.º 4
0
def set_relevant(calc_id, flag):
    """
    Set the `relevant` field of the given calculation record
    """
    job = models.get(models.OqJob, pk=calc_id)
    job.relevant = flag
    job.save()
Ejemplo n.º 5
0
def del_calc(job_id, user):
    """
    Delete a calculation and all associated outputs.

    :param job_id:
        ID of a :class:`~openquake.server.db.models.OqJob`.
    """
    try:
        job = models.get(models.OqJob, pk=job_id)
    except exceptions.ObjectDoesNotExist:
        raise RuntimeError('Unable to delete hazard calculation: '
                           'ID=%s does not exist' % job_id)
    if job.user_name == user:
        # we are allowed to delete this

        # but first, check if any risk calculations are referencing any of our
        # outputs, or the hazard calculation itself
        msg = UNABLE_TO_DEL_HC_FMT % (
            'The following risk calculations are referencing this hazard'
            ' calculation: %s')

        assoc_outputs = models.OqJob.objects.filter(hazard_calculation=job)
        if assoc_outputs.count() > 0:
            raise RuntimeError(msg %
                               ', '.join(str(x.id) for x in assoc_outputs))
        job.delete()
    else:
        # this doesn't belong to the current user
        raise RuntimeError(UNABLE_TO_DEL_HC_FMT % 'Access denied')
    try:
        os.remove(job.ds_calc_dir + '.hdf5')
    except:  # already removed or missing permission
        pass
Ejemplo n.º 6
0
def get_result(result_id):
    """
    :returns: (job_id, job_status, datadir, datastore_key)
    """
    output = models.get(models.Output, pk=result_id)
    job = output.oq_job
    return job.id, job.status, os.path.dirname(job.ds_calc_dir), output.ds_key
Ejemplo n.º 7
0
def get_results(job_id):
    """
    :returns: (datadir, datastore_keys)
    """
    job = models.get(models.OqJob, pk=job_id)
    datadir = os.path.dirname(job.ds_calc_dir)
    return datadir, [output.ds_key for output in get_outputs(job_id)]
Ejemplo n.º 8
0
def set_relevant(calc_id, flag):
    """
    Set the `relevant` field of the given calculation record
    """
    job = models.get(models.OqJob, pk=calc_id)
    job.relevant = flag
    job.save()
Ejemplo n.º 9
0
def get_results(job_id):
    """
    :returns: (datadir, datastore_keys)
    """
    job = models.get(models.OqJob, pk=job_id)
    datadir = os.path.dirname(job.ds_calc_dir)
    return datadir, [output.ds_key for output in get_outputs(job_id)]
Ejemplo n.º 10
0
def get_output(output_id):
    """
    :param output_id: ID of an Output object
    :returns: (ds_key, calc_id, dirname)
    """
    out = models.get(models.Output, pk=output_id)
    return out.ds_key, out.oq_job.id, os.path.dirname(out.oq_job.ds_calc_dir)
Ejemplo n.º 11
0
def get_output(output_id):
    """
    :param output_id: ID of an Output object
    :returns: (ds_key, calc_id, dirname)
    """
    out = models.get(models.Output, pk=output_id)
    return out.ds_key, out.oq_job.id, os.path.dirname(out.oq_job.ds_calc_dir)
Ejemplo n.º 12
0
def del_calc(job_id, user):
    """
    Delete a calculation and all associated outputs.

    :param job_id:
        ID of a :class:`~openquake.server.db.models.OqJob`.
    """
    try:
        job = models.get(models.OqJob, pk=job_id)
    except exceptions.ObjectDoesNotExist:
        raise RuntimeError('Unable to delete hazard calculation: '
                           'ID=%s does not exist' % job_id)
    if job.user_name == user:
        # we are allowed to delete this

        # but first, check if any risk calculations are referencing any of our
        # outputs, or the hazard calculation itself
        msg = UNABLE_TO_DEL_HC_FMT % (
            'The following risk calculations are referencing this hazard'
            ' calculation: %s')

        assoc_outputs = models.OqJob.objects.filter(hazard_calculation=job)
        if assoc_outputs.count() > 0:
            raise RuntimeError(
                msg % ', '.join(str(x.id) for x in assoc_outputs))
        job.delete()
    else:
        # this doesn't belong to the current user
        raise RuntimeError(UNABLE_TO_DEL_HC_FMT % 'Access denied')
    try:
        os.remove(job.ds_calc_dir + '.hdf5')
    except:  # already removed or missing permission
        pass
Ejemplo n.º 13
0
def finish(job_id, status):
    """
    Set the job columns `is_running`, `status`, and `stop_time`
    """
    job = models.get(models.OqJob, pk=job_id)
    job.is_running = False
    job.status = status
    job.stop_time = datetime.utcnow()
    job.save()
Ejemplo n.º 14
0
def finish(job_id, status):
    """
    Set the job columns `is_running`, `status`, and `stop_time`
    """
    job = models.get(models.OqJob, pk=job_id)
    job.is_running = False
    job.status = status
    job.stop_time = datetime.utcnow()
    job.save()
Ejemplo n.º 15
0
def create_outputs(job_id, dskeys):
    """
    Build a correspondence between the outputs in the datastore and the
    ones in the database.

    :param job_id: ID of the current job
    :param dskeys: a list of datastore keys
    """
    job = models.get(models.OqJob, pk=job_id)
    for key in dskeys:
        models.Output.objects.create_output(
            job, DISPLAY_NAME.get(key, key), ds_key=key)
Ejemplo n.º 16
0
def calc_info(calc_id):
    """
    :param calc_id: calculation ID
    :returns: dictionary of info about the given calculation
    """
    job = models.get(models.OqJob, pk=calc_id)
    response_data = {}
    response_data['user_name'] = job.user_name
    response_data['status'] = job.status
    response_data['start_time'] = str(job.start_time)
    response_data['stop_time'] = str(job.stop_time)
    response_data['is_running'] = job.is_running
    return response_data
Ejemplo n.º 17
0
def create_outputs(job_id, dskeys):
    """
    Build a correspondence between the outputs in the datastore and the
    ones in the database.

    :param job_id: ID of the current job
    :param dskeys: a list of datastore keys
    """
    job = models.get(models.OqJob, pk=job_id)
    for key in dskeys:
        models.Output.objects.create_output(job,
                                            DISPLAY_NAME.get(key, key),
                                            ds_key=key)
Ejemplo n.º 18
0
def calc_info(calc_id):
    """
    :param calc_id: calculation ID
    :returns: dictionary of info about the given calculation
    """
    job = models.get(models.OqJob, pk=calc_id)
    response_data = {}
    response_data['user_name'] = job.user_name
    response_data['status'] = job.status
    response_data['start_time'] = str(job.start_time)
    response_data['stop_time'] = str(job.stop_time)
    response_data['is_running'] = job.is_running
    return response_data
Ejemplo n.º 19
0
def del_calc(job_id, user):
    """
    Delete a calculation and all associated outputs.

    :param job_id:
        ID of a :class:`~openquake.server.db.models.OqJob`.
    """
    try:
        job = models.get(models.OqJob, pk=job_id)
    except models.NotFound:
        return ('Unable to delete calculation from db: '
                'ID=%s does not exist' % job_id)
    if job.user_name == user:
        # we are allowed to delete this

        # but first, check if any risk calculations are referencing any of our
        # outputs, or the hazard calculation itself
        msg = UNABLE_TO_DEL_HC_FMT % (
            'The following risk calculations are referencing this hazard'
            ' calculation: %s')

        assoc_outputs = models.OqJob.objects.filter(hazard_calculation=job)
        if assoc_outputs.count() > 0:
            raise RuntimeError(
                msg % ', '.join(str(x.id) for x in assoc_outputs))
        # not using Django since we got strange errors on Ubuntu 12.04
        # like https://ci.openquake.org/job/master_oq-engine/2581/console
        db.connection.cursor().execute(
            'DELETE FROM job WHERE id=%d' % job_id)
    else:
        # this doesn't belong to the current user
        raise RuntimeError(UNABLE_TO_DEL_HC_FMT % 'Access denied')
    try:
        os.remove(job.ds_calc_dir + '.hdf5')
    except:  # already removed or missing permission
        pass