Skip to content

Commit be4e481

Browse files
committed
feat: update git stats windows
1 parent 9f66265 commit be4e481

File tree

3 files changed

+98
-61
lines changed

3 files changed

+98
-61
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import com.intellij.openapi.components.Service
44
import com.intellij.openapi.diagnostic.thisLogger
55
import com.intellij.openapi.project.Project
66
import com.huayi.intellijplatform.gitstats.MyBundle
7+
import com.huayi.intellijplatform.gitstats.toolWindow.StatsTableModel
78
import com.huayi.intellijplatform.gitstats.utils.GitUtil
89
import com.huayi.intellijplatform.gitstats.utils.UserStats
10+
import java.text.SimpleDateFormat
11+
import java.util.*
912

1013
@Service(Service.Level.PROJECT)
1114
class GitStatsService(p: Project) {
@@ -24,4 +27,46 @@ class GitStatsService(p: Project) {
2427
val gitUtil = GitUtil("/Users/sunzhenxuan/work/qcc/code/qcc_pro/pro-front")
2528
return gitUtil.getUserStats(startDate, endDate)
2629
}
30+
31+
fun getUserStats(startTime: Date, endTime: Date): StatsTableModel {
32+
val gitUtil = GitUtil("/Users/sunzhenxuan/work/qcc/code/qcc_pro/pro-front")
33+
val userStats = gitUtil.getUserStats(
34+
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime),
35+
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime)
36+
)
37+
val data = userStats.map { item ->
38+
arrayOf(
39+
item.author,
40+
item.commitCount.toString(),
41+
item.addedLines.toString(),
42+
item.deletedLines.toString(),
43+
item.modifiedFileCount.toString()
44+
)
45+
}.toTypedArray()
46+
return StatsTableModel(
47+
data,
48+
arrayOf("Author", "CommitCount", "AddedLines", "DeletedLines", "ModifiedFileCount")
49+
)
50+
}
51+
52+
fun getTopSpeedUserStats(startTime: Date, endTime: Date) {
53+
val gitUtil = GitUtil("/Users/sunzhenxuan/work/qcc/code/qcc_pro/pro-front")
54+
val userStats = gitUtil.getTopSpeedUserStats(
55+
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime),
56+
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime)
57+
)
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+
// )
71+
}
2772
}

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

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
package com.huayi.intellijplatform.gitstats.toolWindow
22

33
import com.huayi.intellijplatform.gitstats.MyBundle
4+
import com.huayi.intellijplatform.gitstats.components.SettingDialogWrapper
45
import com.huayi.intellijplatform.gitstats.services.GitStatsService
56
import com.huayi.intellijplatform.gitstats.utils.Utils
7+
import com.intellij.icons.AllIcons
68
import com.intellij.openapi.components.service
79
import com.intellij.openapi.diagnostic.thisLogger
810
import com.intellij.openapi.project.Project
11+
import com.intellij.openapi.ui.popup.IconButton
912
import com.intellij.openapi.wm.ToolWindow
1013
import com.intellij.openapi.wm.ToolWindowFactory
11-
import com.intellij.ui.components.JBBox
12-
import com.intellij.ui.components.JBLabel
13-
import com.intellij.ui.components.JBPanel
14-
import com.intellij.ui.components.JBScrollPane
15-
import com.intellij.ui.components.JBLoadingPanel
14+
import com.intellij.ui.components.*
1615
import com.intellij.ui.content.ContentFactory
1716
import com.intellij.ui.table.JBTable
1817
import com.michaelbaranov.microba.calendar.DatePicker
1918
import java.awt.BorderLayout
2019
import java.awt.CardLayout
2120
import java.awt.Dimension
2221
import java.awt.Font
23-
import java.text.SimpleDateFormat
24-
import java.time.LocalDate
25-
import java.time.ZoneId
2622
import java.util.*
27-
import javax.swing.BorderFactory
28-
import javax.swing.BoxLayout
29-
import javax.swing.JButton
30-
import javax.swing.ListSelectionModel
23+
import javax.swing.*
24+
import kotlin.concurrent.thread
3125

3226

3327
class GitStatsWindowFactory : ToolWindowFactory {
@@ -50,27 +44,7 @@ class GitStatsWindowFactory : ToolWindowFactory {
5044

5145
private val service = toolWindow.project.service<GitStatsService>()
5246

53-
private fun getUserStatsTableModel(startTime: Date, endTime: Date): StatsTableModel {
54-
val userStats = service.getUserStats(
55-
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime),
56-
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime)
57-
)
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-
)
71-
}
72-
73-
fun getContent(toolWindow: ToolWindow) = JBPanel<JBPanel<*>>().apply {
47+
fun getContent(toolWindow: ToolWindow) = JBPanel<JBPanel<*>>(BorderLayout()).apply {
7448
var (startTime, endTime) = Utils.getThisWeekDateTimeRange()
7549
val table = JBTable().apply {
7650
font = Font("Arial", Font.PLAIN, 14)
@@ -81,7 +55,6 @@ class GitStatsWindowFactory : ToolWindowFactory {
8155
rowHeight = 30
8256
setSelectionMode(ListSelectionModel.SINGLE_SELECTION)
8357
}
84-
layout = BorderLayout()
8558
border = BorderFactory.createEmptyBorder(0, 0, 0, 0)
8659

8760
val contentPanel = JBPanel<JBPanel<*>>().apply {
@@ -105,8 +78,11 @@ class GitStatsWindowFactory : ToolWindowFactory {
10578
layout = BoxLayout(this, BoxLayout.X_AXIS)
10679
add(JBBox.createHorizontalStrut(10))
10780
add(JBLabel(MyBundle.message("filterStartTimeLabel", "")))
108-
// val dateTimePicker = LocalDateTimePicker(LocalDateTime.now())
109-
// add(dateTimePicker)
81+
// add(LocalDateTimePicker(LocalDateTime.now()))
82+
// datePicker.dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
83+
// add(CalendarView())
84+
// val datePicker = JXDatePicker()
85+
// datePicker.date = Date()
11086
// add(DatePicker(Date.from(startTime.atStartOfDay(ZoneId.systemDefault()).toInstant())).apply {
11187
add(DatePicker(startTime).apply {
11288
isDropdownFocusable = false
@@ -115,26 +91,28 @@ class GitStatsWindowFactory : ToolWindowFactory {
11591
preferredSize = size
11692
maximumSize = size
11793
minimumSize = size
94+
isShowNoneButton = false
95+
isShowNumberOfWeek = true
96+
isStripTime = true
97+
components.last().preferredSize = Dimension(30, 30)
11898
addPropertyChangeListener {
11999
if (it.propertyName == "date" && it.newValue != null) {
120100
startTime = it.newValue as Date
121101
}
122102
}
123103
})
124104
add(JBBox.createHorizontalStrut(10))
125-
// datePicker.dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
126-
// add(CalendarView())
127-
// val datePicker = JXDatePicker()
128-
// datePicker.date = Date()
129105
add(JBLabel(MyBundle.message("filterEndTimeLabel", "")))
130-
// add(DatePicker(Date.from(endTime.atStartOfDay(ZoneId.systemDefault()).toInstant())).apply {
131106
add(DatePicker(endTime).apply {
132107
isDropdownFocusable = false
133-
// isFieldEditable = false
134108
val size = Dimension(200, preferredSize.height)
135109
preferredSize = size
136110
maximumSize = size
137111
minimumSize = size
112+
isShowNoneButton = false
113+
isShowNumberOfWeek = true
114+
isStripTime = true
115+
components.last().preferredSize = Dimension(30, 30)
138116
addPropertyChangeListener {
139117
if (it.propertyName == "date" && it.newValue != null) {
140118
val calendar = Calendar.getInstance()
@@ -150,17 +128,24 @@ class GitStatsWindowFactory : ToolWindowFactory {
150128
add(JButton(MyBundle.message("refreshButtonLabel")).apply {
151129
addActionListener {
152130
(contentPanel.layout as CardLayout).show(contentPanel, "content_loading")
153-
table.model = getUserStatsTableModel(startTime, endTime)
154-
(contentPanel.layout as CardLayout).show(contentPanel, "content_table")
155-
// label.text = MyBundle.message("randomLabel", service.getRandomNumber())
131+
thread {
132+
// service.getTopSpeedUserStats(startTime, endTime)
133+
table.model = service.getUserStats(startTime, endTime)
134+
SwingUtilities.invokeLater {
135+
(contentPanel.layout as CardLayout).show(contentPanel, "content_table")
136+
}
137+
}
156138
}
139+
doClick()
157140
})
158-
// val datePicker = JXDatePicker()
159-
// datePicker.setFormats(SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
160-
// datePicker.timeZone = TimeZone.getDefault()
161-
// datePicker.editor.isEditable = true
162-
// add(datePicker)
163-
// datePicker.editor = DateEditor(datePicker, "yyyy-MM-dd HH:mm:ss")
141+
add(Box.createHorizontalGlue())
142+
// 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))
164149
}
165150
add(headerPanel, BorderLayout.NORTH)
166151

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,24 @@ data class CommitFilesStats(
2525
)
2626

2727
class GitUtil(private val repoPath: String) {
28-
fun runCommand(cmd: List<String>, timeoutAmount: Long = 60L, timeUnit: TimeUnit = TimeUnit.SECONDS): Process? {
29-
return runCatching {
30-
ProcessBuilder(cmd)
31-
.directory(File(repoPath))
32-
.redirectErrorStream(true)
33-
.redirectOutput(ProcessBuilder.Redirect.PIPE)
34-
.start().also { it.waitFor(timeoutAmount, timeUnit) }
35-
}.onFailure { it.printStackTrace() }.getOrNull()
36-
}
3728

38-
fun runCommand(vararg cmd: String): Process? = runCommand(listOf(*cmd))
29+
fun getTopSpeedUserStats(
30+
startDate: String, endDate: String, timeoutAmount: Long = 60L, timeUnit: TimeUnit = TimeUnit.SECONDS
31+
) {
32+
val os = Utils.getOS()
33+
val command = listOf(
34+
if (os == "Windows") "cmd" else "/bin/sh",
35+
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"
37+
)
38+
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+
}
45+
}
3946

4047
fun getUserStats(
4148
startDate: String,

0 commit comments

Comments
 (0)