All files / packages/core/src/loaders ProgressiveRetrieveImages.ts

3.22% Statements 4/124
0% Branches 0/78
0% Functions 0/17
3.3% Lines 4/121

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381                                                                                                                                                                                              1x   1x       1x       1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
import {
  IRetrieveConfiguration,
  IImagesLoader,
  IImage,
  RetrieveStage,
  EventTypes,
  ImageLoadListener,
  RetrieveOptions,
} from '../types';
import singleRetrieveStages from './configuration/singleRetrieve';
import sequentialRetrieveStages from './configuration/sequentialRetrieve';
import interleavedRetrieveStages from './configuration/interleavedRetrieve';
import { loadAndCacheImage } from './imageLoader';
import { triggerEvent, ProgressiveIterator, decimate } from '../utilities';
import imageLoadPoolManager from '../requestPool/imageLoadPoolManager';
import { ImageQualityStatus, RequestType, Events } from '../enums';
import cache from '../cache';
import eventTarget from '../eventTarget';
import { fillNearbyFrames } from './fillNearbyFrames';
 
export {
  sequentialRetrieveStages,
  interleavedRetrieveStages,
  singleRetrieveStages,
};
 
type StageStatus = {
  stageId: string;
  // startTime is the overall start of loading a given image id
  startTime?: number;
  // stageStartTime is the time to start loading this stage item
  stageStartTime?: number;
  totalImageCount: number;
  imageLoadFailedCount: number;
  imageLoadPendingCount: number;
};
 
/**
 * A nearby request is a request that can be fulfilled by copying another image
 */
export type NearbyRequest = {
  // The item id to fill
  itemId: string;
  linearId?: string;
  // The new status of the filled image (will only fill if the existing status
  // is less than this one)
  imageQualityStatus: ImageQualityStatus;
};
 
export type ProgressiveRequest = {
  imageId: string;
  stage: RetrieveStage;
  next?: ProgressiveRequest;
  /**
   * Nearby requests are a set of requests for filling nearby images which
   * could be filled by using this image as a copied image to generate the
   * nearby data as a low-resolution alternative image.
   */
  nearbyRequests?: NearbyRequest[];
};
 
/**
 * A progressive loader is given some number of images to load,
 * and calls a success or failure callback some number of times in some
 * ordering, possibly calling back multiple times.
 * This allows the progressive loader to be configured for different setups
 * and to return render results for various images.
 *
 * When used by the a stack viewport, the progressive loader can return multiple
 * representations to the viewport, replacing earlier/more lossy versions with better ones.
 *
 * When used by a streaming loader, the progressive loader can change the ordering
 * of the rendering to retrieve high priority images first, and the lower priority
 * images later to provide a complete final rendering.
 *
 * Requests are held in a queue, such that subsequent requests for a given
 * image can be cancelled or ensured to be not initiated until the higher
 * priority image sets have been completed.
 *
 * This loader is also used for the base streamimg image volume, configured with
 * a minimal interleaved load order, combined with filling nearby volume slices
 * on load, resulting in much faster initial apparent display.
 *
 * The loader will load images from existing cached images, cached volumes, and
 * from other nearby images or one or more calls to back end services.
 *
 * @param imageIds - the set of images to load.  For a volume, these should be
 *                   ordered from top to bottom.
 * @param listener - has success and failure callbacks to listen for image deliver events, and may
 *                   have a getTargetOptions to get information on the retrieve
 * @param retrieveOptions - is a set of retrieve options to use
 */
export class ProgressiveRetrieveImages
  implements IImagesLoader, IRetrieveConfiguration
{
  public static createProgressive = createProgressive;
 
  public static interleavedRetrieveStages = {
    stages: interleavedRetrieveStages,
  };
 
  public static singleRetrieveStages = {
    stages: singleRetrieveStages,
  };
 
  public static sequentialRetrieveStages = {
    stages: sequentialRetrieveStages,
  };
 
  stages: RetrieveStage[];
  retrieveOptions: Record<string, RetrieveOptions>;
 
  constructor(imageRetrieveConfiguration: IRetrieveConfiguration) {
    this.stages = imageRetrieveConfiguration.stages || singleRetrieveStages;
    this.retrieveOptions = imageRetrieveConfiguration.retrieveOptions || {};
  }
 
  public loadImages(imageIds: string[], listener: ImageLoadListener) {
    const instance = new ProgressiveRetrieveImagesInstance(
      this,
      imageIds,
      listener
    );
    return instance.loadImages();
  }
}
 
class ProgressiveRetrieveImagesInstance {
  imageIds: string[];
  listener: ImageLoadListener;
  stages: RetrieveStage[];
  retrieveOptions: Record<string, RetrieveOptions>;
  outstandingRequests = 0;
 
  stageStatusMap = new Map<string, StageStatus>();
  imageQualityStatusMap = new Map<string, ImageQualityStatus>();
  displayedIterator = new ProgressiveIterator<void | IImage>('displayed');
 
  constructor(configuration: IRetrieveConfiguration, imageIds, listener) {
    this.stages = configuration.stages;
    this.retrieveOptions = configuration.retrieveOptions;
    this.imageIds = imageIds;
    this.listener = listener;
  }
 
  public async loadImages() {
    // The actual function is to just setup the interleave and add the
    // requests, with all the actual work being handled by the nested functions
    const interleaved = this.createStageRequests();
    this.outstandingRequests = interleaved.length;
    for (const request of interleaved) {
      this.addRequest(request);
    }
    if (this.outstandingRequests === 0) {
      return Promise.resolve(null);
    }
 
    return this.displayedIterator.getDonePromise();
  }
 
  protected sendRequest(request, options) {
    const { imageId, next } = request;
    const errorCallback = (reason, done) => {
      this.listener.errorCallback(imageId, complete || !next, reason);
      if (done) {
        this.updateStageStatus(request.stage, reason);
      }
    };
    const loadedPromise = (options.loader || loadAndCacheImage)(
      imageId,
      options
    );
    const uncompressedIterator = ProgressiveIterator.as(loadedPromise);
    let complete = false;
 
    uncompressedIterator
      .forEach(async (image, done) => {
        const oldStatus = this.imageQualityStatusMap.get(imageId);
        if (!image) {
          console.warn('No image retrieved', imageId);
          return;
        }
        const { imageQualityStatus } = image;
        complete ||= imageQualityStatus === ImageQualityStatus.FULL_RESOLUTION;
        if (oldStatus !== undefined && oldStatus > imageQualityStatus) {
          // We already have a better status, so don't update it
          this.updateStageStatus(request.stage, null, true);
          return;
        }
 
        this.listener.successCallback(imageId, image);
        this.imageQualityStatusMap.set(imageId, imageQualityStatus);
        this.displayedIterator.add(image);
        if (done) {
          this.updateStageStatus(request.stage);
        }
        fillNearbyFrames(
          this.listener,
          this.imageQualityStatusMap,
          request,
          image,
          options
        );
      }, errorCallback)
      .finally(() => {
        if (!complete && next) {
          if (cache.getImageLoadObject(imageId)) {
            cache.removeImageLoadObject(imageId);
          }
          this.addRequest(next, options.streamingData);
        } else {
          if (!complete) {
            this.listener.errorCallback(imageId, true, "Couldn't decode");
          }
          this.outstandingRequests--;
          for (let skip = next; skip; skip = skip.next) {
            this.updateStageStatus(skip.stage, null, true);
          }
        }
        if (this.outstandingRequests <= 0) {
          this.displayedIterator.resolve();
        }
      });
    const doneLoad = uncompressedIterator.getDonePromise();
    // Errors already handled above in the callback
    return doneLoad.catch((e) => null);
  }
 
  /** Adds a rquest to the image load pool manager */
  protected addRequest(request, streamingData = {}) {
    const { imageId, stage } = request;
    const baseOptions = this.listener.getLoaderImageOptions(imageId);
    if (!baseOptions) {
      // Image no longer of interest
      return;
    }
    const { retrieveType = 'default' } = stage;
    const { retrieveOptions: keyedRetrieveOptions } = this;
    const retrieveOptions =
      keyedRetrieveOptions[retrieveType] || keyedRetrieveOptions.default;
    const options = {
      ...baseOptions,
      retrieveType,
      retrieveOptions,
      streamingData,
    };
    const priority = stage.priority ?? -5;
    const requestType = stage.requestType || RequestType.Interaction;
    const additionalDetails = { imageId };
 
    imageLoadPoolManager.addRequest(
      this.sendRequest.bind(this, request, options),
      requestType,
      additionalDetails,
      priority
    );
  }
 
  protected updateStageStatus(stage, failure?, skipped = false) {
    const { id } = stage;
    const stageStatus = this.stageStatusMap.get(id);
    if (!stageStatus) {
      return;
    }
    stageStatus.imageLoadPendingCount--;
    if (failure) {
      stageStatus.imageLoadFailedCount++;
    } else if (!skipped) {
      stageStatus.totalImageCount++;
    }
    if (!skipped && !stageStatus.stageStartTime) {
      stageStatus.stageStartTime = Date.now();
    }
    if (!stageStatus.imageLoadPendingCount) {
      const {
        imageLoadFailedCount: numberOfFailures,
        totalImageCount: numberOfImages,
        stageStartTime = Date.now(),
        startTime,
      } = stageStatus;
      const detail: EventTypes.ImageLoadStageEventDetail = {
        stageId: id,
        numberOfFailures,
        numberOfImages,
        stageDurationInMS: stageStartTime ? Date.now() - stageStartTime : null,
        startDurationInMS: Date.now() - startTime,
      };
      triggerEvent(eventTarget, Events.IMAGE_RETRIEVAL_STAGE, detail);
      this.stageStatusMap.delete(id);
    }
  }
 
  /** Interleaves the values according to the stages definition */
  protected createStageRequests() {
    const interleaved = new Array<ProgressiveRequest>();
    // Maps image id to the LAST progressive request - to allow tail append
    const imageRequests = new Map<string, ProgressiveRequest>();
 
    const addStageInstance = (stage, position) => {
      const index =
        position < 0
          ? this.imageIds.length + position
          : position < 1
          ? Math.floor((this.imageIds.length - 1) * position)
          : position;
      const imageId = this.imageIds[index];
      if (!imageId) {
        throw new Error(`No value found to add to requests at ${position}`);
      }
      const request: ProgressiveRequest = {
        imageId,
        stage,
        nearbyRequests: this.findNearbyRequests(index, stage),
      };
      this.addStageStatus(stage);
      const existingRequest = imageRequests.get(imageId);
      if (existingRequest) {
        existingRequest.next = request;
      } else {
        interleaved.push(request);
      }
      imageRequests.set(imageId, request);
    };
 
    for (const stage of this.stages) {
      const indices =
        stage.positions ||
        decimate(this.imageIds, stage.decimate || 1, stage.offset ?? 0);
      indices.forEach((index) => addStageInstance(stage, index));
    }
    return interleaved;
  }
 
  /**
   * Finds nearby requests to fulfill to show the merge information earlier.
   * @param index - to use as the base value
   * @param imageIds - set of image ids to request
   * @param stage - to find information from
   * @returns Array of nearby frames to fill when the main stage is done
   */
  protected findNearbyRequests(index: number, stage): NearbyRequest[] {
    const nearby = new Array<NearbyRequest>();
    if (!stage.nearbyFrames) {
      return nearby;
    }
    for (const nearbyItem of stage.nearbyFrames) {
      const nearbyIndex = index + nearbyItem.offset;
      if (nearbyIndex < 0 || nearbyIndex >= this.imageIds.length) {
        continue;
      }
      nearby.push({
        itemId: this.imageIds[nearbyIndex],
        imageQualityStatus: nearbyItem.imageQualityStatus,
      });
    }
 
    return nearby;
  }
 
  protected addStageStatus(stage) {
    const { id } = stage;
    const stageStatus = this.stageStatusMap.get(id) || {
      stageId: id,
      startTime: Date.now(),
      stageStartTime: null,
      totalImageCount: 0,
      imageLoadFailedCount: 0,
      imageLoadPendingCount: 0,
    };
    stageStatus.imageLoadPendingCount++;
    this.stageStatusMap.set(id, stageStatus);
    return stageStatus;
  }
}
 
export function createProgressive(configuration: IRetrieveConfiguration) {
  return new ProgressiveRetrieveImages(configuration);
}
 
export default ProgressiveRetrieveImages;