{
  "repo": "tidbcloud/metrics-server",
  "binary": "central_metrics",
  "binary_path": "cmd/central_metrics/main.go",
  "dependencies": {
    "ClusterService.CreateMetricsKey": {
      "infrastructure": {
        "mysql": {
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "Get all existing metrics keys by project",
              "conditions": ["Always"],
              "implementation": "dbutil.GetAllMetricsKeyByProject(tx, projectID)"
            },
            {
              "operation": "INSERT",
              "trigger": "Create new metrics key",
              "conditions": ["Always"],
              "implementation": "dbutil.CreateMetricsKey(tx, projectID, user.ID, key)"
            }
          ]
        }
      },
      "services": []
    },
    "ClusterService.GetAllMetricsKey": {
      "infrastructure": {
        "mysql": {
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "Get all metrics keys by project",
              "conditions": ["Always"],
              "implementation": "dbutil.GetAllMetricsKeyByProject(tx, projectID)"
            }
          ]
        }
      },
      "services": []
    },
    "ClusterService.DeleteMetricsKey": {
      "infrastructure": {
        "mysql": {
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "Get metrics key by ID",
              "conditions": ["Always"],
              "implementation": "dbutil.GetMetricsKeyByID(tx, keyID, projectID)"
            },
            {
              "operation": "DELETE",
              "trigger": "Delete metrics key",
              "conditions": ["Key exists and belongs to project"],
              "implementation": "dbutil.DeleteMetricsKey(tx, metricsKey.ID, projectID)"
            }
          ]
        }
      },
      "services": []
    },
    "IntegrationService.CreateIntegration": {
      "infrastructure": {
        "mysql": {
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "SELECT/INSERT/UPDATE",
              "trigger": "Create or enable integration",
              "conditions": ["Always"],
              "implementation": "model.CreateOrEnableDatadogIntegration/CreateOrEnableNewrelicIntegration"
            }
          ]
        }
      },
      "services": [
        {
          "name": "datadog",
          "target_interface": "POST /api/v1/validate",
          "operation_type": "http_post",
          "conditions": ["IntegrationType == DATADOG"],
          "operations": [
            {
              "operation": "HTTP POST",
              "trigger": "Validate Datadog API key",
              "conditions": ["Always"],
              "implementation": "model.ValidateDatadogAPIKey(ctx, apiKey, site)"
            }
          ]
        },
        {
          "name": "newrelic",
          "target_interface": "POST /log/v1",
          "operation_type": "http_post",
          "conditions": ["IntegrationType == NEWRELIC"],
          "operations": [
            {
              "operation": "HTTP POST",
              "trigger": "Validate New Relic API key",
              "conditions": ["Always"],
              "implementation": "model.ValidateNewrelicAPIKey(ctx, apiKey, site) -> HTTP POST to GetNewrelicSiteLogEp(site)"
            }
          ]
        }
      ]
    },
    "IntegrationService.DeleteIntegration": {
      "infrastructure": {
        "mysql": {
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "UPDATE",
              "trigger": "Disable integration",
              "conditions": ["Always"],
              "implementation": "model.DisableDatadogIntegration/DisableNewrelicIntegration"
            }
          ]
        }
      },
      "services": []
    },
    "IntegrationService.ValidateDatadogAPIKey": {
      "infrastructure": {},
      "services": [
        {
          "name": "datadog",
          "target_interface": "POST /api/v1/validate",
          "operation_type": "http_post",
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "HTTP POST",
              "trigger": "Validate Datadog API key",
              "conditions": ["Always"],
              "implementation": "model.ValidateDatadogAPIKey(ctx, req.DatadogApiKey, req.DatadogSite)"
            }
          ]
        }
      ]
    },
    "IntegrationService.ValidateNewrelicAPIKey": {
      "infrastructure": {},
      "services": [
        {
          "name": "newrelic",
          "target_interface": "POST /log/v1",
          "operation_type": "http_post",
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "HTTP POST",
              "trigger": "Validate New Relic API key",
              "conditions": ["Always"],
              "implementation": "model.ValidateNewrelicAPIKey(ctx, req.NewrelicApiKey, req.NewrelicSite) -> HTTP POST to GetNewrelicSiteLogEp(site)"
            }
          ]
        }
      ]
    },
    "IntegrationService.ListIntegrations": {
      "infrastructure": {
        "mysql": {
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "List integrations by project",
              "conditions": ["Always"],
              "implementation": "model.ListIntegrations(ctx, projectID, type)"
            }
          ]
        }
      },
      "services": []
    },
    "UserAlertService.SubscribeUserAlert": {
      "infrastructure": {
        "mysql": {
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "Check existing user alert",
              "conditions": ["Always"],
              "implementation": "entity.NewUserAlertQuerySet(tx).UserIDEq(userID).ProjectIDEq(projectID).One(&userAlert)"
            },
            {
              "operation": "INSERT",
              "trigger": "Create user alert subscription",
              "conditions": ["User not already subscribed"],
              "implementation": "tx.Create(&entity.UserAlert{...})"
            }
          ]
        }
      },
      "services": [
        {
          "name": "account-svc",
          "target_interface": "AccountAPIService.GetUserByEmail",
          "operation_type": "grpc_unary",
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "gRPC GetUserByEmail",
              "trigger": "Get user by email",
              "conditions": ["Always"],
              "implementation": "grpcli.GetUserByEmail(ctx, &account.GetUserByEmailRequest{Email: req.Email})"
            }
          ]
        },
        {
          "name": "account-svc",
          "target_interface": "AccountAPIService.ListOrgsByUser",
          "operation_type": "grpc_unary",
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "gRPC ListOrgsByUser",
              "trigger": "Verify user belongs to org",
              "conditions": ["Always"],
              "implementation": "grpcli.ListOrgsByUser(ctx, &account.ListOrgsByUserRequest{UserId: user.User.Id})"
            }
          ]
        }
      ]
    },
    "UserAlertService.UnsubscribeUserAlert": {
      "infrastructure": {
        "mysql": {
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "Get user alert by ID",
              "conditions": ["Always"],
              "implementation": "entity.NewUserAlertQuerySet(tx).IDEq(userAlertID).One(&userAlert)"
            },
            {
              "operation": "DELETE",
              "trigger": "Delete user alert",
              "conditions": ["Alert belongs to project"],
              "implementation": "entity.NewUserAlertQuerySet(tx).ProjectIDEq(projectID).IDIn(userAlert.ID).Delete()"
            }
          ]
        }
      },
      "services": []
    },
    "UserAlertService.GetUserAlertList": {
      "infrastructure": {
        "mysql": {
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "Get all user alerts by project",
              "conditions": ["Always"],
              "implementation": "entity.NewUserAlertQuerySet(tx).ProjectIDEq(projectID).TypeEq(Email).All(&userAlerts)"
            }
          ]
        }
      },
      "services": [
        {
          "name": "account-svc",
          "target_interface": "AccountAPIService.GetUserByID",
          "operation_type": "grpc_unary",
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "gRPC GetUserByID",
              "trigger": "Get user info for each alert",
              "conditions": ["Always"],
              "implementation": "grpcli.GetUserByID(ctx, &account.GetUserByIDRequest{UserId: uint64(i.UserID)})"
            }
          ]
        }
      ]
    },
    "HTTP GET /healthz": {
      "infrastructure": {},
      "services": []
    },
    "HTTP GET /version": {
      "infrastructure": {},
      "services": []
    },
    "HTTP GET /metrics": {
      "infrastructure": {},
      "services": []
    },
    "HTTP POST /api/useralerts/v1/alert": {
      "infrastructure": {
        "mysql": {
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "Get email user alerts by project",
              "conditions": ["Always"],
              "implementation": "userAlertService.GetEmailUserAlertsByProjectID(ctx, tx, projectID)"
            }
          ]
        }
      },
      "services": [
        {
          "name": "core-svc",
          "target_interface": "CoreService.ListClusters",
          "operation_type": "grpc_unary",
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "gRPC ListClusterByID",
              "trigger": "Get cluster info",
              "conditions": ["Always"],
              "implementation": "grpcli.ListClusterByID(ctx, clusterID)"
            }
          ]
        },
        {
          "name": "account-svc",
          "target_interface": "AccountAPIService.GetProjectByID",
          "operation_type": "grpc_unary",
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "gRPC GetProjectByID",
              "trigger": "Get project info",
              "conditions": ["Always"],
              "implementation": "grpcli.GetProjectByID(ctx, &account.GetProjectByIDRequest{Id: cluster.ProjectId})"
            }
          ]
        },
        {
          "name": "account-svc",
          "target_interface": "AccountAPIService.GetUserByID",
          "operation_type": "grpc_unary",
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "gRPC GetUserByID",
              "trigger": "Get user email for alert",
              "conditions": ["Always"],
              "implementation": "grpcli.GetUserByID(ctx, &account.GetUserByIDRequest{UserId: uint64(i.UserID)})"
            }
          ]
        },
        {
          "name": "account-svc",
          "target_interface": "AccountAPIService.SendEmail",
          "operation_type": "grpc_unary",
          "conditions": ["User emails found"],
          "operations": [
            {
              "operation": "gRPC SendEmail",
              "trigger": "Send alert email to users",
              "conditions": ["Always"],
              "implementation": "grpcli.SendEmail(ctx, &account.SendEmailReq{...})"
            }
          ]
        }
      ]
    },
    "HTTP POST /api/useralerts/v1/alert-webhook": {
      "infrastructure": {
        "mysql": {
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "Get webhook user alerts by project",
              "conditions": ["Always"],
              "implementation": "userAlertService.GetWebhookUserAlertsByProjectID(ctx, tx, projectID)"
            }
          ]
        }
      },
      "services": [
        {
          "name": "core-svc",
          "target_interface": "CoreService.ListClusters",
          "operation_type": "grpc_unary",
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "gRPC ListClusterByID",
              "trigger": "Get cluster info",
              "conditions": ["Always"],
              "implementation": "grpcli.ListClusterByID(ctx, clusterID)"
            }
          ]
        },
        {
          "name": "account-svc",
          "target_interface": "AccountAPIService.GetProjectByID",
          "operation_type": "grpc_unary",
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "gRPC GetProjectByID",
              "trigger": "Get project info",
              "conditions": ["Always"],
              "implementation": "grpcli.GetProjectByID(ctx, &account.GetProjectByIDRequest{Id: cluster.ProjectId})"
            }
          ]
        },
        {
          "name": "external-webhook",
          "target_interface": "POST {userAlert.WebhookUrl}",
          "operation_type": "http_post",
          "conditions": ["Webhook user alerts found"],
          "operations": [
            {
              "operation": "HTTP POST",
              "trigger": "Send alert to user webhook",
              "conditions": ["Always"],
              "implementation": "client.Do(req) where req = http.NewRequest('POST', userAlert.WebhookUrl, payload)"
            }
          ]
        }
      ]
    },
    "Runtime CentralMetricsLifecycle.Startup": {
      "infrastructure": {
        "mysql": {
          "conditions": ["Always"],
          "operations": [
            {
              "operation": "CONNECT",
              "trigger": "Initialize database connection pool",
              "conditions": ["Always"],
              "implementation": "dbinit.InitializeV2(mysqlConfig, false)"
            }
          ]
        }
      },
      "services": []
    }
  }
}
