name: generate_svg_common

on:
  workflow_call:
    inputs:
      backend:
        type: string
      hardware_name:
        type: string
      layer:
        type: string
      pbs_kind: # Valid values are 'classical', 'multi_bit' or 'any'
        type: string
      grouping_factor: # Valid values are 2, 3, or 4
        type: string
        default: 4
      bench_type: # Valid values are 'latency', 'throughput'
        type: string
      backend_comparison:
        type: boolean
        default: false
      time_span_days:
        type: string
        default: 60
      output_filename:
        type: string
        required: true
    secrets:
      DATA_EXTRACTOR_DATABASE_USER:
        required: true
      DATA_EXTRACTOR_DATABASE_HOST:
        required: true
      DATA_EXTRACTOR_DATABASE_PASSWORD:
        required: true

permissions: {}

# zizmor: ignore[concurrency-limits] caller workflow is responsible for the concurrency

jobs:
  generate-table:
    name: generate_svg_common/generate-table
    runs-on: ubuntu-latest
    steps:
      - name: Checkout tfhe-rs
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8
        with:
          persist-credentials: 'false'

      - name: Produce table from database
        if: inputs.backend_comparison == false
        run: |
          python3 -m pip install -r ci/data_extractor/requirements.txt
          python3 ci/data_extractor/src/data_extractor.py "${OUTPUT_FILENAME}" \
          --generate-svg \
          --branch "${REF_NAME}" \
          --backend "${BACKEND}" \
          --hardware "${HARDWARE_NAME}" \
          --tfhe-rs-layer "${LAYER}" \
          --pbs-kind "${PBS_KIND}" \
          --grouping-factor "${GROUPING_FACTOR}" \
          --bench-type "${BENCH_TYPE}" \
          --time-span-days "${TIME_SPAN}"
        env:
          OUTPUT_FILENAME: ${{ inputs.output_filename }}
          REF_NAME: ${{ github.ref_name }}
          BACKEND: ${{ inputs.backend }}
          HARDWARE_NAME: ${{ inputs.hardware_name }}
          LAYER: ${{ inputs.layer }}
          PBS_KIND: ${{ inputs.pbs_kind }}
          GROUPING_FACTOR: ${{ inputs.grouping_factor }}
          BENCH_TYPE: ${{ inputs.bench_type }}
          TIME_SPAN: ${{ inputs.time_span_days }}
          DATA_EXTRACTOR_DATABASE_USER: ${{ secrets.DATA_EXTRACTOR_DATABASE_USER }}
          DATA_EXTRACTOR_DATABASE_HOST: ${{ secrets.DATA_EXTRACTOR_DATABASE_HOST }}
          DATA_EXTRACTOR_DATABASE_PASSWORD: ${{ secrets.DATA_EXTRACTOR_DATABASE_PASSWORD }}

      - name: Produce backends comparison table from database
        if: inputs.backend_comparison == true
        run: |
          python3 -m pip install -r ci/data_extractor/requirements.txt
          python3 ci/data_extractor/src/data_extractor.py "${OUTPUT_FILENAME}" \
          --generate-svg \
          --backends-comparison \
          --time-span-days "${TIME_SPAN}"
        env:
          OUTPUT_FILENAME: ${{ inputs.output_filename }}
          TIME_SPAN: ${{ inputs.time_span_days }}
          DATA_EXTRACTOR_DATABASE_USER: ${{ secrets.DATA_EXTRACTOR_DATABASE_USER }}
          DATA_EXTRACTOR_DATABASE_HOST: ${{ secrets.DATA_EXTRACTOR_DATABASE_HOST }}
          DATA_EXTRACTOR_DATABASE_PASSWORD: ${{ secrets.DATA_EXTRACTOR_DATABASE_PASSWORD }}

      - name: Upload tables
        uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4
        with:
          name: ${{ github.sha }}_${{ inputs.backend }}_${{ inputs.layer }}_${{ inputs.pbs_kind }}_${{ inputs.bench_type }}_tables
          # This will upload all the file generated
          path: ${{ inputs.output_filename }}*.svg
          retention-days: 60
