initial commit [141.14.140.180,mike]
This commit is contained in:
commit
e34ba75b7b
33 files changed
+6267
No files matched your search
@@ -0,0 +1,244 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
"nscan/internal/store"
|
||||
)
|
||||
|
||||
// histState backs the timeline view: a ribbon with one bar per hour (or day)
|
||||
// whose height shows how much changed on the network.
|
||||
type histState struct {
|
||||
daily bool
|
||||
cursor int // selected bucket, counted from the newest (0)
|
||||
events []store.Event
|
||||
}
|
||||
|
||||
type bucket struct {
|
||||
start, end time.Time
|
||||
scans int
|
||||
news, joins, leaves int
|
||||
events []store.Event
|
||||
}
|
||||
|
||||
func (b *bucket) changes() int { return b.news + b.joins + b.leaves }
|
||||
|
||||
func (m *model) loadHistory() {
|
||||
m.hist.events = m.cfg.Store.Events(time.Now().Add(-store.Retention))
|
||||
}
|
||||
|
||||
func (m *model) handleHistoryKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
n, _ := m.bucketLayout()
|
||||
switch msg.String() {
|
||||
case "esc", "q", "h":
|
||||
m.mode = viewList
|
||||
case "left":
|
||||
m.hist.cursor = min(n-1, m.hist.cursor+1)
|
||||
case "right":
|
||||
m.hist.cursor = max(0, m.hist.cursor-1)
|
||||
case "home":
|
||||
m.hist.cursor = n - 1
|
||||
case "end":
|
||||
m.hist.cursor = 0
|
||||
case "z", "t":
|
||||
m.hist.daily = !m.hist.daily
|
||||
m.hist.cursor = 0
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// bucketLayout returns how many buckets fit and how wide each is drawn.
|
||||
func (m *model) bucketLayout() (n, width int) {
|
||||
n = 48
|
||||
if m.hist.daily {
|
||||
n = 30
|
||||
}
|
||||
avail := max(10, m.w-4)
|
||||
width = max(1, min(3, avail/n))
|
||||
return min(n, avail/width), width
|
||||
}
|
||||
|
||||
func (m *model) buckets(n int, now time.Time) []bucket {
|
||||
step := time.Hour
|
||||
end := now.Truncate(time.Hour).Add(time.Hour)
|
||||
if m.hist.daily {
|
||||
step = 24 * time.Hour
|
||||
y, mo, d := now.Date()
|
||||
end = time.Date(y, mo, d+1, 0, 0, 0, 0, now.Location())
|
||||
}
|
||||
bs := make([]bucket, n)
|
||||
for i := range bs {
|
||||
// AddDate keeps days aligned to local midnight across DST changes.
|
||||
var start time.Time
|
||||
if m.hist.daily {
|
||||
start = end.AddDate(0, 0, -(n - i))
|
||||
bs[i] = bucket{start: start, end: start.AddDate(0, 0, 1)}
|
||||
} else {
|
||||
start = end.Add(-time.Duration(n-i) * step)
|
||||
bs[i] = bucket{start: start, end: start.Add(step)}
|
||||
}
|
||||
}
|
||||
// Events are sorted by time, so buckets and events advance together.
|
||||
first, i := bs[0].start, 0
|
||||
for _, e := range m.hist.events {
|
||||
if e.Time.Before(first) {
|
||||
continue
|
||||
}
|
||||
if !e.Time.Before(end) {
|
||||
break
|
||||
}
|
||||
for !e.Time.Before(bs[i].end) {
|
||||
i++
|
||||
}
|
||||
b := &bs[i]
|
||||
switch e.Kind {
|
||||
case store.EventScan:
|
||||
b.scans++
|
||||
case store.EventNew:
|
||||
b.news++
|
||||
case store.EventJoin:
|
||||
b.joins++
|
||||
case store.EventLeave:
|
||||
b.leaves++
|
||||
}
|
||||
if e.Kind != store.EventScan {
|
||||
b.events = append(b.events, e)
|
||||
}
|
||||
}
|
||||
return bs
|
||||
}
|
||||
|
||||
var (
|
||||
styleArrive = fg(mwxGreen)
|
||||
styleDepart = fg(mwxRed)
|
||||
styleMixed = fg(mwxBlue)
|
||||
styleHatch = fg(mwxDarker)
|
||||
)
|
||||
|
||||
func (m *model) viewHistory() string {
|
||||
now := time.Now()
|
||||
n, bw := m.bucketLayout()
|
||||
bs := m.buckets(n, now)
|
||||
sel := n - 1 - min(m.hist.cursor, n-1)
|
||||
|
||||
unit := fmt.Sprintf("hourly, last %d hours", n)
|
||||
if m.hist.daily {
|
||||
unit = fmt.Sprintf("daily, last %d days", n)
|
||||
}
|
||||
lines := []string{m.titleBar(""), "", " " + styleBold.Render("History") + styleDim.Render(" "+unit), ""}
|
||||
|
||||
maxChanges := 1
|
||||
for i := range bs {
|
||||
maxChanges = max(maxChanges, bs[i].changes())
|
||||
}
|
||||
ribbonRows := max(2, min(6, m.h-14))
|
||||
levels := ribbonRows * 8
|
||||
blocks := []rune(" ▁▂▃▄▅▆▇█")
|
||||
for row := ribbonRows - 1; row >= 0; row-- {
|
||||
var b strings.Builder
|
||||
b.WriteString(" ")
|
||||
for i := range bs {
|
||||
bk := &bs[i]
|
||||
var cell string
|
||||
if bk.scans == 0 {
|
||||
cell = styleHatch.Render(strings.Repeat("░", bw))
|
||||
} else {
|
||||
height := 0
|
||||
if c := bk.changes(); c > 0 {
|
||||
height = max(1, c*levels/maxChanges)
|
||||
}
|
||||
fill := max(0, min(8, height-row*8))
|
||||
style := styleMixed
|
||||
switch {
|
||||
case bk.news+bk.joins > bk.leaves:
|
||||
style = styleArrive
|
||||
case bk.leaves > bk.news+bk.joins:
|
||||
style = styleDepart
|
||||
}
|
||||
if i == sel {
|
||||
style = onSel(style)
|
||||
}
|
||||
cell = style.Render(strings.Repeat(string(blocks[fill]), bw))
|
||||
}
|
||||
b.WriteString(cell)
|
||||
}
|
||||
lines = append(lines, b.String())
|
||||
}
|
||||
|
||||
// Axis with labels every 6 hours or every Monday.
|
||||
axis := []rune(strings.Repeat("─", n*bw))
|
||||
labels := []rune(strings.Repeat(" ", n*bw+8))
|
||||
for i := range bs {
|
||||
var label string
|
||||
if m.hist.daily {
|
||||
if bs[i].start.Weekday() == time.Monday {
|
||||
label = bs[i].start.Format("Jan 2")
|
||||
}
|
||||
} else if bs[i].start.Hour()%6 == 0 {
|
||||
label = bs[i].start.Format("15:04")
|
||||
}
|
||||
if label == "" {
|
||||
continue
|
||||
}
|
||||
pos := i * bw
|
||||
axis[pos] = '┴'
|
||||
if free(labels, pos, len(label)+1) {
|
||||
copy(labels[pos:], []rune(label))
|
||||
}
|
||||
}
|
||||
marker := []rune(strings.Repeat(" ", n*bw))
|
||||
for j := 0; j < bw; j++ {
|
||||
marker[sel*bw+j] = '▲'
|
||||
}
|
||||
lines = append(lines, " "+styleRule.Render(string(axis)), " "+styleDim.Render(strings.TrimRight(string(labels), " ")), " "+styleAccent.Render(string(marker)), "")
|
||||
|
||||
b := bs[sel]
|
||||
span := b.start.Format("Mon Jan 2 15:04") + "–" + b.end.Format("15:04")
|
||||
if m.hist.daily {
|
||||
span = b.start.Format("Mon Jan 2 2006")
|
||||
}
|
||||
summary := " " + styleBold.Render(span)
|
||||
if b.scans == 0 {
|
||||
summary += styleMuted.Render(" · not monitored")
|
||||
} else {
|
||||
summary += styleDim.Render(fmt.Sprintf(" · %d scans · ", b.scans)) + fmt.Sprintf("%s · %s · %s",
|
||||
styleNew.Render(fmt.Sprintf("%d new", b.news)),
|
||||
styleArrive.Render(fmt.Sprintf("%d back", b.joins)),
|
||||
styleDepart.Render(fmt.Sprintf("%d gone", b.leaves)))
|
||||
}
|
||||
lines = append(lines, summary, "")
|
||||
|
||||
avail := max(0, m.h-len(lines)-3)
|
||||
if len(b.events) == 0 && b.scans > 0 {
|
||||
lines = append(lines, styleMuted.Render(" no changes"))
|
||||
}
|
||||
for i := len(b.events) - 1; i >= 0 && len(b.events)-1-i < avail; i-- {
|
||||
e := b.events[i]
|
||||
line := " " + renderEvent(e, false)
|
||||
if e.IP != "" && e.Label != e.IP {
|
||||
line += styleMuted.Render(" " + e.IP)
|
||||
}
|
||||
lines = append(lines, styleTime.Render(" "+e.Time.Format("15:04"))+line)
|
||||
}
|
||||
lines = append(padLines(lines, m.h-2),
|
||||
styleDim.Render(" ")+styleArrive.Render("█")+styleDim.Render(" more arrivals ")+styleDepart.Render("█")+
|
||||
styleDim.Render(" more departures ")+styleMixed.Render("█")+styleDim.Render(" balanced ")+
|
||||
styleHatch.Render("░")+styleDim.Render(" not monitored · height = number of changes"))
|
||||
return m.screen(lines, m.footer("←/→ select", "z hours/days", "esc back"))
|
||||
}
|
||||
|
||||
func free(line []rune, pos, n int) bool {
|
||||
if pos+n > len(line) {
|
||||
return false
|
||||
}
|
||||
for _, r := range line[pos : pos+n] {
|
||||
if r != ' ' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in new issue
Block a user