mirror of
https://github.com/wavetermdev/backup.git
synced 2026-08-05 13:57:07 -07:00
bug fixes, allow cmd filtering
This commit is contained in:
@@ -1849,6 +1849,23 @@ func HistoryPurgeCommand(ctx context.Context, pk *scpacket.FeCommandPacketType)
|
||||
|
||||
const HistoryViewPageSize = 50
|
||||
|
||||
var cmdFilterLs = regexp.MustCompile(`^ls(\s|$)`)
|
||||
var cmdFilterCd = regexp.MustCompile(`^cd(\s|$)`)
|
||||
|
||||
func historyCmdFilter(hitem *sstore.HistoryItemType) bool {
|
||||
cmdStr := hitem.CmdStr
|
||||
if cmdStr == "" || strings.Index(cmdStr, ";") != -1 || strings.Index(cmdStr, "\n") != -1 {
|
||||
return true
|
||||
}
|
||||
if cmdFilterLs.MatchString(cmdStr) {
|
||||
return false
|
||||
}
|
||||
if cmdFilterCd.MatchString(cmdStr) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func HistoryViewAllCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) {
|
||||
_, err := resolveUiIds(ctx, pk, 0)
|
||||
if err != nil {
|
||||
@@ -1894,6 +1911,9 @@ func HistoryViewAllCommand(ctx context.Context, pk *scpacket.FeCommandPacketType
|
||||
if pk.Kwargs["meta"] != "" {
|
||||
opts.NoMeta = !resolveBool(pk.Kwargs["meta"], true)
|
||||
}
|
||||
if resolveBool(pk.Kwargs["filter"], false) {
|
||||
opts.FilterFn = historyCmdFilter
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid meta arg (must be boolean): %v", err)
|
||||
}
|
||||
@@ -1904,6 +1924,7 @@ func HistoryViewAllCommand(ctx context.Context, pk *scpacket.FeCommandPacketType
|
||||
hvdata := &sstore.HistoryViewData{
|
||||
Items: hresult.Items,
|
||||
Offset: hresult.Offset,
|
||||
RawOffset: hresult.RawOffset,
|
||||
NextRawOffset: hresult.NextRawOffset,
|
||||
HasMore: hresult.HasMore,
|
||||
}
|
||||
|
||||
+17
-24
@@ -219,41 +219,33 @@ func _getNextHistoryItem(items []*HistoryItemType, index int, filterFn func(*His
|
||||
return nil, index
|
||||
}
|
||||
|
||||
// returns true if done, false if we still need to process more items
|
||||
func (result *HistoryQueryResult) processItem(item *HistoryItemType, rawOffset int) bool {
|
||||
if result.Offset < result.prevItems {
|
||||
result.prevItems++
|
||||
return false
|
||||
}
|
||||
if len(result.Items) == result.MaxItems {
|
||||
result.HasMore = true
|
||||
result.NextRawOffset = rawOffset
|
||||
return false
|
||||
return true
|
||||
}
|
||||
result.Items = append(result.Items, item)
|
||||
return true
|
||||
return false
|
||||
}
|
||||
|
||||
func runHistoryQueryWithFilter(tx *TxWrap, opts HistoryQueryOpts, filterFn func(*HistoryItemType) bool) (*HistoryQueryResult, error) {
|
||||
func runHistoryQueryWithFilter(tx *TxWrap, opts HistoryQueryOpts) (*HistoryQueryResult, error) {
|
||||
if opts.MaxItems == 0 {
|
||||
return nil, fmt.Errorf("invalid query, maxitems is 0")
|
||||
}
|
||||
if opts.RawOffset < opts.Offset {
|
||||
return nil, fmt.Errorf("invalid query, rawoffset[%d] is less than offset[%d]", opts.RawOffset, opts.Offset)
|
||||
}
|
||||
rtn := &HistoryQueryResult{Offset: opts.RawOffset, MaxItems: opts.MaxItems}
|
||||
if filterFn == nil {
|
||||
results, err := runHistoryQuery(tx, opts, opts.RawOffset, opts.MaxItems+1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(results) > opts.MaxItems {
|
||||
rtn.Items = results[0:opts.MaxItems]
|
||||
rtn.HasMore = true
|
||||
rtn.NextRawOffset = opts.RawOffset + opts.MaxItems
|
||||
} else {
|
||||
rtn.Items = results
|
||||
rtn.HasMore = false
|
||||
rtn.NextRawOffset = 0
|
||||
}
|
||||
return rtn, nil
|
||||
var rawOffset int
|
||||
if opts.RawOffset >= opts.Offset {
|
||||
rtn.prevItems = opts.Offset
|
||||
rawOffset = opts.RawOffset
|
||||
} else {
|
||||
rawOffset = 0
|
||||
}
|
||||
rawOffset := opts.RawOffset
|
||||
for {
|
||||
resultItems, err := runHistoryQuery(tx, opts, rawOffset, HistoryQueryChunkSize)
|
||||
if err != nil {
|
||||
@@ -261,7 +253,7 @@ func runHistoryQueryWithFilter(tx *TxWrap, opts HistoryQueryOpts, filterFn func(
|
||||
}
|
||||
isDone := false
|
||||
for resultIdx := 0; resultIdx < len(resultItems); resultIdx++ {
|
||||
if !filterFn(resultItems[resultIdx]) {
|
||||
if opts.FilterFn != nil && !opts.FilterFn(resultItems[resultIdx]) {
|
||||
continue
|
||||
}
|
||||
isDone = rtn.processItem(resultItems[resultIdx], rawOffset+resultIdx)
|
||||
@@ -275,6 +267,7 @@ func runHistoryQueryWithFilter(tx *TxWrap, opts HistoryQueryOpts, filterFn func(
|
||||
if len(resultItems) < HistoryQueryChunkSize {
|
||||
break
|
||||
}
|
||||
rawOffset += HistoryQueryChunkSize
|
||||
}
|
||||
return rtn, nil
|
||||
}
|
||||
@@ -341,7 +334,7 @@ func GetHistoryItems(ctx context.Context, opts HistoryQueryOpts) (*HistoryQueryR
|
||||
var rtn *HistoryQueryResult
|
||||
txErr := WithTx(ctx, func(tx *TxWrap) error {
|
||||
var err error
|
||||
rtn, err = runHistoryQueryWithFilter(tx, opts, nil)
|
||||
rtn, err = runHistoryQueryWithFilter(tx, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -540,14 +540,18 @@ type HistoryQueryOpts struct {
|
||||
WindowId string
|
||||
NoMeta bool
|
||||
RawOffset int
|
||||
FilterFn func(*HistoryItemType) bool
|
||||
}
|
||||
|
||||
type HistoryQueryResult struct {
|
||||
MaxItems int
|
||||
Items []*HistoryItemType
|
||||
Offset int // the offset shown to user
|
||||
RawOffset int // internal offset
|
||||
HasMore bool
|
||||
NextRawOffset int // internal offset used by pager for next query
|
||||
|
||||
prevItems int // holds number of items skipped by RawOffset
|
||||
}
|
||||
|
||||
type TermOpts struct {
|
||||
|
||||
@@ -79,7 +79,8 @@ func InfoMsgUpdate(infoMsgFmt string, args ...interface{}) *ModelUpdate {
|
||||
type HistoryViewData struct {
|
||||
Items []*HistoryItemType `json:"items"`
|
||||
Offset int `json:"offset"`
|
||||
NextRawOffset int `json:"rawoffset"`
|
||||
RawOffset int `json:"rawoffset"`
|
||||
NextRawOffset int `json:"nextrawoffset"`
|
||||
HasMore bool `json:"hasmore"`
|
||||
Lines []*LineType `json:"lines"`
|
||||
Cmds []*CmdType `json:"cmds"`
|
||||
|
||||
Reference in New Issue
Block a user