Skip to content

Commit 2546d02

Browse files
committed
Remove duplicate pprof flags, use more precise "time ago" func
1 parent b98316d commit 2546d02

3 files changed

Lines changed: 46 additions & 45 deletions

File tree

cmd/run.go

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"os"
77
"os/signal"
88
"runtime"
9-
"runtime/pprof"
109
"syscall"
1110

1211
"github.com/spf13/cobra"
12+
"github.com/spf13/pflag"
1313
"github.com/taoky/ayano/pkg/analyze"
1414
"github.com/taoky/ayano/pkg/fileiter"
1515
"github.com/taoky/ayano/pkg/systemd"
@@ -123,49 +123,24 @@ func runCmd() *cobra.Command {
123123
return cmd
124124
}
125125

126+
func normalizeAnalyzeFlags(f *pflag.FlagSet, name string) pflag.NormalizedName {
127+
switch name {
128+
case "cpuprofile":
129+
name = "cpuprof"
130+
case "memprofile":
131+
name = "memprof"
132+
}
133+
return pflag.NormalizedName(name)
134+
}
135+
126136
// First, create a helper function to handle common command configuration
127137
func setupAnalyzeCommand(cmd *cobra.Command, cmdType string) (analyze.AnalyzerConfig, error) {
128138
config := analyze.DefaultConfig()
129139
config.InstallFlags(cmd.Flags(), cmd.Name())
130-
131-
var cpuProf string
132-
var memProf string
133-
cmd.Flags().StringVar(&cpuProf, "cpuprof", "", "write CPU pprof data to file")
134-
cmd.Flags().StringVar(&memProf, "memprof", "", "write memory pprof data to file")
135-
136-
// Return a function to handle performance profiling logic
137-
handleProf := func() error {
138-
if cpuProf != "" {
139-
f, err := os.Create(cpuProf)
140-
if err != nil {
141-
return fmt.Errorf("failed to create CPU pprof file: %w", err)
142-
}
143-
defer f.Close()
144-
if err := pprof.StartCPUProfile(f); err != nil {
145-
return fmt.Errorf("failed to start CPU pprof: %w", err)
146-
}
147-
defer pprof.StopCPUProfile()
148-
}
149-
150-
if memProf != "" {
151-
f, err := os.Create(memProf)
152-
if err != nil {
153-
return fmt.Errorf("failed to create memory pprof file: %w", err)
154-
}
155-
defer f.Close()
156-
if err := pprof.WriteHeapProfile(f); err != nil {
157-
return fmt.Errorf("failed to write memory pprof: %w", err)
158-
}
159-
}
160-
return nil
161-
}
140+
cmd.Flags().SetNormalizeFunc(normalizeAnalyzeFlags)
162141

163142
// Set the command execution function
164143
cmd.RunE = func(cmd *cobra.Command, args []string) error {
165-
if err := handleProf(); err != nil {
166-
return err
167-
}
168-
169144
switch cmdType {
170145
case "analyze":
171146
config.Analyze = true
@@ -192,8 +167,9 @@ func analyzeCmd() *cobra.Command {
192167

193168
func dirAnalyzeCmd() *cobra.Command {
194169
cmd := &cobra.Command{
195-
Use: "dir-analyze [filename...]",
196-
Short: "Analyze log by directory (show statistics for each first-level directory)",
170+
Use: "dir-analyze [filename...]",
171+
Aliases: []string{"dir-analyse"},
172+
Short: "Analyze log by directory (show statistics for each first-level directory)",
197173
}
198174
setupAnalyzeCommand(cmd, "dir-analyze")
199175
return cmd

pkg/analyze/analyze.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,8 @@ func (c *AnalyzerConfig) InstallFlags(flags *pflag.FlagSet, cmdname string) {
185185
flags.BoolVar(&c.Truncate, "truncate", c.Truncate, "Truncate long URLs from output")
186186
flags.IntVar(&c.Truncate2, "truncate-to", c.Truncate2, "Truncate URLs to given length, overrides --truncate")
187187

188-
flags.StringVar(&c.CpuProfile, "cpuprofile", c.CpuProfile, "Write CPU profiling information")
189-
flags.StringVar(&c.MemProfile, "memprofile", c.MemProfile, "Write memory profiling information")
190-
flags.MarkHidden("cpuprofile")
191-
flags.MarkHidden("memprofile")
188+
flags.StringVar(&c.CpuProfile, "cpuprof", c.CpuProfile, "Write CPU profiling information")
189+
flags.StringVar(&c.MemProfile, "memprof", c.MemProfile, "Write memory profiling information")
192190

193191
if cmdname == "analyze" {
194192
c.Whole = true
@@ -671,6 +669,7 @@ func (a *Analyzer) PrintTopValues(displayRecord map[netip.Prefix]time.Time, sort
671669
table.SetColumnAlignment(tAlignment)
672670
table.SetHeader(tHeaders)
673671

672+
now := time.Now()
674673
for i := range top {
675674
key := keys[i]
676675
ipStats := a.stats[key]
@@ -689,8 +688,8 @@ func (a *Analyzer) PrintTopValues(displayRecord map[netip.Prefix]time.Time, sort
689688
lastUpdateTime = ipStats.LastURLUpdate.Format(TimeFormat)
690689
lastAccessTime = ipStats.LastURLAccess.Format(TimeFormat)
691690
} else {
692-
lastUpdateTime = humanize.Time(ipStats.LastURLUpdate)
693-
lastAccessTime = humanize.Time(ipStats.LastURLAccess)
691+
lastUpdateTime = HumanizeAgo(now.Sub(ipStats.LastURLUpdate))
692+
lastAccessTime = HumanizeAgo(now.Sub(ipStats.LastURLAccess))
694693
}
695694

696695
average := total / uint64(reqTotal)

pkg/analyze/util.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"slices"
99
"strconv"
1010
"strings"
11+
"time"
1112

1213
"github.com/dustin/go-humanize"
1314
)
@@ -202,3 +203,28 @@ func TruncateFilenameLen(input string, target int) string {
202203
// truncating basename alone would not suffice, keep characters from end
203204
return input[len(input)-target:]
204205
}
206+
207+
func HumanizeAgo(d time.Duration) string {
208+
if d < 0 {
209+
return "in the future"
210+
}
211+
if d < time.Second {
212+
return "now"
213+
}
214+
if d < time.Minute {
215+
return fmt.Sprintf("%ds ago", int(d.Seconds()))
216+
}
217+
if d < time.Hour {
218+
minutes := int(d.Minutes())
219+
seconds := int(d.Seconds()) - minutes*60
220+
return fmt.Sprintf("%dm%2ds ago", minutes, seconds)
221+
}
222+
if d < 24*time.Hour {
223+
hours := int(d.Hours())
224+
minutes := int(d.Minutes()) - hours*60
225+
return fmt.Sprintf("%dh%2dm ago", hours, minutes)
226+
}
227+
days := int(d.Hours()) / 24
228+
hours := int(d.Hours()) - days*24
229+
return fmt.Sprintf("%dd%2dh ago", days, hours)
230+
}

0 commit comments

Comments
 (0)