Преглед изворни кода

Refresh execution timeline statuses while active

Timeline statuses used to be merged from the
executions embedded in the transfer details response.
Since those are no longer returned, refresh the
current page from the executions endpoint during
polling instead.

Signed-off-by: Mihaela Balutoiu <mbalutoiu@cloudbasesolutions.com>
Mihaela Balutoiu пре 6 дана
родитељ
комит
5b281b1291
2 измењених фајлова са 73 додато и 43 уклоњено
  1. 8 0
      src/sources/TransferSource.ts
  2. 65 43
      src/stores/TransferStore.ts

+ 8 - 0
src/sources/TransferSource.ts

@@ -151,6 +151,8 @@ class TransferSource {
       limit?: number;
       limit?: number;
       marker?: string | null;
       marker?: string | null;
       quietError?: boolean;
       quietError?: boolean;
+      sortKeys?: string[];
+      sortDirs?: string[];
     },
     },
   ): Promise<Execution[]> {
   ): Promise<Execution[]> {
     const params: string[] = [];
     const params: string[] = [];
@@ -160,6 +162,12 @@ class TransferSource {
     if (options?.limit !== undefined) {
     if (options?.limit !== undefined) {
       params.push(`limit=${options.limit}`);
       params.push(`limit=${options.limit}`);
     }
     }
+    options?.sortKeys?.forEach(key => {
+      params.push(`sort_keys=${encodeURIComponent(key)}`);
+    });
+    options?.sortDirs?.forEach(dir => {
+      params.push(`sort_dirs=${encodeURIComponent(dir)}`);
+    });
     const queryString = params.length > 0 ? `?${params.join("&")}` : "";
     const queryString = params.length > 0 ? `?${params.join("&")}` : "";
     const response = await Api.send({
     const response = await Api.send({
       url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/transfers/${transferId}/executions${queryString}`,
       url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/transfers/${transferId}/executions${queryString}`,

+ 65 - 43
src/stores/TransferStore.ts

@@ -14,10 +14,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
 import { observable, action, runInAction } from "mobx";
 import { observable, action, runInAction } from "mobx";
 
 
-import TransferSource, {
-  TransferSourceUtils,
-  sortTasks,
-} from "@src/sources/TransferSource";
+import TransferSource from "@src/sources/TransferSource";
 import type {
 import type {
   UpdateData,
   UpdateData,
   TransferItem,
   TransferItem,
@@ -123,9 +120,11 @@ class TransferStore {
     try {
     try {
       const raw = await TransferSource.getExecutions(transferId, {
       const raw = await TransferSource.getExecutions(transferId, {
         limit: this.executionsPageSize,
         limit: this.executionsPageSize,
+        sortKeys: ["number"],
+        sortDirs: ["desc"],
       });
       });
       const hasOlderPage = raw.length === this.executionsPageSize;
       const hasOlderPage = raw.length === this.executionsPageSize;
-      TransferSourceUtils.sortExecutions(raw);
+      raw.reverse();
       runInAction(() => {
       runInAction(() => {
         this.executionsList = raw;
         this.executionsList = raw;
         this.executionsHasOlderPage = hasOlderPage;
         this.executionsHasOlderPage = hasOlderPage;
@@ -162,9 +161,11 @@ class TransferStore {
         limit: this.executionsPageSize,
         limit: this.executionsPageSize,
         marker,
         marker,
         quietError: true,
         quietError: true,
+        sortKeys: ["number"],
+        sortDirs: ["desc"],
       });
       });
       const hasOlderPage = raw.length === this.executionsPageSize;
       const hasOlderPage = raw.length === this.executionsPageSize;
-      TransferSourceUtils.sortExecutions(raw);
+      raw.reverse();
       runInAction(() => {
       runInAction(() => {
         this.executionsList = [...raw, ...this.executionsList];
         this.executionsList = [...raw, ...this.executionsList];
         this.executionsHasOlderPage = hasOlderPage;
         this.executionsHasOlderPage = hasOlderPage;
@@ -268,48 +269,67 @@ class TransferStore {
         includeTaskInfo,
         includeTaskInfo,
       });
       });
 
 
+      const activeStatuses = [
+        "RUNNING",
+        "PENDING",
+        "CANCELLING",
+        "AWAITING_MINION_ALLOCATIONS",
+      ];
+      const newestExecution =
+        this.executionsList[this.executionsList.length - 1];
+      const hasActiveExecution =
+        activeStatuses.includes(transfer.last_execution_status) ||
+        (newestExecution != null &&
+          activeStatuses.includes(newestExecution.status));
+      const shouldRefreshExecutions =
+        this.executionsList.length > 0 && (!polling || hasActiveExecution);
+      let freshExecutions: Execution[] | null = null;
+      if (shouldRefreshExecutions) {
+        try {
+          freshExecutions = await TransferSource.getExecutions(transferId, {
+            limit: this.executionsPageSize,
+            quietError: polling,
+            sortKeys: ["number"],
+            sortDirs: ["desc"],
+          });
+          freshExecutions.reverse();
+        } catch (err) {
+          console.error(err);
+        }
+      }
+
       runInAction(() => {
       runInAction(() => {
         this.transferDetails = transfer;
         this.transferDetails = transfer;
-        let statusChanged = false;
-        const updatedList = this.executionsList.map(e => {
-          const fresh = transfer.executions?.find(te => te.id === e.id);
-          if (fresh && fresh.status !== e.status) {
-            statusChanged = true;
-            return { ...e, status: fresh.status };
-          }
-          return e;
-        });
-        if (statusChanged) {
-          this.executionsList = updatedList;
-        }
 
 
-        if (this.executionsList.length > 0 && transfer.executions?.length) {
-          const newestNumber = Math.max(
-            ...this.executionsList.map(e => e.number),
-          );
-          const incoming = transfer.executions.filter(
-            e =>
-              e.number > newestNumber &&
-              !this.deletedExecutionIds.has(e.id) &&
-              !this.executionsList.find(l => l.id === e.id),
-          );
-          if (incoming.length > 0) {
-            TransferSourceUtils.sortExecutions(incoming);
-            this.executionsList = [...this.executionsList, ...incoming];
+        if (freshExecutions) {
+          let statusChanged = false;
+          const updatedList = this.executionsList.map(e => {
+            const fresh = freshExecutions!.find(te => te.id === e.id);
+            if (fresh && fresh.status !== e.status) {
+              statusChanged = true;
+              return { ...e, status: fresh.status };
+            }
+            return e;
+          });
+          if (statusChanged) {
+            this.executionsList = updatedList;
           }
           }
-        }
 
 
-        transfer.executions?.forEach(exec => {
-          const withTasks = exec as ExecutionTasks;
-          if (
-            Array.isArray(withTasks.tasks) &&
-            !this.deletedExecutionIds.has(exec.id) &&
-            !this.executionsTasks.find(et => et.id === exec.id)
-          ) {
-            sortTasks(withTasks.tasks, TransferSourceUtils.sortTaskUpdates);
-            this.executionsTasks = [...this.executionsTasks, withTasks];
+          if (this.executionsList.length > 0) {
+            const newestNumber = Math.max(
+              ...this.executionsList.map(e => e.number),
+            );
+            const incoming = freshExecutions.filter(
+              e =>
+                e.number > newestNumber &&
+                !this.deletedExecutionIds.has(e.id) &&
+                !this.executionsList.find(l => l.id === e.id),
+            );
+            if (incoming.length > 0) {
+              this.executionsList = [...this.executionsList, ...incoming];
+            }
           }
           }
-        });
+        }
       });
       });
     } finally {
     } finally {
       runInAction(() => {
       runInAction(() => {
@@ -561,8 +581,10 @@ class TransferStore {
           const executions = await TransferSource.getExecutions(transfer.id, {
           const executions = await TransferSource.getExecutions(transfer.id, {
             limit: this.executionsPageSize,
             limit: this.executionsPageSize,
             quietError: true,
             quietError: true,
+            sortKeys: ["number"],
+            sortDirs: ["desc"],
           });
           });
-          TransferSourceUtils.sortExecutions(executions);
+          executions.reverse();
           return { transfer, hasDisks: this.testTransferHasDisks(executions) };
           return { transfer, hasDisks: this.testTransferHasDisks(executions) };
         }),
         }),
       );
       );