Skip to content

Commit

Permalink
optimized queries in business layer
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Dec 14, 2024
1 parent 852dfb7 commit 1794c20
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 147 deletions.
135 changes: 58 additions & 77 deletions apps/api/src/secret/service/secret.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,8 @@ export class SecretService {
},
versions: {
select: {
value: true,
version: true,
environment: {
select: {
name: true,
Expand All @@ -687,93 +689,72 @@ export class SecretService {
}
})

const secretsWithEnvironmentalValues = new Map<
Secret['id'],
{
secret: Secret
values: {
environment: {
name: Environment['name']
id: Environment['id']
}
value: SecretVersion['value']
version: SecretVersion['version']
}[]
}
>()

// Find all the environments for this project
const environments = await this.prisma.environment.findMany({
where: {
projectId
}
})
const environmentIds = new Map(
environments.map((env) => [env.id, env.name])
)
const secretsWithEnvironmentalValues = new Set<{
secret: Partial<Secret>
values: {
environment: {
name: Environment['name']
id: Environment['id']
slug: Environment['slug']
}
value: SecretVersion['value']
version: SecretVersion['version']
}[]
}>()

for (const secret of secrets) {
// Make a copy of the environment IDs
const envIds = new Map(environmentIds)
let iterations = envIds.size

// Find the latest version for each environment
while (iterations--) {
const latestVersion = await this.prisma.secretVersion.findFirst({
where: {
secretId: secret.id,
environmentId: {
in: Array.from(envIds.keys())
}
},
orderBy: {
version: 'desc'
},
include: {
environment: {
select: {
id: true,
slug: true
}
}
// Logic to update the map:
// 1. If the environment ID is not present in the key, insert the environment ID and the secret version
// 2. If the environment ID is already present, check if the existing secret version is lesser than the new secret version.
// If it is, update the secret version
const envIdToSecretVersionMap = new Map<
Environment['id'],
Partial<SecretVersion> & {
environment: {
id: Environment['id']
slug: Environment['slug']
name: Environment['name']
}
})
}
>()

if (!latestVersion) continue
for (const secretVersion of secret.versions) {
const environmentId = secretVersion.environment.id
const existingSecretVersion = envIdToSecretVersionMap.get(environmentId)

if (secretsWithEnvironmentalValues.has(secret.id)) {
secretsWithEnvironmentalValues.get(secret.id).values.push({
environment: {
id: latestVersion.environmentId,
name: envIds.get(latestVersion.environmentId)
},
value: decryptValue
? await decrypt(project.privateKey, latestVersion.value)
: latestVersion.value,
version: latestVersion.version
})
if (!existingSecretVersion) {
envIdToSecretVersionMap.set(environmentId, secretVersion)
} else {
secretsWithEnvironmentalValues.set(secret.id, {
secret,
values: [
{
environment: {
id: latestVersion.environmentId,
name: envIds.get(latestVersion.environmentId)
},
value: decryptValue
? await decrypt(project.privateKey, latestVersion.value)
: latestVersion.value,
version: latestVersion.version
}
]
})
if (existingSecretVersion.version < secretVersion.version) {
envIdToSecretVersionMap.set(environmentId, secretVersion)
}
}

envIds.delete(latestVersion.environmentId)
}

delete secret.versions

// Add the secret to the map
secretsWithEnvironmentalValues.add({
secret,
values: await Promise.all(
Array.from(envIdToSecretVersionMap.values()).map(
async (secretVersion) => ({
environment: {
id: secretVersion.environment.id,
name: secretVersion.environment.name,
slug: secretVersion.environment.slug
},
value: decryptValue
? await decrypt(project.privateKey, secretVersion.value)
: secretVersion.value,
version: secretVersion.version
})
)
)
})
}

// console.log(secretsWithEnvironmentalValues)
const items = Array.from(secretsWithEnvironmentalValues.values())

// Calculate pagination metadata
Expand Down
126 changes: 56 additions & 70 deletions apps/api/src/variable/service/variable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,11 @@ export class VariableService {
},
versions: {
select: {
value: true,
version: true,
environment: {
select: {
name: true,
id: true,
slug: true
}
Expand All @@ -606,85 +609,68 @@ export class VariableService {
}
})

const variablesWithEnvironmentalValues = new Map<
Variable['id'],
{
variable: Variable
values: {
const variablesWithEnvironmentalValues = new Set<{
variable: Partial<Variable>
values: {
environment: {
name: Environment['name']
id: Environment['id']
slug: Environment['slug']
}
value: VariableVersion['value']
version: VariableVersion['version']
}[]
}>()

for (const variable of variables) {
// Logic to update the map:
// 1. If the environment ID is not present in the key, insert the environment ID and the variable version
// 2. If the environment ID is already present, check if the existing variable version is lesser than the new variable version.
// If it is, update the variable version
const envIdToVariableVersionMap = new Map<
Environment['id'],
Partial<VariableVersion> & {
environment: {
name: Environment['name']
id: Environment['id']
slug: Environment['slug']
name: Environment['name']
}
value: VariableVersion['value']
version: VariableVersion['version']
}[]
}
>()

// Find all the environments for this project
const environments = await this.prisma.environment.findMany({
where: {
projectId
}
})
const environmentIds = new Map(
environments.map((env) => [env.id, env.name])
)

for (const variable of variables) {
// Make a copy of the environment IDs
const envIds = new Map(environmentIds)
let iterations = envIds.size

// Find the latest version for each environment
while (iterations--) {
const latestVersion = await this.prisma.variableVersion.findFirst({
where: {
variableId: variable.id,
environmentId: {
in: Array.from(envIds.keys())
}
},
orderBy: {
version: 'desc'
},
include: {
environment: true
}
})
}
>()

if (!latestVersion) continue
for (const variableVersion of variable.versions) {
const environmentId = variableVersion.environment.id
const existingVariableVersion =
envIdToVariableVersionMap.get(environmentId)

if (variablesWithEnvironmentalValues.has(variable.id)) {
variablesWithEnvironmentalValues.get(variable.id).values.push({
environment: {
id: latestVersion.environmentId,
name: envIds.get(latestVersion.environmentId),
slug: latestVersion.environment.slug
},
value: latestVersion.value,
version: latestVersion.version
})
if (!existingVariableVersion) {
envIdToVariableVersionMap.set(environmentId, variableVersion)
} else {
variablesWithEnvironmentalValues.set(variable.id, {
variable,
values: [
{
environment: {
id: latestVersion.environmentId,
name: envIds.get(latestVersion.environmentId),
slug: latestVersion.environment.slug
},
value: latestVersion.value,
version: latestVersion.version
}
]
})
if (existingVariableVersion.version < variableVersion.version) {
envIdToVariableVersionMap.set(environmentId, variableVersion)
}
}

envIds.delete(latestVersion.environmentId)
}

delete variable.versions

// Add the variable to the map
variablesWithEnvironmentalValues.add({
variable,
values: await Promise.all(
Array.from(envIdToVariableVersionMap.values()).map(
async (variableVersion) => ({
environment: {
id: variableVersion.environment.id,
name: variableVersion.environment.name,
slug: variableVersion.environment.slug
},
value: variableVersion.value,
version: variableVersion.version
})
)
)
})
}

const items = Array.from(variablesWithEnvironmentalValues.values())
Expand Down

0 comments on commit 1794c20

Please sign in to comment.