added lua filter

This commit is contained in:
BroodjeAap 2022-11-22 20:06:12 +00:00
parent c4a95b95b2
commit 882130053c
6 changed files with 102 additions and 4 deletions

3
go.mod
View file

@ -9,8 +9,10 @@ require (
github.com/gin-gonic/gin v1.8.1
github.com/go-playground/validator/v10 v10.11.0
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/robfig/cron/v3 v3.0.0
github.com/spf13/viper v1.12.0
github.com/tidwall/gjson v1.14.2
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64
golang.org/x/net v0.0.0-20220722155237-a158d28d115b
gorm.io/driver/sqlite v1.3.6
gorm.io/gorm v1.23.8
@ -37,7 +39,6 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/robfig/cron/v3 v3.0.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect

2
go.sum
View file

@ -243,6 +243,8 @@ github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64 h1:5mLPGnFdSsevFRFc9q3yYbBkB6tsm4aCwwQV/j1JQAQ=
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=

View file

@ -16,6 +16,7 @@ import (
"github.com/andybalholm/cascadia"
"github.com/antchfx/htmlquery"
"github.com/tidwall/gjson"
lua "github.com/yuin/gopher-lua"
"golang.org/x/net/html"
"gorm.io/gorm"
)
@ -130,6 +131,10 @@ func getFilterResult(filters []Filter, filter *Filter, watch *Watch, web *Web, d
{
notifyFilter(filters, filter, watch, web, debug)
}
case filter.Type == "lua":
{
luaFilter(filter)
}
case filter.Type == "cron":
{
@ -791,3 +796,28 @@ func triggerSchedule(watchID uint, web *Web) {
buildFilterTree(filters, connections)
processFilters(filters, web, watch, false)
}
func luaFilter(filter *Filter) {
L := lua.NewState()
defer L.Close()
inputs := L.CreateTable(10, 0)
for _, parent := range filter.Parents {
for _, result := range parent.Results {
inputs.Append(lua.LString(result))
}
}
L.SetGlobal("inputs", inputs)
outputs := L.CreateTable(10, 0)
L.SetGlobal("outputs", outputs)
err := L.DoString(filter.Var1)
if err != nil {
filter.log(err)
return
}
outputs.ForEach(
func(key lua.LValue, value lua.LValue) {
filter.Results = append(filter.Results, value.String())
},
)
}

View file

@ -469,7 +469,7 @@ function onTypeChange(node) {
var1Input.value = var1Value;
var1Input.classList.add("form-control");
var1Label.innerHTML = "Template";
var1Input.placeholder = "{{ .Watch.Name }} new lowest price: {{ Price }}!";
var1Input.placeholder = "{{ .WatchName }} new lowest price: {{ .Price }}!";
var1Div.appendChild(var1Input);
var var2Input = document.createElement("input");
var2Input.name = "var2";
@ -528,6 +528,37 @@ function onTypeChange(node) {
var3Div.appendChild(var3Input);
break;
}
case "lua": {
var var1Input = document.createElement("textarea");
var1Input.name = "var1";
var1Input.id = "var1Input";
var1Input.value = var1Value;
var1Input.classList.add("form-control");
var1Label.innerHTML = "Template";
var1Input.placeholder = "for i,input in pairs(inputs) do\n\ttable.insert(outputs, input)\nend";
if (var1Value == "") {
var1Input.value = "for i,input in pairs(inputs) do\n\ttable.insert(outputs, input)\nend";
}
var1Input.rows = 10;
var1Div.appendChild(var1Input);
var var2Input = document.createElement("input");
var2Input.name = "var2";
var2Input.id = "var2Input";
var2Input.value = var2Value;
var2Input.classList.add("form-control");
var2Input.disabled = true;
var2Label.innerHTML = "-";
var2Div.appendChild(var2Input);
var var3Input = document.createElement("input");
var3Input.name = "var3";
var3Input.id = "var3Input";
var3Input.value = var3Value;
var3Input.classList.add("form-control");
var3Input.disabled = true;
var3Label.innerHTML = "-";
var3Div.appendChild(var3Input);
break;
}
}
}
function onMathChange(node) {

View file

@ -463,7 +463,7 @@ function onTypeChange(node: DiagramNode | null = null){
var1Input.value = var1Value;
var1Input.classList.add("form-control")
var1Label.innerHTML = "Template";
var1Input.placeholder = "{{ .Watch.Name }} new lowest price: {{ Price }}!";
var1Input.placeholder = "{{ .WatchName }} new lowest price: {{ .Price }}!";
var1Div.appendChild(var1Input);
let var2Input = document.createElement("input");
@ -515,6 +515,39 @@ function onTypeChange(node: DiagramNode | null = null){
var2Div.appendChild(enabledSelect);
var2Label.innerHTML = "Enabled"
let var3Input = document.createElement("input");
var3Input.name = "var3";
var3Input.id = "var3Input";
var3Input.value = var3Value;
var3Input.classList.add("form-control");
var3Input.disabled = true;
var3Label.innerHTML = "-";
var3Div.appendChild(var3Input);
break;
}
case "lua":{
let var1Input = document.createElement("textarea");
var1Input.name = "var1";
var1Input.id = "var1Input";
var1Input.value = var1Value;
var1Input.classList.add("form-control")
var1Label.innerHTML = "Template";
var1Input.placeholder = "for i,input in pairs(inputs) do\n\ttable.insert(outputs, input)\nend";
if (var1Value == "") {
var1Input.value = "for i,input in pairs(inputs) do\n\ttable.insert(outputs, input)\nend";
}
var1Input.rows = 10;
var1Div.appendChild(var1Input);
let var2Input = document.createElement("input");
var2Input.name = "var2";
var2Input.id = "var2Input";
var2Input.value = var2Value;
var2Input.classList.add("form-control")
var2Input.disabled = true;
var2Label.innerHTML = "-";
var2Div.appendChild(var2Input);
let var3Input = document.createElement("input");
var3Input.name = "var3";
var3Input.id = "var3Input";

View file

@ -83,8 +83,9 @@
<option value="math">Math</option>
<option value="store">Store</option>
<option value="condition">Condition</option>
<option value="notify">Notify</option>`
<option value="notify">Notify</option>
<option value="cron">Schedule</option>
<option value="lua">Lua</option>
</select>
</div>