Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Butler fix log endpoint #1125

Merged
merged 2 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ export class DeploymentsController {
@UsePipes(new ValidationPipe({ transform: true }))
public async findDeploymentEvents(
@Param('id') deploymentId: string,
@Headers('x-workspace-id') workspaceId: string,
): Promise<ReadLogsDto> {
return this.findDeploymentLogsByIdUseCase.execute(deploymentId, workspaceId)
return this.findDeploymentLogsByIdUseCase.execute(deploymentId)
}

@Post('/:id/undeploy')
Expand Down
18 changes: 10 additions & 8 deletions butler/src/app/v2/api/deployments/repository/log.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ import { LogEntity } from '../entity/logs.entity'
@EntityRepository(LogEntity)
export class LogRepository extends Repository<LogEntity> {

public async findDeploymentLogs(deploymentId: string, workspaceId?: string): Promise<LogEntity | undefined> {
const queryBuilder = this.createQueryBuilder('v2logs')
.leftJoinAndSelect('v2logs.deployment', 'deployment')
.andWhere('deployment.id = :deploymentId', { deploymentId })
public async findDeploymentLogs(deploymentId: string): Promise<LogEntity | undefined> {
const logEntries = await this.query(`
SELECT l.deployment_id, json_agg((select * from jsonb_array_elements(l.logs))) as logs
FROM v2logs l
WHERE l.deployment_id = $1
GROUP BY deployment_id`, [deploymentId])

return workspaceId ?
queryBuilder.leftJoinAndSelect('deployment.cdConfiguration', 'c')
.andWhere('c.workspaceId = :workspaceId', { workspaceId }).getOne() :
queryBuilder.getOne()
return this.toLogEntity(deploymentId, logEntries)
}

private toLogEntity(deploymentId: string, logEntries: LogEntity[]) {
return new LogEntity(deploymentId, logEntries ? logEntries.flatMap(e => e.logs) : [])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class FindDeploymentLogsByIdUsecase {
private readonly logsRepository: LogRepository
){}

public async execute(deploymentId: string, workspaceId: string): Promise<ReadLogsDto> {
const deploymentLogs = await this.logsRepository.findDeploymentLogs(deploymentId, workspaceId)
public async execute(deploymentId: string): Promise<ReadLogsDto> {
const deploymentLogs = await this.logsRepository.findDeploymentLogs(deploymentId)
if (!deploymentLogs) {
throw new NotFoundException(`No logs found associated with the deployment: ${deploymentId}`)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,6 @@ export class ReceiveNotificationUseCase {
}

private async getDeploymentLogs(deploymentNotificationDto: DeploymentNotificationRequestDto, deployment: DeploymentEntityV2) {
const deploymentLogs = await this.logRepository.findDeploymentLogs(deployment.id)
if (deploymentLogs) {
deploymentLogs.concatLogs(deploymentNotificationDto.logs)
return deploymentLogs
}
return new LogEntity(deployment.id, deploymentNotificationDto.logs)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { UrlConstants } from '../test-constants'
import { TestSetupUtils } from '../test-setup-utils'
import { EntityManager } from 'typeorm'
import { ComponentEntityV2 } from '../../../../app/v2/api/deployments/entity/component.entity'
import { LogEntity } from '../../../../app/v2/api/deployments/entity/logs.entity'

describe('DeploymentController v2', () => {
let fixtureUtilsService: FixtureUtilsService
Expand Down Expand Up @@ -1001,4 +1002,60 @@ BSAwlmwpOpK27k2yXj4g1x2VaF9GGl//Ere+xUY=
expect(response.body).toEqual(errorResponse)
})
})

it('returns logs from deployment id', async() => {
const deploymentId = '6d1e1881-72d3-4fb5-84da-8bd61bb8e2d3'
const deployment = new DeploymentEntityV2(
deploymentId,
'580a7726-a274-4fc3-9ec1-44e3563d58af',
'ad03d665-f689-42aa-b1de-d19653e89b86',
UrlConstants.deploymentCallbackUrl,
[
new ComponentEntityV2(
UrlConstants.helmRepository,
'currenttag',
'imageurl.com:currenttag',
'my-component',
'777765f8-bb29-49f7-bf2b-3ec956a71583',
'host-value-1',
'gateway-name-1',
[]
)
],
true,
'default',
120,
)

await manager.save(deployment)

const log = new LogEntity (
deploymentId,
[
{
type: 'INFO',
title: 'Created',
details: '{"message":"Container image "paulczar/gb-frontend:v5" already present on machine","object":"Pod/frontend-7cb5fb8b96-prqxv"}',
timestamp: '2021-04-29T10:17:24-03:00'
}
]
)
await manager.save(log)

await request(app.getHttpServer())
.get(`/v2/deployments/${deploymentId}/logs`)
.expect(200)
.expect(response => {
expect(response.body).toEqual({
logs: [
{
type: 'INFO',
title: 'Created',
details: '{"message":"Container image "paulczar/gb-frontend:v5" already present on machine","object":"Pod/frontend-7cb5fb8b96-prqxv"}',
timestamp: '2021-04-29T10:17:24-03:00'
}
]
})
})
})
})