-
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.
- Loading branch information
1 parent
935755d
commit dee92fc
Showing
10 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
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
5 changes: 5 additions & 0 deletions
5
...n/xquare/app/xquareinfra/domain/container/adapter/dto/response/GetContainerLogResponse.kt
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,5 @@ | ||
package xquare.app.xquareinfra.domain.container.adapter.dto.response | ||
|
||
data class GetContainerLogResponse( | ||
val logs: List<Any?> | ||
) |
8 changes: 8 additions & 0 deletions
8
...lin/xquare/app/xquareinfra/domain/container/application/port/in/GetContainerLogUseCase.kt
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,8 @@ | ||
package xquare.app.xquareinfra.domain.container.application.port.`in` | ||
|
||
import xquare.app.xquareinfra.domain.container.adapter.dto.response.GetContainerLogResponse | ||
import xquare.app.xquareinfra.domain.container.domain.ContainerEnvironment | ||
|
||
interface GetContainerLogUseCase { | ||
fun getContainerLog(deployName: String, environment: ContainerEnvironment): GetContainerLogResponse | ||
} |
53 changes: 53 additions & 0 deletions
53
...kotlin/xquare/app/xquareinfra/domain/container/application/service/GetContainerService.kt
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,53 @@ | ||
package xquare.app.xquareinfra.domain.container.application.service | ||
|
||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
import xquare.app.xquareinfra.domain.container.adapter.dto.response.GetContainerLogResponse | ||
import xquare.app.xquareinfra.domain.container.application.port.`in`.GetContainerLogUseCase | ||
import xquare.app.xquareinfra.domain.container.application.port.out.FindContainerPort | ||
import xquare.app.xquareinfra.domain.container.domain.ContainerEnvironment | ||
import xquare.app.xquareinfra.domain.deploy.application.port.out.FindDeployPort | ||
import xquare.app.xquareinfra.infrastructure.exception.BusinessLogicException | ||
import xquare.app.xquareinfra.infrastructure.feign.client.log.LogClient | ||
import xquare.app.xquareinfra.infrastructure.feign.client.log.LogUtil | ||
import xquare.app.xquareinfra.infrastructure.feign.client.log.dto.GetLogRequest | ||
import xquare.app.xquareinfra.infrastructure.feign.client.log.dto.QueryDto | ||
import java.time.Instant | ||
|
||
@Transactional(readOnly = true) | ||
@Service | ||
class GetContainerService( | ||
private val findContainerPort: FindContainerPort, | ||
private val findDeployPort: FindDeployPort, | ||
private val logClient: LogClient | ||
): GetContainerLogUseCase { | ||
override fun getContainerLog(deployName: String, environment: ContainerEnvironment): GetContainerLogResponse { | ||
val deploy = findDeployPort.findByDeployName(deployName) ?: throw BusinessLogicException.DEPLOY_NOT_FOUND | ||
val container = findContainerPort.findByDeployAndEnvironment(deploy, environment) | ||
?: throw BusinessLogicException.CONTAINER_NOT_FOUND | ||
|
||
val currentTimeMillis = Instant.now().toEpochMilli() | ||
val twentyFourHoursAgoMillis = currentTimeMillis - (24 * 60 * 60 * 1000) | ||
|
||
val response = logClient.getLogs( | ||
GetLogRequest( | ||
queries = listOf( | ||
QueryDto( | ||
expr = LogUtil.makeLogQuery( | ||
team = deploy.team.teamNameEn, | ||
containerName = deployName, | ||
serviceType = deploy.deployType, | ||
envType = environment | ||
), | ||
refId = "A", | ||
datasource = "loki" | ||
) | ||
), | ||
from = twentyFourHoursAgoMillis.toString(), | ||
to = currentTimeMillis.toString() | ||
) | ||
) | ||
|
||
return GetContainerLogResponse(response.results.a.frames[0].data.values[2]) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/xquare/app/xquareinfra/infrastructure/feign/client/log/LogClient.kt
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,18 @@ | ||
package xquare.app.xquareinfra.infrastructure.feign.client.log | ||
|
||
import org.springframework.cloud.openfeign.FeignClient | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import xquare.app.xquareinfra.infrastructure.feign.client.log.dto.GetLogRequest | ||
import xquare.app.xquareinfra.infrastructure.feign.client.log.dto.LogResponse | ||
import xquare.app.xquareinfra.infrastructure.feign.config.FeignConfig | ||
|
||
@FeignClient( | ||
name = "tsdata-client", | ||
url = "\${url.log}", | ||
configuration = [FeignConfig::class] | ||
) | ||
interface LogClient { | ||
@PostMapping("/api/ds/query") | ||
fun getLogs(@RequestBody getLogRequest: GetLogRequest): LogResponse | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/xquare/app/xquareinfra/infrastructure/feign/client/log/LogUtil.kt
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,29 @@ | ||
package xquare.app.xquareinfra.infrastructure.feign.client.log | ||
|
||
import xquare.app.xquareinfra.domain.container.domain.ContainerEnvironment | ||
import xquare.app.xquareinfra.domain.deploy.domain.DeployType | ||
|
||
object LogUtil { | ||
fun extractOrganization(githubUrl: String): String { | ||
val parts = githubUrl.split("/") | ||
return if (parts.size >= 4) { | ||
parts[3] | ||
} else { | ||
"" | ||
} | ||
} | ||
|
||
fun getRepository(githubUrl: String): String { | ||
val parts = githubUrl.split("/") | ||
return if (parts.size >= 4) { | ||
"${parts[3]}/${parts[4]}" | ||
} else { | ||
"" | ||
} | ||
} | ||
|
||
fun makeLogQuery(team: String, containerName: String, serviceType: DeployType, envType: ContainerEnvironment): String { | ||
val fullName = "${containerName}-${serviceType.toString().lowercase()}-${envType.toString().lowercase()}" | ||
return "{job=\"$team-${envType.toString().lowercase()}/$fullName\", container=~\"$fullName\", stream=~\"stdout\"} |~ \"(?i)\" \n" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/xquare/app/xquareinfra/infrastructure/feign/client/log/dto/GetLogRequest.kt
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,7 @@ | ||
package xquare.app.xquareinfra.infrastructure.feign.client.log.dto | ||
|
||
data class GetLogRequest( | ||
val queries: List<QueryDto>, | ||
val from: String, | ||
val to: String | ||
) |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/xquare/app/xquareinfra/infrastructure/feign/client/log/dto/LogResponse.kt
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,24 @@ | ||
package xquare.app.xquareinfra.infrastructure.feign.client.log.dto | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
data class LogResponse( | ||
val results: Results, | ||
) | ||
|
||
data class Results( | ||
@JsonProperty("A") | ||
val a: A, | ||
) | ||
|
||
data class A( | ||
val frames: List<Frame>, | ||
) | ||
|
||
data class Frame( | ||
val data: Data, | ||
) | ||
|
||
data class Data( | ||
val values: List<List<Any?>>, | ||
) |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/xquare/app/xquareinfra/infrastructure/feign/client/log/dto/QueryDto.kt
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,8 @@ | ||
package xquare.app.xquareinfra.infrastructure.feign.client.log.dto | ||
|
||
data class QueryDto( | ||
val expr: String, | ||
val refId: String, | ||
val datasource: String | ||
) | ||
|
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 |
---|---|---|
|
@@ -56,6 +56,7 @@ server: | |
|
||
url: | ||
deploy: ${DEPLOY_URL} | ||
log: ${LOG_URL} | ||
|
||
secret: | ||
projectSecret: ${PROJECT_SECRET} | ||
|