Просмотр исходного кода

Merge pull request #404 from smiclea/sort-tasks

Improve tasks sorting when there are parallel ones
Dorin Paslaru 6 лет назад
Родитель
Сommit
38413cf06a
2 измененных файлов с 58 добавлено и 26 удалено
  1. 13 15
      src/sources/MigrationSource.js
  2. 45 11
      src/sources/ReplicaSource.js

+ 13 - 15
src/sources/MigrationSource.js

@@ -17,6 +17,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 import moment from 'moment'
 import moment from 'moment'
 
 
 import { OptionsSchemaPlugin } from '../plugins/endpoint'
 import { OptionsSchemaPlugin } from '../plugins/endpoint'
+import { sortTasks } from './ReplicaSource'
 
 
 import Api from '../utils/ApiCaller'
 import Api from '../utils/ApiCaller'
 import type { MainItem } from '../types/MainItem'
 import type { MainItem } from '../types/MainItem'
@@ -27,27 +28,24 @@ import type { Endpoint, StorageMap } from '../types/Endpoint'
 import { servicesUrl } from '../constants'
 import { servicesUrl } from '../constants'
 
 
 class MigrationSourceUtils {
 class MigrationSourceUtils {
-  static sortTaskUpdates(migration) {
-    if (migration && migration.tasks) {
-      migration.tasks.forEach(task => {
-        if (task && task.progress_updates) {
-          task.progress_updates.sort((a, b) => {
-            let sortNull = !a && b ? 1 : a && !b ? -1 : !a && !b ? 0 : false
-            if (sortNull !== false) {
-              return sortNull
-            }
-            return moment(a.created_at).toDate().getTime() - moment(b.created_at).toDate().getTime()
-          })
-        }
-      })
+  static sortTaskUpdates(updates) {
+    if (!updates) {
+      return
     }
     }
+    updates.sort((a, b) => {
+      let sortNull = !a && b ? 1 : a && !b ? -1 : !a && !b ? 0 : false
+      if (sortNull !== false) {
+        return sortNull
+      }
+      return moment(a.created_at).toDate().getTime() - moment(b.created_at).toDate().getTime()
+    })
   }
   }
 
 
   static sortMigrations(migrations) {
   static sortMigrations(migrations) {
     migrations.sort((a, b) => moment(b.created_at).diff(moment(a.created_at)))
     migrations.sort((a, b) => moment(b.created_at).diff(moment(a.created_at)))
 
 
     migrations.forEach(migration => {
     migrations.forEach(migration => {
-      MigrationSourceUtils.sortTaskUpdates(migration)
+      sortTasks(migration.tasks, MigrationSourceUtils.sortTaskUpdates)
     })
     })
   }
   }
 }
 }
@@ -64,7 +62,7 @@ class MigrationSource {
   static getMigration(migrationId: string): Promise<MainItem> {
   static getMigration(migrationId: string): Promise<MainItem> {
     return Api.get(`${servicesUrl.coriolis}/${Api.projectId}/migrations/${migrationId}`).then(response => {
     return Api.get(`${servicesUrl.coriolis}/${Api.projectId}/migrations/${migrationId}`).then(response => {
       let migration = response.data.migration
       let migration = response.data.migration
-      MigrationSourceUtils.sortTaskUpdates(migration)
+      sortTasks(migration.tasks, MigrationSourceUtils.sortTaskUpdates)
       return migration
       return migration
     })
     })
   }
   }

+ 45 - 11
src/sources/ReplicaSource.js

@@ -23,8 +23,45 @@ import { servicesUrl } from '../constants'
 import type { MainItem, UpdateData } from '../types/MainItem'
 import type { MainItem, UpdateData } from '../types/MainItem'
 import type { Execution } from '../types/Execution'
 import type { Execution } from '../types/Execution'
 import type { Endpoint } from '../types/Endpoint'
 import type { Endpoint } from '../types/Endpoint'
+import type { Task, ProgressUpdate } from '../types/Task'
 import type { Field } from '../types/Field'
 import type { Field } from '../types/Field'
 
 
+export const sortTasks = (tasks: Task[], taskUpdatesSortFunction: (updates: ProgressUpdate[]) => void) => {
+  if (!tasks) {
+    return
+  }
+  let sortedTasks = []
+  let buffer = []
+  let runningBuffer = []
+  let completedBuffer = []
+  tasks.forEach(task => {
+    taskUpdatesSortFunction(task.progress_updates)
+    buffer.push(task)
+    if (task.status === 'RUNNING') {
+      runningBuffer.push(task)
+    } else if (task.status === 'COMPLETED' || task.status === 'ERROR') {
+      completedBuffer.push(task)
+    } else {
+      if (runningBuffer.length >= 2) {
+        sortedTasks = sortedTasks.concat([...completedBuffer, ...runningBuffer, task])
+      } else {
+        sortedTasks = sortedTasks.concat([...buffer])
+      }
+      buffer = []
+      runningBuffer = []
+      completedBuffer = []
+    }
+  })
+  if (buffer.length) {
+    if (runningBuffer.length >= 2) {
+      sortedTasks = sortedTasks.concat([...completedBuffer, ...runningBuffer])
+    } else {
+      sortedTasks = sortedTasks.concat([...buffer])
+    }
+  }
+  tasks.splice(0, tasks.length, ...sortedTasks)
+}
+
 class ReplicaSourceUtils {
 class ReplicaSourceUtils {
   static filterDeletedExecutionsInReplicas(replicas) {
   static filterDeletedExecutionsInReplicas(replicas) {
     return replicas.map(replica => {
     return replicas.map(replica => {
@@ -66,21 +103,18 @@ class ReplicaSourceUtils {
     }
     }
   }
   }
 
 
-  static sortExecutionsAndTaskUpdates(executions) {
+  static sortExecutionsAndTasks(executions) {
     this.sortExecutions(executions)
     this.sortExecutions(executions)
     executions.forEach(execution => {
     executions.forEach(execution => {
-      this.sortTaskUpdates(execution)
+      sortTasks(execution.tasks, ReplicaSourceUtils.sortTaskUpdates)
     })
     })
   }
   }
 
 
-  static sortTaskUpdates(execution) {
-    if (execution.tasks) {
-      execution.tasks.forEach(task => {
-        if (task.progress_updates) {
-          task.progress_updates.sort((a, b) => moment(a.created_at).toDate().getTime() - moment(b.created_at).toDate().getTime())
-        }
-      })
+  static sortTaskUpdates(updates) {
+    if (!updates) {
+      return
     }
     }
+    updates.sort((a, b) => moment(a.created_at).toDate().getTime() - moment(b.created_at).toDate().getTime())
   }
   }
 }
 }
 
 
@@ -97,7 +131,7 @@ class ReplicaSource {
   static getReplicaExecutions(replicaId: string): Promise<Execution[]> {
   static getReplicaExecutions(replicaId: string): Promise<Execution[]> {
     return Api.get(`${servicesUrl.coriolis}/${Api.projectId}/replicas/${replicaId}/executions/detail`).then((response) => {
     return Api.get(`${servicesUrl.coriolis}/${Api.projectId}/replicas/${replicaId}/executions/detail`).then((response) => {
       let executions = response.data.executions
       let executions = response.data.executions
-      ReplicaSourceUtils.sortExecutionsAndTaskUpdates(executions)
+      ReplicaSourceUtils.sortExecutionsAndTasks(executions)
 
 
       return executions
       return executions
     })
     })
@@ -125,7 +159,7 @@ class ReplicaSource {
       data: payload,
       data: payload,
     }).then((response) => {
     }).then((response) => {
       let execution = response.data.execution
       let execution = response.data.execution
-      ReplicaSourceUtils.sortTaskUpdates(execution)
+      sortTasks(execution.tasks, ReplicaSourceUtils.sortTaskUpdates)
       return execution
       return execution
     })
     })
   }
   }