add slim cli record to speed up CLI start up

pull/124/head
Simon Let 6 years ago
parent 4d3a6fd1c4
commit 5deccd6c3d
  1. 4
      cmd/cli/item.go
  2. 26
      cmd/cli/main.go
  3. 6
      cmd/daemon/dump.go
  4. 5
      pkg/histcli/histcli.go
  5. 14
      pkg/histfile/histfile.go
  6. 10
      pkg/msg/msg.go
  7. 29
      pkg/records/records.go

@ -93,7 +93,7 @@ func properMatch(str, term, padChar string) bool {
// newItemFromRecordForQuery creates new item from record based on given query
// returns error if the query doesn't match the record
func newItemFromRecordForQuery(record records.EnrichedRecord, query query, debug bool) (item, error) {
func newItemFromRecordForQuery(record records.CliRecord, query query, debug bool) (item, error) {
const hitScore = 1.0
const hitScoreConsecutive = 0.1
const properMatchScore = 0.3
@ -237,7 +237,7 @@ type rawItem struct {
// newRawItemFromRecordForQuery creates new item from record based on given query
// returns error if the query doesn't match the record
func newRawItemFromRecordForQuery(record records.EnrichedRecord, terms []string, debug bool) (rawItem, error) {
func newRawItemFromRecordForQuery(record records.CliRecord, terms []string, debug bool) (rawItem, error) {
const hitScore = 1.0
const hitScoreConsecutive = 0.1
const properMatchScore = 0.3

@ -96,15 +96,15 @@ func runReshCli() (string, int) {
// g.SelBgColor = gocui.ColorGreen
g.Highlight = true
mess := msg.DumpMsg{
mess := msg.CliMsg{
SessionID: *sessionID,
PWD: *pwd,
}
resp := SendDumpMsg(mess, strconv.Itoa(config.Port))
resp := SendCliMsg(mess, strconv.Itoa(config.Port))
st := state{
// lock sync.Mutex
fullRecords: resp.FullRecords,
cliRecords: resp.CliRecords,
initialQuery: *query,
}
@ -157,7 +157,7 @@ func runReshCli() (string, int) {
type state struct {
lock sync.Mutex
fullRecords []records.EnrichedRecord
cliRecords []records.CliRecord
data []item
rawData []rawItem
highlightedItem int
@ -210,7 +210,7 @@ type dedupRecord struct {
func (m manager) UpdateData(input string) {
if debug {
log.Println("EDIT start")
log.Println("len(fullRecords) =", len(m.s.fullRecords))
log.Println("len(fullRecords) =", len(m.s.cliRecords))
log.Println("len(data) =", len(m.s.data))
}
query := newQueryFromString(input, m.host, m.pwd, m.gitOriginRemote)
@ -218,7 +218,7 @@ func (m manager) UpdateData(input string) {
itemSet := make(map[string]int)
m.s.lock.Lock()
defer m.s.lock.Unlock()
for _, rec := range m.s.fullRecords {
for _, rec := range m.s.cliRecords {
itm, err := newItemFromRecordForQuery(rec, query, m.config.Debug)
if err != nil {
// records didn't match the query
@ -254,7 +254,7 @@ func (m manager) UpdateData(input string) {
}
m.s.highlightedItem = 0
if debug {
log.Println("len(fullRecords) =", len(m.s.fullRecords))
log.Println("len(fullRecords) =", len(m.s.cliRecords))
log.Println("len(data) =", len(m.s.data))
log.Println("EDIT end")
}
@ -263,7 +263,7 @@ func (m manager) UpdateData(input string) {
func (m manager) UpdateRawData(input string) {
if debug {
log.Println("EDIT start")
log.Println("len(fullRecords) =", len(m.s.fullRecords))
log.Println("len(fullRecords) =", len(m.s.cliRecords))
log.Println("len(data) =", len(m.s.data))
}
query := getRawTermsFromString(input)
@ -271,7 +271,7 @@ func (m manager) UpdateRawData(input string) {
itemSet := make(map[string]bool)
m.s.lock.Lock()
defer m.s.lock.Unlock()
for _, rec := range m.s.fullRecords {
for _, rec := range m.s.cliRecords {
itm, err := newRawItemFromRecordForQuery(rec, query, m.config.Debug)
if err != nil {
// records didn't match the query
@ -301,7 +301,7 @@ func (m manager) UpdateRawData(input string) {
}
m.s.highlightedItem = 0
if debug {
log.Println("len(fullRecords) =", len(m.s.fullRecords))
log.Println("len(fullRecords) =", len(m.s.cliRecords))
log.Println("len(data) =", len(m.s.data))
log.Println("EDIT end")
}
@ -393,8 +393,8 @@ func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
// SendDumpMsg to daemon
func SendDumpMsg(m msg.DumpMsg, port string) msg.DumpResponse {
// SendCliMsg to daemon
func SendCliMsg(m msg.CliMsg, port string) msg.CliResponse {
recJSON, err := json.Marshal(m)
if err != nil {
log.Fatal("send err 1", err)
@ -419,7 +419,7 @@ func SendDumpMsg(m msg.DumpMsg, port string) msg.DumpResponse {
log.Fatal("read response error")
}
// log.Println(string(body))
response := msg.DumpResponse{}
response := msg.CliResponse{}
err = json.Unmarshal(body, &response)
if err != nil {
log.Fatal("unmarshal resp error: ", err)

@ -25,7 +25,7 @@ func (h *dumpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
mess := msg.DumpMsg{}
mess := msg.CliMsg{}
if Debug {
log.Println("/dump unmarshaling record ...")
}
@ -38,12 +38,12 @@ func (h *dumpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if Debug {
log.Println("/dump dumping ...")
}
fullRecords := h.histfileBox.DumpRecords()
fullRecords := h.histfileBox.DumpCliRecords()
if err != nil {
log.Println("Dump error:", err)
}
resp := msg.DumpResponse{FullRecords: fullRecords.List}
resp := msg.CliResponse{CliRecords: fullRecords.List}
jsn, err = json.Marshal(&resp)
if err != nil {
log.Println("Encoding error:", err)

@ -7,7 +7,7 @@ import (
// Histcli is a dump of history preprocessed for resh cli purposes
type Histcli struct {
// list of records
List []records.EnrichedRecord
List []records.CliRecord
}
// New Histcli
@ -18,6 +18,7 @@ func New() Histcli {
// AddRecord to the histcli
func (h *Histcli) AddRecord(record records.Record) {
enriched := records.Enriched(record)
cli := records.NewCliRecord(enriched)
h.List = append(h.List, enriched)
h.List = append(h.List, cli)
}

@ -27,7 +27,7 @@ type Histfile struct {
bashCmdLines histlist.Histlist
zshCmdLines histlist.Histlist
fullRecords histcli.Histcli
cliRecords histcli.Histcli
}
// New creates new histfile and runs its gorutines
@ -41,7 +41,7 @@ func New(input chan records.Record, sessionsToDrop chan string,
historyPath: reshHistoryPath,
bashCmdLines: histlist.New(),
zshCmdLines: histlist.New(),
fullRecords: histcli.New(),
cliRecords: histcli.New(),
}
go hf.loadHistory(bashHistoryPath, zshHistoryPath, maxInitHistSize, minInitHistSizeKB)
go hf.writer(input, signals, shutdownDone)
@ -53,7 +53,7 @@ func New(input chan records.Record, sessionsToDrop chan string,
func (h *Histfile) loadFullRecords(recs []records.Record) {
for i := len(recs) - 1; i >= 0; i-- {
rec := recs[i]
h.fullRecords.AddRecord(rec)
h.cliRecords.AddRecord(rec)
}
}
@ -178,7 +178,7 @@ func (h *Histfile) mergeAndWriteRecord(part1, part2 records.Record) {
cmdLine := part1.CmdLine
h.bashCmdLines.AddCmdLine(cmdLine)
h.zshCmdLines.AddCmdLine(cmdLine)
h.fullRecords.AddRecord(part1)
h.cliRecords.AddRecord(part1)
}()
writeRecord(part1, h.historyPath)
@ -224,10 +224,10 @@ func (h *Histfile) GetRecentCmdLines(shell string, limit int) histlist.Histlist
return hl
}
// DumpRecords returns enriched records
func (h *Histfile) DumpRecords() histcli.Histcli {
// DumpCliRecords returns enriched records
func (h *Histfile) DumpCliRecords() histcli.Histcli {
// don't forget locks in the future
return h.fullRecords
return h.cliRecords
}
func loadCmdLines(recs []records.Record) histlist.Histlist {

@ -2,15 +2,15 @@ package msg
import "github.com/curusarn/resh/pkg/records"
// DumpMsg struct
type DumpMsg struct {
// CliMsg struct
type CliMsg struct {
SessionID string `json:"sessionID"`
PWD string `json:"pwd"`
}
// DumpResponse struct
type DumpResponse struct {
FullRecords []records.EnrichedRecord `json:"fullRecords"`
// CliResponse struct
type CliResponse struct {
CliRecords []records.CliRecord `json:"cliRecords"`
}
// InspectMsg struct

@ -147,6 +147,35 @@ type SlimRecord struct {
}
// CliRecord used for sending records to RESH-CLI
type CliRecord struct {
SessionID string `json:"sessionId"`
CmdLine string `json:"cmdLine"`
Host string `json:"host"`
Pwd string `json:"pwd"`
Home string `json:"home"` // helps us to collapse /home/user to tilde
GitOriginRemote string `json:"gitOriginRemote"`
ExitCode int `json:"exitCode"`
// RealtimeBefore float64 `json:"realtimeBefore"`
// RealtimeAfter float64 `json:"realtimeAfter"`
// RealtimeDuration float64 `json:"realtimeDuration"`
}
// NewCliRecord from EnrichedRecord
func NewCliRecord(r EnrichedRecord) CliRecord {
return CliRecord{
SessionID: r.SessionID,
CmdLine: r.CmdLine,
Host: r.Host,
Pwd: r.Pwd,
Home: r.Home,
GitOriginRemote: r.GitOriginRemote,
ExitCode: r.ExitCode,
}
}
// Convert from FallbackRecord to Record
func Convert(r *FallbackRecord) Record {
return Record{

Loading…
Cancel
Save