Repository navigation
feat(insights): adoption tab with active members by surface, member table, breakdowns and CSV export #10540
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
+1,473
−12
Merged
feat(insights): adoption tab with active members by surface, member table, breakdowns and CSV export #10540
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
275c10e
feat(insights): adoption tab with active members by surface, member t…
waleedlatif1 56c2078
improvement(insights): review pass on the adoption tab
waleedlatif1 988a9b0
chore(insights): keep adoption schemas module-private
waleedlatif1 4808b9a
fix(insights): adoption review — current members only in MCP breakdow…
waleedlatif1 c3554d3
fix(insights): rate-limit the adoption export, read future windows as…
waleedlatif1 32bbc80
fix(insights): keep a future window's day range empty
waleedlatif1 4652df8
fix(insights): headlines end today for future windows, flag truncated…
waleedlatif1 01f40eb
fix(insights): fades follow the table scroller, period-scoped last ac…
waleedlatif1 975773e
fix(insights): classify export outages like the JSON routes, state th…
waleedlatif1 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
28 changes: 28 additions & 0 deletions
28
apps/sim/app/api/organizations/[id]/usage/adoption/breakdown/route.ts
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,28 @@ | ||
| import { getOrganizationAdoptionBreakdownContract } from '@/lib/api/contracts/organization-adoption' | ||
| import { | ||
| defineInternalJsonRoute, | ||
| internalRateLimits, | ||
| internalSessionAuth, | ||
| } from '@/lib/api/server/routes' | ||
| import { getOrganizationAdoptionBreakdown } from '@/lib/billing/application/organization-usage/get-organization-adoption' | ||
| import { organizationUsageOperations } from '@/lib/billing/application/organization-usage/operations' | ||
| import { organizationUsageErrorPolicy } from '@/app/api/organizations/[id]/usage/error-policy' | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
|
|
||
| export const GET = defineInternalJsonRoute({ | ||
| contract: getOrganizationAdoptionBreakdownContract, | ||
| auth: internalSessionAuth, | ||
| operation: organizationUsageOperations.readAdoption, | ||
| rateLimit: internalRateLimits.user({ bucketName: 'organization-adoption' }), | ||
| errorPolicy: organizationUsageErrorPolicy, | ||
| staticResponseHeaders: { 'Cache-Control': 'private, no-store' }, | ||
| mapInput: ({ params, query }) => ({ | ||
| ...query, | ||
| organizationId: params.id, | ||
| startDate: query.startDate ? new Date(query.startDate) : undefined, | ||
| endDate: query.endDate ? new Date(query.endDate) : undefined, | ||
| }), | ||
| useCase: getOrganizationAdoptionBreakdown, | ||
| present: (result) => result, | ||
| }) |
95 changes: 95 additions & 0 deletions
95
apps/sim/app/api/organizations/[id]/usage/adoption/export/route.ts
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,95 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { getErrorMessage } from '@sim/utils/errors' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { | ||
| ADOPTION_SURFACE_LABELS, | ||
| ADOPTION_SURFACES, | ||
| exportOrganizationAdoptionContract, | ||
| type OrganizationAdoptionMember, | ||
| } from '@/lib/api/contracts/organization-adoption' | ||
| import { getValidationErrorMessage, parseRequest } from '@/lib/api/server' | ||
| import { projectInternalError } from '@/lib/api/server/routes' | ||
| import { getSession } from '@/lib/auth' | ||
| import { exportOrganizationAdoption } from '@/lib/billing/application/organization-usage/get-organization-adoption' | ||
| import { enforceUserRateLimit } from '@/lib/core/rate-limiter/route-helpers' | ||
| import { formatCsvValue, toCsvRow } from '@/lib/core/utils/csv' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
| import { organizationUsageErrorPolicy } from '@/app/api/organizations/[id]/usage/error-policy' | ||
|
|
||
| const logger = createLogger('OrganizationAdoptionExportAPI') | ||
|
|
||
| const CSV_HEADER = toCsvRow([ | ||
| 'Name', | ||
| 'Email', | ||
| 'Department', | ||
| 'Days active', | ||
| ...ADOPTION_SURFACES.map((surface) => ADOPTION_SURFACE_LABELS[surface]), | ||
| 'Last active in period (UTC)', | ||
| ]) | ||
|
|
||
| /** `formatCsvValue` neutralizes formula injection: names and departments are user-controlled. */ | ||
| function toCsvLine(row: OrganizationAdoptionMember): string { | ||
| return toCsvRow([ | ||
| formatCsvValue(row.name), | ||
| formatCsvValue(row.email), | ||
| formatCsvValue(row.department ?? ''), | ||
| formatCsvValue(String(row.daysActive)), | ||
| ...ADOPTION_SURFACES.map((surface) => formatCsvValue(String(row.uses[surface]))), | ||
| formatCsvValue(row.lastActiveAt ?? ''), | ||
| ]) | ||
| } | ||
|
|
||
| /** | ||
| * One CSV row per member over the window. A raw handler rather than a JSON builder: the body is | ||
| * `text/csv`, and `X-Export-Truncated` tells the client when the member list was capped. | ||
| */ | ||
| export const GET = withRouteHandler(async (request: NextRequest, context) => { | ||
| try { | ||
| const session = await getSession() | ||
| const sessionId = session?.session?.id | ||
| if (!session?.user?.id || !sessionId) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
| const limited = await enforceUserRateLimit('organization-adoption-export', session.user.id) | ||
| if (limited) return limited | ||
|
|
||
| const parsed = await parseRequest(exportOrganizationAdoptionContract, request, context, { | ||
| validationErrorResponse: (error) => | ||
| NextResponse.json( | ||
| { error: getValidationErrorMessage(error, 'Invalid query parameters') }, | ||
| { status: 400 } | ||
| ), | ||
| }) | ||
| if (!parsed.success) return parsed.response | ||
|
|
||
| const { query, params } = parsed.data | ||
| const result = await exportOrganizationAdoption.execute({ | ||
| principal: { kind: 'session', userId: session.user.id, sessionId }, | ||
| input: { | ||
| organizationId: params.id, | ||
| preset: query.preset, | ||
| startDate: query.startDate ? new Date(query.startDate) : undefined, | ||
| endDate: query.endDate ? new Date(query.endDate) : undefined, | ||
| timezone: query.timezone, | ||
| }, | ||
| }) | ||
|
|
||
| const csv = [CSV_HEADER, ...result.rows.map(toCsvLine)].join('\n') | ||
| return new NextResponse(csv, { | ||
| headers: { | ||
| 'Content-Type': 'text/csv; charset=utf-8', | ||
| // Every member's activity for one organization, behind session auth; never cached. | ||
| 'Cache-Control': 'no-store', | ||
| 'Content-Disposition': `attachment; filename="organization-adoption-${params.id}.csv"`, | ||
| ...(result.truncated ? { 'X-Export-Truncated': '1' } : {}), | ||
| }, | ||
| }) | ||
| } catch (error) { | ||
| const refusal = projectInternalError(organizationUsageErrorPolicy, error) | ||
| if (refusal) { | ||
| return NextResponse.json(refusal.body, { status: refusal.status, headers: refusal.headers }) | ||
| } | ||
| logger.error('Failed to export organization adoption', { error: getErrorMessage(error) }) | ||
| return NextResponse.json({ error: 'Failed to export adoption' }, { status: 500 }) | ||
| } | ||
| }) | ||
28 changes: 28 additions & 0 deletions
28
apps/sim/app/api/organizations/[id]/usage/adoption/members/route.ts
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,28 @@ | ||
| import { listOrganizationAdoptionMembersContract } from '@/lib/api/contracts/organization-adoption' | ||
| import { | ||
| defineInternalJsonRoute, | ||
| internalRateLimits, | ||
| internalSessionAuth, | ||
| } from '@/lib/api/server/routes' | ||
| import { listOrganizationAdoptionMembers } from '@/lib/billing/application/organization-usage/get-organization-adoption' | ||
| import { organizationUsageOperations } from '@/lib/billing/application/organization-usage/operations' | ||
| import { organizationUsageErrorPolicy } from '@/app/api/organizations/[id]/usage/error-policy' | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
|
|
||
| export const GET = defineInternalJsonRoute({ | ||
| contract: listOrganizationAdoptionMembersContract, | ||
| auth: internalSessionAuth, | ||
| operation: organizationUsageOperations.readAdoption, | ||
| rateLimit: internalRateLimits.user({ bucketName: 'organization-adoption' }), | ||
| errorPolicy: organizationUsageErrorPolicy, | ||
| staticResponseHeaders: { 'Cache-Control': 'private, no-store' }, | ||
| mapInput: ({ params, query }) => ({ | ||
| ...query, | ||
| organizationId: params.id, | ||
| startDate: query.startDate ? new Date(query.startDate) : undefined, | ||
| endDate: query.endDate ? new Date(query.endDate) : undefined, | ||
| }), | ||
| useCase: listOrganizationAdoptionMembers, | ||
| present: (result) => result, | ||
| }) |
28 changes: 28 additions & 0 deletions
28
apps/sim/app/api/organizations/[id]/usage/adoption/summary/route.ts
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,28 @@ | ||
| import { getOrganizationAdoptionSummaryContract } from '@/lib/api/contracts/organization-adoption' | ||
| import { | ||
| defineInternalJsonRoute, | ||
| internalRateLimits, | ||
| internalSessionAuth, | ||
| } from '@/lib/api/server/routes' | ||
| import { getOrganizationAdoptionSummary } from '@/lib/billing/application/organization-usage/get-organization-adoption' | ||
| import { organizationUsageOperations } from '@/lib/billing/application/organization-usage/operations' | ||
| import { organizationUsageErrorPolicy } from '@/app/api/organizations/[id]/usage/error-policy' | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
|
|
||
| export const GET = defineInternalJsonRoute({ | ||
| contract: getOrganizationAdoptionSummaryContract, | ||
| auth: internalSessionAuth, | ||
| operation: organizationUsageOperations.readAdoption, | ||
| rateLimit: internalRateLimits.user({ bucketName: 'organization-adoption' }), | ||
| errorPolicy: organizationUsageErrorPolicy, | ||
| staticResponseHeaders: { 'Cache-Control': 'private, no-store' }, | ||
| mapInput: ({ params, query }) => ({ | ||
| ...query, | ||
| organizationId: params.id, | ||
| startDate: query.startDate ? new Date(query.startDate) : undefined, | ||
| endDate: query.endDate ? new Date(query.endDate) : undefined, | ||
| }), | ||
| useCase: getOrganizationAdoptionSummary, | ||
| present: (result) => 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
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
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.