{
  "repo": "tidbcloud/devtier-store-service",
  "binary": "billing",
  "binary_path": "cmd/billing/main.go",
  "notes": [
    "Fresh codex rescan rewrote billing around the real runtime shape: one Gin listener on :8888, one Prometheus listener on :9090, four cron-triggered jobs, and two cold-start backfills.",
    "Conditions were expanded from the old blanket Always markers. GCP BigQuery calls only work in prod, route fan-out depends on the :tag value, monthly CSV bootstrap depends on the current date suffix, and several external calls are retried up to three times."
  ],
  "dependencies": {
    "BillingLifecycle.AwsRoleInit": {
      "infrastructure": {
        "environment": {
          "conditions": [
            "Always"
          ],
          "operations": [
            {
              "operation": "READ ENV",
              "paths": [
                "AWS_ROLE_ARN"
              ],
              "conditions": [
                "Always"
              ],
              "implementation": "utils.AwsRoleInit"
            }
          ]
        }
      },
      "services": [
        {
          "name": "aws-sts",
          "target_interface": "AssumeRole credentials for shared S3 client",
          "operation_type": "aws_sdk",
          "conditions": [
            "Always"
          ],
          "implementation": "utils.AwsRoleInit -> utils.S3Init -> stscreds.NewCredentials"
        }
      ]
    },
    "BillingLifecycle.RegisterDailyCostCron": {
      "infrastructure": {},
      "services": [
        {
          "name": "local:BillingJobs",
          "target_interface": "BillingJobs.FetchServerlessDailyCost",
          "operation_type": "local_call",
          "conditions": [
            "Triggered by cron expression 0 8 * * *"
          ],
          "implementation": "main.main -> cron.AddFunc"
        }
      ]
    },
    "BillingLifecycle.RegisterCloudTenantsCron": {
      "infrastructure": {},
      "services": [
        {
          "name": "local:BillingJobs",
          "target_interface": "BillingJobs.FetchCloudTenants",
          "operation_type": "local_call",
          "conditions": [
            "Triggered by cron expression 0 0 * * *"
          ],
          "implementation": "main.main -> cron.AddFunc"
        }
      ]
    },
    "BillingLifecycle.RegisterTenantAccessInfoCron": {
      "infrastructure": {},
      "services": [
        {
          "name": "local:BillingJobs",
          "target_interface": "BillingJobs.FetchServerlessTenantAccessInfo",
          "operation_type": "local_call",
          "conditions": [
            "Triggered by cron expression 0 1 * * *"
          ],
          "implementation": "main.main -> cron.AddFunc"
        }
      ]
    },
    "BillingLifecycle.RegisterActiveClustersCron": {
      "infrastructure": {},
      "services": [
        {
          "name": "local:BillingJobs",
          "target_interface": "BillingJobs.FetchServerlessActiveClusters",
          "operation_type": "local_call",
          "conditions": [
            "Triggered by cron expression 0 2 * * *"
          ],
          "implementation": "main.main -> cron.AddFunc"
        }
      ]
    },
    "BillingLifecycle.StartCronScheduler": {
      "infrastructure": {},
      "services": []
    },
    "BillingLifecycle.StartMetricsServer": {
      "infrastructure": {
        "network": {
          "conditions": [
            "Always"
          ],
          "operations": [
            {
              "operation": "LISTEN",
              "address": "0.0.0.0:9090",
              "conditions": [
                "Always"
              ],
              "implementation": "utils.RunMetricsServer -> http.ListenAndServe"
            }
          ]
        }
      },
      "services": []
    },
    "BillingLifecycle.BootstrapDailyCost": {
      "infrastructure": {},
      "services": [
        {
          "name": "local:BillingJobs",
          "target_interface": "BillingJobs.FetchServerlessDailyCost",
          "operation_type": "local_call",
          "conditions": [
            "Always"
          ],
          "implementation": "main.main"
        }
      ]
    },
    "BillingLifecycle.BootstrapActiveClusters": {
      "infrastructure": {},
      "services": [
        {
          "name": "local:BillingJobs",
          "target_interface": "BillingJobs.FetchServerlessActiveClusters",
          "operation_type": "local_call",
          "conditions": [
            "Always"
          ],
          "implementation": "main.main"
        }
      ]
    },
    "BillingLifecycle.StartHTTPServer": {
      "infrastructure": {
        "network": {
          "conditions": [
            "Always"
          ],
          "operations": [
            {
              "operation": "LISTEN",
              "address": ":8888",
              "conditions": [
                "Always"
              ],
              "implementation": "gin.Engine.Run"
            }
          ]
        }
      },
      "services": []
    },
    "BillingMetricsHTTP.GetMetrics": {
      "infrastructure": {},
      "services": []
    },
    "BillingHTTP.TriggerTagJob": {
      "infrastructure": {},
      "services": [
        {
          "name": "local:BillingJobs",
          "target_interface": "BillingJobs.FetchServerlessTenantAccessInfo",
          "operation_type": "local_call",
          "conditions": [
            "tag == tenant"
          ],
          "implementation": "cmd/billing/main.go route GET /:tag"
        },
        {
          "name": "local:BillingJobs",
          "target_interface": "BillingJobs.FetchServerlessDailyCost",
          "operation_type": "local_call",
          "conditions": [
            "tag == cost"
          ],
          "implementation": "cmd/billing/main.go route GET /:tag"
        },
        {
          "name": "local:BillingJobs",
          "target_interface": "BillingJobs.FetchCloudTenants",
          "operation_type": "local_call",
          "conditions": [
            "tag == tenants"
          ],
          "implementation": "cmd/billing/main.go route GET /:tag"
        },
        {
          "name": "local:BillingJobs",
          "target_interface": "BillingJobs.FetchServerlessActiveClusters",
          "operation_type": "local_call",
          "conditions": [
            "tag == active_clusters"
          ],
          "implementation": "cmd/billing/main.go route GET /:tag"
        }
      ]
    },
    "BillingHTTP.CreateS3Object": {
      "infrastructure": {},
      "services": [
        {
          "name": "aws-s3",
          "target_interface": "CreateBucket, HeadBucket, PutObject",
          "operation_type": "aws_sdk",
          "conditions": [
            "Always"
          ],
          "implementation": "utils.S3NewObject"
        }
      ]
    },
    "BillingJobs.FetchServerlessDailyCost": {
      "infrastructure": {},
      "services": [
        {
          "name": "local:BillingJobs",
          "target_interface": "BillingJobs.ProcessCostSnapshot",
          "operation_type": "local_call",
          "conditions": [
            "Up to 3 attempts until processer returns nil"
          ],
          "implementation": "cmd/billing/main.go FatchServerlessDailyCost"
        },
        {
          "name": "local:BillingJobs",
          "target_interface": "BillingJobs.UpdateServerlessDb",
          "operation_type": "local_call",
          "conditions": [
            "Started in a goroutine on every FetchServerlessDailyCost run",
            "Runs offsets 4, 2, and 1"
          ],
          "implementation": "cmd/billing/main.go processer"
        }
      ]
    },
    "BillingJobs.ProcessCostSnapshot": {
      "infrastructure": {},
      "services": [
        {
          "name": "aws-athena",
          "target_interface": "StartQueryExecution, GetQueryExecution, GetQueryResults",
          "operation_type": "aws_sdk",
          "conditions": [
            "Always"
          ],
          "implementation": "pkg/billing.GetAwsServerlessCostFromAthena"
        },
        {
          "name": "o11y-prometheus",
          "target_interface": "GET /api/v1/query across all readRemote regions",
          "operation_type": "http_get",
          "conditions": [
            "Always",
            "Queries 1d total and active cluster counts"
          ],
          "implementation": "pkg/billing.GetServerlessClusterNumFromMetrics -> utils.DoO11yQueryALL"
        },
        {
          "name": "gcp-bigquery",
          "target_interface": "Query.Read",
          "operation_type": "gcp_sdk",
          "conditions": [
            "Env == prod"
          ],
          "implementation": "pkg/billing.GetServerlessClusterNumFromGCP"
        },
        {
          "name": "aws-secrets-manager",
          "target_interface": "GetSecretValue",
          "operation_type": "aws_sdk",
          "conditions": [
            "Env == prod"
          ],
          "implementation": "pkg/billing.getGcpClient -> utils.GetAwsSecret"
        },
        {
          "name": "aws-s3",
          "target_interface": "GetObject, PutObject",
          "operation_type": "aws_sdk",
          "conditions": [
            "Always",
            "Two CSV outputs are appended after 7d and 30d active-cluster metrics are fetched"
          ],
          "implementation": "pkg/billing.S3UpdateObject -> utils.S3KeyAppend"
        },
        {
          "name": "o11y-prometheus",
          "target_interface": "GET /api/v1/query across all readRemote regions",
          "operation_type": "http_get",
          "conditions": [
            "Always",
            "Extra 7d and 30d active-cluster queries happen inside S3UpdateObject"
          ],
          "implementation": "pkg/billing.S3UpdateObject -> pkg/billing.GetServerlessClusterNumFromMetrics"
        }
      ]
    },
    "BillingJobs.UpdateServerlessDb": {
      "infrastructure": {},
      "services": [
        {
          "name": "aws-athena",
          "target_interface": "StartQueryExecution, GetQueryExecution, GetQueryResults",
          "operation_type": "aws_sdk",
          "conditions": [
            "Always"
          ],
          "implementation": "pkg/billing.GetAwsServerlessCostByRegion"
        },
        {
          "name": "prod-serverless-rds",
          "target_interface": "SELECT region_name, COUNT(*) FROM clusters GROUP BY region_name",
          "operation_type": "mysql",
          "conditions": [
            "Always"
          ],
          "implementation": "pkg/billing.GetServerlessClusterByRegion"
        },
        {
          "name": "tidb-cloud-mysql-gateway",
          "target_interface": "SELECT paid cost aggregates; REPLACE INTO serverless_billing_daily; REPLACE INTO serverless_billing_free_cluster_daily",
          "operation_type": "mysql",
          "conditions": [
            "Always",
            "Free-cluster cost is reduced when paidCostMap contains provider+region"
          ],
          "implementation": "cmd/billing/main.go updateServerlessDb"
        }
      ]
    },
    "BillingJobs.FetchCloudTenants": {
      "infrastructure": {},
      "services": [
        {
          "name": "gcp-bigquery",
          "target_interface": "Query.Read",
          "operation_type": "gcp_sdk",
          "conditions": [
            "Env == prod",
            "Up to 3 retries if query or downstream DB writes fail"
          ],
          "implementation": "pkg/billing.GetNewTenantsFromGCP"
        },
        {
          "name": "aws-secrets-manager",
          "target_interface": "GetSecretValue",
          "operation_type": "aws_sdk",
          "conditions": [
            "Env == prod"
          ],
          "implementation": "pkg/billing.getGcpClient -> utils.GetAwsSecret"
        },
        {
          "name": "tidb-cloud-mysql-gateway",
          "target_interface": "REPLACE INTO tenant_meta.immutable_gcp_dwm_cloud_tenants",
          "operation_type": "mysql",
          "conditions": [
            "For each tenant returned by BigQuery"
          ],
          "implementation": "cmd/billing/main.go FatchCloudTenants"
        }
      ]
    },
    "BillingJobs.FetchServerlessTenantAccessInfo": {
      "infrastructure": {},
      "services": [
        {
          "name": "aws-s3",
          "target_interface": "CreateBucket, HeadBucket, PutObject",
          "operation_type": "aws_sdk",
          "conditions": [
            "Only when date suffix is -01",
            "Up to 3 retries for the monthly CSV bootstrap file"
          ],
          "implementation": "pkg/billing.FetchServerlessTenantAccessInfo -> utils.S3NewObject"
        },
        {
          "name": "o11y-prometheus",
          "target_interface": "GET /api/v1/query across all readRemote regions",
          "operation_type": "http_get",
          "conditions": [
            "Always",
            "Runs one query for tidb_connection and one for tidb_statement"
          ],
          "implementation": "pkg/billing.FetchServerlessTenantAccessInfo -> utils.DoO11yQueryALL"
        },
        {
          "name": "aws-s3",
          "target_interface": "GetObject, PutObject",
          "operation_type": "aws_sdk",
          "conditions": [
            "Always",
            "Up to 3 retries while appending tenant activity CSV rows"
          ],
          "implementation": "pkg/billing.FetchServerlessTenantAccessInfo -> utils.S3KeyAppend"
        }
      ]
    },
    "BillingJobs.FetchServerlessActiveClusters": {
      "infrastructure": {},
      "services": [
        {
          "name": "tidb-cloud-mysql-gateway",
          "target_interface": "REPLACE INTO serverless_tier.serverless_active_clusters",
          "operation_type": "mysql",
          "conditions": [
            "For each provider/region pair returned by metrics",
            "Retry loop runs up to 3 times"
          ],
          "implementation": "cmd/billing/main.go FatchServerlessActiveClusters"
        },
        {
          "name": "o11y-prometheus",
          "target_interface": "GET /api/v1/query per region",
          "operation_type": "http_get",
          "conditions": [
            "Always",
            "Queries step 1d and 30d separately"
          ],
          "implementation": "pkg/billing.GetServerlessActiveClusterFromMetrics -> utils.DoO11yQueryWithRegion"
        }
      ]
    }
  }
}
