funcs_global_state.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package function
  15. import (
  16. "github.com/lf-edge/ekuiper/pkg/api"
  17. "github.com/lf-edge/ekuiper/pkg/ast"
  18. )
  19. const (
  20. countKey = "$$last_hit_count"
  21. timeKey = "$$last_hit_time"
  22. aggCountKey = "$$last_agg_hit_count"
  23. aggTimeKey = "$$last_agg_hit_time"
  24. )
  25. func registerGlobalStateFunc() {
  26. builtins["last_hit_count"] = builtinFunc{
  27. fType: ast.FuncTypeScalar,
  28. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  29. doUpdate := args[0].(bool)
  30. lv, err := ctx.GetCounter(countKey)
  31. if err != nil {
  32. return err, false
  33. }
  34. if doUpdate {
  35. err := ctx.IncrCounter(countKey, 1)
  36. if err != nil {
  37. return nil, false
  38. }
  39. }
  40. return lv, true
  41. },
  42. val: ValidateNoArg,
  43. }
  44. builtins["last_hit_time"] = builtinFunc{
  45. fType: ast.FuncTypeScalar,
  46. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  47. args0 := args[0].(bool)
  48. args1 := args[1].(int64)
  49. lv, err := ctx.GetState(timeKey)
  50. if err != nil {
  51. return err, false
  52. }
  53. if args0 {
  54. err := ctx.PutState(timeKey, args1)
  55. if err != nil {
  56. return nil, false
  57. }
  58. }
  59. return lv, true
  60. },
  61. val: ValidateNoArg,
  62. }
  63. builtins["last_agg_hit_count"] = builtinFunc{
  64. fType: ast.FuncTypeAgg,
  65. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  66. doUpdate := args[0].(bool)
  67. lv, err := ctx.GetCounter(aggCountKey)
  68. if err != nil {
  69. return err, false
  70. }
  71. if doUpdate {
  72. err := ctx.IncrCounter(aggCountKey, 1)
  73. if err != nil {
  74. return nil, false
  75. }
  76. }
  77. return lv, true
  78. },
  79. val: ValidateNoArg,
  80. }
  81. builtins["last_agg_hit_time"] = builtinFunc{
  82. fType: ast.FuncTypeAgg,
  83. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  84. args0 := args[0].(bool)
  85. args1 := args[1].(int64)
  86. lv, err := ctx.GetState(aggTimeKey)
  87. if err != nil {
  88. return err, false
  89. }
  90. if args0 {
  91. err := ctx.PutState(aggTimeKey, args1)
  92. if err != nil {
  93. return nil, false
  94. }
  95. }
  96. return lv, true
  97. },
  98. val: ValidateNoArg,
  99. }
  100. }