Skip to content

Commit

Permalink
Best peformance
Browse files Browse the repository at this point in the history
  • Loading branch information
paulosuzart committed Feb 25, 2024
1 parent e3ddbbb commit 12e12f5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
26 changes: 16 additions & 10 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,33 +203,39 @@ func (l *LoadResult) GetUpperBound() int {

func Load(offset int, filter *Filter) *LoadResult {

selectClause := `
select tuples.*, p.action from (select *, row_number() over (order by timestamp desc) as row_number from tuples) tuples
left join pending_actions p on tuples.tuple_key = p.tuple_key
where row_number >= :offset and row_number <= :offset + 200
`

var params = map[string]interface{}{
"offset": offset,
}

var whereClauses []string
if filter != nil && filter.Search != nil && len(strings.TrimSpace(*filter.Search)) > 3 {
selectClause = fmt.Sprintf("%s and tuples.tuple_key like :query\n", selectClause)
whereClauses = append(whereClauses, "tuples.tuple_key like :query\n")
params["query"] = filter.Search
}
if filter != nil && filter.UserType != nil {
selectClause = fmt.Sprintf("%s and tuples.user_type = :userType\n", selectClause)
whereClauses = append(whereClauses, "tuples.user_type = :userType\n")
params["userType"] = filter.UserType
}
if filter != nil && filter.Relation != nil {
selectClause = fmt.Sprintf("%s and tuples.relation = :relation\n", selectClause)
whereClauses = append(whereClauses, "tuples.relation = :relation\n")
params["relation"] = filter.Relation
}
if filter != nil && filter.ObjectType != nil {
selectClause = fmt.Sprintf("%s and tuples.object_type = :objectType\n", selectClause)
whereClauses = append(whereClauses, "tuples.object_type = :objectType\n")
params["objectType"] = filter.ObjectType
}

finalWhere := strings.Join(whereClauses[:], " and ")
if finalWhere != "" {
finalWhere = " where " + finalWhere
}

selectClause := fmt.Sprintf(`
select tuples.*, p.action from (select *, row_number() over (order by timestamp desc) as row_number from tuples %v) tuples
left join pending_actions p on tuples.tuple_key = p.tuple_key
where row_number >= :offset and row_number <= :offset + 200
`, finalWhere)

log.Printf("Load Query: %v\noffset: %v", selectClause, offset)
rows, err := db.NamedQuery(selectClause, params)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func AddComponents(context context.Context, app *tview.Application) *tview.Grid

infoTable.SetCell(1, 4, tview.NewTableCell("Continuation Token:").
SetTextColor(tcell.ColorDarkOrange))
tokenView := tview.NewTableCell("??")
tokenView := tview.NewTableCell("??").SetMaxWidth(60)

infoTable.SetCell(2, 0, tview.NewTableCell("W:").
SetTextColor(tcell.ColorLightGreen))
Expand Down Expand Up @@ -305,13 +305,13 @@ func AddComponents(context context.Context, app *tview.Application) *tview.Grid
if searchText := search.GetText(); searchText != "" {
filter.Search = &searchText
}
if i, userType := userTypes.GetCurrentOption(); i > 1 {
if i, userType := userTypes.GetCurrentOption(); i > 0 {
filter.UserType = &userType
}
if i, relation := relations.GetCurrentOption(); i > 1 {
if i, relation := relations.GetCurrentOption(); i > 0 {
filter.Relation = &relation
}
if i, objectType := objectTypes.GetCurrentOption(); i > 1 {
if i, objectType := objectTypes.GetCurrentOption(); i > 0 {
filter.ObjectType = &objectType
}
tupleView.setFilter(filter)
Expand All @@ -336,6 +336,7 @@ func AddComponents(context context.Context, app *tview.Application) *tview.Grid

// Layout for screens narrower than 100 cells (menu and side bar are hidden).
grid.AddItem(tupleTable, 3, 0, 10, 1, 0, 0, false)

watchUpdatesChan := make(chan WatchUpdate, 10)
go func() {
for {
Expand Down

0 comments on commit 12e12f5

Please sign in to comment.