Skip to content

Commit

Permalink
Merge pull request #85 from tkuchiki/fix-count-subcommand
Browse files Browse the repository at this point in the history
Move from `alp count` to `alp (json|ltsv|regexp) count`
  • Loading branch information
tkuchiki authored Oct 1, 2023
2 parents a3edaa0 + e1bdfda commit 4e95db4
Show file tree
Hide file tree
Showing 14 changed files with 398 additions and 272 deletions.
19 changes: 15 additions & 4 deletions cmd/alp/cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,26 @@ type Command struct {
jsonDiffCmd *cobra.Command
// alp json topN
jsonTopNCmd *cobra.Command
// alp json count
jsonCountCmd *cobra.Command

// alp ltsv
ltsvCmd *cobra.Command
// alp ltsv diff
ltsvDiffCmd *cobra.Command
// alp ltsv topN
ltsvTopNCmd *cobra.Command
// alp ltsv count
ltsvCountCmd *cobra.Command

// alp regexp
regexpCmd *cobra.Command
// alp regexp diff
regexpDiffCmd *cobra.Command
// alp regexp topN
regexpTopNCmd *cobra.Command
// alp regexp count
regexpCountCmd *cobra.Command

// alp pcap
pcapCmd *cobra.Command
Expand Down Expand Up @@ -62,6 +68,9 @@ func NewCommand(version string) *Command {
// alp ltsv topN
command.ltsvTopNCmd = newLTSVTopNCmd(command.flags)
command.ltsvCmd.AddCommand(command.ltsvTopNCmd)
// alp ltsv count
command.ltsvCountCmd = newLTSVCountCmd(command.flags)
command.ltsvCmd.AddCommand(command.ltsvCountCmd)

// alp json
command.jsonCmd = newJSONCmd(command.flags)
Expand All @@ -72,6 +81,9 @@ func NewCommand(version string) *Command {
// alp json topN
command.jsonTopNCmd = newJsonTopNCmd(command.flags)
command.jsonCmd.AddCommand(command.jsonTopNCmd)
// alp json count
command.jsonCountCmd = newJsonCountCmd(command.flags)
command.jsonCmd.AddCommand(command.jsonCountCmd)

// alp regexp
command.regexpCmd = newRegexpCmd(command.flags)
Expand All @@ -82,6 +94,9 @@ func NewCommand(version string) *Command {
// alp regexp topN
command.regexpTopNCmd = newRegexpTopNCmd(command.flags)
command.regexpCmd.AddCommand(command.regexpTopNCmd)
// alp regexp count
command.regexpCountCmd = newRegexpCountCmd(command.flags)
command.regexpCmd.AddCommand(command.regexpCountCmd)

// alp pcap
command.pcapCmd = newPcapCmd(command.flags)
Expand All @@ -97,10 +112,6 @@ func NewCommand(version string) *Command {
command.diffCmd = newDiffCmd(command.flags)
command.rootCmd.AddCommand(command.diffCmd)

// alp count
command.countCmd = newCountCmd(command.flags)
command.rootCmd.AddCommand(command.countCmd)

return command
}

Expand Down
82 changes: 7 additions & 75 deletions cmd/alp/cmd/count.go
Original file line number Diff line number Diff line change
@@ -1,89 +1,21 @@
package cmd

import (
"os"

"github.com/tkuchiki/alp/options"

"github.com/spf13/cobra"
"github.com/tkuchiki/alp/counter"
"github.com/tkuchiki/alp/options"
"github.com/tkuchiki/alp/parsers"
)

func newCountCmd(flags *flags) *cobra.Command {
var countCmd = &cobra.Command{
func newCountSubCmd() *cobra.Command {
return &cobra.Command{
Use: "count",
Short: "Count by log entries",
Long: `Count by log entries`,
RunE: func(cmd *cobra.Command, args []string) error {
opts, err := flags.createCountOptions(cmd)
if err != nil {
return err
}

// TODO: start
// Remove these after implementing `alp (json|ltsv|regex) count`.
pattern, err := cmd.PersistentFlags().GetString("pattern")
if err != nil {
return err
}

format, err := cmd.PersistentFlags().GetString("format")
if err != nil {
return err
}

opts = options.SetOptions(opts,
options.Pattern(pattern),
options.Format(format),
)
// TODO: end

cnter := counter.NewCounter(os.Stdout, os.Stderr, opts.Reverse)

f, err := cnter.Open(opts.File)
if err != nil {
return err
}
defer f.Close()

var parser parsers.Parser
switch format {
case "json":
jsonKeys := parsers.NewJSONKeys(opts.JSON.UriKey, opts.JSON.MethodKey, opts.JSON.TimeKey,
opts.JSON.ResponseTimeKey, opts.JSON.RequestTimeKey, opts.JSON.BodyBytesKey, opts.JSON.StatusKey)
parser = parsers.NewJSONParser(f, jsonKeys, opts.QueryString, opts.QueryStringIgnoreValues)
case "ltsv":
label := parsers.NewLTSVLabel(opts.LTSV.UriLabel, opts.LTSV.MethodLabel, opts.LTSV.TimeLabel,
opts.LTSV.ApptimeLabel, opts.LTSV.ReqtimeLabel, opts.LTSV.SizeLabel, opts.LTSV.StatusLabel,
)
parser = parsers.NewLTSVParser(f, label, opts.QueryString, opts.QueryStringIgnoreValues)
case "regexp":
names := parsers.NewSubexpNames(opts.Regexp.UriSubexp, opts.Regexp.MethodSubexp, opts.Regexp.TimeSubexp,
opts.Regexp.ResponseTimeSubexp, opts.Regexp.RequestTimeSubexp, opts.Regexp.BodyBytesSubexp, opts.Regexp.StatusSubexp)
parser, err = parsers.NewRegexpParser(f, opts.Regexp.Pattern, names, opts.QueryString, opts.QueryStringIgnoreValues)
if err != nil {
return err
}
}

cnter.SetParser(parser)

err = cnter.CountAndPrint(opts.Count.Keys)

return err
},
}
}

flags.defineCountOptions(countCmd)

// TODO: Remove these after implementing `alp (json|ltsv|regex) count`.
countCmd.PersistentFlags().StringP("pattern", "", options.DefaultPatternOption, "Regular expressions pattern matching the log")
countCmd.PersistentFlags().StringP("format", "", "json", "Log format (json,ltsv,regexp)")

countCmd.Flags().SortFlags = false
countCmd.PersistentFlags().SortFlags = false
countCmd.InheritedFlags().SortFlags = false

return countCmd
func runCount(counter *counter.Counter, parser parsers.Parser, opts *options.Options) error {
counter.SetParser(parser)
return counter.CountAndPrint(opts.Count.Keys)
}
23 changes: 0 additions & 23 deletions cmd/alp/cmd/count_test.go

This file was deleted.

Loading

0 comments on commit 4e95db4

Please sign in to comment.