1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-10-31 16:00:05 -04:00

Split reports into models

This commit is contained in:
Aaron Lindsay 2017-12-05 05:58:36 -05:00
parent 5f296e8669
commit 3e3038295d
7 changed files with 122 additions and 117 deletions

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"github.com/aclindsa/gorp" "github.com/aclindsa/gorp"
"github.com/aclindsa/moneygo/internal/config" "github.com/aclindsa/moneygo/internal/config"
"github.com/aclindsa/moneygo/internal/handlers"
"github.com/aclindsa/moneygo/internal/models" "github.com/aclindsa/moneygo/internal/models"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq" _ "github.com/lib/pq"
@ -44,8 +43,8 @@ func GetDbMap(db *sql.DB, dbtype config.DbType) (*gorp.DbMap, error) {
dbmap.AddTableWithName(models.Transaction{}, "transactions").SetKeys(true, "TransactionId") dbmap.AddTableWithName(models.Transaction{}, "transactions").SetKeys(true, "TransactionId")
dbmap.AddTableWithName(models.Split{}, "splits").SetKeys(true, "SplitId") dbmap.AddTableWithName(models.Split{}, "splits").SetKeys(true, "SplitId")
dbmap.AddTableWithName(models.Price{}, "prices").SetKeys(true, "PriceId") dbmap.AddTableWithName(models.Price{}, "prices").SetKeys(true, "PriceId")
rtable := dbmap.AddTableWithName(handlers.Report{}, "reports").SetKeys(true, "ReportId") rtable := dbmap.AddTableWithName(models.Report{}, "reports").SetKeys(true, "ReportId")
rtable.ColMap("Lua").SetMaxSize(handlers.LuaMaxLength + luaMaxLengthBuffer) rtable.ColMap("Lua").SetMaxSize(models.LuaMaxLength + luaMaxLengthBuffer)
err := dbmap.CreateTablesIfNotExists() err := dbmap.CreateTablesIfNotExists()
if err != nil { if err != nil {

View File

@ -2,14 +2,12 @@ package handlers
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/aclindsa/moneygo/internal/models" "github.com/aclindsa/moneygo/internal/models"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
"log" "log"
"net/http" "net/http"
"strings"
"time" "time"
) )
@ -26,67 +24,8 @@ const (
const luaTimeoutSeconds time.Duration = 30 // maximum time a lua request can run for const luaTimeoutSeconds time.Duration = 30 // maximum time a lua request can run for
type Report struct { func GetReport(tx *Tx, reportid int64, userid int64) (*models.Report, error) {
ReportId int64 var r models.Report
UserId int64
Name string
Lua string
}
// The maximum length (in bytes) the Lua code may be. This is used to set the
// max size of the database columns (with an added fudge factor)
const LuaMaxLength int = 65536
func (r *Report) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(r)
}
func (r *Report) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(r)
}
type ReportList struct {
Reports *[]Report `json:"reports"`
}
func (rl *ReportList) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(rl)
}
func (rl *ReportList) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(rl)
}
type Series struct {
Values []float64
Series map[string]*Series
}
type Tabulation struct {
ReportId int64
Title string
Subtitle string
Units string
Labels []string
Series map[string]*Series
}
func (t *Tabulation) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(t)
}
func (t *Tabulation) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(t)
}
func GetReport(tx *Tx, reportid int64, userid int64) (*Report, error) {
var r Report
err := tx.SelectOne(&r, "SELECT * from reports where UserId=? AND ReportId=?", userid, reportid) err := tx.SelectOne(&r, "SELECT * from reports where UserId=? AND ReportId=?", userid, reportid)
if err != nil { if err != nil {
@ -95,8 +34,8 @@ func GetReport(tx *Tx, reportid int64, userid int64) (*Report, error) {
return &r, nil return &r, nil
} }
func GetReports(tx *Tx, userid int64) (*[]Report, error) { func GetReports(tx *Tx, userid int64) (*[]models.Report, error) {
var reports []Report var reports []models.Report
_, err := tx.Select(&reports, "SELECT * from reports where UserId=?", userid) _, err := tx.Select(&reports, "SELECT * from reports where UserId=?", userid)
if err != nil { if err != nil {
@ -105,7 +44,7 @@ func GetReports(tx *Tx, userid int64) (*[]Report, error) {
return &reports, nil return &reports, nil
} }
func InsertReport(tx *Tx, r *Report) error { func InsertReport(tx *Tx, r *models.Report) error {
err := tx.Insert(r) err := tx.Insert(r)
if err != nil { if err != nil {
return err return err
@ -113,7 +52,7 @@ func InsertReport(tx *Tx, r *Report) error {
return nil return nil
} }
func UpdateReport(tx *Tx, r *Report) error { func UpdateReport(tx *Tx, r *models.Report) error {
count, err := tx.Update(r) count, err := tx.Update(r)
if err != nil { if err != nil {
return err return err
@ -124,7 +63,7 @@ func UpdateReport(tx *Tx, r *Report) error {
return nil return nil
} }
func DeleteReport(tx *Tx, r *Report) error { func DeleteReport(tx *Tx, r *models.Report) error {
count, err := tx.Delete(r) count, err := tx.Delete(r)
if err != nil { if err != nil {
return err return err
@ -135,7 +74,7 @@ func DeleteReport(tx *Tx, r *Report) error {
return nil return nil
} }
func runReport(tx *Tx, user *models.User, report *Report) (*Tabulation, error) { func runReport(tx *Tx, user *models.User, report *models.Report) (*models.Tabulation, error) {
// Create a new LState without opening the default libs for security // Create a new LState without opening the default libs for security
L := lua.NewState(lua.Options{SkipOpenLibs: true}) L := lua.NewState(lua.Options{SkipOpenLibs: true})
defer L.Close() defer L.Close()
@ -189,7 +128,7 @@ func runReport(tx *Tx, user *models.User, report *Report) (*Tabulation, error) {
value := L.Get(-1) value := L.Get(-1)
if ud, ok := value.(*lua.LUserData); ok { if ud, ok := value.(*lua.LUserData); ok {
if tabulation, ok := ud.Value.(*Tabulation); ok { if tabulation, ok := ud.Value.(*models.Tabulation); ok {
return tabulation, nil return tabulation, nil
} else { } else {
return nil, fmt.Errorf("generate() for %s (Id: %d) didn't return a tabulation", report.Name, report.ReportId) return nil, fmt.Errorf("generate() for %s (Id: %d) didn't return a tabulation", report.Name, report.ReportId)
@ -224,14 +163,14 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter {
} }
if r.Method == "POST" { if r.Method == "POST" {
var report Report var report models.Report
if err := ReadJSON(r, &report); err != nil { if err := ReadJSON(r, &report); err != nil {
return NewError(3 /*Invalid Request*/) return NewError(3 /*Invalid Request*/)
} }
report.ReportId = -1 report.ReportId = -1
report.UserId = user.UserId report.UserId = user.UserId
if len(report.Lua) >= LuaMaxLength { if len(report.Lua) >= models.LuaMaxLength {
return NewError(3 /*Invalid Request*/) return NewError(3 /*Invalid Request*/)
} }
@ -245,7 +184,7 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter {
} else if r.Method == "GET" { } else if r.Method == "GET" {
if context.LastLevel() { if context.LastLevel() {
//Return all Reports //Return all Reports
var rl ReportList var rl models.ReportList
reports, err := GetReports(context.Tx, user.UserId) reports, err := GetReports(context.Tx, user.UserId)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
@ -278,13 +217,13 @@ func ReportHandler(r *http.Request, context *Context) ResponseWriterWriter {
} }
if r.Method == "PUT" { if r.Method == "PUT" {
var report Report var report models.Report
if err := ReadJSON(r, &report); err != nil || report.ReportId != reportid { if err := ReadJSON(r, &report); err != nil || report.ReportId != reportid {
return NewError(3 /*Invalid Request*/) return NewError(3 /*Invalid Request*/)
} }
report.UserId = user.UserId report.UserId = user.UserId
if len(report.Lua) >= LuaMaxLength { if len(report.Lua) >= models.LuaMaxLength {
return NewError(3 /*Invalid Request*/) return NewError(3 /*Invalid Request*/)
} }

View File

@ -1,6 +1,7 @@
package handlers package handlers
import ( import (
"github.com/aclindsa/moneygo/internal/models"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
) )
@ -21,9 +22,9 @@ func luaRegisterTabulations(L *lua.LState) {
} }
// Checks whether the first lua argument is a *LUserData with *Tabulation and returns *Tabulation // Checks whether the first lua argument is a *LUserData with *Tabulation and returns *Tabulation
func luaCheckTabulation(L *lua.LState, n int) *Tabulation { func luaCheckTabulation(L *lua.LState, n int) *models.Tabulation {
ud := L.CheckUserData(n) ud := L.CheckUserData(n)
if tabulation, ok := ud.Value.(*Tabulation); ok { if tabulation, ok := ud.Value.(*models.Tabulation); ok {
return tabulation return tabulation
} }
L.ArgError(n, "tabulation expected") L.ArgError(n, "tabulation expected")
@ -31,9 +32,9 @@ func luaCheckTabulation(L *lua.LState, n int) *Tabulation {
} }
// Checks whether the first lua argument is a *LUserData with *Series and returns *Series // Checks whether the first lua argument is a *LUserData with *Series and returns *Series
func luaCheckSeries(L *lua.LState, n int) *Series { func luaCheckSeries(L *lua.LState, n int) *models.Series {
ud := L.CheckUserData(n) ud := L.CheckUserData(n)
if series, ok := ud.Value.(*Series); ok { if series, ok := ud.Value.(*models.Series); ok {
return series return series
} }
L.ArgError(n, "series expected") L.ArgError(n, "series expected")
@ -43,9 +44,9 @@ func luaCheckSeries(L *lua.LState, n int) *Series {
func luaTabulationNew(L *lua.LState) int { func luaTabulationNew(L *lua.LState) int {
numvalues := L.CheckInt(1) numvalues := L.CheckInt(1)
ud := L.NewUserData() ud := L.NewUserData()
ud.Value = &Tabulation{ ud.Value = &models.Tabulation{
Labels: make([]string, numvalues), Labels: make([]string, numvalues),
Series: make(map[string]*Series), Series: make(map[string]*models.Series),
} }
L.SetMetatable(ud, L.GetTypeMetatable(luaTabulationTypeName)) L.SetMetatable(ud, L.GetTypeMetatable(luaTabulationTypeName))
L.Push(ud) L.Push(ud)
@ -94,8 +95,8 @@ func luaTabulationSeries(L *lua.LState) int {
if ok { if ok {
ud.Value = s ud.Value = s
} else { } else {
tabulation.Series[name] = &Series{ tabulation.Series[name] = &models.Series{
Series: make(map[string]*Series), Series: make(map[string]*models.Series),
Values: make([]float64, cap(tabulation.Labels)), Values: make([]float64, cap(tabulation.Labels)),
} }
ud.Value = tabulation.Series[name] ud.Value = tabulation.Series[name]
@ -175,8 +176,8 @@ func luaSeriesSeries(L *lua.LState) int {
if ok { if ok {
ud.Value = s ud.Value = s
} else { } else {
parent.Series[name] = &Series{ parent.Series[name] = &models.Series{
Series: make(map[string]*Series), Series: make(map[string]*models.Series),
Values: make([]float64, cap(parent.Values)), Values: make([]float64, cap(parent.Values)),
} }
ud.Value = parent.Series[name] ud.Value = parent.Series[name]

View File

@ -2,7 +2,7 @@ package handlers_test
import ( import (
"fmt" "fmt"
"github.com/aclindsa/moneygo/internal/handlers" "github.com/aclindsa/moneygo/internal/models"
"net/http" "net/http"
"testing" "testing"
) )
@ -25,7 +25,7 @@ function generate()
t:title(tostring(test())) t:title(tostring(test()))
return t return t
end`, lt.Lua) end`, lt.Lua)
r := handlers.Report{ r := models.Report{
Name: lt.Name, Name: lt.Name,
Lua: lua, Lua: lua,
} }

View File

@ -2,19 +2,20 @@ package handlers_test
import ( import (
"github.com/aclindsa/moneygo/internal/handlers" "github.com/aclindsa/moneygo/internal/handlers"
"github.com/aclindsa/moneygo/internal/models"
"net/http" "net/http"
"strconv" "strconv"
"testing" "testing"
) )
func createReport(client *http.Client, report *handlers.Report) (*handlers.Report, error) { func createReport(client *http.Client, report *models.Report) (*models.Report, error) {
var r handlers.Report var r models.Report
err := create(client, report, &r, "/v1/reports/") err := create(client, report, &r, "/v1/reports/")
return &r, err return &r, err
} }
func getReport(client *http.Client, reportid int64) (*handlers.Report, error) { func getReport(client *http.Client, reportid int64) (*models.Report, error) {
var r handlers.Report var r models.Report
err := read(client, &r, "/v1/reports/"+strconv.FormatInt(reportid, 10)) err := read(client, &r, "/v1/reports/"+strconv.FormatInt(reportid, 10))
if err != nil { if err != nil {
return nil, err return nil, err
@ -22,8 +23,8 @@ func getReport(client *http.Client, reportid int64) (*handlers.Report, error) {
return &r, nil return &r, nil
} }
func getReports(client *http.Client) (*handlers.ReportList, error) { func getReports(client *http.Client) (*models.ReportList, error) {
var rl handlers.ReportList var rl models.ReportList
err := read(client, &rl, "/v1/reports/") err := read(client, &rl, "/v1/reports/")
if err != nil { if err != nil {
return nil, err return nil, err
@ -31,8 +32,8 @@ func getReports(client *http.Client) (*handlers.ReportList, error) {
return &rl, nil return &rl, nil
} }
func updateReport(client *http.Client, report *handlers.Report) (*handlers.Report, error) { func updateReport(client *http.Client, report *models.Report) (*models.Report, error) {
var r handlers.Report var r models.Report
err := update(client, report, &r, "/v1/reports/"+strconv.FormatInt(report.ReportId, 10)) err := update(client, report, &r, "/v1/reports/"+strconv.FormatInt(report.ReportId, 10))
if err != nil { if err != nil {
return nil, err return nil, err
@ -40,7 +41,7 @@ func updateReport(client *http.Client, report *handlers.Report) (*handlers.Repor
return &r, nil return &r, nil
} }
func deleteReport(client *http.Client, r *handlers.Report) error { func deleteReport(client *http.Client, r *models.Report) error {
err := remove(client, "/v1/reports/"+strconv.FormatInt(r.ReportId, 10)) err := remove(client, "/v1/reports/"+strconv.FormatInt(r.ReportId, 10))
if err != nil { if err != nil {
return err return err
@ -48,8 +49,8 @@ func deleteReport(client *http.Client, r *handlers.Report) error {
return nil return nil
} }
func tabulateReport(client *http.Client, reportid int64) (*handlers.Tabulation, error) { func tabulateReport(client *http.Client, reportid int64) (*models.Tabulation, error) {
var t handlers.Tabulation var t models.Tabulation
err := read(client, &t, "/v1/reports/"+strconv.FormatInt(reportid, 10)+"/tabulations") err := read(client, &t, "/v1/reports/"+strconv.FormatInt(reportid, 10)+"/tabulations")
if err != nil { if err != nil {
return nil, err return nil, err
@ -73,7 +74,7 @@ func TestCreateReport(t *testing.T) {
t.Errorf("Lua doesn't match") t.Errorf("Lua doesn't match")
} }
r.Lua = string(make([]byte, handlers.LuaMaxLength+1)) r.Lua = string(make([]byte, models.LuaMaxLength+1))
_, err := createReport(d.clients[orig.UserId], &r) _, err := createReport(d.clients[orig.UserId], &r)
if err == nil { if err == nil {
t.Fatalf("Expected error creating report with too-long Lua") t.Fatalf("Expected error creating report with too-long Lua")
@ -173,7 +174,7 @@ func TestUpdateReport(t *testing.T) {
t.Errorf("Lua doesn't match") t.Errorf("Lua doesn't match")
} }
r.Lua = string(make([]byte, handlers.LuaMaxLength+1)) r.Lua = string(make([]byte, models.LuaMaxLength+1))
_, err = updateReport(d.clients[orig.UserId], r) _, err = updateReport(d.clients[orig.UserId], r)
if err == nil { if err == nil {
t.Fatalf("Expected error updating report with too-long Lua") t.Fatalf("Expected error updating report with too-long Lua")
@ -214,7 +215,7 @@ func TestDeleteReport(t *testing.T) {
} }
}) })
} }
func seriesEqualityHelper(t *testing.T, orig, curr map[string]*handlers.Series, name string) { func seriesEqualityHelper(t *testing.T, orig, curr map[string]*models.Series, name string) {
if orig == nil || curr == nil { if orig == nil || curr == nil {
if orig != nil { if orig != nil {
t.Fatalf("`%s` series unexpectedly nil", name) t.Fatalf("`%s` series unexpectedly nil", name)
@ -242,7 +243,7 @@ func seriesEqualityHelper(t *testing.T, orig, curr map[string]*handlers.Series,
} }
} }
func tabulationEqualityHelper(t *testing.T, orig, curr *handlers.Tabulation) { func tabulationEqualityHelper(t *testing.T, orig, curr *models.Tabulation) {
if orig.Title != curr.Title { if orig.Title != curr.Title {
t.Errorf("Tabulation Title doesn't match") t.Errorf("Tabulation Title doesn't match")
} }

View File

@ -3,7 +3,6 @@ package handlers_test
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/aclindsa/moneygo/internal/handlers"
"github.com/aclindsa/moneygo/internal/models" "github.com/aclindsa/moneygo/internal/models"
"net/http" "net/http"
"strings" "strings"
@ -41,8 +40,8 @@ type TestData struct {
prices []models.Price prices []models.Price
accounts []models.Account // accounts must appear after their parents in this slice accounts []models.Account // accounts must appear after their parents in this slice
transactions []models.Transaction transactions []models.Transaction
reports []handlers.Report reports []models.Report
tabulations []handlers.Tabulation tabulations []models.Tabulation
} }
type TestDataFunc func(*testing.T, *TestData) type TestDataFunc func(*testing.T, *TestData)
@ -382,7 +381,7 @@ var data = []TestData{
}, },
}, },
}, },
reports: []handlers.Report{ reports: []models.Report{
{ {
UserId: 0, UserId: 0,
Name: "This Year's Monthly Expenses", Name: "This Year's Monthly Expenses",
@ -440,39 +439,39 @@ function generate()
end`, end`,
}, },
}, },
tabulations: []handlers.Tabulation{ tabulations: []models.Tabulation{
{ {
ReportId: 0, ReportId: 0,
Title: "2017 Monthly Expenses", Title: "2017 Monthly Expenses",
Subtitle: "This is my subtitle", Subtitle: "This is my subtitle",
Units: "USD", Units: "USD",
Labels: []string{"2017-01-01", "2017-02-01", "2017-03-01", "2017-04-01", "2017-05-01", "2017-06-01", "2017-07-01", "2017-08-01", "2017-09-01", "2017-10-01", "2017-11-01", "2017-12-01"}, Labels: []string{"2017-01-01", "2017-02-01", "2017-03-01", "2017-04-01", "2017-05-01", "2017-06-01", "2017-07-01", "2017-08-01", "2017-09-01", "2017-10-01", "2017-11-01", "2017-12-01"},
Series: map[string]*handlers.Series{ Series: map[string]*models.Series{
"Assets": { "Assets": {
Values: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Values: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Series: map[string]*handlers.Series{ Series: map[string]*models.Series{
"Credit Union Checking": { "Credit Union Checking": {
Values: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Values: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Series: map[string]*handlers.Series{}, Series: map[string]*models.Series{},
}, },
}, },
}, },
"Expenses": { "Expenses": {
Values: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Values: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Series: map[string]*handlers.Series{ Series: map[string]*models.Series{
"Groceries": { "Groceries": {
Values: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 87.19, 0, 0}, Values: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 87.19, 0, 0},
Series: map[string]*handlers.Series{}, Series: map[string]*models.Series{},
}, },
"Cable": { "Cable": {
Values: []float64{0, 0, 0, 0, 0, 0, 0, 0, 39.99, 0, 0, 0}, Values: []float64{0, 0, 0, 0, 0, 0, 0, 0, 39.99, 0, 0, 0},
Series: map[string]*handlers.Series{}, Series: map[string]*models.Series{},
}, },
}, },
}, },
"Credit Card": { "Credit Card": {
Values: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Values: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Series: map[string]*handlers.Series{}, Series: map[string]*models.Series{},
}, },
}, },
}, },

View File

@ -0,0 +1,66 @@
package models
import (
"encoding/json"
"net/http"
"strings"
)
type Report struct {
ReportId int64
UserId int64
Name string
Lua string
}
// The maximum length (in bytes) the Lua code may be. This is used to set the
// max size of the database columns (with an added fudge factor)
const LuaMaxLength int = 65536
func (r *Report) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(r)
}
func (r *Report) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(r)
}
type ReportList struct {
Reports *[]Report `json:"reports"`
}
func (rl *ReportList) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(rl)
}
func (rl *ReportList) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(rl)
}
type Series struct {
Values []float64
Series map[string]*Series
}
type Tabulation struct {
ReportId int64
Title string
Subtitle string
Units string
Labels []string
Series map[string]*Series
}
func (t *Tabulation) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(t)
}
func (t *Tabulation) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(t)
}