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 4, 2022
1 parent 57c395c commit 798ff62
Showing 1 changed file with 29 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,48 +138,40 @@ class GitService @Autowired constructor(
logger.info("Start to get the projects by user $userId")
val startEpoch = System.currentTimeMillis()
try {
var page = 1

val result = mutableListOf<Project>()
while (true) {
val projectUrl = if (name.isNullOrBlank()) {
"${gitConfig.gitApiUrl}/projects?access_token=$accessToken&page=$page&per_page=100"
} else {
"${gitConfig.gitApiUrl}/projects?access_token=$accessToken&page=$page&per_page=100&search=$name"
}
page++
val request = Request.Builder()
.url(projectUrl)
.get()
.build()
val projectUrl = if (name.isNullOrBlank()) {
"${gitConfig.gitApiUrl}/projects?access_token=$accessToken&page=1&per_page=100"
} else {
"${gitConfig.gitApiUrl}/projects?access_token=$accessToken&page=1&per_page=100&search=$name"
}
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["http_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["http_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

0 comments on commit 798ff62

Please sign in to comment.