2017-01-23 20:40:39 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-02-11 14:59:08 -05:00
|
|
|
"encoding/json"
|
2017-02-11 06:06:45 -05:00
|
|
|
"errors"
|
2017-06-16 20:55:22 -04:00
|
|
|
"fmt"
|
2017-01-23 20:40:39 -05:00
|
|
|
"github.com/yuin/gopher-lua"
|
2017-01-27 11:04:39 -05:00
|
|
|
"log"
|
2017-01-23 20:40:39 -05:00
|
|
|
"net/http"
|
2017-06-16 20:55:22 -04:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2017-01-23 20:40:39 -05:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
var reportTabulationRE *regexp.Regexp
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
reportTabulationRE = regexp.MustCompile(`^/report/[0-9]+/tabulation/?$`)
|
|
|
|
}
|
|
|
|
|
2017-01-23 20:40:39 -05:00
|
|
|
//type and value to store user in lua's Context
|
|
|
|
type key int
|
|
|
|
|
2017-01-27 11:04:39 -05:00
|
|
|
const (
|
|
|
|
userContextKey key = iota
|
|
|
|
accountsContextKey
|
|
|
|
securitiesContextKey
|
2017-01-27 21:50:02 -05:00
|
|
|
balanceContextKey
|
2017-01-27 11:04:39 -05:00
|
|
|
)
|
2017-01-23 20:40:39 -05:00
|
|
|
|
2017-02-19 07:50:36 -05:00
|
|
|
const luaTimeoutSeconds time.Duration = 30 // maximum time a lua request can run for
|
2017-01-23 20:40:39 -05:00
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
type Report struct {
|
|
|
|
ReportId int64
|
|
|
|
UserId int64
|
|
|
|
Name string
|
|
|
|
Lua string
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-02-11 14:59:08 -05:00
|
|
|
type Series struct {
|
2017-02-17 10:01:31 -05:00
|
|
|
Values []float64
|
|
|
|
Series map[string]*Series
|
2017-02-11 14:59:08 -05:00
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
type Tabulation struct {
|
|
|
|
ReportId int64
|
|
|
|
Title string
|
|
|
|
Subtitle string
|
|
|
|
Units string
|
|
|
|
Labels []string
|
|
|
|
Series map[string]*Series
|
2017-02-11 14:59:08 -05:00
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
func (r *Tabulation) Write(w http.ResponseWriter) error {
|
2017-02-11 14:59:08 -05:00
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
return enc.Encode(r)
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
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) {
|
2017-02-11 06:06:45 -05:00
|
|
|
// Create a new LState without opening the default libs for security
|
|
|
|
L := lua.NewState(lua.Options{SkipOpenLibs: true})
|
|
|
|
defer L.Close()
|
|
|
|
|
|
|
|
// Create a new context holding the current user with a timeout
|
|
|
|
ctx := context.WithValue(context.Background(), userContextKey, user)
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, luaTimeoutSeconds*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
L.SetContext(ctx)
|
|
|
|
|
|
|
|
for _, pair := range []struct {
|
|
|
|
n string
|
|
|
|
f lua.LGFunction
|
|
|
|
}{
|
|
|
|
{lua.LoadLibName, lua.OpenPackage}, // Must be first
|
|
|
|
{lua.BaseLibName, lua.OpenBase},
|
|
|
|
{lua.TabLibName, lua.OpenTable},
|
|
|
|
{lua.StringLibName, lua.OpenString},
|
|
|
|
{lua.MathLibName, lua.OpenMath},
|
|
|
|
} {
|
|
|
|
if err := L.CallByParam(lua.P{
|
|
|
|
Fn: L.NewFunction(pair.f),
|
|
|
|
NRet: 0,
|
|
|
|
Protect: true,
|
|
|
|
}, lua.LString(pair.n)); err != nil {
|
|
|
|
return nil, errors.New("Error initializing Lua packages")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
luaRegisterAccounts(L)
|
|
|
|
luaRegisterSecurities(L)
|
|
|
|
luaRegisterBalances(L)
|
|
|
|
luaRegisterDates(L)
|
2017-06-16 20:55:22 -04:00
|
|
|
luaRegisterTabulations(L)
|
2017-02-11 06:06:45 -05:00
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
err := L.DoString(report.Lua)
|
2017-02-11 06:06:45 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := L.CallByParam(lua.P{
|
|
|
|
Fn: L.GetGlobal("generate"),
|
|
|
|
NRet: 1,
|
|
|
|
Protect: true,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
value := L.Get(-1)
|
|
|
|
if ud, ok := value.(*lua.LUserData); ok {
|
2017-06-16 20:55:22 -04:00
|
|
|
if tabulation, ok := ud.Value.(*Tabulation); ok {
|
|
|
|
return tabulation, nil
|
2017-02-11 06:06:45 -05:00
|
|
|
} else {
|
2017-06-16 20:55:22 -04:00
|
|
|
return nil, fmt.Errorf("generate() for %s (Id: %d) didn't return a tabulation", report.Name, report.ReportId)
|
2017-02-11 06:06:45 -05:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-16 20:55:22 -04:00
|
|
|
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
|
2017-02-11 06:06:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-23 20:40:39 -05:00
|
|
|
func ReportHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user, err := GetUserFromSession(r)
|
|
|
|
if err != nil {
|
|
|
|
WriteError(w, 1 /*Not Signed In*/)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
if r.Method == "POST" {
|
|
|
|
report_json := r.PostFormValue("report")
|
|
|
|
if report_json == "" {
|
2017-02-07 20:59:52 -05:00
|
|
|
WriteError(w, 3 /*Invalid Request*/)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
var report Report
|
|
|
|
err := report.Read(report_json)
|
|
|
|
if err != nil {
|
2017-02-11 06:06:45 -05:00
|
|
|
WriteError(w, 3 /*Invalid Request*/)
|
|
|
|
return
|
2017-01-23 20:40:39 -05:00
|
|
|
}
|
2017-06-16 20:55:22 -04:00
|
|
|
report.ReportId = -1
|
|
|
|
report.UserId = user.UserId
|
2017-01-27 11:04:39 -05:00
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
err = InsertReport(&report)
|
2017-02-11 06:06:45 -05:00
|
|
|
if err != nil {
|
|
|
|
WriteError(w, 999 /*Internal Error*/)
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
2017-01-23 20:40:39 -05:00
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
w.WriteHeader(201 /*Created*/)
|
2017-02-11 06:06:45 -05:00
|
|
|
err = report.Write(w)
|
2017-01-23 20:40:39 -05:00
|
|
|
if err != nil {
|
2017-02-11 06:06:45 -05:00
|
|
|
WriteError(w, 999 /*Internal Error*/)
|
|
|
|
log.Print(err)
|
2017-02-07 20:59:52 -05:00
|
|
|
return
|
2017-01-23 20:40:39 -05:00
|
|
|
}
|
2017-06-16 20:55:22 -04:00
|
|
|
} 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)
|
|
|
|
}
|
2017-01-23 20:40:39 -05:00
|
|
|
}
|
|
|
|
}
|