Added save to json, parse type of output file

This commit is contained in:
Dmitry Lyukov
2016-03-28 23:01:28 +03:00
parent ccddb24e1d
commit b10cb2caa5
3 changed files with 60 additions and 9 deletions

View File

@@ -240,6 +240,7 @@ func main() {
keyboardID := flag.Int("id", -1, "Your keyboard id")
outputPath := flag.String("o", "", "Path to export file")
fullExport := flag.Bool("full", false, "Export full stats")
flag.Parse()
log.Println("keyboardID =", *keyboardID, "outputPath =", *outputPath)
@@ -299,7 +300,17 @@ func main() {
time.Sleep(SLEEP_TIME)
}
case *outputPath != "":
exportingData := GetStatTimesFromDb(db, 0, keyMap) //exporting here
SaveToCsvFile(exportingData, keyMap, *outputPath, false)
re := regexp.MustCompile("\\.\\S+")
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")
}
}
}

View File

@@ -3,6 +3,7 @@ package main
import (
"encoding/csv"
"encoding/json"
"io"
"log"
"os"
@@ -10,7 +11,7 @@ import (
"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)
for key := range keyMap {
@@ -24,7 +25,7 @@ func SaveToCsvWriter(data []StatForTime, keyMap map[uint8]string, writerOut io.W
titleLine := make([]string, 0)
titleLine = append(titleLine, "Time")
if !isOnlySum {
if fullExport {
for _, key := range numKeys {
titleLine = append(titleLine, keyMap[key])
}
@@ -39,7 +40,7 @@ func SaveToCsvWriter(data []StatForTime, keyMap map[uint8]string, writerOut io.W
var sum int
for _, key := range numKeys {
sum += rec.keys[key]
if !isOnlySum {
if fullExport {
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)
}
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)
if err != nil {
log.Fatal(err)
@@ -59,5 +60,44 @@ func SaveToCsvFile(data []StatForTime, keyMap map[uint8]string, path string, isO
}
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)
}

View File

@@ -39,7 +39,7 @@ func BenchmarkCsvSavingOnlySum(b *testing.B) {
b.ResetTimer()
SaveToCsvWriter(data, keyMap, tmpFile, true)
SaveToCsvWriter(data, keyMap, tmpFile, false)
}
func BenchmarkCsvSaving(b *testing.B) {
@@ -55,5 +55,5 @@ func BenchmarkCsvSaving(b *testing.B) {
b.ResetTimer()
SaveToCsvWriter(data, keyMap, tmpFile, false)
SaveToCsvWriter(data, keyMap, tmpFile, true)
}