mirror of https://github.com/status-im/msgstat.git
Add commit of cli and stats pkg
This commit is contained in:
parent
87f81f5ab9
commit
03ea9391f8
|
@ -0,0 +1,153 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/status-im/msgstat/stats"
|
||||
)
|
||||
|
||||
var (
|
||||
gitCommit = "" // rely on linker: -ldflags -X main.gitCommit"
|
||||
buildStamp = "" // rely on linker: -ldflags -X main.buildStamp"
|
||||
versionStamp = "0.0.1" // rely on linker -ldflags -X main.versionStamp"
|
||||
)
|
||||
|
||||
var (
|
||||
sourceFile = flag.String("out", "", "output file to save processed data into")
|
||||
targetFile = flag.String("file", "", "input file containing status-go logs")
|
||||
format = flag.String("format", "json", "format of output data. -format=json|yaml|csv")
|
||||
version = flag.Bool("v", false, "Print version")
|
||||
)
|
||||
|
||||
type wopCloser struct {
|
||||
io.Writer
|
||||
}
|
||||
|
||||
// Close does nothing.
|
||||
func (wopCloser) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Usage = printUsage
|
||||
flag.Parse()
|
||||
|
||||
// if we are to print version.
|
||||
if *version {
|
||||
printVersion()
|
||||
return
|
||||
}
|
||||
|
||||
if *sourceFile == "" && !hasIncomingData() {
|
||||
printUsage()
|
||||
return
|
||||
}
|
||||
|
||||
var wc io.WriteCloser
|
||||
|
||||
if *targetFile != "" {
|
||||
targetOutputFile, err := os.Open(*targetFile)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open output file %+q: %v", *targetFile, err)
|
||||
return
|
||||
}
|
||||
|
||||
wc = targetOutputFile
|
||||
} else {
|
||||
wc = wopCloser{Writer: os.Stdout}
|
||||
}
|
||||
|
||||
var rc io.ReadCloser
|
||||
|
||||
if *sourceFile != "" {
|
||||
targetInputFile, err := os.OpenFile(*sourceFile, os.O_RDONLY, 0700)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open input file %+q: %v", *sourceFile, err)
|
||||
return
|
||||
}
|
||||
|
||||
rc = targetInputFile
|
||||
} else {
|
||||
rc = ioutil.NopCloser(os.Stdin)
|
||||
}
|
||||
|
||||
if err := stats.AggregateLogs(rc, wc, *format); err != nil {
|
||||
log.Fatalf("Failed to process logs successfully: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("Aggregation Complete!")
|
||||
|
||||
if *targetFile != "" {
|
||||
log.Printf("See output in %+q\n", *targetFile)
|
||||
}
|
||||
}
|
||||
|
||||
func printVersion() {
|
||||
var vers []string
|
||||
vers = append(vers, versionStamp)
|
||||
|
||||
if buildStamp != "" {
|
||||
vers = append(vers, fmt.Sprintf("build#%s", buildStamp))
|
||||
}
|
||||
|
||||
if gitCommit != "" {
|
||||
vers = append(vers, fmt.Sprintf("git#%s", gitCommit))
|
||||
}
|
||||
|
||||
fmt.Fprint(os.Stdout, strings.Join(vers, " "))
|
||||
}
|
||||
|
||||
func printUsage() {
|
||||
fmt.Fprintf(os.Stdout, `Usage: msgstat [options]
|
||||
Msgstat processes status-go lines to generate useful message delivery facts.
|
||||
|
||||
EXAMPLES:
|
||||
|
||||
1. Read from stdin
|
||||
|
||||
cat ./statusim/status.log | msgstat read
|
||||
|
||||
2. Read from file
|
||||
|
||||
msgstat read -file=./statusim/status.log
|
||||
|
||||
3. Read from file and write to output file
|
||||
|
||||
msgstat read -file=./statusim/status.log -out=./processed-log.json
|
||||
|
||||
4. Read from file and write in yaml format
|
||||
|
||||
msgstat read -format=yaml -file=./statusim/status.log -out=./processed-log.json
|
||||
|
||||
FLAGS:
|
||||
|
||||
-v Print version.
|
||||
-file Path to status-go log file for processing.
|
||||
-out Path to store processed data from logs.
|
||||
-format Format to use to store processed data (json, yaml, csv).
|
||||
|
||||
`)
|
||||
|
||||
// flag.PrintDefaults()
|
||||
}
|
||||
|
||||
// hasIncomingData returns true/false if data is pending in stdin file.
|
||||
func hasIncomingData() bool {
|
||||
stat, err := os.Stdin.Stat()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if stat.Size() == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package stats
|
||||
|
||||
import "io"
|
||||
|
||||
// AggregateLogs processes incoming data from the reader and writes appropriate
|
||||
// data with respect to format required.
|
||||
func AggregateLogs(r io.ReadCloser, w io.WriteCloser, format string) error {
|
||||
defer w.Close()
|
||||
defer r.Close()
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue