Skip to content

Commit

Permalink
feat: add contextual search (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhth committed Mar 7, 2024
1 parent 6146bc6 commit c2ab3b4
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 58 deletions.
26 changes: 22 additions & 4 deletions ui/model/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -14,17 +15,17 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

func FetchMessages(client *sqs.Client, queueUrl string, maxMessages int32, waitTime int32, msgConsumptionConf MsgConsumptionConf) tea.Cmd {
func (m model) FetchMessages(maxMessages int32, waitTime int32) tea.Cmd {
return func() tea.Msg {

var messages []types.Message
var messagesValues []string
var keyValues []string
result, err := client.ReceiveMessage(context.TODO(),
result, err := m.sqsClient.ReceiveMessage(context.TODO(),
// WaitTimeSeconds > 0 enables long polling
// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-short-and-long-polling.html#sqs-long-polling
&sqs.ReceiveMessageInput{
QueueUrl: aws.String(queueUrl),
QueueUrl: aws.String(m.queueUrl),
MaxNumberOfMessages: maxMessages,
WaitTimeSeconds: waitTime,
VisibilityTimeout: 30,
Expand All @@ -37,7 +38,7 @@ func FetchMessages(client *sqs.Client, queueUrl string, maxMessages int32, waitT
} else {
messages = result.Messages
for _, message := range messages {
msgValue, keyValue, _ := getMessageData(&message, msgConsumptionConf)
msgValue, keyValue, _ := getMessageData(&message, m.msgConsumptionConf)
messagesValues = append(messagesValues, msgValue)
keyValues = append(keyValues, keyValue)
}
Expand Down Expand Up @@ -128,6 +129,17 @@ func saveRecordValueToDisk(filePath string, msgValue string, msgFmt MsgFmt) tea.
}
}

func setContextSearchValues(userInput string) tea.Cmd {
return func() tea.Msg {
valuesEls := strings.Split(userInput, ",")
var values []string
for _, v := range valuesEls {
values = append(values, strings.TrimSpace(v))
}
return ContextSearchValuesSetMsg{values}
}
}

func showItemDetails(key string) tea.Cmd {
return func() tea.Msg {
return KMsgChosenMsg{key}
Expand All @@ -139,3 +151,9 @@ func tickEvery(interval time.Duration) tea.Cmd {
return MsgCountTickMsg{}
})
}

func hideHelp(interval time.Duration) tea.Cmd {
return tea.Tick(interval, func(time.Time) tea.Msg {
return HideHelpMsg{}
})
}
11 changes: 8 additions & 3 deletions ui/model/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ var (
h/<Up> Move cursor up
k/<Down> Move cursor down
n Fetch the next message from the queue
N Fetch the next 10 messages from the queue
} Fetch the next 100 messages from the queue
N Fetch up to 10 more messages from the queue
} Fetch up to 100 more messages from the queue
d Toggle deletion mode; cueitup will delete messages
after reading them
<ctrl+p Toggle queue message count polling ON/OFF; ON by default
<ctrl+s> Toggle contextual search prompt
<ctrl+f> Toggle contextual filtering ON/OFF
<ctrl+p> Toggle queue message count polling ON/OFF; ON by default
p Toggle persist mode (cueitup will start persisting
messages, at the location
messages/<topic-name>/<timestamp>/<message-id>.md
s Toggle skipping mode; cueitup will consume messages,
but not populate its internal list, effectively
skipping over them
Expand Down
19 changes: 18 additions & 1 deletion ui/model/initial.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package model

import (
"fmt"
"os"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
)

func InitialModel(sqsClient *sqs.Client, queueUrl string, msgConsumptionConf MsgConsumptionConf) model {
Expand All @@ -21,17 +23,32 @@ func InitialModel(sqsClient *sqs.Client, queueUrl string, msgConsumptionConf Msg
timeString := currentTime.Format("2006-01-02-15-04-05")
persistDir := fmt.Sprintf("messages/%s/%s", queueName, timeString)

ti := textinput.New()
ti.Prompt = fmt.Sprintf("Filter messages where %s in > ", msgConsumptionConf.ContextKey)
ti.Focus()
ti.CharLimit = 100
ti.Width = 100

var dbg bool
if len(os.Getenv("DEBUG")) > 0 {
dbg = true
}

m := model{
sqsClient: sqsClient,
queueUrl: queueUrl,
msgConsumptionConf: msgConsumptionConf,
pollForQueueMsgCount: true,
kMsgsList: list.New(jobItems, appDelegate, 60, 0),
kMsgsList: list.New(jobItems, appDelegate, listWidth+10, 0),
recordMetadataStore: make(map[string]string),
recordValueStore: make(map[string]string),
persistDir: persistDir,
contextSearchInput: ti,
showHelpIndicator: true,
debugMode: dbg,
}
m.kMsgsList.Title = "Messages"
m.kMsgsList.SetStatusBarItemName("message", "messages")
m.kMsgsList.SetFilteringEnabled(false)
m.kMsgsList.SetShowHelp(false)

Expand Down
9 changes: 8 additions & 1 deletion ui/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
)
Expand All @@ -16,6 +17,7 @@ const (
kMsgMetadataView
kMsgValueView
helpView
contextualSearchView
)

type MsgFmt uint
Expand Down Expand Up @@ -43,11 +45,14 @@ type model struct {
pollForQueueMsgCount bool
kMsgsList list.Model
helpVP viewport.Model
helpSeen uint
showHelpIndicator bool
msgMetadataVP viewport.Model
msgValueVP viewport.Model
recordMetadataStore map[string]string
recordValueStore map[string]string
contextSearchInput textinput.Model
contextSearchValues []string
filterMessages bool
deleteMsgs bool
skipRecords bool
persistRecords bool
Expand All @@ -61,11 +66,13 @@ type model struct {
terminalHeight int
msg string
errorMsg string
debugMode bool
}

func (m model) Init() tea.Cmd {
return tea.Batch(
GetQueueMsgCount(m.sqsClient, m.queueUrl),
tickEvery(msgCountTickInterval),
hideHelp(time.Second*30),
)
}
5 changes: 5 additions & 0 deletions ui/model/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

type MsgCountTickMsg struct{}
type HideHelpMsg struct{}

type KMsgFetchedMsg struct {
messages []types.Message
Expand All @@ -26,6 +27,10 @@ type KMsgChosenMsg struct {
key string
}

type ContextSearchValuesSetMsg struct {
values []string
}

type RecordSavedToDiskMsg struct {
path string
err error
Expand Down
4 changes: 2 additions & 2 deletions ui/model/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ var (

baseListStyle = lipgloss.NewStyle().PaddingTop(1).PaddingRight(2).PaddingLeft(1).PaddingBottom(1)

stackListStyle = baseListStyle.Copy().Width(listWidth + 10)
viewPortStyle = baseListStyle.Copy().Width(150)
stackListStyle = baseListStyle.Copy().Width(listWidth+5).Border(lipgloss.NormalBorder(), false, true, false, false).BorderForeground(lipgloss.Color("#3c3836"))
viewPortStyle = baseListStyle.Copy().Width(150).PaddingLeft(4)

modeStyle = baseStyle.Copy().
Align(lipgloss.Center).
Expand Down
4 changes: 2 additions & 2 deletions ui/model/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ type KMsgItem struct {
}

func (item KMsgItem) Title() string {
return fmt.Sprintf("%s: %s", RightPadTrim("msgId", 10), RightPadTrim(*item.message.MessageId, 40))
return RightPadTrim(fmt.Sprintf("%s: %s", RightPadTrim("msgId", 10), *item.message.MessageId), listWidth)
}

func (item KMsgItem) Description() string {
if item.contextKeyValue != "" {
return fmt.Sprintf("%s: %s", RightPadTrim(item.contextKeyName, 10), RightPadTrim(item.contextKeyValue, 40))
return RightPadTrim(fmt.Sprintf("%s: %s", RightPadTrim(item.contextKeyName, 10), item.contextKeyValue), listWidth)
}
return ""
}
Expand Down

0 comments on commit c2ab3b4

Please sign in to comment.