Skip to content

Commit

Permalink
feature: 代码库获取项目列表耗时太长 TencentBlueKing#4290
Browse files Browse the repository at this point in the history
  • Loading branch information
mingshewhe committed May 16, 2022
1 parent cb3350c commit c2e6a5e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ interface ServiceGitResource {
accessToken: String,
@ApiParam("用户id", required = true)
@QueryParam("userId")
userId: String,
@ApiParam(value = "搜索工蜂代码库名字", required = false)
@QueryParam("search")
search: String?
userId: String
): Result<List<Project>>

@ApiOperation("获取用户所有git项目,分页方式获取")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ class ServiceGitResourceImpl @Autowired constructor(
return gitService.updateGitProjectInfo(projectName, updateGitProjectInfo, token, tokenType)
}

override fun getProject(accessToken: String, userId: String, search: String?): Result<List<Project>> {
return Result(gitService.getProject(accessToken, userId, search))
override fun getProject(accessToken: String, userId: String): Result<List<Project>> {
return Result(gitService.getProject(accessToken, userId))
}

override fun getProjectList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ class GitOauthService @Autowired constructor(
val authResult = AuthorizeResult(200, "")
return try {
authResult.project.addAll(
gitService.getProject(
gitService.getProjectList(
accessToken = accessToken.accessToken,
userId = userId,
page = 1,
pageSize = 100,
search = search
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,44 +133,49 @@ class GitService @Autowired constructor(

private val executorService = Executors.newFixedThreadPool(2)

override fun getProject(accessToken: String, userId: String, search: String?): List<Project> {
override fun getProject(accessToken: String, userId: String): List<Project> {

logger.info("Start to get the projects by user $userId")
val startEpoch = System.currentTimeMillis()
try {
var page = 1

val result = mutableListOf<Project>()
var projectUrl = "${gitConfig.gitApiUrl}/projects?access_token=$accessToken&page=1&per_page=100"
if (!search.isNullOrBlank()) {
projectUrl = "$projectUrl&search=$search"
}
val request = Request.Builder()
.url(projectUrl)
.get()
.build()
while (true) {
val projectUrl = "${gitConfig.gitApiUrl}/projects?access_token=$accessToken&page=$page&per_page=100"
page++
val request = Request.Builder()
.url(projectUrl)
.get()
.build()

OkhttpUtils.doHttp(request).use { response ->
val data = response.body()!!.string()
val repoList = JsonParser().parse(data).asJsonArray
repoList.forEach {
val obj = it.asJsonObject
val lastActivityTime = obj["last_activity_at"].asString.removeSuffix("+0000")
result.add(
Project(
id = obj["id"].asString,
name = obj["name"].asString,
nameWithNameSpace = obj["name_with_namespace"].asString,
sshUrl = obj["ssh_url_to_repo"].asString,
httpUrl = obj["https_url_to_repo"].asString,
lastActivity = DateTimeUtil.convertLocalDateTimeToTimestamp(
LocalDateTime.parse(
lastActivityTime
)
) * 1000L
OkhttpUtils.doHttp(request).use { response ->
val data = response.body()!!.string()
val repoList = JsonParser().parse(data).asJsonArray
repoList.forEach {
val obj = it.asJsonObject
val lastActivityTime = obj["last_activity_at"].asString.removeSuffix("+0000")
result.add(
Project(
id = obj["id"].asString,
name = obj["name"].asString,
nameWithNameSpace = obj["name_with_namespace"].asString,
sshUrl = obj["ssh_url_to_repo"].asString,
httpUrl = obj["https_url_to_repo"].asString,
lastActivity = DateTimeUtil.convertLocalDateTimeToTimestamp(
LocalDateTime.parse(
lastActivityTime
)
) * 1000L
)
)
)
}
if (repoList.size() < 100) {
logger.info("Finish get the project by user with size ${result.size}")
return result.sortedBy { 0 - it.lastActivity }
} // 倒序排序
}
}
return result.sortedBy { 0 - it.lastActivity }
} finally {
logger.info("It took ${System.currentTimeMillis() - startEpoch}ms to get the project")
}
Expand Down Expand Up @@ -219,7 +224,7 @@ class GitService @Autowired constructor(
name = project["name"].asString,
nameWithNameSpace = project["name_with_namespace"].asString,
sshUrl = project["ssh_url_to_repo"].asString,
httpUrl = project["http_url_to_repo"].asString,
httpUrl = project["https_url_to_repo"].asString,
lastActivity = DateTimeUtil.convertLocalDateTimeToTimestamp(
LocalDateTime.parse(lastActivityTime)) * 1000L
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import javax.servlet.http.HttpServletResponse

@Suppress("ALL")
interface IGitService {
fun getProject(accessToken: String, userId: String, search: String? = null): List<Project>
fun getProject(accessToken: String, userId: String): List<Project>
fun getBranch(
accessToken: String,
userId: String,
Expand Down

0 comments on commit c2e6a5e

Please sign in to comment.