-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
570 lines (496 loc) · 15.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/buger/jsonparser"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
"github.com/tkanos/gonfig"
)
type Configuration struct {
Cookie string
CrawlIntervalSecond int
DailyGoal int
EasyScore int
MediumScore int
HardScore int
StartTimeOfTheNewDay int
}
type Problem struct {
ID int64
Title string
Level int64
TimeStamp int64
RunTime string
Memory string
State string
}
var subl *widgets.List
var acl *widgets.List
var curList *widgets.List //current selected list
var msg *widgets.Paragraph
var clock *widgets.Paragraph
var todaybc *widgets.BarChart
var weekSubsbc *widgets.StackedBarChart
var configuration Configuration
var progress *widgets.Gauge
var weekACsbc *widgets.StackedBarChart
var problemMap = make(map[string]Problem)
var curSubmissionData = make([]Problem, 0)
var msgDefault = "Press 'q' to quit, press 'r' to refresh, KeyArrow to view Sub/AC list"
func main() {
if len(os.Args) == 1 {
fmt.Println("Usage: lcdash ./lcdashconfig.json \nYou can download the config file template in https://github.com/yangchenxi/LeetcodeDashboard")
return
}
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
subl = widgets.NewList()
curList = subl
msg = widgets.NewParagraph()
clock = widgets.NewParagraph()
acl = widgets.NewList()
todaybc = widgets.NewBarChart()
progress = widgets.NewGauge()
weekACsbc = widgets.NewStackedBarChart()
weekSubsbc = widgets.NewStackedBarChart()
InitUI()
msg.Text = "Loading configuration..."
ui.Render(msg)
err := gonfig.GetConf(os.Args[1], &configuration)
if err != nil {
panic(err)
}
//
//operations
GetAllProblems()
RefreshData()
//
uiEvents := ui.PollEvents()
ticker := time.NewTicker(time.Second).C
refreshTicker := time.NewTicker(time.Duration(configuration.CrawlIntervalSecond) * time.Second).C
for {
select {
case e := <-uiEvents:
switch e.ID { // event string/identifier
case "q", "<C-c>": // press 'q' or 'C-c' to quit
return
case "r": //press 'r' to refresh data
RefreshData()
RefreshGrid()
case "<Resize>":
RefreshGrid()
case "<Up>":
curList.ScrollUp()
ui.Render(curList)
case "<Down>":
curList.ScrollDown()
ui.Render(curList)
case "<Right>":
if curList == subl {
curList = acl
subl.BorderStyle = ui.NewStyle(ui.ColorWhite)
curList.BorderStyle = ui.NewStyle(ui.ColorGreen)
ui.Render(curList, subl)
}
case "<Left>":
if curList == acl {
curList = subl
acl.BorderStyle = ui.NewStyle(ui.ColorWhite)
curList.BorderStyle = ui.NewStyle(ui.ColorGreen)
ui.Render(curList, acl)
}
}
// use Go's built-in tickers for updating and drawing data
case <-ticker:
//update clock
UpdateClock()
case <-refreshTicker:
RefreshData()
}
}
}
func UpdateClock() {
clock.Text = time.Now().Format(time.RFC850)
ui.Render(subl, clock)
}
func GetSubmissions() error {
msg.Text = "Requesting Submission Data..."
ui.Render(msg)
curSubmissionData = make([]Problem, 0)
hasNext := true
lastKey := ""
CurTime := time.Now()
LastTimeStamp := CurTime.Unix()
TargetTimeStamp := CurTime.Unix() - int64(CurTime.Hour()-configuration.StartTimeOfTheNewDay)*3600 - 86400*8 //nowTs-(nowHour-startHour)*tshour-7daysTs
for hasNext && LastTimeStamp > TargetTimeStamp {
requestURL := "https://leetcode.com/api/submissions/?offset=0&limit=20&lastkey=" + lastKey
//println(requestURL)
client := &http.Client{}
req, err := http.NewRequest("GET", requestURL, nil)
req.Header.Set("cookie", configuration.Cookie)
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
log.Fatalln(err)
return err
}
resp, err := client.Do(req)
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return err
}
if resp.StatusCode != 200 {
msg.Text = "Error:" + "Http Respcode Error, Code " + strconv.Itoa(resp.StatusCode)
ui.Render(msg)
return errors.New("Http Respcode Error, Code" + strconv.Itoa(resp.StatusCode))
}
hasNext, _ = jsonparser.GetBoolean(body, "has_next")
lastKey, _ = jsonparser.GetString(body, "last_key")
_, err = jsonparser.ArrayEach(body, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
ProblemTS, err := jsonparser.GetInt(value, "timestamp")
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return
}
ProblemStat, err := jsonparser.GetString(value, "status_display")
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return
}
ProblemRT, err := jsonparser.GetString(value, "runtime")
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return
}
ProblemTitle, err := jsonparser.GetString(value, "title")
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return
}
ProblemMem, err := jsonparser.GetString(value, "memory")
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return
}
prob := problemMap[ProblemTitle]
prob.Memory = ProblemMem
prob.RunTime = ProblemRT
prob.TimeStamp = ProblemTS
prob.State = ProblemStat
LastTimeStamp = ProblemTS
curSubmissionData = append(curSubmissionData, prob)
}, "submissions_dump")
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return err
}
}
msg.Text = msgDefault
ui.Render(msg)
return nil
}
func RefreshData() {
err := GetSubmissions()
if err != nil {
return
}
//get Today Submissions:
msg.Text = "Processing Data..."
ui.Render(msg)
todaySub := make([]Problem, 0)
twoBSub := make([]Problem, 0)
threeBSub := make([]Problem, 0)
fourBSub := make([]Problem, 0)
fiveBSub := make([]Problem, 0)
sixBSub := make([]Problem, 0)
sevenBSub := make([]Problem, 0)
CurTime := time.Now()
var TodayTargetTimeStamp int64
if configuration.StartTimeOfTheNewDay < CurTime.Hour() {
//set today's start time of day
TodayTargetTimeStamp = CurTime.Add(-time.Hour/2).Round(time.Hour).Unix() - int64(CurTime.Hour()-configuration.StartTimeOfTheNewDay)*3600
} else {
//set yesterday's start time of day
TodayTargetTimeStamp = CurTime.Add(-time.Hour/2).Round(time.Hour).Add(-time.Hour*24).Unix() - int64(CurTime.Hour()-configuration.StartTimeOfTheNewDay)*3600
}
for _, data := range curSubmissionData {
if data.TimeStamp >= TodayTargetTimeStamp {
todaySub = append(todaySub, data)
} else if data.TimeStamp < TodayTargetTimeStamp && data.TimeStamp >= TodayTargetTimeStamp-86400 {
//2 day ago
twoBSub = append(twoBSub, data)
} else if data.TimeStamp < TodayTargetTimeStamp-86400 && data.TimeStamp >= TodayTargetTimeStamp-86400*2 {
//3 day ago
threeBSub = append(threeBSub, data)
} else if data.TimeStamp < TodayTargetTimeStamp-86400*2 && data.TimeStamp >= TodayTargetTimeStamp-86400*3 {
//4 day ago
fourBSub = append(fourBSub, data)
} else if data.TimeStamp < TodayTargetTimeStamp-86400*3 && data.TimeStamp >= TodayTargetTimeStamp-86400*4 {
//5 day ago
fiveBSub = append(fiveBSub, data)
} else if data.TimeStamp < TodayTargetTimeStamp-86400*4 && data.TimeStamp >= TodayTargetTimeStamp-86400*5 {
//6 day ago
sixBSub = append(sixBSub, data)
} else if data.TimeStamp < TodayTargetTimeStamp-86400*5 && data.TimeStamp >= TodayTargetTimeStamp-86400*6 {
//7 day ago
sevenBSub = append(sevenBSub, data)
}
}
//submission List
uiSubtodayData := make([]string, len(todaySub))
for i, subTodayData := range todaySub {
uiSubtodayData[i] = "[" + strconv.FormatInt(subTodayData.ID, 10) + "] [" + subTodayData.State + "-" + subTodayData.Title + "]"
if subTodayData.State == "Accepted" {
uiSubtodayData[i] = uiSubtodayData[i] + "(fg:green)"
} else {
uiSubtodayData[i] = uiSubtodayData[i] + "(fg:red)"
}
}
subl.Title = "Submissions(" + strconv.Itoa(len(todaySub)) + ")"
subl.Rows = uiSubtodayData
ui.Render(subl)
//AC List
easyNum := make(map[int64]bool)
mediumNum := make(map[int64]bool)
hardNum := make(map[int64]bool)
//===
uiActodayData := make([]string, 0)
for _, acTodayData := range todaySub {
if acTodayData.State == "Accepted" {
var tmp string
tmp = "[" + strconv.FormatInt(acTodayData.ID, 10) + "] [" + acTodayData.RunTime + "-" + acTodayData.Memory + "-" + acTodayData.Title + "]"
if acTodayData.Level == 1 {
tmp += "(fg:green)"
easyNum[acTodayData.ID] = true
} else if acTodayData.Level == 2 {
tmp += "(fg:yellow)"
mediumNum[acTodayData.ID] = true
} else {
tmp += "(fg:red)"
hardNum[acTodayData.ID] = true
}
uiActodayData = append(uiActodayData, tmp)
}
}
acl.Title = "Accepted(" + strconv.Itoa(len(uiActodayData)) + ")"
acl.Rows = uiActodayData
ui.Render(acl)
//Solved Today
easy := len(easyNum)
medium := len(mediumNum)
hard := len(hardNum)
todaybc.Data = []float64{float64(easy), float64(medium), float64(hard)}
ui.Render(todaybc)
//=======progress bar
percent := (easy*configuration.EasyScore + medium*configuration.MediumScore + hard*configuration.HardScore) * 100 / configuration.DailyGoal
if percent > 100 {
percent = 100
}
progress.Percent = percent
ui.Render(progress)
//===data processing
//=======weekly sub
weeklabel := []string{CurTime.Add(-24 * time.Hour * 6).Weekday().String(), CurTime.Add(-24 * time.Hour * 5).Weekday().String(), CurTime.Add(-24 * time.Hour * 4).Weekday().String(), CurTime.Add(-24 * time.Hour * 3).Weekday().String(), CurTime.Add(-24 * time.Hour * 2).Weekday().String(), CurTime.Add(-24 * time.Hour * 1).Weekday().String(), CurTime.Weekday().String()}
weekSubsbc.Labels = weeklabel
weekACsbc.Labels = weeklabel
weekSubsbc.Data = make([][]float64, 7)
weekACsbc.Data = make([][]float64, 7)
total, ea, me, ha := getSolvedData(todaySub)
weekSubsbc.Data[6] = []float64{float64(total), float64(len(todaySub) - total)}
weekACsbc.Data[6] = []float64{float64(ea), float64(me), float64(ha)}
total, ea, me, ha = getSolvedData(twoBSub)
weekSubsbc.Data[5] = []float64{float64(total), float64(len(twoBSub) - total)}
weekACsbc.Data[5] = []float64{float64(ea), float64(me), float64(ha)}
total, ea, me, ha = getSolvedData(threeBSub)
weekSubsbc.Data[4] = []float64{float64(total), float64(len(threeBSub) - total)}
weekACsbc.Data[4] = []float64{float64(ea), float64(me), float64(ha)}
total, ea, me, ha = getSolvedData(fourBSub)
weekSubsbc.Data[3] = []float64{float64(total), float64(len(fourBSub) - total)}
weekACsbc.Data[3] = []float64{float64(ea), float64(me), float64(ha)}
total, ea, me, ha = getSolvedData(fiveBSub)
weekSubsbc.Data[2] = []float64{float64(total), float64(len(fiveBSub) - total)}
weekACsbc.Data[2] = []float64{float64(ea), float64(me), float64(ha)}
total, ea, me, ha = getSolvedData(sixBSub)
weekSubsbc.Data[1] = []float64{float64(total), float64(len(sixBSub) - total)}
weekACsbc.Data[1] = []float64{float64(ea), float64(me), float64(ha)}
total, ea, me, ha = getSolvedData(sevenBSub)
weekSubsbc.Data[0] = []float64{float64(total), float64(len(sevenBSub) - total)}
weekACsbc.Data[0] = []float64{float64(ea), float64(me), float64(ha)}
//=========
ui.Render(weekSubsbc, weekACsbc)
msg.Text = msgDefault
ui.Render(msg)
}
//Return TotalAC, ACEasy ACmedium ACHard
func getSolvedData(todaySub []Problem) (int, int, int, int) {
//AC List
total := 0
easy := 0
medium := 0
hard := 0
//===
for _, acTodayData := range todaySub {
if acTodayData.State == "Accepted" {
total++
if acTodayData.Level == 1 {
easy++
} else if acTodayData.Level == 2 {
medium++
} else {
hard++
}
}
}
return total, easy, medium, hard
}
func GetAllProblems() {
msg.Text = "Loading Problems...."
ui.Render(msg)
requestURL := "https://leetcode.com/api/problems/all/"
//println(requestURL)
client := &http.Client{}
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
log.Fatalln(err)
return
}
resp, err := client.Do(req)
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return
}
if resp.StatusCode != 200 {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return
}
_, err = jsonparser.ArrayEach(body, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
ProblemID, err := jsonparser.GetInt(value, "stat", "question_id")
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return
}
DiffLevel, err := jsonparser.GetInt(value, "difficulty", "level")
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return
}
ProblemTitle, err := jsonparser.GetString(value, "stat", "question__title")
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return
}
problemMap[ProblemTitle] = Problem{
Title: ProblemTitle,
ID: ProblemID,
Level: DiffLevel,
}
}, "stat_status_pairs")
if err != nil {
msg.Text = "Error:" + err.Error()
ui.Render(msg)
return
}
msg.Text = msgDefault
ui.Render(msg)
return
}
func RefreshGrid() {
//======
grid := ui.NewGrid()
termWidth, termHeight := ui.TerminalDimensions()
grid.SetRect(0, 0, termWidth, termHeight)
grid.Set(
ui.NewRow(1.0/10,
ui.NewCol(3.0/5, msg),
ui.NewCol(2.0/5, clock),
),
ui.NewRow(2.0/5,
ui.NewCol(2.0/5, subl),
ui.NewCol(2.0/5, acl),
ui.NewCol(1.0/5, todaybc),
),
ui.NewRow(2.0/5,
ui.NewCol(1.0/2, weekSubsbc),
ui.NewCol(1.0/2, weekACsbc),
),
ui.NewRow(1.0/10, progress),
)
ui.Render(grid)
}
func InitUI() {
//Message
msg.Title = "Message"
msg.Text = msgDefault
//Clock
clock.Title = "Current Time"
subl.Title = "Submissions(0)"
acl.Title = "Accepted(0)"
//Today Solved Unique Questions:
todaybc.Title = "Solved Today"
todaybc.Labels = []string{"Easy", "Medium", "Hard"}
todaybc.BarWidth = 6
todaybc.BarGap = 2
todaybc.LabelStyles[0] = ui.NewStyle(ui.ColorGreen)
todaybc.BarColors[0] = ui.ColorGreen
todaybc.NumStyles[0] = ui.NewStyle(ui.ColorRed)
todaybc.BarColors[1] = ui.ColorYellow
todaybc.LabelStyles[1] = ui.NewStyle(ui.ColorYellow)
todaybc.NumStyles[1] = ui.NewStyle(ui.ColorRed)
todaybc.BarColors[2] = ui.ColorRed
todaybc.LabelStyles[2] = ui.NewStyle(ui.ColorRed)
todaybc.NumStyles[2] = ui.NewStyle(ui.ColorRed)
//week submission stacked barchart stack:(AC/TLE/wrong Answer/Runtime Error/Compile Error/MemoryLimitExceded)
weekSubsbc.Title = "Submissions in last 7 days (AC/Error)"
weekSubsbc.BarColors = []ui.Color{ui.ColorGreen, ui.ColorRed}
weekSubsbc.NumStyles = []ui.Style{ui.NewStyle(ui.ColorBlue)}
weekSubsbc.BarWidth = 7
weekSubsbc.BarGap = 2
//week AC stacked barchart stack:(easy/medium/hard)
weekACsbc.Title = "Accepted in last 7 days (Easy Medium Hard)"
weekACsbc.BarWidth = 7
weekACsbc.BarGap = 2
//Today target progress
progress.Title = "Today's Progress"
progress.Percent = 0
progress.BarColor = ui.ColorGreen
progress.BorderStyle.Fg = ui.ColorWhite
progress.TitleStyle.Fg = ui.ColorCyan
RefreshGrid()
}