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-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-02-11 06:06:45 -05:00
|
|
|
"os"
|
2017-02-07 20:59:52 -05:00
|
|
|
"path"
|
2017-01-23 20:40:39 -05:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
//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-01-27 11:04:39 -05:00
|
|
|
const luaTimeoutSeconds time.Duration = 5 // maximum time a lua request can run for
|
2017-01-23 20:40:39 -05:00
|
|
|
|
2017-02-11 14:59:08 -05:00
|
|
|
type Series struct {
|
|
|
|
Values []float64
|
|
|
|
Children map[string]*Series
|
|
|
|
}
|
|
|
|
|
|
|
|
type Report struct {
|
|
|
|
ReportId string
|
|
|
|
Title string
|
|
|
|
Subtitle string
|
|
|
|
XAxisLabel string
|
|
|
|
YAxisLabel string
|
|
|
|
Labels []string
|
|
|
|
Series map[string]*Series
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Report) Write(w http.ResponseWriter) error {
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
return enc.Encode(r)
|
|
|
|
}
|
|
|
|
|
2017-02-11 06:06:45 -05:00
|
|
|
func runReport(user *User, reportpath string) (*Report, error) {
|
|
|
|
// 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)
|
|
|
|
luaRegisterReports(L)
|
|
|
|
|
|
|
|
err := L.DoFile(reportpath)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if report, ok := ud.Value.(*Report); ok {
|
|
|
|
return report, nil
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("generate() in " + reportpath + " didn't return a report")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("generate() in " + reportpath + " didn't return a report")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method == "GET" {
|
2017-02-07 20:59:52 -05:00
|
|
|
var reportname string
|
|
|
|
n, err := GetURLPieces(r.URL.Path, "/report/%s", &reportname)
|
|
|
|
if err != nil || n != 1 {
|
|
|
|
WriteError(w, 3 /*Invalid Request*/)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reportpath := path.Join(baseDir, "reports", reportname+".lua")
|
2017-02-11 06:06:45 -05:00
|
|
|
report_stat, err := os.Stat(reportpath)
|
|
|
|
if err != nil || !report_stat.Mode().IsRegular() {
|
|
|
|
WriteError(w, 3 /*Invalid Request*/)
|
|
|
|
return
|
2017-01-23 20:40:39 -05:00
|
|
|
}
|
2017-01-27 11:04:39 -05:00
|
|
|
|
2017-02-11 06:06:45 -05:00
|
|
|
report, err := runReport(user, reportpath)
|
|
|
|
if err != nil {
|
|
|
|
WriteError(w, 999 /*Internal Error*/)
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
2017-02-11 14:59:08 -05:00
|
|
|
report.ReportId = reportname
|
2017-01-23 20:40:39 -05:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|