-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
219 lines (210 loc) Β· 5.31 KB
/
Copy pathquery.go
File metadata and controls
219 lines (210 loc) Β· 5.31 KB
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
package main
import (
"fmt"
"strconv"
"strings"
"time"
)
func parseDuration(s string) int64 {
s = strings.ToLower(strings.TrimSpace(s))
if s == "" {
return 24 * 3600
}
units := map[byte]int64{'s': 1, 'm': 60, 'h': 3600, 'd': 86400, 'w': 604800}
last := s[len(s)-1]
if u, ok := units[last]; ok {
if v, err := strconv.ParseFloat(s[:len(s)-1], 64); err == nil {
return int64(v * float64(u))
}
}
if v, err := strconv.ParseFloat(s, 64); err == nil {
return int64(v * 60)
}
return 24 * 3600
}
func prettyDur(sec int64) string {
for _, u := range []struct {
s string
n int64
}{{"d", 86400}, {"h", 3600}, {"m", 60}} {
if sec >= u.n && sec%u.n == 0 {
return fmt.Sprintf("%d%s", sec/u.n, u.s)
}
}
return fmt.Sprintf("%ds", sec)
}
// extractFlag pulls "--name value" or "--name=value" out of args (any position).
func extractFlag(args []string, name string) (string, []string) {
var rest []string
val := ""
for i := 0; i < len(args); i++ {
if args[i] == name && i+1 < len(args) {
val = args[i+1]
i++
continue
}
if strings.HasPrefix(args[i], name+"=") {
val = args[i][len(name)+1:]
continue
}
rest = append(rest, args[i])
}
return val, rest
}
func cmdStats() {
rows := DailySummary()
if len(rows) == 0 {
fmt.Println("No usage logged yet. Run 'datasip' first.")
return
}
fmt.Println(title("daily usage (this Mac)"))
var td, tu int64
for _, r := range rows {
td += r.Down
tu += r.Up
fmt.Printf(" %s %s %s %s\n", r.Day,
green.Render("β"+human(float64(r.Down))), magenta.Render("β"+human(float64(r.Up))),
human(float64(r.Down+r.Up)))
}
fmt.Printf(" %-10s β%-10s β%-10s %s\n", "TOTAL", human(float64(td)), human(float64(tu)), human(float64(td+tu)))
cfg := LoadConfig()
if cfg.AllowanceGB > 0 {
d, u := CycleUsage(cfg.CycleDay)
total := cfg.AllowanceGB * 1024 * 1024 * 1024
pct := float64(d+u) / total * 100
fmt.Printf("\nThis cycle (since day %d): %s of %.0f GB %.0f%%\n",
cfg.CycleDay, human(float64(d+u)), cfg.AllowanceGB, pct)
}
}
func cmdApps(args []string) {
since, rest := extractFlag(args, "--since")
topStr, _ := extractFlag(rest, "--top")
if since == "" {
since = "24h"
}
top := 12
if n, err := strconv.Atoi(topStr); err == nil {
top = n
}
sec := parseDuration(since)
rows := AppSummary(sec)
if len(rows) == 0 {
fmt.Printf("No per-app data in the last %s.\n", prettyDur(sec))
return
}
fmt.Println(title(fmt.Sprintf("top apps β last %s", prettyDur(sec))))
for i, r := range rows {
if i >= top {
break
}
nm := r.App
if len(nm) > 28 {
nm = nm[:28]
}
fmt.Printf(" %-28s %s %s %s\n", nm,
green.Render("β"+human(float64(r.Down))), magenta.Render("β"+human(float64(r.Up))),
title(human(float64(r.Down+r.Up))))
}
}
func cmdApp(args []string) {
since, rest := extractFlag(args, "--since")
if since == "" {
since = "24h"
}
if len(rest) == 0 {
fmt.Println("usage: datasip app <name> --since 30m")
return
}
query := strings.ToLower(strings.Join(rest, " "))
sec := parseDuration(since)
rows := AppSummary(sec)
var td, tu int64
var matched []appRow
for _, r := range rows {
if strings.Contains(strings.ToLower(r.App), query) {
td += r.Down
tu += r.Up
matched = append(matched, r)
}
}
if len(matched) == 0 {
fmt.Printf("No traffic for '%s' in the last %s.\n", strings.Join(rest, " "), prettyDur(sec))
return
}
fmt.Printf("%s β last %s: %s %s total %s\n",
title(strings.Join(rest, " ")), prettyDur(sec),
green.Render("β"+human(float64(td))), magenta.Render("β"+human(float64(tu))),
title(human(float64(td+tu))))
if len(matched) > 1 {
for _, r := range matched {
nm := r.App
if len(nm) > 30 {
nm = nm[:30]
}
fmt.Printf(" %-30s %s\n", nm, human(float64(r.Down+r.Up)))
}
}
}
func cmdSpikes(args []string) {
since, _ := extractFlag(args, "--since")
if since == "" {
since = "24h"
}
sec := parseDuration(since)
evs := RecentSpikes(sec)
if len(evs) == 0 {
fmt.Printf("No spikes in the last %s.\n", prettyDur(sec))
return
}
fmt.Println(title(fmt.Sprintf("spikes β last %s", prettyDur(sec))))
for i := len(evs) - 1; i >= 0; i-- {
e := evs[i]
ts := time.Unix(e.T, 0).Format("Jan 02 15:04:05")
fmt.Printf(" %s %-26s %s\n", ts, e.App, red.Render(human(float64(e.Down+e.Up))+"/s"))
}
}
func cmdConfig(args []string) {
cfg := LoadConfig()
changed := false
if v, r := extractFlag(args, "--allowance"); v != "" {
if f, e := strconv.ParseFloat(v, 64); e == nil {
cfg.AllowanceGB = f
changed = true
}
args = r
}
if v, r := extractFlag(args, "--cycle-day"); v != "" {
if n, e := strconv.Atoi(v); e == nil {
if n < 1 {
n = 1
}
if n > 28 {
n = 28
}
cfg.CycleDay = n
changed = true
}
args = r
}
if v, r := extractFlag(args, "--spike-floor"); v != "" {
if f, e := strconv.ParseFloat(v, 64); e == nil {
cfg.SpikeFloorKBps = f
changed = true
}
args = r
}
if v, _ := extractFlag(args, "--spike-mult"); v != "" {
if f, e := strconv.ParseFloat(v, 64); e == nil {
cfg.SpikeMult = f
changed = true
}
}
if changed {
SaveConfig(cfg)
}
fmt.Println(title("datasip config"))
fmt.Printf(" allowance : %.0f GB\n", cfg.AllowanceGB)
fmt.Printf(" cycle day : %d\n", cfg.CycleDay)
fmt.Printf(" spike floor : %.0f KB/s\n", cfg.SpikeFloorKBps)
fmt.Printf(" spike multiple : %.1fx baseline\n", cfg.SpikeMult)
}