now working on windows, included more reward types

This commit is contained in:
Nils Stinnesbeck 2020-11-17 20:28:06 +01:00
parent 95587ba68d
commit 2d97ec49a4
Signed by: nils
GPG Key ID: 86D4882C6C6CA48B
2 changed files with 21 additions and 4 deletions

1
go.mod
View File

@ -3,7 +3,6 @@ module git.nils.zone/nils/goCake
go 1.15
require (
git.nils.zone/nils/prettify v0.0.4
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.0.3

View File

@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"net/http"
"runtime"
"strconv"
"strings"
"time"
@ -29,6 +30,8 @@ type rewards struct {
LapisDFI map[time.Month]string
Lapis map[time.Month]string
Referral map[time.Month]string
Airdrop map[time.Month]string
Swapped map[time.Month]string
}
// Rewards needs a comment
@ -115,6 +118,7 @@ func getOtherColors(color string, factor float64) string {
func uploadFile(w http.ResponseWriter, r *http.Request) ([]line, error) {
// Maximum upload of 10 MB files
r.ParseMultipartForm(10 << 20)
defer r.Body.Close()
// Get handler for filename, size and headers
file, handler, err := r.FormFile("csvFile")
@ -125,7 +129,8 @@ func uploadFile(w http.ResponseWriter, r *http.Request) ([]line, error) {
defer file.Close()
// check if we got a csv file
if handler.Header["Content-Type"][0] == "text/csv" {
switch handler.Header["Content-Type"][0] {
case "text/csv", "application/vnd.ms-excel":
// accept this gift and read content of file
fileContents, err := ioutil.ReadAll(file)
if err != nil {
@ -181,6 +186,8 @@ func monthlyRewardOverview(lines []line) rewards {
lapisDFI := make(map[time.Month]float64)
lapis := make(map[time.Month]float64)
referral := make(map[time.Month]float64)
airdrop := make(map[time.Month]float64)
swapped := make(map[time.Month]float64)
// loop through all lines
for i := range lines {
@ -196,6 +203,10 @@ func monthlyRewardOverview(lines []line) rewards {
lapis[lines[i].Date.Month()] += lines[i].Amount
case "Referral reward":
referral[lines[i].Date.Month()] += lines[i].Amount
case "Bonus/Airdrop":
airdrop[lines[i].Date.Month()] += lines[i].Amount
case "Swapped in":
swapped[lines[i].Date.Month()] += lines[i].Amount
}
}
@ -209,7 +220,6 @@ func monthlyRewardOverview(lines []line) rewards {
Lapis: fillSums(lapis, precision),
Referral: fillSums(referral, precision),
}
return r
}
@ -232,7 +242,15 @@ func fillSums(s map[time.Month]float64, precision int) map[time.Month]string {
func readUploadedFile(fileContents []byte) ([]line, error) {
var lines []line
csvLines := strings.Split(string(fileContents), "\n")
var csvLines []string
// newlines need to be handled differently on windows
switch runtime.GOOS {
case "windows":
csvLines = strings.Split(string(fileContents), "\r\n")
default:
csvLines = strings.Split(string(fileContents), "\n")
}
// loop through all lines (except headers)
for _, csvLine := range csvLines[1:] {