Added save to json, parse type of output file
This commit is contained in:
15
src/main.go
15
src/main.go
@@ -240,6 +240,7 @@ func main() {
|
|||||||
|
|
||||||
keyboardID := flag.Int("id", -1, "Your keyboard id")
|
keyboardID := flag.Int("id", -1, "Your keyboard id")
|
||||||
outputPath := flag.String("o", "", "Path to export file")
|
outputPath := flag.String("o", "", "Path to export file")
|
||||||
|
fullExport := flag.Bool("full", false, "Export full stats")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
log.Println("keyboardID =", *keyboardID, "outputPath =", *outputPath)
|
log.Println("keyboardID =", *keyboardID, "outputPath =", *outputPath)
|
||||||
@@ -299,7 +300,17 @@ func main() {
|
|||||||
time.Sleep(SLEEP_TIME)
|
time.Sleep(SLEEP_TIME)
|
||||||
}
|
}
|
||||||
case *outputPath != "":
|
case *outputPath != "":
|
||||||
exportingData := GetStatTimesFromDb(db, 0, keyMap) //exporting here
|
re := regexp.MustCompile("\\.\\S+")
|
||||||
SaveToCsvFile(exportingData, keyMap, *outputPath, false)
|
filetype := re.FindString(*outputPath)
|
||||||
|
exportingData := GetStatTimesFromDb(db, 0, keyMap)
|
||||||
|
log.Println(filetype)
|
||||||
|
switch filetype {
|
||||||
|
case ".csv":
|
||||||
|
SaveToCsvFile(exportingData, keyMap, *outputPath, *fullExport)
|
||||||
|
case ".json":
|
||||||
|
SaveToJSONFile(exportingData, keyMap, *outputPath, *fullExport)
|
||||||
|
default:
|
||||||
|
log.Fatal("Incorrect file type")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@@ -10,7 +11,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SaveToCsvWriter(data []StatForTime, keyMap map[uint8]string, writerOut io.Writer, isOnlySum bool) {
|
func SaveToCsvWriter(data []StatForTime, keyMap map[uint8]string, writerOut io.Writer, fullExport bool) {
|
||||||
|
|
||||||
numKeysInt := make([]int, 0)
|
numKeysInt := make([]int, 0)
|
||||||
for key := range keyMap {
|
for key := range keyMap {
|
||||||
@@ -24,7 +25,7 @@ func SaveToCsvWriter(data []StatForTime, keyMap map[uint8]string, writerOut io.W
|
|||||||
|
|
||||||
titleLine := make([]string, 0)
|
titleLine := make([]string, 0)
|
||||||
titleLine = append(titleLine, "Time")
|
titleLine = append(titleLine, "Time")
|
||||||
if !isOnlySum {
|
if fullExport {
|
||||||
for _, key := range numKeys {
|
for _, key := range numKeys {
|
||||||
titleLine = append(titleLine, keyMap[key])
|
titleLine = append(titleLine, keyMap[key])
|
||||||
}
|
}
|
||||||
@@ -39,7 +40,7 @@ func SaveToCsvWriter(data []StatForTime, keyMap map[uint8]string, writerOut io.W
|
|||||||
var sum int
|
var sum int
|
||||||
for _, key := range numKeys {
|
for _, key := range numKeys {
|
||||||
sum += rec.keys[key]
|
sum += rec.keys[key]
|
||||||
if !isOnlySum {
|
if fullExport {
|
||||||
line = append(line, strconv.Itoa(rec.keys[key]))
|
line = append(line, strconv.Itoa(rec.keys[key]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,7 +52,7 @@ func SaveToCsvWriter(data []StatForTime, keyMap map[uint8]string, writerOut io.W
|
|||||||
writer.WriteAll(table)
|
writer.WriteAll(table)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveToCsvFile(data []StatForTime, keyMap map[uint8]string, path string, isOnlySum bool) {
|
func SaveToCsvFile(data []StatForTime, keyMap map[uint8]string, path string, fullExport bool) {
|
||||||
csvfile, err := os.Create(path)
|
csvfile, err := os.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -59,5 +60,44 @@ func SaveToCsvFile(data []StatForTime, keyMap map[uint8]string, path string, isO
|
|||||||
}
|
}
|
||||||
defer csvfile.Close()
|
defer csvfile.Close()
|
||||||
|
|
||||||
SaveToCsvWriter(data, keyMap, csvfile, isOnlySum)
|
SaveToCsvWriter(data, keyMap, csvfile, fullExport)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SaveToJSONWriter(data []StatForTime, keyMap map[uint8]string, writerOut io.Writer, fullExport bool) {
|
||||||
|
type JSONStatForTime struct {
|
||||||
|
Time int64
|
||||||
|
Keys map[string]int
|
||||||
|
}
|
||||||
|
|
||||||
|
table := make([]JSONStatForTime, len(data))
|
||||||
|
for i, stat := range data {
|
||||||
|
table[i].Keys = make(map[string]int)
|
||||||
|
|
||||||
|
table[i].Time = stat.time
|
||||||
|
var sum int
|
||||||
|
for numKey, key := range keyMap {
|
||||||
|
if fullExport {
|
||||||
|
table[i].Keys[key] = stat.keys[numKey]
|
||||||
|
}
|
||||||
|
sum += stat.keys[numKey]
|
||||||
|
}
|
||||||
|
table[i].Keys["sum"] = sum
|
||||||
|
}
|
||||||
|
|
||||||
|
outString, err := json.Marshal(table)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
writerOut.Write(outString)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SaveToJSONFile(data []StatForTime, keyMap map[uint8]string, path string, fullExport bool) {
|
||||||
|
jsonFile, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer jsonFile.Close()
|
||||||
|
|
||||||
|
SaveToJSONWriter(data, keyMap, jsonFile, fullExport)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ func BenchmarkCsvSavingOnlySum(b *testing.B) {
|
|||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
||||||
SaveToCsvWriter(data, keyMap, tmpFile, true)
|
SaveToCsvWriter(data, keyMap, tmpFile, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkCsvSaving(b *testing.B) {
|
func BenchmarkCsvSaving(b *testing.B) {
|
||||||
@@ -55,5 +55,5 @@ func BenchmarkCsvSaving(b *testing.B) {
|
|||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
||||||
SaveToCsvWriter(data, keyMap, tmpFile, false)
|
SaveToCsvWriter(data, keyMap, tmpFile, true)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user