-
-
Notifications
You must be signed in to change notification settings - Fork 386
feat(availability): 可用性监控 outbox + 1 分钟投影桶 #1416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ding113
merged 3 commits into
ding113:dev
from
LamClod:feat/availability-projection-outbox
Aug 12, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| -- Availability projection: outbox + 1-minute buckets (read path no longer scans message_request) | ||
| CREATE EXTENSION IF NOT EXISTS pgcrypto;--> statement-breakpoint | ||
|
|
||
| CREATE TABLE IF NOT EXISTS "outbox_events" ( | ||
| "id" bigserial PRIMARY KEY, | ||
| "event_id" uuid DEFAULT gen_random_uuid() NOT NULL, | ||
| "event_type" text NOT NULL, | ||
| "aggregate_type" text NOT NULL, | ||
| "aggregate_id" bigint NOT NULL, | ||
| "occurred_at" timestamp with time zone NOT NULL, | ||
| "payload" jsonb NOT NULL, | ||
| "created_at" timestamp with time zone DEFAULT now() NOT NULL, | ||
| "published_at" timestamp with time zone, | ||
| "attempts" integer DEFAULT 0 NOT NULL, | ||
| "last_error" text, | ||
| CONSTRAINT "outbox_events_event_id_key" UNIQUE("event_id") | ||
| );--> statement-breakpoint | ||
|
|
||
| CREATE INDEX IF NOT EXISTS "idx_outbox_events_unpublished" | ||
| ON "outbox_events" USING btree ("id" ASC) | ||
| WHERE "published_at" IS NULL;--> statement-breakpoint | ||
|
|
||
| CREATE TABLE IF NOT EXISTS "outbox_processed" ( | ||
| "event_id" uuid PRIMARY KEY, | ||
| "processed_at" timestamp with time zone DEFAULT now() NOT NULL | ||
| );--> statement-breakpoint | ||
|
|
||
| CREATE TABLE IF NOT EXISTS "proj_applied_requests" ( | ||
| "request_id" bigint PRIMARY KEY, | ||
| "event_id" uuid NOT NULL, | ||
| "applied_at" timestamp with time zone DEFAULT now() NOT NULL | ||
| );--> statement-breakpoint | ||
|
|
||
| CREATE TABLE IF NOT EXISTS "avail_bucket_1m" ( | ||
| "provider_id" integer NOT NULL, | ||
| "bucket_start" timestamp with time zone NOT NULL, | ||
| "success_cnt" integer DEFAULT 0 NOT NULL, | ||
| "failure_cnt" integer DEFAULT 0 NOT NULL, | ||
| "excluded_cnt" integer DEFAULT 0 NOT NULL, | ||
| "latency_cnt" integer DEFAULT 0 NOT NULL, | ||
| "latency_sum_ms" bigint DEFAULT 0 NOT NULL, | ||
| "last_request_at" timestamp with time zone, | ||
| CONSTRAINT "avail_bucket_1m_pkey" PRIMARY KEY("provider_id","bucket_start") | ||
| );--> statement-breakpoint | ||
|
|
||
| CREATE INDEX IF NOT EXISTS "idx_avail_bucket_1m_time" | ||
| ON "avail_bucket_1m" USING btree ("bucket_start" DESC);--> statement-breakpoint | ||
|
|
||
| CREATE TABLE IF NOT EXISTS "avail_current" ( | ||
| "provider_id" integer PRIMARY KEY, | ||
| "state" text DEFAULT 'unknown' NOT NULL, | ||
| "availability" double precision DEFAULT 0 NOT NULL, | ||
| "request_count" integer DEFAULT 0 NOT NULL, | ||
| "last_request_at" timestamp with time zone, | ||
| "updated_at" timestamp with time zone DEFAULT now() NOT NULL | ||
| );--> statement-breakpoint | ||
|
|
||
| CREATE TABLE IF NOT EXISTS "projection_meta" ( | ||
| "key" text PRIMARY KEY, | ||
| "value" jsonb DEFAULT '{}'::jsonb NOT NULL, | ||
| "updated_at" timestamp with time zone DEFAULT now() NOT NULL | ||
| );--> statement-breakpoint | ||
|
|
||
| INSERT INTO "projection_meta" ("key", "value") | ||
| VALUES ('bootstrap', jsonb_build_object('version', 1, 'note', 'availability outbox projections')) | ||
| ON CONFLICT ("key") DO NOTHING;--> statement-breakpoint | ||
|
|
||
| CREATE OR REPLACE FUNCTION trg_message_request_outbox() | ||
| RETURNS trigger | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| DECLARE | ||
| v_outcome text; | ||
| BEGIN | ||
| IF NEW.status_code IS NULL THEN | ||
| RETURN NEW; | ||
| END IF; | ||
|
|
||
| IF TG_OP = 'UPDATE' AND OLD.status_code IS NOT NULL THEN | ||
| RETURN NEW; | ||
| END IF; | ||
|
|
||
| IF COALESCE(NEW.is_replay, false) THEN | ||
| RETURN NEW; | ||
| END IF; | ||
|
|
||
| BEGIN | ||
| v_outcome := fn_compute_message_request_success_rate_outcome( | ||
| NEW.blocked_by, | ||
| NEW.status_code, | ||
| NEW.error_message, | ||
| NEW.provider_chain | ||
| ); | ||
| EXCEPTION WHEN OTHERS THEN | ||
| RAISE WARNING 'trg_message_request_outbox: outcome compute failed for request %: %', | ||
| NEW.id, SQLERRM; | ||
| v_outcome := NULL; | ||
| END; | ||
|
|
||
| IF v_outcome IS NULL THEN | ||
| RETURN NEW; | ||
| END IF; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| INSERT INTO outbox_events ( | ||
| event_type, aggregate_type, aggregate_id, occurred_at, payload | ||
| ) VALUES ( | ||
| 'request_finalized', | ||
| 'message_request', | ||
| NEW.id, | ||
| COALESCE(NEW.created_at, now()), | ||
| jsonb_build_object( | ||
| 'request_id', NEW.id, | ||
| 'provider_id', NEW.provider_id, | ||
| 'model', NEW.model, | ||
| 'occurred_at', COALESCE(NEW.created_at, now()), | ||
| 'status_code', NEW.status_code, | ||
| 'duration_ms', NEW.duration_ms, | ||
| 'ttfb_ms', NEW.ttfb_ms, | ||
| 'blocked_by', NEW.blocked_by, | ||
| 'outcome', v_outcome, | ||
| 'group_tag', (SELECT group_tag FROM providers p WHERE p.id = NEW.provider_id), | ||
| 'is_replay', COALESCE(NEW.is_replay, false) | ||
| ) | ||
| ); | ||
|
|
||
| RETURN NEW; | ||
| END; | ||
| $$;--> statement-breakpoint | ||
|
|
||
| DROP TRIGGER IF EXISTS message_request_outbox_aiud ON message_request;--> statement-breakpoint | ||
| CREATE TRIGGER message_request_outbox_aiud | ||
| AFTER INSERT OR UPDATE OF status_code, duration_ms, error_message, provider_chain, blocked_by | ||
| ON message_request | ||
| FOR EACH ROW | ||
| EXECUTE FUNCTION trg_message_request_outbox(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This migration creates new tables, but they are not added to the schema used by
drizzle.config.ts(src/drizzle/schema.ts), and nodrizzle/meta/0120_snapshot.jsonwas added; the nextbun run db:generatewill be based on a schema history that does not know about these projection tables and can drift or try to undo them. Please add the tables tosrc/drizzle/schema.tsand regenerate the migration.AGENTS.md reference: AGENTS.md:L55-L58
Useful? React with 👍 / 👎.