Exemple #1
0
    def commit_result(wf_module: WfModule, result: ProcessResult,
                      stored_object_json: Optional[Dict[str, Any]]=None
                      ) -> None:
        """
        Store fetched result, if it is a change from wfm's existing data.

        Save the WfModule's `status` and `fetch_error`.

        Set wfm.last_update_check, regardless.

        If there is no error and there is new data, create (and run) a
        ChangeDataVersionCommand.

        Notify the user.
        """
        if result.dataframe.empty and result.error:
            with wf_module.workflow.cooperative_lock():
                wf_module.last_update_check = timezone.now()
                wf_module.fetch_error = result.error
                wf_module.is_busy = False
                wf_module.save()
            websockets.ws_client_rerender_workflow(wf_module.workflow)
        else:
            save_result_if_changed(
                wf_module,
                result,
                stored_object_json=stored_object_json
            )
def notify_client_workflow_version_changed(workflow):
    websockets.ws_client_rerender_workflow(workflow)
Exemple #3
0
 def set_error(self, message, notify=True):
     self.error_msg = message
     self.status = self.ERROR
     if notify:
         ws_client_rerender_workflow(self.workflow)
     self.save()
Exemple #4
0
 def set_ready(self, notify=True):
     self.status = self.READY
     if notify:
         ws_client_rerender_workflow(self.workflow)
     self.save()
Exemple #5
0
def save_result_if_changed(
        wfm: WfModule,
        new_result: ProcessResult,
        stored_object_json: Optional[Dict[str,
                                          Any]] = None) -> datetime.datetime:
    """
    Store fetched table, if it is a change from wfm's existing data.

    "Change" here means either a changed table or changed error message.

    Set `fetch_error` to `new_result.error`.

    Set sfm.is_busy to False.

    Set wfm.last_update_check.

    Create (and run) a ChangeDataVersionCommand.

    Notify the user.

    Return the timestamp (if changed) or None (if not).
    """
    with wfm.workflow.cooperative_lock():
        wfm.last_update_check = timezone.now()

        # Store this data only if it's different from most recent data
        old_result = ProcessResult(dataframe=wfm.retrieve_fetched_table(),
                                   error=wfm.error_msg)
        new_table = new_result.dataframe
        version_added = wfm.store_fetched_table_if_different(
            new_table, metadata=json.dumps(stored_object_json))

        if version_added:
            enforce_storage_limits(wfm)

            output_deltas = \
                find_output_deltas_to_notify_from_fetched_tables(wfm,
                                                                 old_result,
                                                                 new_result)
        else:
            output_deltas = []

        wfm.is_busy = False
        wfm.fetch_error = new_result.error
        wfm.save()

        # Mark has_unseen_notifications via direct SQL
        WfModule.objects \
            .filter(id__in=[od.wf_module_id for od in output_deltas]) \
            .update(has_unseen_notification=True)

    # un-indent: COMMIT so we notify the client _after_ COMMIT
    if version_added:
        ChangeDataVersionCommand.create(wfm, version_added)  # notifies client

        for output_delta in output_deltas:
            email_output_delta(output_delta, version_added)
    else:
        # no new data version, but we still want client to update WfModule
        # status and last update check time
        websockets.ws_client_rerender_workflow(wfm.workflow)

    return version_added
Exemple #6
0
 def set_is_collapsed(self, collapsed, notify=True):
     self.is_collapsed = collapsed
     self.save()
     if notify:
         websockets.ws_client_rerender_workflow(self.workflow)
Exemple #7
0
 def set_ready(self, notify=True):
     self.status = self.READY
     self.error_msg = ''
     self.save()
     if notify:
         websockets.ws_client_rerender_workflow(self.workflow)