Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
feat: get elements command (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Dec 3, 2023
1 parent 1b04331 commit b2c9184
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.21'
go-version: '1.21.1'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: '1.21.1'

- name: Unit tests
run: make unit_test
1 change: 1 addition & 0 deletions core/TQL/execute/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var Executors ExecutorMap = ExecutorMap{
"CNTE": database.IDataBase.CountElements,
"CNTS": database.IDataBase.CountSets,
"CNTSS": database.IDataBase.CountSubSets,
"GET": database.IDataBase.GetElements,
}

func Execute(q database.Query, db database.IDataBase) string {
Expand Down
24 changes: 24 additions & 0 deletions core/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,27 @@ func (db *Database) CountElements(args []string) string {

return fmt.Sprint(i)
}

func (db *Database) GetElements(args []string) string {
if len(args) < 2 {
return "INVALID"
}

subSet, ok := db.Sets[args[0]][args[1]]
if !ok {
return "SSNF"
}

if len(args) == 3 {
n, err := strconv.Atoi(args[2])
if err != nil || len(subSet) < n {
return "INVALID"
}

lastN := subSet[len(subSet)-n:]

return lastN.String()
}

return subSet.String()
}
33 changes: 33 additions & 0 deletions core/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"fmt"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -156,4 +157,36 @@ func TestDataBase(t *testing.T) {
result = db.CountElements([]string{"testSet", "subSetOne"})
assert.Equal(t, "1", result)
})

t.Run("getTest", func(t *testing.T) {
db.AddSet([]string{"testSet"})
db.AddSubSet([]string{"testSet", "subSetOne"})

time := time.Now()
timeStr := strconv.Itoa(int(time.Unix()))
for i := 0; i < 50; i++ {
db.PushElement([]string{"testSet", "subSetOne", "testValue", timeStr})
}

result := db.GetElements([]string{"testSet", "subSetOne"})

trimmedResult, _ := strings.CutPrefix(result, " ")
trimmedResult, _ = strings.CutPrefix(trimmedResult, " ")
fmt.Print(trimmedResult)
resultsArray := strings.Split(trimmedResult, " ")

assert.NotEqual(t, "INVALID", result)
assert.NotEqual(t, "SSNF", result)
assert.Equal(t, 50, len(resultsArray))

result = db.GetElements([]string{"testSet", "subSetOne", "10"})

trimmedResult, _ = strings.CutPrefix(result, " ")
trimmedResult, _ = strings.CutPrefix(trimmedResult, " ")
resultsArray = strings.Split(trimmedResult, " ")

assert.NotEqual(t, "INVALID", result)
assert.NotEqual(t, "SSNF", result)
assert.Equal(t, 10, len(resultsArray))
})
}
1 change: 1 addition & 0 deletions core/database/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ type IDataBase interface {
CountSets([]string) string
CountSubSets([]string) string
CountElements([]string) string
GetElements([]string) string
}
15 changes: 14 additions & 1 deletion core/database/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package database

import "time"
import (
"fmt"
"strings"
"time"
)

type Element struct {
value string // currently ttrace only supports string datatype for value.
Expand All @@ -17,3 +21,12 @@ type (
Set map[string]SubSet
SubSet []Element
)

func (ss SubSet) String() string {
var builder strings.Builder
for _, s := range ss {
builder.WriteString(fmt.Sprintf(" %v-%d ", s.value, s.time.Unix()))
}

return builder.String()
}
2 changes: 1 addition & 1 deletion doc/TQL/TQL.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Time trace is using a query language called TQL. Here is documentation and speci
| SET * | make a new set | set-name |
| SSET * | make a new subset | set-name - subset-name |
| PUSH * | push an element to a subset | set-name - subset-name - value-of-element - time(unix-timestamp) |
| GET | get elements of a subset | set-name - subset-name - [last n elements (optional)] - [regex filter] - [unix-timestamp]|
| GET * | get elements of a subset | set-name - subset-name - [last n elements (optional)] |
| CNTS * | returns count of sets | |
| CNTSS * | returns count of subsets | set-name |
| CNTE * | returns count of elements | set-name - subset-name |
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/zurvan-lab/TimeTrace

go 1.20
go 1.21.1

require (
github.com/rs/zerolog v1.31.0
Expand Down

0 comments on commit b2c9184

Please sign in to comment.