Skip to content

Commit

Permalink
Feat: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasticmao committed Nov 10, 2021
1 parent b87e31a commit 9f8c875
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 48 deletions.
16 changes: 8 additions & 8 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package handler
import "github.com/fantasticmao/nginx-json-log-analyzer/ioutil"

const (
AnalyzeTypePvUv = iota
AnalyzeTypeFieldIp
AnalyzeTypeFieldUri
AnalyzeTypeFieldUserAgent
AnalyzeTypeFieldUserCity
AnalyzeTypeResponseStatus
AnalyzeTypeTimeMeanCostUris
AnalyzeTypeTimePercentCostUris
AnalysisTypePvUv = iota
AnalysisTypeFieldIp
AnalysisTypeFieldUri
AnalysisTypeFieldUserAgent
AnalysisTypeFieldUserCity
AnalysisTypeResponseStatus
AnalysisTypeTimeMeanCostUris
AnalysisTypeTimePercentCostUris
)

type Handler interface {
Expand Down
6 changes: 3 additions & 3 deletions handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestPvAndUv(t *testing.T) {
}

func TestMostMatchFieldIp(t *testing.T) {
handler := NewMostMatchFieldHandler(AnalyzeTypeFieldIp)
handler := NewMostMatchFieldHandler(AnalysisTypeFieldIp)
handler.Input(&ioutil.LogInfo{RemoteAddr: ip1})
handler.Input(&ioutil.LogInfo{RemoteAddr: ip1})
handler.Input(&ioutil.LogInfo{RemoteAddr: ip1})
Expand All @@ -60,7 +60,7 @@ func TestMostMatchFieldIp(t *testing.T) {
}

func TestMostMatchFieldUri(t *testing.T) {
handler := NewMostMatchFieldHandler(AnalyzeTypeFieldUri)
handler := NewMostMatchFieldHandler(AnalysisTypeFieldUri)
handler.Input(&ioutil.LogInfo{Request: uri1})
handler.Input(&ioutil.LogInfo{Request: uri1})
handler.Input(&ioutil.LogInfo{Request: uri1})
Expand All @@ -75,7 +75,7 @@ func TestMostMatchFieldUri(t *testing.T) {
}

func TestMostMatchFieldUserAgent(t *testing.T) {
handler := NewMostMatchFieldHandler(AnalyzeTypeFieldUserAgent)
handler := NewMostMatchFieldHandler(AnalysisTypeFieldUserAgent)
handler.Input(&ioutil.LogInfo{HttpUserAgent: userAgent1})
handler.Input(&ioutil.LogInfo{HttpUserAgent: userAgent1})
handler.Input(&ioutil.LogInfo{HttpUserAgent: userAgent1})
Expand Down
20 changes: 10 additions & 10 deletions handler/most_match_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import (
)

type MostMatchFieldHandler struct {
analyzeType int
countMap map[string]int
analysisType int
countMap map[string]int
}

func NewMostMatchFieldHandler(analyzeType int) *MostMatchFieldHandler {
func NewMostMatchFieldHandler(analysisType int) *MostMatchFieldHandler {
return &MostMatchFieldHandler{
analyzeType: analyzeType,
countMap: make(map[string]int),
analysisType: analysisType,
countMap: make(map[string]int),
}
}

func (handler *MostMatchFieldHandler) Input(info *ioutil.LogInfo) {
var field string
switch handler.analyzeType {
case AnalyzeTypeFieldIp:
switch handler.analysisType {
case AnalysisTypeFieldIp:
field = info.RemoteAddr
case AnalyzeTypeFieldUri:
case AnalysisTypeFieldUri:
field = info.Request
case AnalyzeTypeFieldUserAgent:
case AnalysisTypeFieldUserAgent:
field = info.HttpUserAgent
default:
ioutil.Fatal("unsupported analyze type: %v\n", handler.analyzeType)
ioutil.Fatal("unsupported analysis type: %v\n", handler.analysisType)
return
}

Expand Down
54 changes: 27 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (
)

var (
logFiles []string
showVersion bool
configDir string
analyzeType int
limit int
limitSecond int
percentile float64
timeAfter string
timeBefore string
logFiles []string
showVersion bool
configDir string
analysisType int
limit int
limitSecond int
percentile float64
timeAfter string
timeBefore string
)

var (
Expand All @@ -34,12 +34,12 @@ var (
func init() {
flag.BoolVar(&showVersion, "v", false, "show current version")
flag.StringVar(&configDir, "d", "", "specify the configuration directory")
flag.IntVar(&analyzeType, "t", 0, "specify the analyze type, see documentation for more details:\nhttps://github.com/fantasticmao/nginx-json-log-analyzer#supported-statistical-indicators")
flag.IntVar(&limit, "n", 15, "limit the output number lines")
flag.IntVar(&limitSecond, "n2", 15, "limit the secondary output number lines in '-t 4' mode")
flag.IntVar(&analysisType, "t", 0, "specify the analysis type, see documentation for more details:\nhttps://github.com/fantasticmao/nginx-json-log-analyzer#specify-the-analysis-type--t")
flag.IntVar(&limit, "n", 15, "limit the output lines number")
flag.IntVar(&limitSecond, "n2", 15, "limit the secondary output lines number in '-t 4' mode")
flag.Float64Var(&percentile, "p", 95, "specify the percentile value in '-t 7' mode")
flag.StringVar(&timeAfter, "ta", "", "specify the analyze start time, in format of RFC3339 e.g. '2021-11-01T00:00:00+08:00'")
flag.StringVar(&timeBefore, "tb", "", "specify the analyze end time, in format of RFC3339 e.g. '2021-11-02T00:00:00+08:00'")
flag.StringVar(&timeAfter, "ta", "", "limit the analysis start time, in format of RFC3339 e.g. '2021-11-01T00:00:00+08:00'")
flag.StringVar(&timeBefore, "tb", "", "limit the analysis end time, in format of RFC3339 e.g. '2021-11-02T00:00:00+08:00'")
flag.Parse()
logFiles = flag.Args()
}
Expand Down Expand Up @@ -83,26 +83,26 @@ func main() {
}

func newHandler() handler.Handler {
switch analyzeType {
case handler.AnalyzeTypePvUv:
switch analysisType {
case handler.AnalysisTypePvUv:
return handler.NewPvAndUvHandler()
case handler.AnalyzeTypeFieldIp:
return handler.NewMostMatchFieldHandler(analyzeType)
case handler.AnalyzeTypeFieldUri:
return handler.NewMostMatchFieldHandler(analyzeType)
case handler.AnalyzeTypeFieldUserAgent:
return handler.NewMostMatchFieldHandler(analyzeType)
case handler.AnalyzeTypeFieldUserCity:
case handler.AnalysisTypeFieldIp:
return handler.NewMostMatchFieldHandler(analysisType)
case handler.AnalysisTypeFieldUri:
return handler.NewMostMatchFieldHandler(analysisType)
case handler.AnalysisTypeFieldUserAgent:
return handler.NewMostMatchFieldHandler(analysisType)
case handler.AnalysisTypeFieldUserCity:
const dbFile = "City.mmdb"
return handler.NewMostVisitedCities(path.Join(configDir, dbFile), limitSecond)
case handler.AnalyzeTypeResponseStatus:
case handler.AnalysisTypeResponseStatus:
return handler.NewMostFrequentStatusHandler()
case handler.AnalyzeTypeTimeMeanCostUris:
case handler.AnalysisTypeTimeMeanCostUris:
return handler.NewTopTimeMeanCostUrisHandler()
case handler.AnalyzeTypeTimePercentCostUris:
case handler.AnalysisTypeTimePercentCostUris:
return handler.NewTopTimePercentCostUrisHandler(percentile)
default:
ioutil.Fatal("unsupported analyze type: %v\n", analyzeType)
ioutil.Fatal("unsupported analysis type: %v\n", analysisType)
return nil
}
}
Expand Down

0 comments on commit 9f8c875

Please sign in to comment.