summaryrefslogtreecommitdiff
path: root/windowEvents.go
blob: e13df5b8b13029ff6f3503a1c9c27630916c48ba (plain)
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
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"strings"

	"go.wit.com/gui"
	"go.wit.com/lib/gadgets"
	"go.wit.com/log"
)

func makeEventsWin() {
	me.eventswin = new(stdEventTableWin)
	me.eventswin.win = gadgets.NewGenericWindow("Gus current event log", "who is squirreling around?")
	me.eventswin.win.Custom = func() {
		log.Info("test delete window here")
	}
	grid := me.eventswin.win.Group.RawGrid()

	grid.NewButton("first 20", func() {
		var count int
		found := NewEvents()
		all := me.events.All()
		for all.Scan() {
			e := all.Next()
			count += 1
			found.Append(e)
			if count > 20 {
				break
			}
		}
		doEventsTable(found)
	})

	grid.NewButton("last 20", func() {
		var count int
		total := me.events.Len()
		found := NewEvents()
		all := me.events.All()
		for all.Scan() {
			e := all.Next()
			count += 1
			if count < total-20 {
				continue
			}
			found.Append(e)
		}
		doEventsTable(found)
	})

	grid.NewButton("abnormal", func() {
		found := NewEvents()
		all := me.events.All()
		for all.Scan() {
			e := all.Next()
			if strings.HasPrefix(e.Address, "192.168") {
				continue
			}
			found.Append(e)
		}
		doEventsTable(found)
	})

	grid.NewButton("Save Current", func() {
		log.Info("event log is len =", me.events.Len())
		me.events.Save()
	})

	// make a box at the bottom of the window for the protobuf table
	me.eventswin.box = me.eventswin.win.Bottom.Box().SetProgName("TBOX")
	// doEventsTable(me.events)
}

func doEventsTable(currentEvents *Events) {
	me.eventswin.Lock()
	defer me.eventswin.Unlock()
	if me.eventswin.TB != nil {
		me.eventswin.TB.Delete()
		me.eventswin.TB = nil
	}

	// display the protobuf
	me.eventswin.TB = AddEventsPB(me.eventswin.box, currentEvents)
	f := func(e *Event) {
		log.Info("Triggered. do something here", e.Hostname)
		// m.Enabled = true
	}
	me.eventswin.TB.Custom(f)
	// log.Info("table has uuid", me.eventswin.TB.GetUuid())
}

func AddEventsPB(tbox *gui.Node, pb *Events) *EventsTable {
	t := pb.NewTable("EventsPB")
	t.NewUuid()
	t.SetParent(tbox)

	ctimef := func(e *Event) string {
		ctime := e.Ctime.AsTime()
		return ctime.Format("2006/01/02 15:04")
	}
	t.AddStringFunc("ctime", ctimef)

	etimef := func(e *Event) string {
		etime := e.Etime.AsTime()
		s := etime.Format("2006/01/02 15:04")
		if strings.HasPrefix(s, "1970/") {
			// just show a blank if it's not set
			return ""
		}
		return s
	}
	t.AddStringFunc("etime", etimef)

	t.AddHostname()
	t.AddAddress()
	t.AddWhere()
	t.AddLocalPort()
	t.ShowTable()
	return t
}