Added saving.go

This commit is contained in:
Dmitry Lyukov
2016-03-26 20:21:31 +03:00
parent 6917c4259e
commit 07131f8ab4

57
src/saving.go Normal file
View File

@@ -0,0 +1,57 @@
// saving.go
package main
import (
"encoding/csv"
"log"
"os"
"sort"
"strconv"
)
func SaveToCsvFile(data []StatForTime, keymap map[uint8]string, path string, isOnlySum bool) {
numKeysInt := make([]int, 0)
for key := range keymap {
numKeysInt = append(numKeysInt, int(key))
}
sort.Ints(numKeysInt)
numKeys := make([]uint8, 0)
for _, key := range numKeysInt {
numKeys = append(numKeys, uint8(key))
}
titleLine := make([]string, 0)
titleLine = append(titleLine, "Time")
if !isOnlySum {
for _, key := range numKeys {
titleLine = append(titleLine, keymap[key])
}
}
titleLine = append(titleLine, "Sum")
table := make([][]string, 0)
table = append(table, titleLine)
for _, rec := range data {
line := make([]string, 0)
line = append(line, strconv.Itoa(int(rec.time)))
var sum int
for _, key := range numKeys {
sum += rec.keys[key]
if !isOnlySum {
line = append(line, strconv.Itoa(rec.keys[key]))
}
}
line = append(line, strconv.Itoa(sum))
table = append(table, line)
}
csvfile, err := os.Create(path)
if err != nil {
log.Fatal(err)
return
}
defer csvfile.Close()
writer := csv.NewWriter(csvfile)
writer.WriteAll(table)
}