Skip to content

Instantly share code, notes, and snippets.

@imjasonh
Created July 12, 2013 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imjasonh/5987723 to your computer and use it in GitHub Desktop.
Save imjasonh/5987723 to your computer and use it in GitHub Desktop.
Beginnings of an App Engine app to ingest Flywheel spreadsheets and produce JSON which can be played with using D3 (see https://gist.github.com/ImJasonH/5535583)
package flywheel
import (
"archive/zip"
"encoding/json"
"fmt"
"log"
"strconv"
"time"
"github.com/tealeg/xlsx"
)
type Data struct {
Data []Row `json:"data"`
//Avg Row `json:"avg"`
//Total Row `json:"avg"`
//Min Row `json:"min"`
//Max Row `json:"max"`
}
type Row struct {
Time int64 `json:"time"`
Studio string `json:"studio"`
Instructor string `json:"instructor"`
AvgRPM int `json:"avgRpm"`
MaxRPM int `json:"maxRpm"`
AvgTorq int `json:"avgTorq"`
MaxTorq int `json:"maxTorq"`
AvgSpeed int `json:"avgSpeed"`
Duration float64 `json:"duration"`
Power int `json:"power"`
Distance int `json:"distance"`
MinCal int `json:"minCal"`
MaxCal int `json:"maxCal"`
}
func Convert(z zip.ReadCloser) []byte {
x, err := xlsx.ReadZip(filename)
if err != nil {
log.Fatal(err)
}
data := Data{}
rows := []Row{}
for i := 1; i < len(x.Sheets[0].Rows); i++ {
cells := x.Sheets[0].Rows[i].Cells
row := new(Row)
dateStr := fmt.Sprintf("%s %s", cells[0].Value, cells[1].Value)
t, err := time.Parse("20060102 15:04", dateStr)
if err != nil {
log.Fatal(err)
}
row.Time = t.Unix()
row.Studio = cells[2].Value
row.Instructor = cells[3].Value
row.AvgRPM = toInt(cells[4].Value)
row.MaxRPM = toInt(cells[5].Value)
row.AvgTorq = toInt(cells[6].Value)
row.MaxTorq = toInt(cells[7].Value)
row.AvgSpeed = toInt(cells[8].Value)
row.Duration = toFloat(cells[9].Value)
row.Power = toInt(cells[10].Value)
row.Distance = toInt(cells[11].Value)
row.MinCal = toInt(cells[12].Value)
row.MaxCal = toInt(cells[13].Value)
rows = append(rows, *row)
}
data.Data = rows
b, err := json.Marshal(data)
if err != nil {
log.Fatal(err)
}
return b
}
func toInt(s string) int {
i, err := strconv.ParseInt(s, 10, 0)
if err != nil {
log.Fatal(err)
}
return int(i)
}
func toFloat(s string) float64 {
i, err := strconv.ParseFloat(s, 0)
if err != nil {
log.Fatal(err)
}
return i
}
package flywheel
import (
"archive/zip"
"net/http"
"strings"
)
const maxBytes = 2 >> 20
func init() {
http.HandleFunc("/upload", handleUpload)
}
func handleUpload(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(maxBytes)
f, fh, err := r.FormFile("file")
if err != nil || !strings.HasSuffix(fh.Filename, ".xlsx") {
http.Error(w, "Invalid file", http.StatusBadRequest)
return
}
z, err = zip.NewReader(f, r.ContentLength)
if err != nil {
http.Error(w, "Invalid file", http.StatusBadRequest)
return
}
w.Write(Convert(z))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment