-
Notifications
You must be signed in to change notification settings - Fork 1
feat: expose per-tenant enrichment status and backlog metric (ENG-1670) #111
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
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ed88be8
feat(enrichment): add GET /v1/enrichment-status endpoint (ENG-1670)
xernobyl fffe83e
feat(observability): add aggregate enrichment-backlog gauge (ENG-1670)
xernobyl 92fdeb0
docs(openapi): add /v1/enrichment-status to the API contract (ENG-1670)
xernobyl a907e4c
refactor(enrichment): address pre-PR review nits (ENG-1670)
xernobyl 9788622
perf(enrichment): run the backlog poller every 5m instead of 60s (ENG…
xernobyl 92151a6
chore(deps): bump golang.org/x/text to v0.39.0 (GO-2026-5970)
xernobyl c23c285
refactor(enrichment): address review comments (ENG-1670)
xernobyl bc4fb98
refactor(enrichment): honest content-trim + emotions gate test (ENG-1…
xernobyl b8d3e8b
fix(enrichment): single-flight backlog poll, alertable failures, hone…
xernobyl ef4af84
fix(enrichment): emotions completion marker + sticky poller leadershi…
xernobyl 8fbfddf
fix(enrichment): withdraw gauge on demotion, realign backfill index, …
xernobyl cb51b5b
docs(openapi): declare EnrichmentTypeStatus as a Stainless model (ENG…
xernobyl 9d9c083
docs(openapi): use snake_case for the EnrichmentTypeStatus model path…
xernobyl 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
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
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
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
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,47 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
|
|
||
| "github.com/formbricks/hub/internal/api/response" | ||
| "github.com/formbricks/hub/internal/models" | ||
| ) | ||
|
|
||
| // EnrichmentStatusService is the application service used by the enrichment status handler. | ||
| type EnrichmentStatusService interface { | ||
| GetEnrichmentStatus(ctx context.Context, tenantID string) (*models.EnrichmentStatusResponse, error) | ||
| } | ||
|
|
||
| // EnrichmentStatusHandler hosts the public enrichment-status endpoint. | ||
| type EnrichmentStatusHandler struct { | ||
| service EnrichmentStatusService | ||
| } | ||
|
|
||
| // NewEnrichmentStatusHandler creates an enrichment status handler. | ||
| func NewEnrichmentStatusHandler(service EnrichmentStatusService) *EnrichmentStatusHandler { | ||
| return &EnrichmentStatusHandler{service: service} | ||
| } | ||
|
|
||
| // GetStatus handles GET /v1/enrichment-status?tenant_id=<id>. It returns per-enrichment | ||
| // eligible/done counts for the tenant. tenant_id is required; a missing/blank value yields 400. | ||
| func (h *EnrichmentStatusHandler) GetStatus(w http.ResponseWriter, r *http.Request) { | ||
| if h.service == nil { | ||
| response.RespondServiceUnavailable(w, r, "Enrichment status is not available.") | ||
|
|
||
| return | ||
| } | ||
|
|
||
| tenantID := r.URL.Query().Get("tenant_id") | ||
|
|
||
| result, err := h.service.GetEnrichmentStatus(r.Context(), tenantID) | ||
| if err != nil { | ||
| // RespondError maps a missing/blank tenant_id (huberrors.ValidationError) to 400 and | ||
| // any repository/DB failure to a generic 500 — no internals leak to the client. | ||
| response.RespondError(w, r, err) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| response.RespondJSON(w, http.StatusOK, result) | ||
| } |
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,66 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/formbricks/hub/internal/huberrors" | ||
| "github.com/formbricks/hub/internal/models" | ||
| ) | ||
|
|
||
| type fakeEnrichmentStatusService struct { | ||
| resp *models.EnrichmentStatusResponse | ||
| err error | ||
| } | ||
|
|
||
| func (f *fakeEnrichmentStatusService) GetEnrichmentStatus( | ||
| _ context.Context, _ string, | ||
| ) (*models.EnrichmentStatusResponse, error) { | ||
| return f.resp, f.err | ||
| } | ||
|
|
||
| func TestEnrichmentStatusHandler_GetStatus(t *testing.T) { | ||
| t.Run("service unavailable when not configured", func(t *testing.T) { | ||
| h := NewEnrichmentStatusHandler(nil) | ||
| rec := httptest.NewRecorder() | ||
| h.GetStatus(rec, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/v1/enrichment-status?tenant_id=t1", nil)) | ||
|
|
||
| assert.Equal(t, http.StatusServiceUnavailable, rec.Code) | ||
| }) | ||
|
|
||
| t.Run("missing tenant_id maps to 400", func(t *testing.T) { | ||
| // The service returns a huberrors.ValidationError for a blank tenant_id; the handler must | ||
| // surface it as a 400 via response.RespondError, not a 500. | ||
| h := NewEnrichmentStatusHandler(&fakeEnrichmentStatusService{ | ||
| err: huberrors.NewValidationError("tenant_id", "tenant_id is required and cannot be empty"), | ||
| }) | ||
| rec := httptest.NewRecorder() | ||
| h.GetStatus(rec, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/v1/enrichment-status", nil)) | ||
|
|
||
| assert.Equal(t, http.StatusBadRequest, rec.Code) | ||
| }) | ||
|
|
||
| t.Run("returns 200 with the status body", func(t *testing.T) { | ||
| want := &models.EnrichmentStatusResponse{ | ||
| TenantID: "t1", | ||
| Translation: models.EnrichmentTypeStatus{Enabled: true, Eligible: 10, Done: 4}, | ||
| Sentiment: models.EnrichmentTypeStatus{Enabled: true, Eligible: 8, Done: 8}, | ||
| Emotions: models.EnrichmentTypeStatus{Enabled: false}, | ||
| } | ||
| h := NewEnrichmentStatusHandler(&fakeEnrichmentStatusService{resp: want}) | ||
| rec := httptest.NewRecorder() | ||
| h.GetStatus(rec, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/v1/enrichment-status?tenant_id=t1", nil)) | ||
|
|
||
| require.Equal(t, http.StatusOK, rec.Code) | ||
|
|
||
| var got models.EnrichmentStatusResponse | ||
| require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got)) | ||
| assert.Equal(t, *want, got) | ||
| }) | ||
| } |
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,23 @@ | ||
| package models | ||
|
|
||
| // EnrichmentTypeStatus is one tenant's progress for a single record-level enrichment | ||
| // (translation, sentiment, or emotions). Enabled reports whether the enrichment is both | ||
| // deployment-configured and switched on for the tenant (for translation: has a resolvable | ||
| // target language). Eligible is the number of feedback records that qualify for the | ||
| // enrichment; Done is how many have been enriched. The UI derives "in progress" as | ||
| // Eligible - Done. When Enabled is false, Eligible and Done are zero (no work will run). | ||
| type EnrichmentTypeStatus struct { | ||
| Enabled bool `json:"enabled"` | ||
| Eligible int64 `json:"eligible"` | ||
| Done int64 `json:"done"` | ||
| } | ||
|
|
||
| // EnrichmentStatusResponse reports a tenant's enrichment progress across the record-level | ||
| // enrichments. Counts are directory-level totals; the response never includes record | ||
| // identifiers or feedback content. | ||
| type EnrichmentStatusResponse struct { | ||
| TenantID string `json:"tenant_id"` | ||
| Translation EnrichmentTypeStatus `json:"translation"` | ||
| Sentiment EnrichmentTypeStatus `json:"sentiment"` | ||
| Emotions EnrichmentTypeStatus `json:"emotions"` | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.