name: Import Benchmarks
on:
  repository_dispatch:
    types: [ benchmark-import ]
env:
  BENCH_DIR: 'go/performance/import_benchmarker'
  MYSQL_PORT: 3309
  MYSQL_PASSWORD: password
jobs:
  bench:
    name: Benchmark
    defaults:
      run:
        shell: bash
    strategy:
      fail-fast: true
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        ref: ${{ github.event.client_payload.version }}

    - name: Set up Go 1.x
      id: go
      uses: actions/setup-go@v5
      with:
        go-version-file: go/go.mod

    - name: Dolt version
      id: version
      run: |
        version=${{ github.event.client_payload.version }}

    - name: Install dolt
      working-directory: ./go
      run: go install ./cmd/dolt

    - uses: shogo82148/actions-setup-mysql@v1
      with:
        mysql-version: '8.0'
        auto-start: true
        root-password: ${{ env.MYSQL_PASSWORD }}
        my-cnf: |
          local_infile=1
          socket=/tmp/mysqld2.sock
          port=${{ env.MYSQL_PORT }}

    - name: Setup MySQL
      run: mysql -uroot -p${{ env.MYSQL_PASSWORD }} -h127.0.0.1 -P${{ env.MYSQL_PORT }} -e 'create database test;'

    - name: Run bench
      id: bench
      working-directory: go/
      run: |
        out="$GITHUB_WORKSPACE/results.sql"
        testspec="../${{ env.BENCH_DIR }}/testdata/${{ github.event.client_payload.run_file }}"
        go run \
          "github.com/dolthub/dolt/${{ env.BENCH_DIR }}/cmd" \
          -test "$testspec" \
          -out "$out"
        echo "result_path=$out" >> $GITHUB_OUTPUT

    - name: Report
      id: report
      run: |
        gw=$GITHUB_WORKSPACE
        in="${{ steps.bench.outputs.result_path }}"
        query="$(pwd)/${{ env.BENCH_DIR }}/reporting/${{ github.event.client_payload.report }}"
        summaryq="$(pwd)/${{ env.BENCH_DIR }}/reporting/${{ github.event.client_payload.summary }}"

        out="$gw/results.csv"
        dolt_dir="$gw/import-perf"

        dolt config --global --add user.email "import-perf@dolthub.com"
        dolt config --global --add user.name "import-perf"

        echo '${{ secrets.DOLTHUB_IMPORT_PERF_CREDS_VALUE }}' | dolt creds import
        dolt clone import-perf/import-perf "$dolt_dir"

        cd "$dolt_dir"

        branch="${{ github.event.client_payload.commit_to_branch }}"
        # checkout branch
        if [ -z $(dolt sql -q "select 1 from dolt_branches where name = '$branch';") ]; then
          dolt checkout -b $branch
        else
          dolt checkout $branch
        fi

        dolt sql -q "drop table if exists import_perf_results"

        # load results
        dolt sql < "$in"

        # push results to dolthub
        dolt add import_perf_results
        dolt commit -m "CI commit"
        dolt push -f origin $branch

        # generate report
        dolt sql -r csv < "$query" > "$out"

        cat "$out"
        echo "report_path=$out" >> $GITHUB_OUTPUT

        avg=$(dolt sql -r csv < "$summaryq" | tail -1)
        echo "avg=$avg" >> $GITHUB_OUTPUT

    - name: Format HTML
      id: html
      if: ${{ github.event.client_payload.email_recipient }} != ""
      run: |
        gw="$GITHUB_WORKSPACE"
        in="${{ steps.report.outputs.report_path }}"
        out="$gw/results.html"

        echo "<table>" > "$out"
        print_header=true
        while read line; do
          if "$print_header"; then
            echo "  <tr><th>${line//,/</th><th>}</th></tr>" >> "$out"
            print_header=false
            continue
          fi
          echo "  <tr><td>${line//,/</td><td>}</td></tr>" >> "$out"
        done < "$in"
        echo "</table>" >> "$out"

        avg="${{ steps.report.outputs.avg }}"
        echo "<table><tr><th>Average</th></tr><tr><td>$avg</tr></td></table>" >> "$out"

        cat "$out"
        echo "html=$(echo $out)" >> $GITHUB_OUTPUT

    - name: Configure AWS Credentials
      if: ${{ github.event.client_payload.email_recipient }} != ""
      uses: aws-actions/configure-aws-credentials@v4
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-west-2

    - name: Send Email
      uses: ./.github/actions/ses-email-action
      if: ${{ github.event.client_payload.email_recipient }} != ""
      with:
        region: us-west-2
        toAddresses: '["${{ github.event.client_payload.email_recipient }}"]'
        subject: 'Import Performance Benchmarks: ${{ github.event.client_payload.version }}'
        bodyPath: ${{ steps.html.outputs.html }}
        template: 'SysbenchTemplate'

    - name: Read CSV
      if: ${{ github.event.client_payload.issue_id }} != ""
      id: csv
      uses: juliangruber/read-file-action@v1
      with:
        path: "${{ steps.report.outputs.report_path }}"

    - name: Create MD
      if: ${{ github.event.client_payload.issue_id }} != ""
      uses: dolthub/csv-to-md-table-action@v4
      id: md
      with:
        csvinput: ${{ steps.csv.outputs.content }}

    - uses: mshick/add-pr-comment@v2
      if: ${{ github.event.client_payload.issue_id }} != ""
      with:
        repo-token: ${{ secrets.GITHUB_TOKEN }}
        issue: ${{ github.event.client_payload.issue_id }}
        message-failure: import benchmark failed
        message-cancelled: import benchmark cancelled
        allow-repeats: true
        message: |
          @${{ github.event.client_payload.actor }} __DOLT__
          ${{ steps.md.outputs.markdown-table }}
