Skip to content

Instantly share code, notes, and snippets.

@ivarprudnikov
Created July 4, 2024 11:41
Show Gist options
  • Save ivarprudnikov/fda37f7d3cb29a29dda3b6208d92902f to your computer and use it in GitHub Desktop.
Save ivarprudnikov/fda37f7d3cb29a29dda3b6208d92902f to your computer and use it in GitHub Desktop.
podx-0 /foo/bar/
ledger_1-7.committed ledger_10003-10629.committed ledger_8-10002.committed
podx-0 /foo/baz/
snapshot_10002_10004.committed snapshot_148573_148575.committed snapshot_202606_202607.committed
podx-1 /foo/bar/
ledger_1-7.committed ledger_10003-10629.committed ledger_8-10002.committed
podx-1 /foo/baz/
snapshot_10002_10004.committed snapshot_148573_148575.committed snapshot_202606_202607.committed
pody-0 /foo/bar/
ledger_1-7.committed ledger_10003-10629.committed ledger_8-10002.committed
pody-0 /foo/baz/
snapshot_10002_10004.committed snapshot_148573_148575.committed snapshot_202606_202607.committed
pody-1 /foo/bar/
ledger_1-7.committed ledger_10003-10629.committed ledger_8-10002.committed
pody-1 /foo/baz/
snapshot_10002_10004.committed snapshot_148573_148575.committed snapshot_202606_202607.committed
package main
import (
"bufio"
_ "embed"
"fmt"
"os"
"sort"
"strconv"
"strings"
)
// embed the log file
//
//go:embed logoutput.txt
var logOutput string
type PodDirectory struct {
Pod string
Directory string
}
type TxRange struct {
From int
To int
}
type LedgerFiles struct {
Files []string
TxRanges []TxRange
}
// parse the log file and collect ledger file ranges
// into a slice that contains from and to values
func main() {
// parse numeric argument to check in which pod directory it is present in
checkTransactionId := -1
if len(os.Args) > 1 {
tx := os.Args[1]
txParsed, err := strconv.Atoi(tx)
if err != nil {
fmt.Println("Invalid argument, please provide a transaction number, e.g. 23456", err)
os.Exit(1)
}
checkTransactionId = txParsed
}
scanner := bufio.NewScanner(strings.NewReader(logOutput))
var currentPodDirectory PodDirectory
groups := make(map[PodDirectory]LedgerFiles)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "podx-") || strings.HasPrefix(line, "pody-") {
parts := strings.SplitN(line, " ", 3)
currentPodDirectory = PodDirectory{Pod: parts[0], Directory: parts[1]}
} else {
files := strings.Fields(line)
// parse filenames to collect the tx ranges
var txRanges []TxRange
for _, file := range files {
if strings.HasSuffix(file, ".committed") {
parts := strings.FieldsFunc(file, func(r rune) bool { return strings.ContainsRune(" ._-", r) })
from, _ := strconv.Atoi(parts[1])
to, _ := strconv.Atoi(parts[2])
txRanges = append(txRanges, TxRange{From: from, To: to})
}
}
if group, exists := groups[currentPodDirectory]; exists {
group.Files = append(group.Files, files...)
group.TxRanges = append(group.TxRanges, txRanges...)
groups[currentPodDirectory] = group
} else {
groups[currentPodDirectory] = LedgerFiles{Files: files, TxRanges: txRanges}
}
}
}
// join the tx ranges so that [1,5],[6,9] becomes [1,9]
// sort tx ranges by the first value before merging
for pod, files := range groups {
sort.SliceStable(files.TxRanges, func(i, j int) bool { return files.TxRanges[i].From < files.TxRanges[j].From })
var mergedRanges []TxRange
for _, txRange := range files.TxRanges {
if len(mergedRanges) == 0 {
mergedRanges = append(mergedRanges, txRange)
} else {
lastRange := mergedRanges[len(mergedRanges)-1]
if lastRange.To >= txRange.From-1 {
lastRange.To = txRange.To
mergedRanges = append(mergedRanges[:len(mergedRanges)-1], lastRange)
} else {
mergedRanges = append(mergedRanges, txRange)
}
}
}
files.TxRanges = mergedRanges
groups[pod] = files
}
// Output the result
out := []string{}
for pd, files := range groups {
txCheckString := ""
if checkTransactionId != -1 {
for _, txRange := range files.TxRanges {
if checkTransactionId >= txRange.From && checkTransactionId <= txRange.To {
txCheckString = fmt.Sprintf(", Transaction %d is in the range [%d, %d]", checkTransactionId, txRange.From, txRange.To)
}
}
}
out = append(out, fmt.Sprintf("Pod: %s, Directory: %s%s\nRanges: %v\n\n", pd.Pod, pd.Directory, txCheckString, files.TxRanges))
}
// sort the output to print pods in ascending order
sort.Strings(out)
for _, o := range out {
fmt.Print(o)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment