{
  "name": "MLOps Pipeline",
  "nodes": [
    {
      "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        100,
        300
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "start-pipeline",
        "responseMode": "onReceived",
        "options": {}
      }
    },
    {
      "id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "name": "Prepare Dataset",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        350,
        300
      ],
      "parameters": {
        "command": "=cd /workspace && DOCKER_API_VERSION=1.44 TRAIN_RECORDS={{ $('Webhook').item.json.body?.train_records ?? 20000 }} VAL_RECORDS={{ $('Webhook').item.json.body?.val_records ?? 2000 }} docker compose --profile prepare up --build"
      },
      "onError": "continueErrorOutput"
    },
    {
      "id": "c3d4e5f6-a7b8-9012-cdef-123456789012",
      "name": "Train Model",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        600,
        300
      ],
      "parameters": {
        "command": "=cd /workspace && DOCKER_API_VERSION=1.44 EPOCHS={{ $('Webhook').item.json.body?.epochs ?? 5 }} BATCH_SIZE={{ $('Webhook').item.json.body?.batch_size ?? 32 }} THRESHOLD={{ $('Webhook').item.json.body?.threshold ?? 0.30 }} TRAIN_RECORDS={{ $('Webhook').item.json.body?.train_records ?? 20000 }} VAL_RECORDS={{ $('Webhook').item.json.body?.val_records ?? 2000 }} docker compose --profile train up --exit-code-from train"
      },
      "onError": "continueErrorOutput"
    },
    {
      "id": "d4e5f6a7-b8c9-0123-defa-234567890123",
      "name": "Extract run_id",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        850,
        300
      ],
      "parameters": {
        "jsCode": "const stdout = $('Train Model').item.json.stdout || '';\nconst lines = stdout.trim().split('\\n').filter(l => l.trim() !== '');\nconst lastLine = lines[lines.length - 1];\n\nlet trainResult;\ntry {\n  trainResult = JSON.parse(lastLine);\n} catch (e) {\n  throw new Error(`Failed to parse train JSON from last stdout line: ${lastLine}\\n${e.message}`);\n}\n\nconst webhookBody = $('Webhook').item.json.body || {};\n\nreturn [{\n  json: {\n    run_id: trainResult.run_id,\n    metric_value: trainResult.metric_value,\n    status: trainResult.status,\n    threshold: webhookBody.threshold ?? 0.30,\n    epochs: webhookBody.epochs ?? 5,\n    batch_size: webhookBody.batch_size ?? 32,\n    git_sha: $runIndex !== undefined\n      ? require('child_process').execSync('git -C /workspace rev-parse --short HEAD 2>/dev/null || echo unknown').toString().trim()\n      : 'unknown',\n    metadata_path: trainResult.metadata_path,\n    export_dir: trainResult.export_dir\n  }\n}];"
      },
      "onError": "continueErrorOutput"
    },
    {
      "id": "e5f6a7b8-c9d0-1234-efab-345678901234",
      "name": "Validate",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        1100,
        300
      ],
      "parameters": {
        "command": "=cd /workspace && python3 pipeline/validate.py --run_id {{ $json.run_id }} --threshold {{ $json.threshold }}"
      },
      "onError": "continueErrorOutput"
    },
    {
      "id": "f6a7b8c9-d0e1-2345-fabc-456789012345",
      "name": "Publish",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        1350,
        300
      ],
      "parameters": {
        "command": "=cd /workspace && python3 pipeline/publish.py --run_id {{ $json.run_id }} --git_sha {{ $json.git_sha }} --epochs {{ $json.epochs }} --threshold {{ $json.threshold }} --artifacts_dir artifacts"
      },
      "onError": "continueErrorOutput"
    },
    {
      "id": "a7b8c9d0-e1f2-3456-abcd-567890123456",
      "name": "Deploy",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1600,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "http://api:8000/reload",
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ run_id: $('Extract run_id').item.json.run_id }) }}",
        "options": {}
      },
      "onError": "continueErrorOutput"
    },
    {
      "id": "b8c9d0e1-f2a3-4567-bcde-678901234567",
      "name": "Notify Success",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.3,
      "position": [
        1850,
        300
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "assign-success-status",
              "name": "pipeline_status",
              "value": "success",
              "type": "string"
            },
            {
              "id": "assign-success-run-id",
              "name": "run_id",
              "value": "={{ $('Extract run_id').item.json.run_id }}",
              "type": "string"
            },
            {
              "id": "assign-success-metric",
              "name": "metric_value",
              "value": "={{ $('Extract run_id').item.json.metric_value }}",
              "type": "number"
            },
            {
              "id": "assign-success-message",
              "name": "message",
              "value": "={{ 'Pipeline completed successfully. Model ' + $('Extract run_id').item.json.run_id + ' deployed with val_token_accuracy=' + $('Extract run_id').item.json.metric_value }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "c9d0e1f2-a3b4-5678-cdef-789012345678",
      "name": "Notify Failure",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.3,
      "position": [
        1100,
        550
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "assign-failure-status",
              "name": "pipeline_status",
              "value": "failed",
              "type": "string"
            },
            {
              "id": "assign-failure-error",
              "name": "error",
              "value": "={{ $json.error?.message ?? $json.stderr ?? 'Unknown error' }}",
              "type": "string"
            },
            {
              "id": "assign-failure-message",
              "name": "message",
              "value": "={{ 'Pipeline failed at stage: ' + $runIndex + '. Error: ' + ($json.error?.message ?? $json.stderr ?? 'Unknown error') }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Prepare Dataset",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Dataset": {
      "main": [
        [
          {
            "node": "Train Model",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Notify Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Train Model": {
      "main": [
        [
          {
            "node": "Extract run_id",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Notify Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract run_id": {
      "main": [
        [
          {
            "node": "Validate",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Notify Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate": {
      "main": [
        [
          {
            "node": "Publish",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Notify Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Publish": {
      "main": [
        [
          {
            "node": "Deploy",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Notify Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Deploy": {
      "main": [
        [
          {
            "node": "Notify Success",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Notify Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true,
    "callerPolicy": "workflowsFromSameOwner",
    "errorWorkflow": ""
  },
  "id": "mlops-pipeline-001",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "tags": []
}