ci_now_ms() {
  node -e 'console.log(Date.now())'
}

ci_timing_emit() {
  stage="$1"
  status="$2"
  started_ms="$3"
  finished_ms="$(ci_now_ms)"
  duration_ms="$((finished_ms - started_ms))"
  service_id="${HWLAB_TIMING_SERVICE_ID:-}"
  node -e 'const [stage,status,durationMs,serviceId]=process.argv.slice(1); const payload={event:"node-cicd-timing",schemaVersion:"v1",stage,status,durationMs:Number(durationMs),pipelineRun:process.env.HWLAB_TEKTON_PIPELINERUN||null,taskRun:process.env.HWLAB_TEKTON_TASKRUN||null,task:process.env.HWLAB_TEKTON_TASK||null,revision:process.env.HWLAB_SOURCE_REVISION||null,serviceId:serviceId||null,source:"scripts/gitops-render.mjs",at:new Date().toISOString()}; console.log(JSON.stringify(payload));' "$stage" "$status" "$duration_ms" "$service_id"
}
