mirror of
https://github.com/aclindsa/moneygo.git
synced 2025-07-03 04:38:38 -04:00
Add lots of backend and back-frontend report infrastructure
This commit is contained in:
263
reports.go
263
reports.go
@ -4,14 +4,21 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/yuin/gopher-lua"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var reportTabulationRE *regexp.Regexp
|
||||
|
||||
func init() {
|
||||
reportTabulationRE = regexp.MustCompile(`^/report/[0-9]+/tabulation/?$`)
|
||||
}
|
||||
|
||||
//type and value to store user in lua's Context
|
||||
type key int
|
||||
|
||||
@ -24,19 +31,11 @@ const (
|
||||
|
||||
const luaTimeoutSeconds time.Duration = 30 // maximum time a lua request can run for
|
||||
|
||||
type Series struct {
|
||||
Values []float64
|
||||
Series map[string]*Series
|
||||
}
|
||||
|
||||
type Report struct {
|
||||
ReportId string
|
||||
Title string
|
||||
Subtitle string
|
||||
XAxisLabel string
|
||||
YAxisLabel string
|
||||
Labels []string
|
||||
Series map[string]*Series
|
||||
ReportId int64
|
||||
UserId int64
|
||||
Name string
|
||||
Lua string
|
||||
}
|
||||
|
||||
func (r *Report) Write(w http.ResponseWriter) error {
|
||||
@ -44,7 +43,90 @@ func (r *Report) Write(w http.ResponseWriter) error {
|
||||
return enc.Encode(r)
|
||||
}
|
||||
|
||||
func runReport(user *User, reportpath string) (*Report, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
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 (r *Tabulation) Write(w http.ResponseWriter) error {
|
||||
enc := json.NewEncoder(w)
|
||||
return enc.Encode(r)
|
||||
}
|
||||
|
||||
func GetReport(reportid int64, userid int64) (*Report, error) {
|
||||
var r Report
|
||||
|
||||
err := DB.SelectOne(&r, "SELECT * from reports where UserId=? AND ReportId=?", userid, reportid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func GetReports(userid int64) (*[]Report, error) {
|
||||
var reports []Report
|
||||
|
||||
_, err := DB.Select(&reports, "SELECT * from reports where UserId=?", userid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &reports, nil
|
||||
}
|
||||
|
||||
func InsertReport(r *Report) error {
|
||||
err := DB.Insert(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateReport(r *Report) error {
|
||||
count, err := DB.Update(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count != 1 {
|
||||
return errors.New("Updated more than one report")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteReport(r *Report) error {
|
||||
count, err := DB.Delete(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count != 1 {
|
||||
return errors.New("Deleted more than one report")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func runReport(user *User, report *Report) (*Tabulation, error) {
|
||||
// Create a new LState without opening the default libs for security
|
||||
L := lua.NewState(lua.Options{SkipOpenLibs: true})
|
||||
defer L.Close()
|
||||
@ -78,9 +160,9 @@ func runReport(user *User, reportpath string) (*Report, error) {
|
||||
luaRegisterSecurities(L)
|
||||
luaRegisterBalances(L)
|
||||
luaRegisterDates(L)
|
||||
luaRegisterReports(L)
|
||||
luaRegisterTabulations(L)
|
||||
|
||||
err := L.DoFile(reportpath)
|
||||
err := L.DoString(report.Lua)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -96,13 +178,36 @@ func runReport(user *User, reportpath string) (*Report, error) {
|
||||
|
||||
value := L.Get(-1)
|
||||
if ud, ok := value.(*lua.LUserData); ok {
|
||||
if report, ok := ud.Value.(*Report); ok {
|
||||
return report, nil
|
||||
if tabulation, ok := ud.Value.(*Tabulation); ok {
|
||||
return tabulation, nil
|
||||
} else {
|
||||
return nil, errors.New("generate() in " + reportpath + " didn't return a report")
|
||||
return nil, fmt.Errorf("generate() for %s (Id: %d) didn't return a tabulation", report.Name, report.ReportId)
|
||||
}
|
||||
} else {
|
||||
return nil, errors.New("generate() in " + reportpath + " didn't return a report")
|
||||
return nil, fmt.Errorf("generate() for %s (Id: %d) didn't even return LUserData", report.Name, report.ReportId)
|
||||
}
|
||||
}
|
||||
|
||||
func ReportTabulationHandler(w http.ResponseWriter, r *http.Request, user *User, reportid int64) {
|
||||
report, err := GetReport(reportid, user.UserId)
|
||||
if err != nil {
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
|
||||
tabulation, err := runReport(user, report)
|
||||
if err != nil {
|
||||
// TODO handle different failure cases differently
|
||||
log.Print("runReport returned:", err)
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
|
||||
err = tabulation.Write(w)
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,34 +218,132 @@ func ReportHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == "GET" {
|
||||
var reportname string
|
||||
n, err := GetURLPieces(r.URL.Path, "/report/%s", &reportname)
|
||||
if err != nil || n != 1 {
|
||||
if r.Method == "POST" {
|
||||
report_json := r.PostFormValue("report")
|
||||
if report_json == "" {
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
|
||||
reportpath := path.Join(baseDir, "reports", reportname+".lua")
|
||||
report_stat, err := os.Stat(reportpath)
|
||||
if err != nil || !report_stat.Mode().IsRegular() {
|
||||
var report Report
|
||||
err := report.Read(report_json)
|
||||
if err != nil {
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
report.ReportId = -1
|
||||
report.UserId = user.UserId
|
||||
|
||||
report, err := runReport(user, reportpath)
|
||||
err = InsertReport(&report)
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
report.ReportId = reportname
|
||||
|
||||
w.WriteHeader(201 /*Created*/)
|
||||
err = report.Write(w)
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
} else if r.Method == "GET" {
|
||||
if reportTabulationRE.MatchString(r.URL.Path) {
|
||||
var reportid int64
|
||||
n, err := GetURLPieces(r.URL.Path, "/report/%d/tabulation", &reportid)
|
||||
if err != nil || n != 1 {
|
||||
WriteError(w, 999 /*InternalError*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
ReportTabulationHandler(w, r, user, reportid)
|
||||
return
|
||||
}
|
||||
|
||||
var reportid int64
|
||||
n, err := GetURLPieces(r.URL.Path, "/report/%d", &reportid)
|
||||
if err != nil || n != 1 {
|
||||
//Return all Reports
|
||||
var rl ReportList
|
||||
reports, err := GetReports(user.UserId)
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
rl.Reports = reports
|
||||
err = (&rl).Write(w)
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// Return Report with this Id
|
||||
report, err := GetReport(reportid, user.UserId)
|
||||
if err != nil {
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
|
||||
err = report.Write(w)
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
reportid, err := GetURLID(r.URL.Path)
|
||||
if err != nil {
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == "PUT" {
|
||||
report_json := r.PostFormValue("report")
|
||||
if report_json == "" {
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
|
||||
var report Report
|
||||
err := report.Read(report_json)
|
||||
if err != nil || report.ReportId != reportid {
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
report.UserId = user.UserId
|
||||
|
||||
err = UpdateReport(&report)
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = report.Write(w)
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
} else if r.Method == "DELETE" {
|
||||
report, err := GetReport(reportid, user.UserId)
|
||||
if err != nil {
|
||||
WriteError(w, 3 /*Invalid Request*/)
|
||||
return
|
||||
}
|
||||
|
||||
err = DeleteReport(report)
|
||||
if err != nil {
|
||||
WriteError(w, 999 /*Internal Error*/)
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
|
||||
WriteSuccess(w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user