Skip to content
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

feat: rework application overview db query #8518

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 31 additions & 34 deletions src/lib/db/client-applications-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,29 +313,40 @@ export default class ClientApplicationsStore
),
])
.from('client_metrics_env as cme')
.where('cme.app_name', appName)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra where clause to reduce query scope

.leftJoin('features as f', 'f.name', 'cme.feature_name')
.groupBy('cme.app_name', 'cme.environment', 'f.project');
})
.with('instances', (qb) => {
qb.select([
'ci.app_name',
'ci.environment',
this.db.raw(
Copy link
Contributor Author

@sjaanus sjaanus Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we count in database level, instead of joining and returning all.

'COUNT(DISTINCT ci.instance_id) as unique_instance_count',
),
this.db.raw(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also count these in database.

'ARRAY_AGG(DISTINCT ci.sdk_version) as sdk_versions',
),
this.db.raw('MAX(ci.last_seen) as latest_last_seen'),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also last seen, max will be taken, instead of doing it in memory.

])
.from('client_instances as ci')
.where('ci.app_name', appName)
.groupBy('ci.app_name', 'ci.environment');
})
.select([
'm.project',
'm.environment',
'm.features',
'ci.instance_id',
'ci.sdk_version',
'ci.last_seen',
'a.strategies',
'i.unique_instance_count',
'i.sdk_versions',
'i.latest_last_seen',
'ca.strategies',
])
.from({ a: 'client_applications' })
.leftJoin('metrics as m', 'm.app_name', 'a.app_name')
.leftJoin('client_instances as ci', function () {
this.on('ci.app_name', '=', 'm.app_name').andOn(
'ci.environment',
'=',
'm.environment',
);
})
.where('a.app_name', appName)
.from('client_applications as ca')
.leftJoin('metrics as m', 'm.app_name', 'ca.app_name')
.leftJoin('instances as i', 'i.environment', 'm.environment')
.orderBy('m.environment', 'asc');
console.log(query.toQuery());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log(query.toQuery());

const rows = await query;
stopTimer();
if (!rows.length) {
Expand All @@ -358,9 +369,9 @@ export default class ClientApplicationsStore
const environments = rows.reduce((acc, row) => {
const {
environment,
instance_id,
sdk_version,
last_seen,
unique_instance_count,
sdk_versions,
latest_last_seen,
project,
features,
strategies,
Expand All @@ -383,12 +394,9 @@ export default class ClientApplicationsStore
if (!env) {
env = {
name: environment,
instanceCount: instance_id ? 1 : 0,
sdks: sdk_version ? [sdk_version] : [],
lastSeen: last_seen,
uniqueInstanceIds: new Set(
instance_id ? [instance_id] : [],
),
instanceCount: Number(unique_instance_count),
sdks: sdk_versions,
lastSeen: latest_last_seen,
issues: {
missingFeatures: featuresNotMappedToProject
? features
Expand All @@ -397,25 +405,14 @@ export default class ClientApplicationsStore
};
acc.push(env);
} else {
if (instance_id) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got rid of all of this logic done in memory

env.uniqueInstanceIds.add(instance_id);
env.instanceCount = env.uniqueInstanceIds.size;
}
if (featuresNotMappedToProject) {
env.issues.missingFeatures = features;
}
if (sdk_version && !env.sdks.includes(sdk_version)) {
env.sdks.push(sdk_version);
}
if (new Date(last_seen) > new Date(env.lastSeen)) {
env.lastSeen = last_seen;
}
}

return acc;
}, []);
environments.forEach((env) => {
delete env.uniqueInstanceIds;
env.sdks.sort();
});

Expand Down
Loading