export function makeTasksForStep()

in src/components/Timeline/taskdataUtils.ts [137:171]


export function makeTasksForStep(currentData: Record<string, Task[]>, item: Task): Task[] {
  if (currentData[item.task_id]) {
    const newtasks = currentData[item.task_id];

    // Process duration and start time for task since they are somewhat uncertain from API
    // NOTE: WE ARE MUTATING TASK VALUES HERE BECAUSE VALUES GIVEN BY BACKEND MIGHT NOT BE CORRECT
    // SINCE STARTED AT AND DURATION MIGHT BE INCORRECT IN SOME SITUATIONS!!!
    if (!item.started_at) {
      if (item.attempt_id === 0) {
        item.started_at = item.ts_epoch;
        item.duration = item.duration || (item.finished_at ? item.finished_at - item.ts_epoch : undefined);
      } else {
        const prevTask = currentData[item.task_id].find((t) => t.attempt_id === item.attempt_id - 1);
        item.started_at = item.started_at || prevTask?.finished_at || undefined;
        item.duration = item.started_at && item.finished_at ? item.finished_at - item.started_at : undefined;
      }
    }

    // Track if we replaced old data...
    let added = false;
    for (const index in newtasks) {
      if (newtasks[index].attempt_id === item.attempt_id) {
        added = true;
        newtasks[index] = item;
      }
    }
    // ...else add as new task
    if (!added) {
      newtasks.push(item);
    }
    return newtasks.sort((a, b) => a.attempt_id - b.attempt_id);
  } else {
    return [item];
  }
}