Esempio n. 1
0
    def set_success(self,
                    task_id,
                    finished_at=None,
                    datasets=None,
                    outputs=None,
                    provenance=None):
        """Set status of the module that is associated with the given task
        identifier to success.

        If the web service signals that the task status was updated the result
        will be True, otherwise False. If the project or task were unknown the
        result is None.

        Parameters
        ----------
        task_id : string
            Unique task identifier
        finished_at: datetime.datetime, optional
            Timestamp when module started running
        datasets : dict, optional
            Dictionary of resulting datasets. The user-specified name is the key
            for the dataset identifier.
        outputs: vizier.viztrail.module.output.ModuleOutputs, optional
            Output streams for module
        provenance: vizier.viztrail.module.provenance.ModuleProvenance, optional
            Provenance information about datasets that were read and writen by
            previous execution of the module.

        Returns
        -------
        bool
        """
        # Get the request url and create the request body
        url = self.get_url(task_id)
        data = {labels.STATE: states.MODULE_SUCCESS}
        if not finished_at is None:
            data[labels.FINISHED_AT] = finished_at.isoformat()
        if not outputs is None:
            data[labels.OUTPUTS] = serialize.OUTPUTS(outputs)
        if not provenance is None:
            data[labels.PROVENANCE] = serialmd.PROVENANCE(provenance)
        # Send request
        r = requests.put(url, json=data)
        # A status code 200 will signal success. All other codes indicate an
        # error or unknown project or task identifier.
        if r.status_code != 200:
            return None
        result = json.loads(r.text)[labels.RESULT]
        return (result > 0)
Esempio n. 2
0
    def set_error(self, task_id, finished_at=None, outputs=None):
        """Set status of a given task to error.

        If the web service signals that the task status was updated the result
        will be True, otherwise False. If the project or task were unknown the
        result is None.

        Parameters
        ----------
        project_id : string
            Unique project identifier
        task_id : string
            Unique task identifier
        finished_at: datetime.datetime, optional
            Timestamp when module started running
        outputs: vizier.viztrail.module.output.ModuleOutputs, optional
            Output streams for module

        Returns
        -------
        bool
        """
        # Get the request url and create the request body
        url = self.get_url(task_id)
        data = {labels.STATE: states.MODULE_ERROR}
        if not finished_at is None:
            data[labels.FINISHED_AT] = finished_at.isoformat()
        if not outputs is None:
            data[labels.OUTPUTS] = serialize.OUTPUTS(outputs)
        # Send request
        r = requests.put(url, json=data)
        # A status code 200 will signal success. All other codes indicate an
        # error or unknown project or task identifier.
        if r.status_code != 200:
            return None
        result = json.loads(r.text)[labels.RESULT]
        return (result > 0)
Esempio n. 3
0
def MODULE_HANDLE(project: "ProjectHandle",
                  branch: BranchHandle,
                  module: ModuleHandle,
                  urls: UrlFactory,
                  workflow: Optional[WorkflowHandle] = None,
                  charts: List[ChartViewHandle] = None,
                  include_self: bool = True) -> Dict[str, Any]:
    """Dictionary serialization for a handle in the workflow at the branch
    head.

    The list of references will only contain a self referene if the include_self
    flag is True.

    Parameters
    ----------
    project: vizier.engine.project.base.ProjectHandle
        Handle for the containing project
    branch : vizier.viztrail.branch.BranchHandle
        Branch handle
    workflow: vizier.viztrail.workflow.WorkflowHandle
        Workflow handle
    module: vizier.viztrail.module.base.ModuleHandle
        Module handle
    charts: list(vizier.view.chart.ChartViewHandle)
        List of handles for available chart views
    urls: vizier.api.routes.base.UrlFactory
        Factory for resource urls
    include_self: bool, optional
        Indicate if self link is included

    Returns
    -------
    dict
    """
    project_id = project.identifier
    branch_id = branch.identifier
    module_id = module.identifier
    cmd = module.command
    timestamp = module.timestamp
    actual_workflow = branch.get_head() if workflow is None else workflow
    obj: Dict[str, Any] = {
        labels.ID:
        module_id,
        'state':
        module.state,
        labels.COMMAND: {
            labels.COMMAND_PACKAGE: cmd.package_id,
            labels.COMMAND_ID: cmd.command_id,
            labels.COMMAND_ARGS: cmd.arguments.to_list()
        },
        'text':
        module.external_form,
        labels.TIMESTAMPS: {
            labels.CREATED_AT: timestamp.created_at.isoformat()
        },
        labels.LINKS:
        serialize.HATEOAS({} if module_id is None else {
            ref.MODULE_INSERT:
            urls.workflow_module_insert(project_id=project_id,
                                        branch_id=branch_id,
                                        module_id=module_id)
        })
    }
    if include_self:
        obj[labels.LINKS].extend(
            serialize.HATEOAS({} if module_id is None else {
                ref.SELF:
                urls.get_workflow_module(project_id=project_id,
                                         branch_id=branch_id,
                                         module_id=module_id),
                ref.MODULE_DELETE:
                urls.workflow_module_delete(project_id=project_id,
                                            branch_id=branch_id,
                                            module_id=module_id),
                ref.MODULE_REPLACE:
                urls.workflow_module_replace(project_id=project_id,
                                             branch_id=branch_id,
                                             module_id=module_id)
            }))
    if not timestamp.started_at is None:
        obj[labels.TIMESTAMPS][
            labels.STARTED_AT] = timestamp.started_at.isoformat()
    # Add outputs and datasets if module is not active.
    if not module.is_active:
        artifacts: Dict[str, ArtifactDescriptor] = dict()
        for precursor in actual_workflow.modules:
            artifacts = precursor.provenance.get_database_state(artifacts)
            if precursor == module:
                break
        datasets = list()
        other_artifacts = list()
        for artifact_name in artifacts:
            artifact = artifacts[artifact_name]
            if artifact.is_dataset:
                datasets.append(
                    serialds.DATASET_IDENTIFIER(identifier=artifact.identifier,
                                                name=artifact_name))
            else:
                other_artifacts.append(
                    serialds.ARTIFACT_DESCRIPTOR(artifact=artifact,
                                                 project=project_id))
        available_charts = list()
        if charts is not None:
            for c_handle in charts:
                available_charts.append({
                    labels.NAME:
                    c_handle.chart_name,
                    labels.LINKS:
                    serialize.HATEOAS({} if module_id is None else {
                        ref.SELF:
                        urls.get_chart_view(project_id=project_id,
                                            branch_id=branch_id,
                                            workflow_id=actual_workflow.
                                            identifier,
                                            module_id=module_id,
                                            chart_id=c_handle.identifier)
                    })
                })
        obj[labels.DATASETS] = datasets
        obj[labels.CHARTS] = available_charts
        obj[labels.OUTPUTS] = serialize.OUTPUTS(module.outputs)
        obj[labels.ARTIFACTS] = other_artifacts
        if not timestamp.finished_at is None:
            obj[labels.TIMESTAMPS][
                labels.FINISHED_AT] = timestamp.finished_at.isoformat()
    else:
        # Add empty lists for outputs, datasets and charts if the module is
        # active
        obj[labels.DATASETS] = list()
        obj[labels.CHARTS] = list()
        obj[labels.OUTPUTS] = serialize.OUTPUTS(ModuleOutputs())
        obj[labels.ARTIFACTS] = list()
    return obj