public async computeCacheWarmerOperations()

in controlplane/src/core/repositories/CacheWarmerRepository.ts [139:314]


  public async computeCacheWarmerOperations(props: ComputeCacheWarmerOperationsProps): Promise<CacheWarmerOperations> {
    const operationsRepo = new OperationsRepository(this.db, props.federatedGraphId);

    const computedOperations: Operation[] = [];
    const dbCacheWarmerOperations: CacheWarmupOperation[] = [];

    const manuallyAddedOperations = await this.getCacheWarmerOperations({
      organizationId: props.organizationId,
      federatedGraphId: props.federatedGraphId,
      isManuallyAdded: true,
    });

    for (const operation of manuallyAddedOperations) {
      if (operation.operationPersistedID) {
        const persistedOperation = await operationsRepo.getPersistedOperation({
          operationId: operation.operationPersistedID,
        });

        if (!persistedOperation || !persistedOperation.contents) {
          continue;
        }

        computedOperations.push(
          new Operation({
            request: new OperationRequest({
              operationName: operation.operationName || undefined,
              query: persistedOperation.contents,
              extensions: new Extension({
                persistedQuery: new PersistedQuery({
                  version: 1,
                  sha256Hash: operation.operationPersistedID,
                }),
              }),
            }),
            client: operation.clientName
              ? new ClientInfo({
                  name: operation.clientName,
                  version: operation.clientVersion || undefined,
                })
              : undefined,
          }),
        );
      } else if (operation.operationContent) {
        computedOperations.push(
          new Operation({
            request: new OperationRequest({
              operationName: operation.operationName || undefined,
              query: operation.operationContent,
            }),
            client: operation.clientName
              ? new ClientInfo({
                  name: operation.clientName,
                  version: operation.clientVersion || undefined,
                })
              : undefined,
          }),
        );
      }
    }

    const topOperationsByPlanningTime = await this.getTopOperationsByPlanningTime({
      ...props,
      maxOperationsCount: props.maxOperationsCount - manuallyAddedOperations.length,
    });

    if (topOperationsByPlanningTime.length === 0) {
      return new CacheWarmerOperations({
        operations: computedOperations,
      });
    }

    const operationHashes = topOperationsByPlanningTime.map((op) => op.operationHash);
    const operationContentMap = await this.getOperationContent({
      operationHashes,
      federatedGraphID: props.federatedGraphId,
      organizationID: props.organizationId,
      rangeInHours: 24 * 7,
    });

    for (const operation of topOperationsByPlanningTime) {
      if (operation.operationPersistedID) {
        const persistedOperation = await operationsRepo.getPersistedOperation({
          operationId: operation.operationPersistedID,
        });

        if (!persistedOperation || !persistedOperation.contents) {
          continue;
        }

        computedOperations.push(
          new Operation({
            request: new OperationRequest({
              operationName: operation.operationName,
              query: persistedOperation.contents,
              extensions: new Extension({
                persistedQuery: new PersistedQuery({
                  version: 1,
                  sha256Hash: operation.operationPersistedID,
                }),
              }),
            }),
            client: operation.clientName
              ? new ClientInfo({
                  name: operation.clientName,
                  version: operation.clientVersion,
                })
              : undefined,
          }),
        );

        dbCacheWarmerOperations.push({
          operationName: operation.operationName,
          operationHash: operation.operationHash,
          operationContent: persistedOperation.contents,
          operationPersistedID: operation.operationPersistedID,
          clientName: operation.clientName,
          clientVersion: operation.clientVersion,
          planningTime: operation.planningTime,
          federatedGraphId: props.federatedGraphId,
          organizationId: props.organizationId,
          isManuallyAdded: false,
        });
        continue;
      }

      const operationContent = operationContentMap.get(operation.operationHash);

      if (!operationContent) {
        continue;
      }

      dbCacheWarmerOperations.push({
        operationName: operation.operationName,
        operationHash: operation.operationHash,
        operationPersistedID: operation.operationPersistedID,
        clientName: operation.clientName,
        clientVersion: operation.clientVersion,
        planningTime: operation.planningTime,
        federatedGraphId: props.federatedGraphId,
        organizationId: props.organizationId,
        operationContent,
        isManuallyAdded: false,
      });

      computedOperations.push(
        new Operation({
          request: new OperationRequest({
            operationName: operation.operationName,
            query: operationContent,
          }),
          client: operation.clientName
            ? new ClientInfo({
                name: operation.clientName,
                version: operation.clientVersion,
              })
            : undefined,
        }),
      );
    }

    await this.db.transaction(async (tx) => {
      const cacheWarmerRepo = new CacheWarmerRepository(this.client, tx);
      await cacheWarmerRepo.deleteComputedCacheWarmerOperations({
        organizationId: props.organizationId,
        federatedGraphId: props.federatedGraphId,
      });

      await cacheWarmerRepo.addCacheWarmerOperations({
        operations: dbCacheWarmerOperations,
      });
    });

    return new CacheWarmerOperations({
      operations: computedOperations,
    });
  }