Skip to content

Commit 10556e8

Browse files
committed
feat: add top speed mode
1 parent be4e481 commit 10556e8

File tree

5 files changed

+59
-56
lines changed

5 files changed

+59
-56
lines changed

src/main/kotlin/com/huayi/intellijplatform/gitstats/services/GitStatsService.kt

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.intellij.openapi.project.Project
66
import com.huayi.intellijplatform.gitstats.MyBundle
77
import com.huayi.intellijplatform.gitstats.toolWindow.StatsTableModel
88
import com.huayi.intellijplatform.gitstats.utils.GitUtil
9-
import com.huayi.intellijplatform.gitstats.utils.UserStats
9+
import com.huayi.intellijplatform.gitstats.utils.Utils
1010
import java.text.SimpleDateFormat
1111
import java.util.*
1212

@@ -16,20 +16,16 @@ class GitStatsService(p: Project) {
1616

1717
init {
1818
project = p
19-
thisLogger().info(MyBundle.message("projectService", project.name))
20-
thisLogger().info(project.projectFilePath)
21-
thisLogger().info(project.basePath)
19+
// thisLogger().info(MyBundle.message("projectService", project.name))
2220
}
2321

24-
fun getRandomNumber() = (1..100).random()
25-
26-
fun getUserStats(startDate: String, endDate: String): Array<UserStats> {
27-
val gitUtil = GitUtil("/Users/sunzhenxuan/work/qcc/code/qcc_pro/pro-front")
28-
return gitUtil.getUserStats(startDate, endDate)
29-
}
22+
// fun getRandomNumber() = (1..100).random()
3023

3124
fun getUserStats(startTime: Date, endTime: Date): StatsTableModel {
32-
val gitUtil = GitUtil("/Users/sunzhenxuan/work/qcc/code/qcc_pro/pro-front")
25+
if (!Utils.checkDirectoryExists(project.basePath)) {
26+
return StatsTableModel(arrayOf(), arrayOf())
27+
}
28+
val gitUtil = GitUtil(project.basePath as String)
3329
val userStats = gitUtil.getUserStats(
3430
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime),
3531
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime)
@@ -49,24 +45,26 @@ class GitStatsService(p: Project) {
4945
)
5046
}
5147

52-
fun getTopSpeedUserStats(startTime: Date, endTime: Date) {
53-
val gitUtil = GitUtil("/Users/sunzhenxuan/work/qcc/code/qcc_pro/pro-front")
48+
fun getTopSpeedUserStats(startTime: Date, endTime: Date): StatsTableModel {
49+
if (!Utils.checkDirectoryExists(project.basePath)) {
50+
return StatsTableModel(arrayOf(), arrayOf())
51+
}
52+
val gitUtil = GitUtil(project.basePath as String)
5453
val userStats = gitUtil.getTopSpeedUserStats(
5554
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime),
5655
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime)
5756
)
58-
// val data = userStats.map { item ->
59-
// arrayOf(
60-
// item.author,
61-
// item.commitCount.toString(),
62-
// item.addedLines.toString(),
63-
// item.deletedLines.toString(),
64-
// item.modifiedFileCount.toString()
65-
// )
66-
// }.toTypedArray()
67-
// return StatsTableModel(
68-
// data,
69-
// arrayOf("Author", "CommitCount", "AddedLines", "DeletedLines", "ModifiedFileCount")
70-
// )
57+
val data = userStats.map { item ->
58+
arrayOf(
59+
item.author,
60+
item.addedLines.toString(),
61+
item.deletedLines.toString(),
62+
item.modifiedFileCount.toString()
63+
)
64+
}.toTypedArray()
65+
return StatsTableModel(
66+
data,
67+
arrayOf("Author", "AddedLines", "DeletedLines", "ModifiedFileCount")
68+
)
7169
}
7270
}

src/main/kotlin/com/huayi/intellijplatform/gitstats/toolWindow/GitStatsWindowFactory.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ class GitStatsWindowFactory : ToolWindowFactory {
129129
addActionListener {
130130
(contentPanel.layout as CardLayout).show(contentPanel, "content_loading")
131131
thread {
132-
// service.getTopSpeedUserStats(startTime, endTime)
133-
table.model = service.getUserStats(startTime, endTime)
132+
table.model = service.getTopSpeedUserStats(startTime, endTime)
133+
// table.model = service.getUserStats(startTime, endTime)
134134
SwingUtilities.invokeLater {
135135
(contentPanel.layout as CardLayout).show(contentPanel, "content_table")
136136
}
@@ -140,12 +140,12 @@ class GitStatsWindowFactory : ToolWindowFactory {
140140
})
141141
add(Box.createHorizontalGlue())
142142
// add(IconButton(MyBundle.message("settingButtonTooltipText"), AllIcons.General.Settings))
143-
add(IconLabelButton(AllIcons.General.Settings) {
144-
SettingDialogWrapper().showAndGet()
145-
}.apply {
146-
toolTipText = MyBundle.message("settingButtonTooltipText")
147-
})
148-
add(JBBox.createHorizontalStrut(10))
143+
// add(IconLabelButton(AllIcons.General.Settings) {
144+
// SettingDialogWrapper().showAndGet()
145+
// }.apply {
146+
// toolTipText = MyBundle.message("settingButtonTooltipText")
147+
// })
148+
// add(JBBox.createHorizontalStrut(10))
149149
}
150150
add(headerPanel, BorderLayout.NORTH)
151151

src/main/kotlin/com/huayi/intellijplatform/gitstats/utils/GitUtils.kt

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ class GitUtil(private val repoPath: String) {
2828

2929
fun getTopSpeedUserStats(
3030
startDate: String, endDate: String, timeoutAmount: Long = 60L, timeUnit: TimeUnit = TimeUnit.SECONDS
31-
) {
31+
): Array<UserStats> {
3232
val os = Utils.getOS()
3333
val command = listOf(
3434
if (os == "Windows") "cmd" else "/bin/sh",
3535
if (os == "Windows") "/c" else "-c",
36-
"git log --format=\"%aN\" | sort -u | while read name; do echo \"\$name\\t\"; git log --author=\"\$name\" --pretty=tformat: --since==\"$startDate\" --until=\"$endDate\" --numstat | awk '{ add += \$1; subs += \$2; loc += \$1 - \$2; file++ } END { printf \"added lines: %s, removed lines: %s, total lines: %s, modified files: %s\\n\", add, subs, loc, file }' -; done"
36+
"git log --format=\"%aN\" | sort -u | while read name; do echo \"\$name\"; git log --author=\"\$name\" --pretty=tformat: --since==\"$startDate\" --until=\"$endDate\" --numstat | awk '{ add += \$1; subs += \$2; file++ } END { printf \"added lines: %s, removed lines: %s, modified files: %s\\n\", add ? add : 0, subs ? subs : 0, file ? file : 0 }' -; done"
3737
)
3838
val process = Utils.runCommand(repoPath, command, timeoutAmount, timeUnit)
39-
// val userStatsData = mutableMapOf<String, UserStats>()
40-
process!!.inputStream.bufferedReader().use { reader ->
41-
reader.forEachLine { line ->
42-
println(line)
43-
}
44-
}
39+
val regex = Regex("(.+)\\n+added lines: (\\d*), removed lines: (\\d+), modified files: (\\d+)")
40+
return regex.findAll(process!!.inputStream.bufferedReader().readText())
41+
.map { result ->
42+
val (author, addedLines, deletedLines, modifiedFileCount) = result.destructured
43+
UserStats(author, addedLines.toInt(), deletedLines.toInt(), modifiedFileCount.toInt())
44+
}.sortedByDescending { it.addedLines }.toList().toTypedArray()
4545
}
4646

4747
fun getUserStats(
@@ -63,12 +63,6 @@ class GitUtil(private val repoPath: String) {
6363

6464
val process = Utils.runCommand(repoPath, gitCommand, timeoutAmount, timeUnit)
6565

66-
// val processBuilder = ProcessBuilder(gitCommand)
67-
// .directory(File(repoPath))
68-
// .redirectErrorStream(true)
69-
// .redirectOutput(ProcessBuilder.Redirect.PIPE)
70-
// val process = processBuilder.start().also { it.waitFor(timeoutAmount, timeUnit) }
71-
7266
val userStatsData = mutableMapOf<String, UserStats>()
7367
process!!.inputStream.bufferedReader().use { reader ->
7468
var currentUserStatsData: UserStats? = null

src/main/kotlin/com/huayi/intellijplatform/gitstats/utils/Utils.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.huayi.intellijplatform.gitstats.utils
22

3+
import com.intellij.openapi.diagnostic.thisLogger
34
import java.io.File
45
import java.time.DayOfWeek
56
import java.time.LocalDate
@@ -8,6 +9,14 @@ import java.util.*
89
import java.util.concurrent.TimeUnit
910

1011
object Utils {
12+
fun checkDirectoryExists(directoryPath: String?): Boolean {
13+
if (directoryPath.isNullOrEmpty()) {
14+
return false
15+
}
16+
val directory = File(directoryPath)
17+
return directory.exists() && directory.isDirectory
18+
}
19+
1120
fun getOS(): String {
1221
val os = System.getProperty("os.name").lowercase(Locale.getDefault())
1322
return if (os.contains("win")) {
@@ -33,7 +42,9 @@ object Utils {
3342
.redirectErrorStream(true)
3443
.redirectOutput(ProcessBuilder.Redirect.PIPE)
3544
.start().also { it.waitFor(timeoutAmount, timeUnit) }
36-
}.onFailure { it.printStackTrace() }.getOrNull()
45+
}.onFailure {
46+
thisLogger().info(it.printStackTrace().toString())
47+
}.getOrNull()
3748
}
3849

3950
fun runCommand(repoPath: String, vararg cmd: String): Process? = runCommand(repoPath, listOf(*cmd))

src/test/kotlin/com/huayi/intellijplatform/gitstats/MyPluginTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.huayi.intellijplatform.gitstats
22

33
import com.intellij.ide.highlighter.XmlFileType
4-
import com.intellij.openapi.components.service
4+
//import com.intellij.openapi.components.service
55
import com.intellij.psi.xml.XmlFile
66
import com.intellij.testFramework.TestDataPath
77
import com.intellij.testFramework.fixtures.BasePlatformTestCase
88
import com.intellij.util.PsiErrorElementUtil
9-
import com.huayi.intellijplatform.gitstats.services.GitStatsService
9+
//import com.huayi.intellijplatform.gitstats.services.GitStatsService
1010

1111
@TestDataPath("\$CONTENT_ROOT/src/test/testData")
1212
class MyPluginTest : BasePlatformTestCase() {
@@ -29,11 +29,11 @@ class MyPluginTest : BasePlatformTestCase() {
2929
myFixture.testRename("foo.xml", "foo_after.xml", "a2")
3030
}
3131

32-
fun testProjectService() {
33-
val projectService = project.service<GitStatsService>()
34-
35-
assertNotSame(projectService.getRandomNumber(), projectService.getRandomNumber())
36-
}
32+
// fun testProjectService() {
33+
// val projectService = project.service<GitStatsService>()
34+
//
35+
// assertNotSame(projectService.getRandomNumber(), projectService.getRandomNumber())
36+
// }
3737

3838
override fun getTestDataPath() = "src/test/testData/rename"
3939
}

0 commit comments

Comments
 (0)