"megatron/legacy/model/transformer.py" did not exist on "5942af978a8a8ff706a302b1ba2d9ef3ce144444"
main.go 9.04 KB
Newer Older
liming6's avatar
liming6 committed
1
2
3
package tui

import (
4
	"get-container/cmd/hytop/backend"
liming6's avatar
liming6 committed
5
	"get-container/gpu"
liming6's avatar
liming6 committed
6
	"get-container/utils"
liming6's avatar
liming6 committed
7
	"syscall"
liming6's avatar
liming6 committed
8
9
	"time"

liming6's avatar
liming6 committed
10
11
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
liming6's avatar
liming6 committed
12
13
14
15
)

const (
	DCUTopVersion = "1.0.0"
liming6's avatar
liming6 committed
16
17
)

liming6's avatar
liming6 committed
18
19
20
21
22
23
24
25
26
var (
	HeightLightStyle    = lipgloss.NewStyle().Background(lipgloss.Color("#00fffb7a")) // 高亮风格
	SelectedStyle       = lipgloss.NewStyle().Foreground(lipgloss.Color("#ffb81fe3")) // 被选中的风格
	HeightSelectedStyle = lipgloss.NewStyle().Background(lipgloss.Color("#ffb81fe3")) // 既被选中,又高亮的风格

	LowLeightStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#442d2d"))
	NormalStyle    = lipgloss.NewStyle().Foreground(lipgloss.Color("#e6e6e6eb"))
)

liming6's avatar
liming6 committed
27
28
// ModelMsg 模型信息,在父组件和各个子组件间共享信息
type ModelMsg struct {
liming6's avatar
liming6 committed
29
30
	t          time.Time                        // 当前时间
	Version    *gpu.HYVersionInfo               // gpu版本相关信息
31
32
	DCUPidInfo map[int][]backend.DCUProcessInfo // 使用dcu的进程信息
	systemInfo *utils.SysInfo                   // 系统信息
liming6's avatar
liming6 committed
33
34
	MyVersion  string                           // 本软件的版本
	DCUInfo    *backend.DCUInfoMap              // dcu相关信息
liming6's avatar
liming6 committed
35
36
37
38
}

type TickMsg time.Time

liming6's avatar
liming6 committed
39
40
41
42
43
44
45
46
47
48
49
// ActionMsg 动作消息
type ActionMsg struct {
	SelectPids     map[int32]bool  // 选择的pid进程
	Action         *syscall.Signal // 对选择的pid的动作
	PidView        *int32          // 进程视图指标的pid号,为null表示没有进入进程指标视图
	PointPid       *int32          // 指针指向的pid,为null表示没有选择进程
	PidEnvView     *int32          // 进程环境变量视图的pid号,为null表示不进入进程环境变量视图
	PidTreeView    *int32          // 进入pstree视图的pid号,为null表示不进入pstree视图
	TargetDCUIndex *int            // PointPid使用的dcu index
}

liming6's avatar
liming6 committed
50
51
// ModelMain tui主模型类
type ModelMain struct {
liming6's avatar
liming6 committed
52
53
54
	width, height int // 终端尺寸
	Header        *ModelHeader
	DCUInfo       *ModelDCUInfo
liming6's avatar
liming6 committed
55
	SysLoad       *ModelSysLoad
56
	ProcessInfo   *ModelProcessInfo
liming6's avatar
liming6 committed
57
	modelMsg      *ModelMsg
liming6's avatar
liming6 committed
58
	actionMsg     *ActionMsg
liming6's avatar
liming6 committed
59
60
}

liming6's avatar
liming6 committed
61
func NewModelMain(width, height int) ModelMain {
liming6's avatar
liming6 committed
62
	result := ModelMain{}
liming6's avatar
liming6 committed
63
64
	result.width = width
	result.height = height
65
66
	result.Header = NewModelHeader()
	result.DCUInfo = NewModelDCUInfo(width, height)
liming6's avatar
liming6 committed
67
	result.SysLoad = NewModelSysLoad(width)
68
	result.ProcessInfo = NewModelProcessInfo(width)
liming6's avatar
liming6 committed
69
	result.actionMsg = &ActionMsg{}
liming6's avatar
liming6 committed
70
	return result
liming6's avatar
liming6 committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
}

func tickCmd() tea.Cmd {
	return tea.Every(time.Second, func(t time.Time) tea.Msg {
		return TickMsg(t)
	})
}

func sendMsgCmd(modelMsg *ModelMsg) tea.Cmd {
	return func() tea.Msg {
		return *modelMsg
	}
}

// Init 初始化信息
func (m *ModelMain) Init() tea.Cmd {
liming6's avatar
liming6 committed
87
88
	modelMsg := ModelMsg{}
	if err := initModelInfo(&modelMsg); err != nil {
liming6's avatar
liming6 committed
89
90
		return tea.Quit
	}
liming6's avatar
liming6 committed
91
92
93
94
	cmds := make([]tea.Cmd, 0)
	if c := m.Header.Init(); c != nil {
		cmds = append(cmds, c)
	}
liming6's avatar
liming6 committed
95
96
97
	if c := m.DCUInfo.Init(); c != nil {
		cmds = append(cmds, c)
	}
liming6's avatar
liming6 committed
98
99
100
	if c := m.SysLoad.Init(); c != nil {
		cmds = append(cmds, c)
	}
101
102
103
	if c := m.ProcessInfo.Init(); c != nil {
		cmds = append(cmds, c)
	}
liming6's avatar
liming6 committed
104
	m.modelMsg = &modelMsg
liming6's avatar
liming6 committed
105
	// 将调用初始化方法
liming6's avatar
liming6 committed
106
	cmds = append(cmds, tickCmd(), sendMsgCmd(&modelMsg))
liming6's avatar
liming6 committed
107
108
109
110
111
112
113
114
115
	return tea.Batch(cmds...)
}

func (m *ModelMain) Update(inputMsg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := inputMsg.(type) {
	case tea.KeyMsg: // 键盘事件
		switch msg.String() {
		case "ctrl+c", "q":
			return m, tea.Quit
liming6's avatar
liming6 committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
		case "up":
			cmd := m.handleKeyUp()
			return m, cmd
		case "down":
			cmd := m.handleKeyDown()
			return m, cmd
		case "enter":
			return m, tea.Quit
		case "h":
			return m, tea.Quit
		case "k":
			return m, tea.Quit
		case "esc":
			cmd := m.handleKeyEsc()
			return m, cmd
		case " ":
			cmd := m.handleKeySpace()
			return m, cmd
liming6's avatar
liming6 committed
134
135
		}
	case TickMsg: // 定时事件
liming6's avatar
liming6 committed
136
		updateModelInfo(m.modelMsg, time.Time(msg))
liming6's avatar
liming6 committed
137
138
139
		header, _ := m.Header.Update(m.modelMsg)
		dcuInfo, _ := m.DCUInfo.Update(m.modelMsg)
		sysLoad, _ := m.SysLoad.Update(m.modelMsg)
140
		pidinfo, _ := m.ProcessInfo.Update(m.modelMsg)
liming6's avatar
liming6 committed
141
		m.Header = header.(*ModelHeader)
liming6's avatar
liming6 committed
142
		m.DCUInfo = dcuInfo.(*ModelDCUInfo)
liming6's avatar
liming6 committed
143
		m.SysLoad = sysLoad.(*ModelSysLoad)
144
		m.ProcessInfo = pidinfo.(*ModelProcessInfo)
liming6's avatar
liming6 committed
145
		return m, tickCmd()
liming6's avatar
liming6 committed
146
147
148
	case ModelMsg: // 初始化
		header, _ := m.Header.Update(m.modelMsg)
		dcuInfo, _ := m.DCUInfo.Update(m.modelMsg)
liming6's avatar
liming6 committed
149
		sysLoad, _ := m.SysLoad.Update(m.modelMsg)
150
		pidinfo, _ := m.ProcessInfo.Update(m.modelMsg)
liming6's avatar
liming6 committed
151
152
		m.Header = header.(*ModelHeader)
		m.DCUInfo = dcuInfo.(*ModelDCUInfo)
liming6's avatar
liming6 committed
153
		m.SysLoad = sysLoad.(*ModelSysLoad)
154
		m.ProcessInfo = pidinfo.(*ModelProcessInfo)
liming6's avatar
liming6 committed
155
		return m, nil
156
	case tea.WindowSizeMsg: // 窗口尺寸变化
liming6's avatar
liming6 committed
157
		m.width, m.height = msg.Width, msg.Height
158
159
160
161
162
163
164
165
		header, _ := m.Header.Update(msg)
		dcuInfo, _ := m.DCUInfo.Update(msg)
		sysLoad, _ := m.SysLoad.Update(msg)
		pidinfo, _ := m.ProcessInfo.Update(msg)
		m.Header = header.(*ModelHeader)
		m.DCUInfo = dcuInfo.(*ModelDCUInfo)
		m.SysLoad = sysLoad.(*ModelSysLoad)
		m.ProcessInfo = pidinfo.(*ModelProcessInfo)
liming6's avatar
liming6 committed
166
		return m, nil
liming6's avatar
liming6 committed
167
168
169
170
	}
	return m, nil
}

liming6's avatar
liming6 committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
func (m *ModelMain) handleKeyEsc() tea.Cmd {
	return nil
}

func (m *ModelMain) handleKeySpace() tea.Cmd {
	if m.actionMsg == nil || m.actionMsg.PointPid == nil {
		return nil
	}
	if m.actionMsg.SelectPids == nil {
		m.actionMsg.SelectPids = make(map[int32]bool)
	}
	_, have := m.actionMsg.SelectPids[*m.actionMsg.PointPid]
	if have {
		delete(m.actionMsg.SelectPids, *m.actionMsg.PointPid)
	} else {
		m.actionMsg.SelectPids[*m.actionMsg.PointPid] = true
	}
	m1, cmd1 := m.ProcessInfo.Update(m.actionMsg)
	m.ProcessInfo = m1.(*ModelProcessInfo)
	return cmd1
}

liming6's avatar
liming6 committed
193
func (m *ModelMain) View() string {
194
	return m.Header.View() + m.DCUInfo.View() + m.SysLoad.View() + m.ProcessInfo.View() + "\n"
liming6's avatar
liming6 committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
}

var myBorder = lipgloss.Border{
	Top:          "═",
	TopLeft:      "╒",
	TopRight:     "╕",
	Bottom:       "═",
	BottomLeft:   "╘",
	BottomRight:  "╛",
	Left:         "│",
	Right:        "│",
	MiddleLeft:   "├",
	MiddleRight:  "┤",
	Middle:       "┼",
	MiddleTop:    "┬",
	MiddleBottom: "┴",
}

liming6's avatar
liming6 committed
213
214
func initModelInfo(model *ModelMsg) error {
	model.t = time.Now()
liming6's avatar
liming6 committed
215
216
	model.MyVersion = DCUTopVersion
	if ver, err := gpu.GetHYVersionInfo(); err != nil {
liming6's avatar
liming6 committed
217
		return err
liming6's avatar
liming6 committed
218
	} else {
liming6's avatar
liming6 committed
219
		model.Version = ver
liming6's avatar
liming6 committed
220
	}
221
222
223
224
225
226
227
228
229
230
231
232
233
234
	model.DCUInfo = backend.DCUSInfoMap
	model.DCUPidInfo = backend.DCUSInfoMap.GetDCUProcessInfo2()
	if sinfo, err := utils.GetSysInfo(); err != nil {
		return err
	} else {
		model.systemInfo = sinfo
	}
	if err := model.DCUInfo.UpdateQuickInfo(); err != nil {
		return err
	}
	if err := model.DCUInfo.UpdateSlowInfo(); err != nil {
		return err
	}
	return nil
liming6's avatar
liming6 committed
235
236
}

liming6's avatar
liming6 committed
237
// updateModelInfo 更新模型信息
238
func updateModelInfo(modelMsg *ModelMsg, t time.Time) error {
liming6's avatar
liming6 committed
239
	modelMsg.t = t
240
241
242
243
244
245
246
247
248
249
	if sinfo, err := utils.GetSysInfo(); err != nil {
		return err
	} else {
		modelMsg.systemInfo = sinfo
	}
	modelMsg.DCUPidInfo = modelMsg.DCUInfo.GetDCUProcessInfo2()
	if err := modelMsg.DCUInfo.UpdateQuickInfo(); err != nil {
		return err
	}
	return nil
liming6's avatar
liming6 committed
250
}
liming6's avatar
liming6 committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

func (m *ModelMain) handleKeyUp() tea.Cmd {
	if m.actionMsg == nil {
		m.actionMsg = &ActionMsg{}
	}
	if m.modelMsg.DCUPidInfo == nil {
		return nil
	}
	processes := make([]backend.DCUProcessInfo, 0)
	for index := range 64 {
		p, have := m.modelMsg.DCUPidInfo[index]
		if have && len(p) > 0 {
			processes = append(processes, p...)
		}
	}
	processNum := len(processes)
	if processNum == 0 {
		return nil
	}
	index := 0
	if m.actionMsg.PointPid == nil {
		// 获取列表中的最后一个进程的pid
		index = processNum - 1
	} else {
		var targetIndex = -1
		for l := processNum - 1; l >= 0; l-- {
			if processes[l].Info.Pid == *m.actionMsg.PointPid {
				targetIndex = l - 1
				break
			}
		}
		if targetIndex == -1 {
			index = processNum - 1
		} else {
			index = targetIndex
		}
	}
	pid := processes[index].Info.Pid
	m.actionMsg.PointPid = &pid
	idx := processes[index].DCU
	m.actionMsg.TargetDCUIndex = &idx
	m1, cmd1 := m.ProcessInfo.Update(m.actionMsg)
	m2, cmd2 := m.DCUInfo.Update(m.actionMsg)
	m.ProcessInfo = m1.(*ModelProcessInfo)
	m.DCUInfo = m2.(*ModelDCUInfo)
	return tea.Batch(cmd1, cmd2)
}

func (m *ModelMain) handleKeyDown() tea.Cmd {
	if m.actionMsg == nil {
		m.actionMsg = &ActionMsg{}
	}
	if m.modelMsg.DCUPidInfo == nil {
		return nil
	}
	processes := make([]backend.DCUProcessInfo, 0)
	for index := range 64 {
		p, have := m.modelMsg.DCUPidInfo[index]
		if have && len(p) > 0 {
			processes = append(processes, p...)
		}
	}
	processNum := len(processes)
	if processNum == 0 {
		return nil
	}
	index := 0
	if m.actionMsg.PointPid == nil {
		index = 0
	} else {
		var targetIndex = -1
		for l := 0; l < processNum-1; l++ {
			if processes[l].Info.Pid == *m.actionMsg.PointPid {
				targetIndex = l + 1
				if targetIndex >= processNum {
					targetIndex = 0
				}
				break
			}
		}
		if targetIndex == -1 {
			index = 0
		} else {
			index = targetIndex

		}
	}
	pid := processes[index].Info.Pid
	m.actionMsg.PointPid = &pid
	idx := processes[index].DCU
	m.actionMsg.TargetDCUIndex = &idx
	m1, cmd1 := m.ProcessInfo.Update(m.actionMsg)
	m2, cmd2 := m.DCUInfo.Update(m.actionMsg)
	m.ProcessInfo = m1.(*ModelProcessInfo)
	m.DCUInfo = m2.(*ModelDCUInfo)
	return tea.Batch(cmd1, cmd2)
}