name: Update Content DAG Sizes

on:
  schedule:
    - cron: '38 * * * *'
  workflow_dispatch:
    inputs:
      env:
        required: true
        description: The env to run the cron against. Default is staging.
        options: 
          - staging
          - production
        default: production
      after:
        required: false
        description: Date used as a starting point for dag_sizes updates. This should be a string value specified in a format recognized by the Date.parse() method
        type: string
      before:
        required: false
        description: Date used as confine dag_sizes updates. This should be a string value specified in a format recognized by the Date.parse() method
        type: string

jobs:
  update:
    name: Populate Missing Content DAG Sizes
    runs-on: ubuntu-latest
    timeout-minutes: ${{ (github.event_name == 'workflow_dispatch' && 360) || 60  }}
    steps:
      - uses: actions/checkout@v2
        with: 
          fetch-depth: 0
      - name: Checkout latest cron release tag
       # Be able to run from latest main in staging
        if: github.event.inputs.env != 'staging'
        run: |
          LATEST_TAG=$(git describe --tags --abbrev=0 --match='cron-*')
          git checkout $LATEST_TAG
      - uses: actions/setup-node@v2
        with:
          node-version: 16
      - uses: bahmutov/npm-install@v1
      - name: Run job
        env:
          DEBUG: '*'
          ## By default let's fix only dags in production
          ENV: ${{ github.event.inputs.env || 'production' }}
          STAGING_PG_CONNECTION: ${{ secrets.STAGING_PG_CONNECTION }}
          STAGING_RO_PG_CONNECTION: ${{ secrets.STAGING_PG_CONNECTION }} # no replica for staging
          PROD_PG_CONNECTION: ${{ secrets.PROD_PG_CONNECTION }}
          DAG_CARGO_HOST: ${{ secrets.DAG_CARGO_HOST }}
          DAG_CARGO_DATABASE: ${{ secrets.DAG_CARGO_DATABASE }}
          DAG_CARGO_USER: ${{ secrets.DAG_CARGO_USER }}
          DAG_CARGO_PASSWORD: ${{ secrets.DAG_CARGO_PASSWORD }}
          AFTER: ${{ github.event.inputs.after }}
          BEFORE: ${{ github.event.inputs.before }}
        run: npm run start:dagcargo:sizes -w packages/cron

      - name: Heartbeat
        if: ${{ success() }}
        run: ./packages/tools/scripts/cli.js heartbeat --token ${{ secrets.OPSGENIE_KEY }} --name cron-web3storage-dagcargo-sizes
