forked from NangoHQ/nango
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: in product metrics (NangoHQ#2541)
## Describe your changes Fixes https://linear.app/nango/issue/NAN-1466/empty-state-when-elasticsearch-is-not-present Fixes https://linear.app/nango/issue/NAN-1467/empty-state-when-there-is-no-data Fixes https://linear.app/nango/issue/NAN-1465/endpointquery-to-get-metrics Fixes https://linear.app/nango/issue/NAN-1464/code-the-ui - New endpoint `POST /api/v1/logs/insights` Retrieve insights by operations type, depending on the performance I might add some Redis cache. - Dashboard homepage The UI now displays the homepage by default, if you have finished your interactive demo. ## Test > Currently deployed in staging <img width="1512" alt="Screenshot 2024-07-25 at 10 25 11" src="https://github.com/user-attachments/assets/44cde003-dc9c-4f3b-957d-199f5d877587">
- Loading branch information
1 parent
31bdffd
commit 6a18514
Showing
20 changed files
with
1,202 additions
and
75 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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,50 @@ | ||
import type { estypes } from '@elastic/elasticsearch'; | ||
import { indexMessages } from '../es/schema.js'; | ||
import { client } from '../es/client.js'; | ||
import type { InsightsHistogramEntry } from '@nangohq/types'; | ||
|
||
export async function retrieveInsights(opts: { accountId: number; environmentId: number; type: string }) { | ||
const query: estypes.QueryDslQueryContainer = { | ||
bool: { | ||
must: [{ term: { accountId: opts.accountId } }, { term: { environmentId: opts.environmentId } }, { term: { 'operation.type': opts.type } }], | ||
must_not: { exists: { field: 'parentId' } }, | ||
should: [] | ||
} | ||
}; | ||
|
||
const res = await client.search< | ||
never, | ||
{ | ||
histogram: estypes.AggregationsDateHistogramAggregate; | ||
} | ||
>({ | ||
index: indexMessages.index, | ||
size: 0, | ||
sort: [{ createdAt: 'desc' }, 'id'], | ||
track_total_hits: true, | ||
aggs: { | ||
histogram: { | ||
date_histogram: { field: 'createdAt', calendar_interval: '1d', format: 'yyyy-MM-dd' }, | ||
aggs: { | ||
state_agg: { | ||
terms: { field: 'state' } | ||
} | ||
} | ||
} | ||
}, | ||
query | ||
}); | ||
|
||
const agg = res.aggregations!['histogram']; | ||
|
||
const computed: InsightsHistogramEntry[] = []; | ||
for (const item of agg.buckets as any[]) { | ||
const success = (item.state_agg.buckets as { key: string; doc_count: number }[]).find((i) => i.key === 'success'); | ||
const failure = (item.state_agg.buckets as { key: string; doc_count: number }[]).find((i) => i.key === 'failed'); | ||
computed.push({ key: item.key_as_string, total: item.doc_count, success: success?.doc_count || 0, failure: failure?.doc_count || 0 }); | ||
} | ||
|
||
return { | ||
items: computed | ||
}; | ||
} |
88 changes: 88 additions & 0 deletions
88
packages/server/lib/controllers/v1/logs/postInsights.integration.test.ts
This file contains 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,88 @@ | ||
import { migrateLogsMapping } from '@nangohq/logs'; | ||
import { multipleMigrations } from '@nangohq/database'; | ||
import { seeders } from '@nangohq/shared'; | ||
import { afterAll, beforeAll, describe, expect, it } from 'vitest'; | ||
import { isSuccess, runServer, shouldBeProtected, shouldRequireQueryEnv } from '../../../utils/tests.js'; | ||
|
||
let api: Awaited<ReturnType<typeof runServer>>; | ||
describe('POST /logs/insights', () => { | ||
beforeAll(async () => { | ||
await multipleMigrations(); | ||
await migrateLogsMapping(); | ||
|
||
api = await runServer(); | ||
}); | ||
afterAll(() => { | ||
api.server.close(); | ||
}); | ||
|
||
it('should be protected', async () => { | ||
const res = await api.fetch('/api/v1/logs/insights', { method: 'POST', query: { env: 'dev' }, body: { type: 'action' } }); | ||
|
||
shouldBeProtected(res); | ||
}); | ||
|
||
it('should enforce env query params', async () => { | ||
const { env } = await seeders.seedAccountEnvAndUser(); | ||
const res = await api.fetch( | ||
'/api/v1/logs/insights', | ||
// @ts-expect-error missing query on purpose | ||
{ | ||
method: 'POST', | ||
token: env.secret_key, | ||
body: { type: 'action' } | ||
} | ||
); | ||
|
||
shouldRequireQueryEnv(res); | ||
}); | ||
|
||
it('should validate body', async () => { | ||
const { env } = await seeders.seedAccountEnvAndUser(); | ||
const res = await api.fetch('/api/v1/logs/insights', { | ||
method: 'POST', | ||
query: { | ||
env: 'dev', | ||
// @ts-expect-error on purpose | ||
foo: 'bar' | ||
}, | ||
token: env.secret_key, | ||
body: { | ||
// @ts-expect-error on purpose | ||
type: 'foobar' | ||
} | ||
}); | ||
|
||
expect(res.json).toStrictEqual<typeof res.json>({ | ||
error: { | ||
code: 'invalid_query_params', | ||
errors: [ | ||
{ | ||
code: 'unrecognized_keys', | ||
message: "Unrecognized key(s) in object: 'foo'", | ||
path: [] | ||
} | ||
] | ||
} | ||
}); | ||
expect(res.res.status).toBe(400); | ||
}); | ||
|
||
it('should get empty result', async () => { | ||
const { env } = await seeders.seedAccountEnvAndUser(); | ||
const res = await api.fetch('/api/v1/logs/insights', { | ||
method: 'POST', | ||
query: { env: 'dev' }, | ||
token: env.secret_key, | ||
body: { type: 'sync' } | ||
}); | ||
|
||
isSuccess(res.json); | ||
expect(res.res.status).toBe(200); | ||
expect(res.json).toStrictEqual<typeof res.json>({ | ||
data: { | ||
histogram: [] | ||
} | ||
}); | ||
}); | ||
}); |
This file contains 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,46 @@ | ||
import { z } from 'zod'; | ||
import { asyncWrapper } from '../../../utils/asyncWrapper.js'; | ||
import { requireEmptyQuery, zodErrorToHTTP } from '@nangohq/utils'; | ||
import type { PostInsights } from '@nangohq/types'; | ||
import { envs, modelOperations } from '@nangohq/logs'; | ||
|
||
const validation = z | ||
.object({ | ||
type: z.enum(['sync', 'action', 'proxy', 'webhook_external']) | ||
}) | ||
.strict(); | ||
|
||
export const postInsights = asyncWrapper<PostInsights>(async (req, res) => { | ||
if (!envs.NANGO_LOGS_ENABLED) { | ||
res.status(404).send({ error: { code: 'feature_disabled' } }); | ||
return; | ||
} | ||
|
||
const emptyQuery = requireEmptyQuery(req, { withEnv: true }); | ||
if (emptyQuery) { | ||
res.status(400).send({ error: { code: 'invalid_query_params', errors: zodErrorToHTTP(emptyQuery.error) } }); | ||
return; | ||
} | ||
|
||
const val = validation.safeParse(req.body); | ||
if (!val.success) { | ||
res.status(400).send({ | ||
error: { code: 'invalid_body', errors: zodErrorToHTTP(val.error) } | ||
}); | ||
return; | ||
} | ||
|
||
const env = res.locals['environment']; | ||
const body: PostInsights['Body'] = val.data; | ||
const insights = await modelOperations.retrieveInsights({ | ||
accountId: env.account_id, | ||
environmentId: env.id, | ||
type: body.type | ||
}); | ||
|
||
res.status(200).send({ | ||
data: { | ||
histogram: insights.items | ||
} | ||
}); | ||
}); |
This file contains 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 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 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 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.