now working on windows, included more reward types
This commit is contained in:
parent
95587ba68d
commit
2d97ec49a4
1
go.mod
1
go.mod
@ -3,7 +3,6 @@ module git.nils.zone/nils/goCake
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.nils.zone/nils/prettify v0.0.4
|
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/kr/pretty v0.1.0 // indirect
|
github.com/kr/pretty v0.1.0 // indirect
|
||||||
github.com/lucasb-eyer/go-colorful v1.0.3
|
github.com/lucasb-eyer/go-colorful v1.0.3
|
||||||
|
24
rewards.go
24
rewards.go
@ -6,6 +6,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -29,6 +30,8 @@ type rewards struct {
|
|||||||
LapisDFI map[time.Month]string
|
LapisDFI map[time.Month]string
|
||||||
Lapis map[time.Month]string
|
Lapis map[time.Month]string
|
||||||
Referral map[time.Month]string
|
Referral map[time.Month]string
|
||||||
|
Airdrop map[time.Month]string
|
||||||
|
Swapped map[time.Month]string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rewards needs a comment
|
// 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) {
|
func uploadFile(w http.ResponseWriter, r *http.Request) ([]line, error) {
|
||||||
// Maximum upload of 10 MB files
|
// Maximum upload of 10 MB files
|
||||||
r.ParseMultipartForm(10 << 20)
|
r.ParseMultipartForm(10 << 20)
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
// Get handler for filename, size and headers
|
// Get handler for filename, size and headers
|
||||||
file, handler, err := r.FormFile("csvFile")
|
file, handler, err := r.FormFile("csvFile")
|
||||||
@ -125,7 +129,8 @@ func uploadFile(w http.ResponseWriter, r *http.Request) ([]line, error) {
|
|||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
// check if we got a csv file
|
// 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
|
// accept this gift and read content of file
|
||||||
fileContents, err := ioutil.ReadAll(file)
|
fileContents, err := ioutil.ReadAll(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -181,6 +186,8 @@ func monthlyRewardOverview(lines []line) rewards {
|
|||||||
lapisDFI := make(map[time.Month]float64)
|
lapisDFI := make(map[time.Month]float64)
|
||||||
lapis := make(map[time.Month]float64)
|
lapis := make(map[time.Month]float64)
|
||||||
referral := 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
|
// loop through all lines
|
||||||
for i := range lines {
|
for i := range lines {
|
||||||
@ -196,6 +203,10 @@ func monthlyRewardOverview(lines []line) rewards {
|
|||||||
lapis[lines[i].Date.Month()] += lines[i].Amount
|
lapis[lines[i].Date.Month()] += lines[i].Amount
|
||||||
case "Referral reward":
|
case "Referral reward":
|
||||||
referral[lines[i].Date.Month()] += lines[i].Amount
|
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),
|
Lapis: fillSums(lapis, precision),
|
||||||
Referral: fillSums(referral, precision),
|
Referral: fillSums(referral, precision),
|
||||||
}
|
}
|
||||||
|
|
||||||
return r
|
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) {
|
func readUploadedFile(fileContents []byte) ([]line, error) {
|
||||||
var lines []line
|
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)
|
// loop through all lines (except headers)
|
||||||
for _, csvLine := range csvLines[1:] {
|
for _, csvLine := range csvLines[1:] {
|
||||||
|
Loading…
Reference in New Issue
Block a user