date-fns#subSeconds TypeScript Examples

The following examples show how to use date-fns#subSeconds. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: build.service.ts    From amplication with Apache License 2.0 5 votes vote down vote up
/**
   * Gets the updated status of running "build container" tasks from
   * containerBuilderService, and updates the step status. This function should
   * be called periodically from an external scheduler
   */
  async updateRunningBuildsStatus(): Promise<void> {
    const lastUpdateThreshold = subSeconds(
      new Date(),
      CONTAINER_STATUS_UPDATE_INTERVAL_SEC
    );

    // find all builds that have a running "build docker" step
    const builds = await this.prisma.build.findMany({
      where: {
        containerStatusUpdatedAt: {
          lt: lastUpdateThreshold
        },
        action: {
          steps: {
            some: {
              status: {
                equals: EnumActionStepStatus.Running
              },
              name: {
                equals: BUILD_DOCKER_IMAGE_STEP_NAME
              }
            }
          }
        }
      },
      orderBy: {
        createdAt: Prisma.SortOrder.asc
      },
      include: ACTION_INCLUDE
    });

    const groups = groupBy(builds, build => build.appId);

    //In case we have multiple builds for the same app run them one after the other based on creation time
    await Promise.all(
      Object.entries(groups).map(async ([appId, groupBuilds]) => {
        for (const build of groupBuilds) {
          const stepBuildDocker = build.action.steps.find(
            step => step.name === BUILD_DOCKER_IMAGE_STEP_NAME
          );
          try {
            const result = await this.containerBuilderService.getStatus(
              build.containerStatusQuery
            );
            await this.handleContainerBuilderResult(
              build,
              stepBuildDocker,
              result
            );
          } catch (error) {
            await this.actionService.logInfo(stepBuildDocker, error);
            await this.actionService.complete(
              stepBuildDocker,
              EnumActionStepStatus.Failed
            );
          }
        }
      })
    );
  }
Example #2
Source File: deployment.service.ts    From amplication with Apache License 2.0 5 votes vote down vote up
/**
   * Gets the updated status of running deployments from
   * DeployerService, and updates the step and deployment status. This function should
   * be called periodically from an external scheduler
   */
  async updateRunningDeploymentsStatus(): Promise<void> {
    const lastUpdateThreshold = subSeconds(
      new Date(),
      DEPLOY_STATUS_FETCH_INTERVAL_SEC
    );

    //find all deployments that are still running
    const deployments = await this.findMany({
      where: {
        statusUpdatedAt: {
          lt: lastUpdateThreshold
        },
        status: {
          equals: EnumDeploymentStatus.Waiting
        }
      },
      include: ACTION_INCLUDE
    });
    await Promise.all(
      deployments.map(async deployment => {
        const steps = await this.actionService.getSteps(deployment.actionId);
        const deployStep = steps.find(step => step.name === DEPLOY_STEP_NAME);
        const destroyStep = steps.find(step => step.name === DESTROY_STEP_NAME);

        const currentStep = destroyStep || deployStep; //when destroy step exist it is the current one
        try {
          const result = await this.deployerService.getStatus(
            deployment.statusQuery
          );
          //To avoid too many messages in the log, if the status is still "running" handle the results only if the bigger interval passed
          if (
            result.status !== EnumDeployStatus.Running ||
            differenceInSeconds(new Date(), deployment.statusUpdatedAt) >
              DEPLOY_STATUS_UPDATE_INTERVAL_SEC
          ) {
            this.logger.info(
              `Deployment ${deployment.id}: current status ${result.status}`
            );
            const updatedDeployment = await this.handleDeployResult(
              deployment,
              currentStep,
              result
            );
            return updatedDeployment.status;
          } else {
            return deployment.status;
          }
        } catch (error) {
          await this.actionService.logInfo(currentStep, error);
          await this.actionService.complete(
            currentStep,
            EnumActionStepStatus.Failed
          );
          const status = EnumDeploymentStatus.Failed;
          await this.updateStatus(deployment.id, status);
        }
      })
    );
  }
Example #3
Source File: withings-helper.ts    From nyxo-app with GNU General Public License v3.0 5 votes vote down vote up
formatWithingsSample = (
  withingsSleepObject: WithingsSleepObject
): Night[] => {
  const {
    data: { durationtosleep = 0, durationtowakeup = 0 }
  } = withingsSleepObject

  const startDate = new Date(withingsSleepObject.startdate).toISOString()
  const endDate = new Date(withingsSleepObject.enddate).toISOString()
  const timeInBed = getNightDuration(startDate, endDate)

  const inBedSample: Night = {
    id: `withings_${withingsSleepObject.date}_${startDate}_${endDate}_${Value.InBed}`,
    sourceId: CONFIG.WITHINGS_CONFIG.bundleId,
    sourceName: 'Withings',
    value: Value.InBed,
    startDate,
    endDate,
    totalDuration: timeInBed
  }

  const asleepStartDate = addSeconds(
    new Date(startDate),
    durationtosleep
  ).toISOString()
  const asleepEndDate = subSeconds(
    new Date(startDate),
    durationtowakeup
  ).toISOString()

  const timeAsleep = getNightDuration(asleepStartDate, asleepEndDate)

  const asleepSample: Night = {
    id: `withings_${withingsSleepObject.date}_${asleepStartDate}_${asleepEndDate}_${Value.Asleep}`,
    sourceId: CONFIG.WITHINGS_CONFIG.bundleId,
    sourceName: 'Withings',
    value: Value.Asleep,
    startDate: asleepStartDate,
    endDate: asleepEndDate,
    totalDuration: timeAsleep
  }

  return [inBedSample, asleepSample]
}