name: CI Monitor

on:
  schedule:
    - cron: '0 */12 * * *' # Every 12 hours for main analysis
  workflow_dispatch:
    inputs:
      limit:
        description: 'Number of CI runs to analyze'
        required: false
        default: '1000'
        type: string

concurrency:
  group: ci-monitor-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: write
  actions: read

jobs:
  ci-monitor:
    if: github.repository == 'sgl-project/sglang'|| github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.9'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install requests matplotlib pandas

      - name: Run CI Analysis
        env:
          GITHUB_TOKEN: ${{ secrets.GH_PAT_FOR_NIGHTLY_CI_DATA }}
          PYTHONUNBUFFERED: 1
          PYTHONIOENCODING: utf-8
        run: |
          cd scripts/ci_monitor
          python ci_analyzer.py --token $GITHUB_TOKEN --limit ${{ inputs.limit || '1000' }} --output ci_analysis_$(date +%Y%m%d_%H%M%S).json

      - name: Run Performance Analysis
        env:
          GITHUB_TOKEN: ${{ secrets.GH_PAT_FOR_NIGHTLY_CI_DATA }}
          PYTHONUNBUFFERED: 1
          PYTHONIOENCODING: utf-8
        run: |
          cd scripts/ci_monitor
          python ci_analyzer_perf.py --token $GITHUB_TOKEN --limit ${{ inputs.limit || '1000' }} --output-dir performance_tables_$(date +%Y%m%d_%H%M%S) --upload-to-github

      - name: Upload Analysis Results
        uses: actions/upload-artifact@v4
        with:
          name: ci-analysis-results-${{ github.run_number }}
          path: |
            scripts/ci_monitor/ci_analysis_*.json
            scripts/ci_monitor/performance_tables_*
          retention-days: 30

  ci-monitor-balance:
    needs: ci-monitor
    if: github.repository == 'sgl-project/sglang' || github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.9'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install requests

      - name: Run Test Balance Analysis
        env:
          GITHUB_TOKEN: ${{ secrets.GH_PAT_FOR_NIGHTLY_CI_DATA }}
          PYTHONUNBUFFERED: 1
          PYTHONIOENCODING: utf-8
        run: |
          cd scripts/ci_monitor
          python ci_analyzer_balance.py --token $GITHUB_TOKEN --limit ${{ inputs.limit || '1000' }} --output test_balance_report_$(date +%Y%m%d_%H%M%S).json

      - name: Upload Balance Analysis Results
        uses: actions/upload-artifact@v4
        with:
          name: test-balance-results-${{ github.run_number }}
          path: |
            scripts/ci_monitor/test_balance_report_*.json
            scripts/ci_monitor/test_balance_report_*.csv
          retention-days: 30
