Skip to content

Commit

Permalink
add different than conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
facundoolano committed Jul 27, 2024
1 parent 513bfc4 commit 41747d9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ Count total requests to urls matching a pattern:

$ ngtop -w url=/blog/%

Count total requests to urls excluding a value or pattern:

$ ngtop -w url!=/feed.xml
$ ngtop -w url!=/feed%

Count total requests to one of mutliple urls (one OR another):

$ ngtop -w url=/blog/code-is-run-more-than-read -w url=/blog/a-note-on-essential-complexity
Expand Down
14 changes: 12 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,19 @@ func (spec *RequestCountSpec) buildQuery() (string, []any) {
for i, value := range values {
whereExpression += column
if strings.ContainsRune(value, '%') {
whereExpression += " LIKE ?"
if strings.HasPrefix(value, "!") {
value = strings.TrimPrefix(value, "!")
whereExpression += " NOT LIKE ?"
} else {
whereExpression += " LIKE ?"
}
} else {
whereExpression += " = ?"
if strings.HasPrefix(value, "!") {
value = strings.TrimPrefix(value, "!")
whereExpression += " <> ?"
} else {
whereExpression += " = ?"
}
}
queryArgs = append(queryArgs, value)
if i < len(values)-1 {
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,14 @@ func resolveWhereConditions(clauses []string) (map[string][]string, error) {
conditions := make(map[string][]string)

for _, clause := range clauses {
// for non equal conditions, leave a trailing '!' in the value
clause = strings.Replace(clause, "!=", "=!", 1)

keyvalue := strings.Split(clause, "=")
if len(keyvalue) != 2 {
return nil, fmt.Errorf("invalid where expression %s", clause)
}

if column, found := FIELD_NAMES[keyvalue[0]]; !found {
return nil, fmt.Errorf("unknown field name %s", keyvalue[0])
} else {
Expand Down
17 changes: 16 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ func TestWhereConditionParsing(t *testing.T) {
var cond map[string][]string
var err error

cond, err = resolveWhereConditions([]string{"url=/blog", "useragent=Safari", "ua=Firefox", "referer=olano%"})
cond, err = resolveWhereConditions([]string{"url=/blog", "useragent=Safari", "ua=Firefox", "referer=olano%", "os!=Windows"})
assertEqual(t, err, nil)
assertEqual(t, cond, map[string][]string{
"path": {"/blog"},
"user_agent": {"Safari", "Firefox"},
"referer": {"olano%"},
"os": {"!Windows"},
})

// include error on unknown field
Expand Down Expand Up @@ -224,6 +225,20 @@ func TestWherePattern(t *testing.T) {
assertEqual(t, len(rows), 3)
}

func TestWhereNegation(t *testing.T) {
_, rows := runCommand(t, SAMPLE_LOGS, []string{"url", "-w", "status!=200", "-l", "10"})
assertEqual(t, len(rows), 5)

_, rows = runCommand(t, SAMPLE_LOGS, []string{"url", "-w", "status!=301", "-l", "10"})
assertEqual(t, len(rows), 3)

_, rows = runCommand(t, SAMPLE_LOGS, []string{"url", "-w", "status!=2%", "-l", "10"})
assertEqual(t, len(rows), 5)

_, rows = runCommand(t, SAMPLE_LOGS, []string{"url", "-w", "status!=3%", "-l", "10"})
assertEqual(t, len(rows), 3)
}

func TestMultipleLogFiles(t *testing.T) {
// TODO
// more than one file in a dir, honoring the glob pattern
Expand Down

0 comments on commit 41747d9

Please sign in to comment.