chore_: extract message count from logs

This commit is contained in:
Andrea Maria Piana 2024-07-01 12:24:16 +01:00
parent 6e52565356
commit d17e0ff331
2 changed files with 63 additions and 15 deletions

View File

@ -2,12 +2,12 @@
# extract_logs.go # extract_logs.go
This script analyzes geth.log files in a specific format and extracts information related to "sent-message" actions. It then prints relevant details such as timestamp, recipients, message ID, message type, and hashes to the console. This script analyzes geth.log files in a specific format and extracts information related to "sent-message" actions or received messages. It then prints relevant details such as timestamp, recipients, message ID, message type, and hashes to the console.
## Usage ## Usage
```bash ```bash
go run extract_logs.go <filename> go run extract_logs.go -messages -received-message-count <filename>
``` ```
It will output in tab separated values (TSV) It will output in tab separated values (TSV)

View File

@ -3,6 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"os" "os"
"regexp" "regexp"
@ -12,22 +13,36 @@ import (
var logPattern = regexp.MustCompile(`(?P<timestamp>[\d-]+T[\d:]+\.\d+Z).*(?P<action>sent-message:.*)\s+(?P<logData>{.*})`) var logPattern = regexp.MustCompile(`(?P<timestamp>[\d-]+T[\d:]+\.\d+Z).*(?P<action>sent-message:.*)\s+(?P<logData>{.*})`)
func main() { const (
if len(os.Args) != 2 { filtersNotMatched = "filters did match"
fmt.Println("Usage: go run script.go <filename>") filtersMatched = "filters did not match"
os.Exit(1) storeNodeMessage = "received waku2 store message"
)
func receivedMessageCountInfo(scanner *bufio.Scanner) {
fmt.Printf("Matching\tNot matching\tStore node\tLive\tTotal\n")
filtersNotMatchedCount := 0
filtersMatchedCount := 0
storeNodeReceivedMessageCount := 0
for scanner.Scan() {
line := scanner.Text()
// Check if the line contains "sent-message"
if strings.Contains(line, filtersNotMatched) {
filtersNotMatchedCount++
} else if strings.Contains(line, filtersMatched) {
filtersMatchedCount++
} else if strings.Contains(line, storeNodeMessage) {
storeNodeReceivedMessageCount++
}
// Print the required information
} }
fmt.Printf("%d\t%d\t%d\t%d\t%d\n", filtersNotMatchedCount, filtersMatchedCount, storeNodeReceivedMessageCount, filtersNotMatchedCount+filtersMatchedCount-storeNodeReceivedMessageCount, filtersNotMatchedCount+filtersMatchedCount)
filename := os.Args[1] }
file, err := os.Open(filename)
if err != nil {
fmt.Printf("Error opening file: %s\n", err)
os.Exit(1)
}
defer file.Close()
scanner := bufio.NewScanner(file)
func messagesInfo(scanner *bufio.Scanner) {
fmt.Printf("Timestamp\tMessageType\tContentType\tMessageID\tHashes\tRecipients\n") fmt.Printf("Timestamp\tMessageType\tContentType\tMessageID\tHashes\tRecipients\n")
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
@ -58,6 +73,39 @@ func main() {
} }
} }
} }
}
func main() {
messagesFlag := flag.Bool("messages", false, "Process sent messages in the log file")
receivedMessageCountFlag := flag.Bool("received-message-count", false, "Count the number of sent messages in the log file")
// Parse the flags
flag.Parse()
if flag.NArg() != 1 {
fmt.Println("Usage: go run script.go -messages -received-message-count <filename>")
os.Exit(1)
}
filename := flag.Arg(0)
file, err := os.Open(filename)
if err != nil {
fmt.Printf("Error opening file: %s\n", err)
os.Exit(1)
}
defer file.Close()
if !*messagesFlag && !*receivedMessageCountFlag {
*messagesFlag = true
}
scanner := bufio.NewScanner(file)
if *messagesFlag {
messagesInfo(scanner)
} else if *receivedMessageCountFlag {
receivedMessageCountInfo(scanner)
}
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
fmt.Printf("Error reading file: %s\n", err) fmt.Printf("Error reading file: %s\n", err)