Skip to content

Commit

Permalink
feat: application missing strategies (#6334)
Browse files Browse the repository at this point in the history
Now also showing missing strategies, that SDK sends, but do not exist in
Unleash.
  • Loading branch information
sjaanus committed Feb 26, 2024
1 parent 89d113f commit 3b7b816
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
51 changes: 40 additions & 11 deletions src/lib/db/client-applications-store.ts
Expand Up @@ -27,6 +27,12 @@ const TABLE = 'client_applications';

const TABLE_USAGE = 'client_applications_usage';

const DEPRECATED_STRATEGIES = [
'gradualRolloutRandom',
'gradualRolloutSessionId',
'gradualRolloutUserId',
];

const mapRow: (any) => IClientApplication = (row) => ({
appName: row.app_name,
createdAt: row.created_at,
Expand Down Expand Up @@ -294,6 +300,7 @@ export default class ClientApplicationsStore
'ci.instance_id',
'ci.sdk_version',
'ci.last_seen',
'a.strategies',
])
.from({ a: 'client_applications' })
.leftJoin('client_metrics_env as cme', 'cme.app_name', 'a.app_name')
Expand All @@ -311,12 +318,20 @@ export default class ClientApplicationsStore
throw new NotFoundError(`Could not find appName=${appName}`);
}

return this.mapApplicationOverviewData(rows);
const existingStrategies: string[] = await this.db
.select('name')
.from('strategies')
.pluck('name');
return this.mapApplicationOverviewData(rows, existingStrategies);
}

mapApplicationOverviewData(rows: any[]): IApplicationOverview {
mapApplicationOverviewData(
rows: any[],
existingStrategies: string[],
): IApplicationOverview {
const featureCount = new Set(rows.map((row) => row.feature_name)).size;
const missingFeatures: Set<string> = new Set();
const missingStrategies: Set<string> = new Set();

const environments = rows.reduce((acc, row) => {
const {
Expand All @@ -326,6 +341,7 @@ export default class ClientApplicationsStore
last_seen,
project,
feature_name,
strategies,
} = row;

if (!environment) return acc;
Expand All @@ -334,6 +350,15 @@ export default class ClientApplicationsStore
missingFeatures.add(feature_name);
}

strategies.forEach((strategy) => {
if (
!DEPRECATED_STRATEGIES.includes(strategy) &&
!existingStrategies.includes(strategy)
) {
missingStrategies.add(strategy);
}
});

let env = acc.find((e) => e.name === environment);
if (!env) {
env = {
Expand Down Expand Up @@ -366,15 +391,19 @@ export default class ClientApplicationsStore
env.sdks.sort();
});

const issues: ApplicationOverviewIssuesSchema[] =
missingFeatures.size > 0
? [
{
type: 'missingFeatures',
items: [...missingFeatures],
},
]
: [];
const issues: ApplicationOverviewIssuesSchema[] = [];
if (missingFeatures.size > 0) {
issues.push({
type: 'missingFeatures',
items: [...missingFeatures],
});
}
if (missingStrategies.size > 0) {
issues.push({
type: 'missingStrategies',
items: [...missingStrategies],
});
}

return {
projects: [
Expand Down
8 changes: 6 additions & 2 deletions src/test/e2e/api/admin/applications.e2e.test.ts
Expand Up @@ -136,13 +136,13 @@ test('should show correct number of total', async () => {
expect(body).toMatchObject(expected);
});

test('should show missing features', async () => {
test('should show missing features and strategies', async () => {
await Promise.all([
app.createFeature('toggle-name-1'),
app.request.post('/api/client/register').send({
appName: metrics.appName,
instanceId: metrics.instanceId,
strategies: ['default'],
strategies: ['my-special-strategy'],
sdkVersion: 'unleash-client-test',
started: Date.now(),
interval: 10,
Expand All @@ -168,6 +168,10 @@ test('should show missing features', async () => {
type: 'missingFeatures',
items: ['toggle-name-2', 'toggle-name-3'],
},
{
type: 'missingStrategies',
items: ['my-special-strategy'],
},
],
environments: [
{
Expand Down

0 comments on commit 3b7b816

Please sign in to comment.