From 9f1c4ef9ffdd0c5345a5a970430bab889c493528 Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 8 Oct 2024 17:38:49 +0200 Subject: [PATCH] Improved fact dimension settings & removed useless code (#201) * Improve aggs * Moved dbutils directory to utils * Fixed typos * Removed unused code & moved utils * Enable cors to false * Fixed model handler tests settings * Fixed model_handlers_test * Fixed model_handlers_test * Fixed model_handlers_test * Bumped sdk version --- go.mod | 49 +-- go.sum | 108 ++--- internals/evaluator/evaluator.go.old | 271 ------------ internals/evaluator/evaluator_test.go.old | 151 ------- .../explainer/issues/postgres_repository.go | 11 +- internals/fact/build.go | 151 +++---- internals/handlers/facts_handlers.go | 16 +- internals/handlers/model_handlers_test.go | 7 +- internals/handlers/notification_handlers.go | 2 +- internals/history/service_test.go.old | 384 ------------------ .../notification/postgres_repository.go | 2 +- internals/notifier/notification/repository.go | 3 +- internals/reader/reader.go | 6 +- internals/{ => utils}/dbutils/dbutils.go | 0 internals/{ => utils}/queryutils/utils.go | 0 .../{ => utils}/queryutils/utils_test.go | 0 16 files changed, 182 insertions(+), 979 deletions(-) delete mode 100644 internals/evaluator/evaluator.go.old delete mode 100644 internals/evaluator/evaluator_test.go.old delete mode 100644 internals/history/service_test.go.old rename internals/{ => utils}/dbutils/dbutils.go (100%) rename internals/{ => utils}/queryutils/utils.go (100%) rename internals/{ => utils}/queryutils/utils_test.go (100%) diff --git a/go.mod b/go.mod index 8f7539e6..f86e25cd 100644 --- a/go.mod +++ b/go.mod @@ -8,8 +8,8 @@ require ( github.com/Masterminds/squirrel v1.5.4 github.com/coreos/go-oidc/v3 v3.10.0 github.com/crewjam/saml v0.4.14 - github.com/elastic/go-elasticsearch/v8 v8.13.1 - github.com/go-chi/chi/v5 v5.0.12 + github.com/elastic/go-elasticsearch/v8 v8.15.0 + github.com/go-chi/chi/v5 v5.1.0 github.com/go-chi/cors v1.2.1 github.com/go-chi/jwtauth/v5 v5.3.1 github.com/google/uuid v1.6.0 @@ -20,41 +20,41 @@ require ( github.com/jmoiron/sqlx v1.4.0 github.com/json-iterator/go v1.1.12 github.com/lib/pq v1.10.9 - github.com/myrteametrics/myrtea-sdk/v5 v5.0.6 + github.com/myrteametrics/myrtea-sdk/v5 v5.0.9 github.com/prataprc/goparsec v0.0.0-20211219142520-daac0e635e7e - github.com/prometheus/client_golang v1.19.1 + github.com/prometheus/client_golang v1.20.2 github.com/robfig/cron/v3 v3.0.1 github.com/russellhaering/goxmldsig v1.4.0 github.com/spf13/viper v1.18.2 github.com/swaggo/http-swagger v1.3.4 github.com/swaggo/swag v1.16.3 go.uber.org/zap v1.27.0 - golang.org/x/net v0.25.0 - golang.org/x/oauth2 v0.20.0 + golang.org/x/net v0.28.0 + golang.org/x/oauth2 v0.21.0 google.golang.org/grpc v1.64.0 - google.golang.org/protobuf v1.34.1 + google.golang.org/protobuf v1.34.2 ) require ( - github.com/IBM/sarama v1.43.2 // indirect + github.com/IBM/sarama v1.43.3 // indirect github.com/KyleBanks/depth v1.2.1 // indirect github.com/PaesslerAG/gval v1.2.2 // indirect github.com/beevik/etree v1.1.0 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/buger/jsonparser v1.1.1 // indirect - github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/crewjam/httperr v0.2.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect - github.com/eapache/go-resiliency v1.6.0 // indirect + github.com/eapache/go-resiliency v1.7.0 // indirect github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 // indirect github.com/eapache/queue v1.1.0 // indirect - github.com/elastic/elastic-transport-go/v8 v8.5.0 // indirect + github.com/elastic/elastic-transport-go/v8 v8.6.0 // indirect github.com/fatih/color v1.16.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-jose/go-jose/v4 v4.0.1 // indirect github.com/go-kit/kit v0.13.0 // indirect - github.com/go-logr/logr v1.3.0 // indirect + github.com/go-logr/logr v1.4.1 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-openapi/jsonpointer v0.20.0 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect @@ -78,7 +78,7 @@ require ( github.com/jcmturner/rpc/v2 v2.0.3 // indirect github.com/jonboulle/clockwork v0.2.2 // indirect github.com/josharian/intern v1.0.0 // indirect - github.com/klauspost/compress v1.17.8 // indirect + github.com/klauspost/compress v1.17.9 // indirect github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect github.com/lestrrat-go/blackmagic v1.0.2 // indirect @@ -97,13 +97,14 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/oklog/run v1.0.0 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/pierrec/lz4/v4 v4.1.21 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/prometheus/client_model v0.5.0 // indirect - github.com/prometheus/common v0.48.0 // indirect - github.com/prometheus/procfs v0.12.0 // indirect + github.com/prometheus/client_model v0.6.1 // indirect + github.com/prometheus/common v0.55.0 // indirect + github.com/prometheus/procfs v0.15.1 // indirect github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect github.com/rickar/cal v1.0.2-0.20191101183814-0e8e6fcfe1c2 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect @@ -116,15 +117,15 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe // indirect - go.opentelemetry.io/otel v1.21.0 // indirect - go.opentelemetry.io/otel/metric v1.21.0 // indirect - go.opentelemetry.io/otel/trace v1.21.0 // indirect + go.opentelemetry.io/otel v1.24.0 // indirect + go.opentelemetry.io/otel/metric v1.24.0 // indirect + go.opentelemetry.io/otel/trace v1.24.0 // indirect go.uber.org/multierr v1.10.0 // indirect - golang.org/x/crypto v0.23.0 // indirect - golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect - golang.org/x/sys v0.20.0 // indirect - golang.org/x/text v0.15.0 // indirect - golang.org/x/tools v0.13.0 // indirect + golang.org/x/crypto v0.26.0 // indirect + golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect + golang.org/x/sys v0.24.0 // indirect + golang.org/x/text v0.17.0 // indirect + golang.org/x/tools v0.23.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index f3c97c2a..061b0fa5 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= -github.com/IBM/sarama v1.43.2 h1:HABeEqRUh32z8yzY2hGB/j8mHSzC/HA9zlEjqFNCzSw= -github.com/IBM/sarama v1.43.2/go.mod h1:Kyo4WkF24Z+1nz7xeVUFWIuKVV8RS3wM8mkvPKMdXFQ= +github.com/IBM/sarama v1.43.3 h1:Yj6L2IaNvb2mRBop39N7mmJAHBVY3dTPncr3qGVkxPA= +github.com/IBM/sarama v1.43.3/go.mod h1:FVIRaLrhK3Cla/9FfRF5X9Zua2KpS3SYIXxhac1H+FQ= github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8afzqM= @@ -20,8 +20,8 @@ github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZ github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= -github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= -github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/coreos/go-oidc/v3 v3.10.0 h1:tDnXHnLyiTVyT/2zLDGj09pFPkhND8Gl8lnTRhoEaJU= github.com/coreos/go-oidc/v3 v3.10.0/go.mod h1:5j11xcw0D3+SGxn6Z/WFADsgcWVMyNAlSQupk0KK3ac= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= @@ -35,16 +35,16 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1 github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= -github.com/eapache/go-resiliency v1.6.0 h1:CqGDTLtpwuWKn6Nj3uNUdflaq+/kIPsg0gfNzHton30= -github.com/eapache/go-resiliency v1.6.0/go.mod h1:5yPzW0MIvSe0JDsv0v+DvcjEv2FyD6iZYSs1ZI+iQho= +github.com/eapache/go-resiliency v1.7.0 h1:n3NRTnBn5N0Cbi/IeOHuQn9s2UwVUH7Ga0ZWcP+9JTA= +github.com/eapache/go-resiliency v1.7.0/go.mod h1:5yPzW0MIvSe0JDsv0v+DvcjEv2FyD6iZYSs1ZI+iQho= github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 h1:Oy0F4ALJ04o5Qqpdz8XLIpNA3WM/iSIXqxtqo7UGVws= github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3/go.mod h1:YvSRo5mw33fLEx1+DlK6L2VV43tJt5Eyel9n9XBcR+0= github.com/eapache/queue v1.1.0 h1:YOEu7KNc61ntiQlcEeUIoDTJ2o8mQznoNvUhiigpIqc= github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= -github.com/elastic/elastic-transport-go/v8 v8.5.0 h1:v5membAl7lvQgBTexPRDBO/RdnlQX+FM9fUVDyXxvH0= -github.com/elastic/elastic-transport-go/v8 v8.5.0/go.mod h1:YLHer5cj0csTzNFXoNQ8qhtGY1GTvSqPnKWKaqQE3Hk= -github.com/elastic/go-elasticsearch/v8 v8.13.1 h1:du5F8IzUUyCkzxyHdrO9AtopcG95I/qwi2WK8Kf1xlg= -github.com/elastic/go-elasticsearch/v8 v8.13.1/go.mod h1:DIn7HopJs4oZC/w0WoJR13uMUxtHeq92eI5bqv5CRfI= +github.com/elastic/elastic-transport-go/v8 v8.6.0 h1:Y2S/FBjx1LlCv5m6pWAF2kDJAHoSjSRSJCApolgfthA= +github.com/elastic/elastic-transport-go/v8 v8.6.0/go.mod h1:YLHer5cj0csTzNFXoNQ8qhtGY1GTvSqPnKWKaqQE3Hk= +github.com/elastic/go-elasticsearch/v8 v8.15.0 h1:IZyJhe7t7WI3NEFdcHnf6IJXqpRf+8S8QWLtZYYyBYk= +github.com/elastic/go-elasticsearch/v8 v8.15.0/go.mod h1:HCON3zj4btpqs2N1jjsAy4a/fiAul+YBP00mBH4xik8= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= @@ -54,8 +54,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= -github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s= -github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= +github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= github.com/go-chi/jwtauth/v5 v5.3.1 h1:1ePWrjVctvp1tyBq5b/2ER8Th/+RbYc7x4qNsc5rh5A= @@ -65,8 +65,8 @@ github.com/go-jose/go-jose/v4 v4.0.1/go.mod h1:WVf9LFMHh/QVrmqrOfqun0C45tMe3RoiK github.com/go-kit/kit v0.13.0 h1:OoneCcHKHQ03LfBpoQCUfCluwd2Vt3ohz+kvbJneZAU= github.com/go-kit/kit v0.13.0/go.mod h1:phqEHMMUbyrCFCTgH48JueqrM3md2HcAZ8N3XE4FKDg= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= -github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= +github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= @@ -146,8 +146,8 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= -github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU= -github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= +github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= +github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= @@ -157,6 +157,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw= github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o= github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk= @@ -206,8 +208,10 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= -github.com/myrteametrics/myrtea-sdk/v5 v5.0.6 h1:gJiIBZ1w6QGzWO6Y13/VqF48YWvSZ00bd7SjlDNzRa8= -github.com/myrteametrics/myrtea-sdk/v5 v5.0.6/go.mod h1:ZG+7QE2PgtoxrEzHEwBfLfhJsf+pVZZLsCFm8skyvg8= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/myrteametrics/myrtea-sdk/v5 v5.0.9 h1:1nQtcssx5VltaEzfh32vjQGpDcJRr18i7bcjnXOu1WQ= +github.com/myrteametrics/myrtea-sdk/v5 v5.0.9/go.mod h1:wJJ0R7p8VPtuqvJyYVl+eoxodkYL4IQ06e3zJnjiEIw= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= @@ -224,14 +228,14 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prataprc/goparsec v0.0.0-20211219142520-daac0e635e7e h1:7teoyCCMBovX+/L3/C2adcGNJI6Tsx6a2hbWQ8vWoO8= github.com/prataprc/goparsec v0.0.0-20211219142520-daac0e635e7e/go.mod h1:YbpxZqbf10o5u96/iDpcfDQmbIOTX/iNCH/yBByTfaM= -github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE= -github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho= -github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= -github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= -github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE= -github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc= -github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= -github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= +github.com/prometheus/client_golang v1.20.2 h1:5ctymQzZlyOON1666svgwn3s6IKWgfbjsejTMiXIyjg= +github.com/prometheus/client_golang v1.20.2/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= +github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= +github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= +github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc= +github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8= +github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= +github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rickar/cal v1.0.2-0.20191101183814-0e8e6fcfe1c2 h1:M+IxaqPU4BsXtwWhePi22zCb3dQ3TDEmXRiziUtTpd4= @@ -285,14 +289,14 @@ github.com/swaggo/http-swagger v1.3.4/go.mod h1:9dAh0unqMBAlbp1uE2Uc2mQTxNMU/ha4 github.com/swaggo/swag v1.16.3 h1:PnCYjPCah8FK4I26l2F/KQ4yz3sILcVUN3cTlBFA9Pg= github.com/swaggo/swag v1.16.3/go.mod h1:DImHIuOFXKpMFAQjcC7FG4m3Dg4+QuUgUzJmKjI/gRk= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc= -go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo= -go.opentelemetry.io/otel/metric v1.21.0 h1:tlYWfeo+Bocx5kLEloTjbcDwBuELRrIFxwdQ36PlJu4= -go.opentelemetry.io/otel/metric v1.21.0/go.mod h1:o1p3CA8nNHW8j5yuQLdc1eeqEaPfzug24uvsyIEJRWM= +go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= +go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= +go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI= +go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco= go.opentelemetry.io/otel/sdk v1.21.0 h1:FTt8qirL1EysG6sTQRZ5TokkU8d0ugCj8htOgThZXQ8= go.opentelemetry.io/otel/sdk v1.21.0/go.mod h1:Nna6Yv7PWTdgJHVRD9hIYywQBRx7pbox6nwBnZIxl/E= -go.opentelemetry.io/otel/trace v1.21.0 h1:WD9i5gzvoUPuXIXH24ZNBudiarZDKuekPqi/E8fpfLc= -go.opentelemetry.io/otel/trace v1.21.0/go.mod h1:LGbsEB0f9LGjN+OZaQQ26sohbOmiMR+BaslueVtS/qQ= +go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI= +go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= @@ -302,13 +306,13 @@ go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= -golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= -golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= +golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= +golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8= +golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= -golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8= +golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= @@ -316,14 +320,14 @@ golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qx golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= -golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= -golang.org/x/oauth2 v0.20.0 h1:4mQdhULixXKP1rwYBW0vAijoXnkTG0BLCDRzfe1idMo= -golang.org/x/oauth2 v0.20.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= +golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= +golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -338,8 +342,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= -golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= +golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= @@ -348,20 +352,20 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= -golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= -golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg= +golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 h1:NnYq6UN9ReLM9/Y01KWNOWyI5xQ9kbIms5GGJVwS/Yc= google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY= google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY= google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg= -google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= -google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internals/evaluator/evaluator.go.old b/internals/evaluator/evaluator.go.old deleted file mode 100644 index 6823474e..00000000 --- a/internals/evaluator/evaluator.go.old +++ /dev/null @@ -1,271 +0,0 @@ -package evaluator - -import ( - "errors" - "fmt" - "time" - - "github.com/myrteametrics/myrtea-engine-api/v5/internals/calendar" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/fact" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/reader" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/rule" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/situation" - "github.com/myrteametrics/myrtea-sdk/v5/engine" - "github.com/myrteametrics/myrtea-sdk/v5/expression" - "go.uber.org/zap" -) - -//EvaluateSituations evaluates a slice of situations and return a slice with the evaluated situations -func EvaluateSituations(situations []SituationToEvaluate, engineID string) ([]EvaluatedSituation, error) { - var err error - if _, ok := GetEngine(engineID); !ok { - err = InitEngine(engineID) - if err != nil { - return nil, err - } - } - err = UpdateEngine(engineID) - if err != nil { - return nil, err - } - - localRuleEngine, err := CloneEngine(engineID, true, false) - if err != nil { - return nil, err - } - - evaluatedSituations := make([]EvaluatedSituation, 0) - for _, s := range situations { - - ruleIDs, err := situation.R().GetRules(s.ID) - if err != nil { - return nil, fmt.Errorf("error geting rules for situation instance (%d, %s, %d): %s", s.ID, s.TS, s.TemplateInstanceID, err.Error()) - } - - sKnowledge, err := GetSituationKnowledge(s) - if err != nil { - return nil, fmt.Errorf("error geting knowledge for situation instance (%d, %s, %d): %s", s.ID, s.TS, s.TemplateInstanceID, err.Error()) - } - //Add date keywords in sKnowledge - for key, value := range expression.GetDateKeywords(s.TS) { - sKnowledge[key] = value - } - - localRuleEngine.Reset() - localRuleEngine.GetKnowledgeBase().SetFacts(sKnowledge) - - ruleIDsInt := make([]int64, 0) - for _, id := range ruleIDs { - r, found, err := rule.R().Get(id) - if err != nil { - zap.L().Error("Get Rule", zap.Int64("id", id), zap.Error(err)) - continue - } - if !found { - zap.L().Warn("Rule is missing", zap.Int64("id", id)) - continue - } - - cfound, valid, _ := calendar.CBase().InPeriodFromCalendarID(int64(r.CalendarID), s.TS) - if !cfound || valid { - ruleIDsInt = append(ruleIDsInt, id) - } - } - - localRuleEngine.ExecuteRules(ruleIDsInt) - agenda := localRuleEngine.GetResults() - - situation.SetAsEvaluated(s.ID, s.TS, s.TemplateInstanceID) - - if agenda != nil { - evaluatedSituations = append(evaluatedSituations, EvaluatedSituation{ - ID: s.ID, - TS: s.TS, - TemplateInstanceID: s.TemplateInstanceID, - Agenda: agenda, - }) - } - - } - - return evaluatedSituations, nil -} - -//EvaluateObjectSituations evaluates a slice of situations and return a slice with the evaluated situations -func EvaluateObjectSituations(situations []SituationToEvaluate, factObject engine.Fact, objects []map[string]interface{}, engineID string) ([]EvaluatedSituation, error) { - var err error - if _, ok := _globalREngine[engineID]; !ok { - err = InitEngine(engineID) - if err != nil { - return nil, err - } - } - err = UpdateEngine(engineID) - if err != nil { - return nil, err - } - - localRuleEngine, err := CloneEngine(engineID, true, false) - if err != nil { - return nil, err - } - - evaluatedSituations := make([]EvaluatedSituation, 0) - for _, s := range situations { - - ruleIDs, err := situation.R().GetRules(s.ID) - if err != nil { - return nil, fmt.Errorf("Error geting rules for situation instance (%d, %s, %d): %s", s.ID, s.TS, s.TemplateInstanceID, err.Error()) - } - - historyRecord, sKnowledge, err := buildObjectSituationHistoryRecord(s, factObject.ID) - if err != nil { - return nil, fmt.Errorf("Error building situation instance history record(%d, %s, %d): %s", s.ID, s.TS, s.TemplateInstanceID, err.Error()) - } - - ruleIDsInt := make([]int64, 0) - for _, id := range ruleIDs { - found, valid, _ := calendar.CBase().InPeriodFromCalendarID(id, s.TS) - if !found || valid { - ruleIDsInt = append(ruleIDsInt, id) - } - } - - for _, object := range objects { - - localRuleEngine.Reset() - localRuleEngine.GetKnowledgeBase().SetFacts(sKnowledge) - localRuleEngine.GetKnowledgeBase().InsertFact(factObject.Name, object) - - localRuleEngine.ExecuteRules(ruleIDsInt) - - agenda := localRuleEngine.GetResults() - if len(agenda) > 0 { - - ts := time.Now().UTC() - err = persistObjectSituationHistoryRecord(historyRecord, factObject.ID, object, ts) - if err != nil { - return nil, fmt.Errorf("Error persisting situation instance history record(%d, %s, %d): %s", s.ID, s.TS, s.TemplateInstanceID, err.Error()) - } - - evaluatedSituations = append(evaluatedSituations, EvaluatedSituation{ - ID: s.ID, - TS: ts, - TemplateInstanceID: s.TemplateInstanceID, - Agenda: agenda, - }) - } - } - } - - return evaluatedSituations, nil -} - -func GetSituationKnowledge(situationInstance SituationToEvaluate) (map[string]interface{}, error) { - - situationData := make(map[string]interface{}, 0) - record, err := situation.GetFromHistory(situationInstance.ID, situationInstance.TS, situationInstance.TemplateInstanceID, false) - if err != nil { - return nil, err - } - if record == nil { - return nil, errors.New("situation was not found in the history") - } - - for factID, factTS := range record.FactsIDS { - f, found, err := fact.R().Get(factID) - if err != nil { - return nil, err - } - if !found { - return nil, fmt.Errorf("Fact not found with id=%d", factID) - } - if factTS == nil { - return nil, fmt.Errorf("At least one fact has never been calculated, id=%d, name=%s", f.ID, f.Name) - } - - item, _, err := fact.GetFactResultFromHistory(factID, *factTS, situationInstance.ID, situationInstance.TemplateInstanceID, false, -1) - if err != nil { - return nil, err - } - itemData, err := item.ToAbstractMap() - if err != nil { - return nil, err - } - - situationData[f.Name] = itemData - } - for key, value := range record.Parameters { - situationData[key] = value - } - for key, value := range record.EvaluatedExpressionFacts { - situationData[key] = value - } - return situationData, nil -} - -func buildObjectSituationHistoryRecord(situationInstance SituationToEvaluate, factObjectID int64) (situation.HistoryRecord, map[string]interface{}, error) { - - historyRecord := situation.HistoryRecord{ - ID: situationInstance.ID, - TS: situationInstance.TS, - TemplateInstanceID: situationInstance.TemplateInstanceID, - FactsIDS: make(map[int64]*time.Time, 0), - Parameters: situationInstance.Parameters, - } - - situationData := make(map[string]interface{}, 0) - - for _, factID := range situationInstance.Facts { - if factID != factObjectID { - f, found, err := fact.R().Get(factID) - if err != nil { - return situation.HistoryRecord{}, nil, err - } - if !found { - return situation.HistoryRecord{}, nil, fmt.Errorf("Fact not found with id=%d", factID) - } - - item, ts, err := fact.GetFactResultFromHistory(factID, situationInstance.TS, situationInstance.ID, 0, true, -1) - if err != nil { - return situation.HistoryRecord{}, nil, err - } - itemData, err := item.ToAbstractMap() - if err != nil { - return situation.HistoryRecord{}, nil, err - } - - historyRecord.FactsIDS[factID] = &ts - situationData[f.Name] = itemData - } - } - - return historyRecord, situationData, nil - -} - -func persistObjectSituationHistoryRecord(historyRecord situation.HistoryRecord, factObjectID int64, object map[string]interface{}, ts time.Time) error { - objectItems := make(map[string]*reader.ItemAgg, 0) - for k, v := range object { - objectItems[k] = &reader.ItemAgg{Value: v} - } - item := &reader.Item{ - Key: object["id"].(string), - Aggs: objectItems, - } - - err := fact.PersistFactResult(factObjectID, ts, 0, 0, item, true) - if err != nil { - return fmt.Errorf("Error persisting factObject (%d): %s", factObjectID, err.Error()) - } - - historyRecord.FactsIDS[factObjectID] = &ts - historyRecord.TS = ts - - err = situation.Persist(historyRecord, true) - if err != nil { - return fmt.Errorf("Error persisting historyrecord: %s", err.Error()) - } - - return nil -} diff --git a/internals/evaluator/evaluator_test.go.old b/internals/evaluator/evaluator_test.go.old deleted file mode 100644 index 781d668d..00000000 --- a/internals/evaluator/evaluator_test.go.old +++ /dev/null @@ -1,151 +0,0 @@ -package evaluator - -import ( - "encoding/json" - "testing" - "time" - - "github.com/jmoiron/sqlx" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/calendar" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/fact" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/reader" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/rule" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/situation" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/tests" - "github.com/myrteametrics/myrtea-sdk/v5/engine" - "github.com/myrteametrics/myrtea-sdk/v5/postgres" -) - -func dbInit(dbClient *sqlx.DB, t *testing.T) { - dbDestroy(dbClient, t) - tests.DBExec(dbClient, tests.CalendarTableV3, t, true) - tests.DBExec(dbClient, tests.SituationDefinitionTableV1, t, true) - tests.DBExec(dbClient, tests.FactDefinitionTableV1, t, true) - tests.DBExec(dbClient, tests.SituationFactsTableV1, t, true) - tests.DBExec(dbClient, tests.RulesTableV1, t, true) - tests.DBExec(dbClient, tests.RuleVersionsTableV1, t, true) - tests.DBExec(dbClient, tests.SituationRulesTableV1, t, true) - tests.DBExec(dbClient, tests.SituationHistoryTableV1, t, true) - tests.DBExec(dbClient, tests.FactHistoryTableV1, t, true) -} - -func dbDestroy(dbClient *sqlx.DB, t *testing.T) { - tests.DBExec(dbClient, tests.SituationRulesDropTableV1, t, true) - tests.DBExec(dbClient, tests.SituationFactsDropTableV1, t, true) - tests.DBExec(dbClient, tests.FactDefinitionDropTableV1, t, true) - tests.DBExec(dbClient, tests.SituationDefinitionDropTableV1, t, true) - tests.DBExec(dbClient, tests.RuleVersionsDropTableV1, t, true) - tests.DBExec(dbClient, tests.RulesDropTableV1, t, true) - tests.DBExec(dbClient, tests.SituationHistoryDropTableV1, t, true) - tests.DBExec(dbClient, tests.FactHistoryDropTableV1, t, true) - tests.DBExec(dbClient, tests.CalendarDropTableV3, t, true) -} - -func TestEvaluator(t *testing.T) { - - if testing.Short() { - t.Skip("skipping postgresql test in short mode") - } - tests.CheckDebugLogs(t) - - db := tests.DBClient(t) - defer dbDestroy(db, t) - dbInit(db, t) - postgres.ReplaceGlobals(db) - fact.ReplaceGlobals(fact.NewPostgresRepository(db)) - rule.ReplaceGlobals(rule.NewPostgresRepository(db)) - situation.ReplaceGlobals(situation.NewPostgresRepository(db)) - calendar.ReplaceGlobals(calendar.NewPostgresRepository(db)) - - calendar.Init() - - fact1ID, _ := fact.R().Create(engine.Fact{Name: "fact_test_1"}) - - s := situation.Situation{Name: "test"} - - sID, err := situation.R().Create(s) - if err != nil { - t.Error(err) - } - - t1, _ := time.Parse("2006-01-02T15:04:05", "2019-06-14T15:48:50") - situations := []SituationToEvaluate{ - {ID: sID, TS: t1}, - } - - item := &reader.Item{ - Key: "fact_test_1", - KeyAsString: "fact_test_1", - Aggs: map[string]*reader.ItemAgg{ - "agg0": {Value: 10}, - "doc_count": {Value: 10}, - }, - Buckets: nil, - } - sh := situation.HistoryRecord{ - ID: sID, - TS: t1, - FactsIDS: map[int64]*time.Time{fact1ID: &t1}, - } - - fact.PersistFactResult(1, t1, 0, 0, item, true) - situation.Persist(sh, false) - - var rule1 rule.Rule - json.Unmarshal([]byte(ruleStr), &rule1) - rule1ID, _ := rule.R().Create(rule1) - - situation.R().SetRules(sID, []int64{int64(rule1ID)}) - time.Sleep(500 * time.Millisecond) - - evaluatedSituations, err := EvaluateSituations(situations, "Standard") - - if len(evaluatedSituations) != 1 { - t.Errorf("The number os evaluated situation is not as expected") - } - agenda := evaluatedSituations[0].Agenda - if agenda[0].GetName() != "set" || agenda[1].GetName() != "set" || agenda[2].GetName() != "notify" { - t.Errorf("The actions names are not ar expected") - } - - if agenda[0].GetParameters()["status.A"].(float64) != float64(3) || agenda[1].GetParameters()["status.B"].(float64) != float64(5) || agenda[2].GetParameters()["id"].(string) != "notify-1" { - t.Errorf("The actions parameters are not ar expected") - } -} - -var ruleStr = `{ - "name": "rule1", - "description": "this is the rule 1", - "cases": [ - { - "name": "case1", - "condition": "fact_test_1.aggs.agg0.value == fact_test_1.aggs.doc_count.value", - "actions": [ - { - "name": "\"set\"", - "parameters": { - "status.A": "1 + 2" - } - }, - { - "name": "\"set\"", - "parameters": { - "status.B": "2 + 3" - } - }, - { - "name": "\"notify\"", - "parameters": { - "id": "\"notify-1\"", - "level": "\"info\"", - "title": "\"my_title\"", - "description": "\"my_description\"", - "timeout": "\"10s\"", - "groups": "[1,2]" - } - } - ] - } - ], - "enabled": true - }` diff --git a/internals/explainer/issues/postgres_repository.go b/internals/explainer/issues/postgres_repository.go index 681cb3c5..98cb2fa2 100644 --- a/internals/explainer/issues/postgres_repository.go +++ b/internals/explainer/issues/postgres_repository.go @@ -4,13 +4,13 @@ import ( "database/sql" "encoding/json" "errors" + "github.com/myrteametrics/myrtea-engine-api/v5/internals/utils/queryutils" "strings" "time" "github.com/jmoiron/sqlx" "github.com/lib/pq" "github.com/myrteametrics/myrtea-engine-api/v5/internals/models" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/queryutils" "github.com/myrteametrics/myrtea-engine-api/v5/internals/security/users" "go.uber.org/zap" ) @@ -182,10 +182,10 @@ func (r *PostgresRepository) Update(tx *sqlx.Tx, id int64, issue models.Issue, u return nil } -// GetOpenStateByKey get all issues that belong to the same situation in 'open' or 'draft' states +// GetOpenAndDraftIssuesByKey get all issues that belong to the same situation in 'open' or 'draft' states // if existed func (r *PostgresRepository) GetOpenAndDraftIssuesByKey(key string) (map[int64]models.Issue, error) { - issues := make(map[int64]models.Issue, 0) + issues := make(map[int64]models.Issue) query := `SELECT i.id, i.key, i.name, i.level, i.situation_history_id, i.situation_id, situation_instance_id, i.situation_date, i.expiration_date, i.rule_data, i.state, i.created_at, i.last_modified, i.detection_rating_avg, @@ -244,7 +244,7 @@ func (r *PostgresRepository) GetCloseToTimeoutByKey(key string, firstSituationTS return issues, nil } -//Get used to get issues by key +// Get used to get issues by key func (r *PostgresRepository) GetByKeyByPage(key string, options models.SearchOptions) ([]models.Issue, int, error) { issues := make([]models.Issue, 0) @@ -752,9 +752,8 @@ func (r *PostgresRepository) DeleteOldIssueResolutions(ts time.Time) error { if err != nil { return err } - + zap.L().Info("Auto purge of the table issue_resolution_v1 ", zap.Int64("Number of rows deleted", affectedRows)) return nil } - diff --git a/internals/fact/build.go b/internals/fact/build.go index fbef6f46..eb0390ba 100644 --- a/internals/fact/build.go +++ b/internals/fact/build.go @@ -3,7 +3,6 @@ package fact import ( "errors" "fmt" - "reflect" "strings" "github.com/myrteametrics/myrtea-engine-api/v5/internals/fact/lexer" @@ -31,18 +30,19 @@ func BuildFactsFromFile(path string, file string) (map[string]*engine.Fact, []er factsRaw := conf.GetStringMap("facts") for rawFactKey, rawFactValue := range factsRaw { zap.L().Info("Reflecting fact", zap.String("fact", rawFactKey)) - if reflect.ValueOf(rawFactValue).Kind() != reflect.Map { - zap.L().Info("ERROR: Reflect fact data is not a map", zap.String("fact", rawFactKey)) - continue - } - fact, err := ParseFact(rawFactKey, rawFactValue.(map[string]interface{})) - if err != nil { - errs = append(errs, errors.New(rawFactKey+": "+err.Error())) - } + if factValueMap, ok := rawFactValue.(map[string]interface{}); ok { + fact, err := ParseFact(rawFactKey, factValueMap) + if err != nil { + errs = append(errs, errors.New(rawFactKey+": "+err.Error())) + } - if fact != nil { - facts[rawFactKey] = fact + if fact != nil { + facts[rawFactKey] = fact + } + } else { + zap.L().Info("ERROR: Reflect fact data is not a map", zap.String("fact", rawFactKey)) + continue } } return facts, errs @@ -63,38 +63,37 @@ func ParseFact(factName string, factData map[string]interface{}) (*engine.Fact, switch factFieldKey { case "source": - if reflect.ValueOf(factFieldValue).Kind() == reflect.String { - source = factFieldValue.(string) + if val, ok := factFieldValue.(string); ok { + source = val } else { return nil, errors.New("source is not a string") } case "model": - if reflect.ValueOf(factFieldValue).Kind() == reflect.String { - model = factFieldValue.(string) + if val, ok := factFieldValue.(string); ok { + model = val } else { return nil, errors.New("model is not a string") } case "operator": - if reflect.ValueOf(factFieldValue).Kind() == reflect.String { - intentOperator = factFieldValue.(string) + if val, ok := factFieldValue.(string); ok { + intentOperator = val } else { return nil, errors.New("intent operator is not a string") } case "term": - if reflect.ValueOf(factFieldValue).Kind() == reflect.String { - term = factFieldValue.(string) + if val, ok := factFieldValue.(string); ok { + term = val } else { return nil, errors.New("term is not a string") } case "dimensions": - if reflect.ValueOf(factFieldValue).Kind() == reflect.Slice { - factDimensions := factFieldValue.([]interface{}) + if factDimensions, ok := factFieldValue.([]interface{}); ok { for _, iDimension := range factDimensions { - if reflect.ValueOf(iDimension).Kind() == reflect.String { + if _, ok := iDimension.(string); ok { dim := iDimension.(string) dimParts := strings.Split(dim, " ") operator := strings.ToLower(dimParts[0]) @@ -119,9 +118,7 @@ func ParseFact(factName string, factData map[string]interface{}) (*engine.Fact, } case "filters": - if reflect.ValueOf(factFieldValue).Kind() == reflect.Map { - filters := factFieldValue.(map[string]interface{}) - + if filters, ok := factFieldValue.(map[string]interface{}); ok { if len(filters) > 1 { return nil, errors.New("more than one conditions group on root") } @@ -140,8 +137,8 @@ func ParseFact(factName string, factData map[string]interface{}) (*engine.Fact, } case "comment": - if reflect.ValueOf(factFieldValue).Kind() == reflect.String { - comment = factFieldValue.(string) + if val, ok := factFieldValue.(string); ok { + comment = val } else { return nil, errors.New("comment is not a string") } @@ -196,59 +193,63 @@ func parseFilters(filters map[string]interface{}) ([]engine.ConditionFragment, e for key, value := range filters { switch key { case "conditions": - if reflect.ValueOf(value).Kind() != reflect.Slice { + if valueSlice, ok := value.([]interface{}); ok { + leafFrags, err := getConditions(valueSlice) + if err != nil { + return nil, err + } + fragments = append(fragments, leafFrags...) + } else { return nil, errors.New("conditions value is not a slice") } - leafFrags, err := getConditions(value.([]interface{})) - if err != nil { - return nil, err - } - fragments = append(fragments, leafFrags...) case "and": - if reflect.ValueOf(value).Kind() != reflect.Map { + if valueMap, ok := value.(map[string]interface{}); ok { + frags, err := parseFilters(valueMap) + if err != nil { + return nil, err + } + and, err := engine.GetBooleanFragment(engine.And.String()) + if err != nil { + return nil, err + } + and.Fragments = frags + fragments = append(fragments, and) + } else { return nil, errors.New("and value is not a map or has no children") } - frags, err := parseFilters(value.(map[string]interface{})) - if err != nil { - return nil, err - } - and, err := engine.GetBooleanFragment(engine.And.String()) - if err != nil { - return nil, err - } - and.Fragments = frags - fragments = append(fragments, and) case "or": - if reflect.ValueOf(value).Kind() != reflect.Map { + if valueMap, ok := value.(map[string]interface{}); ok { + frags, err := parseFilters(valueMap) + if err != nil { + return nil, err + } + or, err := engine.GetBooleanFragment(engine.Or.String()) + if err != nil { + return nil, err + } + or.Fragments = frags + fragments = append(fragments, or) + } else { return nil, errors.New("or value is not a map or has no children") } - frags, err := parseFilters(value.(map[string]interface{})) - if err != nil { - return nil, err - } - or, err := engine.GetBooleanFragment(engine.Or.String()) - if err != nil { - return nil, err - } - or.Fragments = frags - fragments = append(fragments, or) case "not": - if reflect.ValueOf(value).Kind() != reflect.Map { + if valueMap, ok := value.(map[string]interface{}); ok { + frags, err := parseFilters(valueMap) + if err != nil { + return nil, err + } + not, err := engine.GetBooleanFragment(engine.Not.String()) + if err != nil { + return nil, err + } + not.Fragments = frags + fragments = append(fragments, not) + } else { return nil, errors.New("not value is not a map or has no children") } - frags, err := parseFilters(value.(map[string]interface{})) - if err != nil { - return nil, err - } - not, err := engine.GetBooleanFragment(engine.Not.String()) - if err != nil { - return nil, err - } - not.Fragments = frags - fragments = append(fragments, not) default: return nil, errors.New("unknown config key " + key) @@ -259,24 +260,26 @@ func parseFilters(filters map[string]interface{}) ([]engine.ConditionFragment, e } func getConditions(conditionsStr []interface{}) ([]engine.ConditionFragment, error) { - lexerC := lexer.L() leafConditions := make([]engine.ConditionFragment, 0) - if len(conditionsStr) > 0 { - for _, conditionStr := range conditionsStr { - if reflect.ValueOf(conditionStr).Kind() != reflect.String { - return nil, errors.New("condition is not a string") - } - nodes, _ := lexerC.Ast.Parsewith(lexerC.Parser, parsec.NewScanner([]byte(conditionStr.(string)))) + if len(conditionsStr) == 0 { + return leafConditions, nil + } + + for _, conditionStr := range conditionsStr { + if condition, ok := conditionStr.(string); ok { + nodes, _ := lexerC.Ast.Parsewith(lexerC.Parser, parsec.NewScanner([]byte(condition))) if nodes == nil { - return nil, errors.New("cannot parse expression : " + conditionStr.(string)) + return nil, errors.New("cannot parse expression : " + condition) } leafCondition, err := astToLeafCondition(nodes) if err != nil { return nil, errors.New("cannot convert condition AST in condition fragment") } leafConditions = append(leafConditions, leafCondition) + } else { + return nil, errors.New("condition is not a string") } } diff --git a/internals/handlers/facts_handlers.go b/internals/handlers/facts_handlers.go index ba9e3172..a1c3ccf1 100644 --- a/internals/handlers/facts_handlers.go +++ b/internals/handlers/facts_handlers.go @@ -298,7 +298,6 @@ func DeleteFact(w http.ResponseWriter, r *http.Request) { // @Param id path string true "Fact ID" // @Param byName query string false "Find fact by it's name" // @Param time query string false "Timestamp used for the fact execution" -// @Param cache query string false "Cache maximum age in minutes(go duration: 10m, 1h, ...). If unset, use cache with no limit of age. If set to 0, disable cache" // @Param nhit query int false "Hit per page" // @Param offset query int false "Offset number" // @Param placeholders query string false "Placeholders (format: key1:value1,key2:value2)" @@ -507,7 +506,7 @@ func GetFactHits(w http.ResponseWriter, r *http.Request) { factParameters, err := ParseFactParameters(r.URL.Query().Get("factParameters")) if err != nil { - zap.L().Error("Parse input FactParametres", zap.Error(err), zap.String("raw offset", r.URL.Query().Get("factParameters"))) + zap.L().Error("Parse input FactParameters", zap.Error(err), zap.String("raw offset", r.URL.Query().Get("factParameters"))) render.Error(w, r, render.ErrAPIParsingInteger, err) return } @@ -580,23 +579,23 @@ func GetFactHits(w http.ResponseWriter, r *http.Request) { } var situationInstance situation.TemplateInstance - situationInstaceIDStr := r.URL.Query().Get("situationInstanceId") - if situationInstaceIDStr != "" { - situationInstaceID, err := strconv.ParseInt(situationInstaceIDStr, 10, 64) + situationInstanceIDStr := r.URL.Query().Get("situationInstanceId") + if situationInstanceIDStr != "" { + situationInstanceID, err := strconv.ParseInt(situationInstanceIDStr, 10, 64) if err != nil { zap.L().Warn("Parse input situationInstanceId", zap.Error(err), zap.String("rawSituationInstanceId", r.URL.Query().Get("situationInstanceId"))) render.Error(w, r, render.ErrAPIParsingInteger, err) return } - situationInstance, found, err = situation.R().GetTemplateInstance(situationInstaceID) + situationInstance, found, err = situation.R().GetTemplateInstance(situationInstanceID) if err != nil { - zap.L().Error("Cannot retrieve situation Instance", zap.Int64("situationInstanceID", situationInstaceID), zap.Error(err)) + zap.L().Error("Cannot retrieve situation Instance", zap.Int64("situationInstanceID", situationInstanceID), zap.Error(err)) render.Error(w, r, render.ErrAPIDBSelectFailed, err) return } if !found { - zap.L().Warn("Situation Instance does not exists", zap.Int64("situationInstanceID", situationInstaceID)) + zap.L().Warn("Situation Instance does not exists", zap.Int64("situationInstanceID", situationInstanceID)) render.Error(w, r, render.ErrAPIDBResourceNotFound, err) return } @@ -640,7 +639,6 @@ func GetFactHits(w http.ResponseWriter, r *http.Request) { // @Param situationid query string false "Optional SituationID" // @Param instanceid query string false "Optional InstanceID" // @Param time query string true "Timestamp used for the fact execution" -// @Param cache query string false "Cache maximum age in minutes(go duration: 10m, 1h, ...). If unset, use cache with no limit of age. If set to 0, disable cache" // @Param nhit query int false "Hit per page" // @Param offset query int false "Offset number" // @Param placeholders query string false "Placeholders (format: key1:value1,key2:value2)" diff --git a/internals/handlers/model_handlers_test.go b/internals/handlers/model_handlers_test.go index cba55e80..549036d6 100644 --- a/internals/handlers/model_handlers_test.go +++ b/internals/handlers/model_handlers_test.go @@ -2,6 +2,7 @@ package handlers import ( "encoding/json" + "github.com/elastic/go-elasticsearch/v8/typedapi/types" "net/http" "testing" @@ -47,7 +48,7 @@ func initModelRepository(t *testing.T) []modeler.Model { EnablePurge: true, PurgeMaxConcurrentIndices: 30, PatchAliasMaxIndices: 2, - AdvancedSettings: map[string]interface{}{"number_of_replica": 2, "number_of_shard": 6}, + AdvancedSettings: types.IndexSettings{NumberOfReplicas: "2", NumberOfShards: "6"}, }, Source: "{}", } @@ -61,7 +62,7 @@ func initModelRepository(t *testing.T) []modeler.Model { EnablePurge: true, PurgeMaxConcurrentIndices: 30, PatchAliasMaxIndices: 2, - AdvancedSettings: map[string]interface{}{"number_of_replica": 2, "number_of_shard": 6}, + AdvancedSettings: types.IndexSettings{NumberOfReplicas: "2", NumberOfShards: "6"}, }, Source: "{}", } @@ -122,7 +123,7 @@ func TestGetModel(t *testing.T) { t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) } - expected := `{"id":1,"name":"test","synonyms":["model1"],"fields":[{"name":"test","type":"string","semantic":false,"synonyms":["other"]},{"name":"test_object","type":"object","keepObjectSeparation":false,"fields":[{"name":"test_subfield","type":"string","semantic":false,"synonyms":["other2"]}]}],"source":"{}","elasticsearchOptions":{"rollmode":"cron","rollcron":"0 0 * * *","enablePurge":true,"purgeMaxConcurrentIndices":30,"patchAliasMaxIndices":2,"advancedSettings":{"number_of_replica":2,"number_of_shard":6}}}` + "\n" + expected := `{"id":1,"name":"test","synonyms":["model1"],"fields":[{"name":"test","type":"string","semantic":false,"synonyms":["other"]},{"name":"test_object","type":"object","keepObjectSeparation":false,"fields":[{"name":"test_subfield","type":"string","semantic":false,"synonyms":["other2"]}]}],"source":"{}","elasticsearchOptions":{"rollmode":"cron","rollcron":"0 0 * * *","enablePurge":true,"purgeMaxConcurrentIndices":30,"patchAliasMaxIndices":2,"advancedSettings":{"number_of_replicas":"2","number_of_shards":"6"}}}` + "\n" if rr.Body.String() != expected { t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected) } diff --git a/internals/handlers/notification_handlers.go b/internals/handlers/notification_handlers.go index d7b4290e..a7d43dcf 100644 --- a/internals/handlers/notification_handlers.go +++ b/internals/handlers/notification_handlers.go @@ -2,11 +2,11 @@ package handlers import ( "github.com/go-chi/chi/v5" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/dbutils" "github.com/myrteametrics/myrtea-engine-api/v5/internals/handlers/render" "github.com/myrteametrics/myrtea-engine-api/v5/internals/models" "github.com/myrteametrics/myrtea-engine-api/v5/internals/notifier/notification" "github.com/myrteametrics/myrtea-engine-api/v5/internals/security/users" + "github.com/myrteametrics/myrtea-engine-api/v5/internals/utils/dbutils" "go.uber.org/zap" "net/http" "strconv" diff --git a/internals/history/service_test.go.old b/internals/history/service_test.go.old deleted file mode 100644 index 774ab2d8..00000000 --- a/internals/history/service_test.go.old +++ /dev/null @@ -1,384 +0,0 @@ -package history - -import ( - "encoding/json" - "testing" - "time" - - "github.com/myrteametrics/myrtea-engine-api/v5/internals/fact" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/models" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/reader" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/scheduler" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/situation" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/tests" - "github.com/myrteametrics/myrtea-sdk/v5/expression" - "go.uber.org/zap" -) - -// builder.newStatement().Update("situation_history_v5"). -// Where("id = ?", history.ID). -// Set("metadatas", history.Metadatas) -// Set("last_update", time.Now()) - -// builder.newStatement().Update("situation_history_v5"). -// Where("id = ?", history.ID). -// Set("situation_id", history.SituationID). -// Set("situation_instance_id", history.SituationInstanceID). -// Set("ts", history.Ts). -// Set("parameters", history.Parameters). -// Set("expression_facts", history.ExpressionFacts). -// Set("metadatas", history.Metadatas) -// Set("last_update", time.Now()) - -func CalculateFact(ti time.Time) reader.Item { - return reader.Item{Aggs: map[string]*reader.ItemAgg{"doc_count": {Value: 100}}} -} - -func (historyService HistoryService) CalculateAndStoreFacts(ti time.Time, factIDs []int64) ([]HistoryFactsV4, error) { - - historyFactsNew := make([]HistoryFactsV4, 0) - - for _, factID := range factIDs { - f, found, err := fact.R().Get(factID) - if err != nil { - zap.L().Error("Error Getting the Fact, skipping fact calculation...", zap.Int64("factID", factID)) - continue - } - if !found { - zap.L().Warn("Fact does not exists, skipping fact calculation...", zap.Int64("factID", factID)) - continue - } - - // Get Situations Linked to fact => For each calculate + persist - situationHistoryRecordV1, err := scheduler.GetEnabledSituations(f, ti) - if err != nil { - continue - } - if len(situationHistoryRecordV1) == 0 { - zap.L().Info("No situation within valid calendar period for the Fact, skipping fact calculation...", zap.Int64("factID", factID)) - // S().RemoveRunningJob(job.ScheduleID) - // return - } - - // Calculate Fact - factValue := CalculateFact(ti) - - // Insert fact history - historyFactNew := HistoryFactsV4{ - // ID: -1, - //FactName: "", - SituationID: 1, - SituationInstanceID: 1, - FactID: 1, - Ts: ti, - Result: factValue, - } - historyFactNew.ID, err = historyService.HistoryFactsQuerier.Insert(historyFactNew) - if err != nil { - // err - } - - historyFactsNew = append(historyFactsNew, historyFactNew) - } - return historyFactsNew, nil -} - -func TestQuery5(t *testing.T) { - t.Fail() - db := tests.DBClient(t) - historyService := New(db) - situation.ReplaceGlobals(situation.NewPostgresRepository(tests.DBClient(t))) - - var err error - ti := time.Now() - - factIDs := []int64{1, 2, 14, 15} - - // Calculate and persist facts => Retrieve all HistoryFactV4 with ID - historyFactsNew, err := historyService.CalculateAndStoreFacts(ti, factIDs) - if err != nil { - t.Error(err) - } - - historyFactNew := historyFactsNew[0] - - // Flatten parameters from situation definition + situation instance definition - s, parameters, err := ExtractSituationData(historyFactNew.SituationID, historyFactNew.SituationInstanceID) - if err != nil { - t.Error(err) - } - t.Log("parameters", parameters) - - historyFactsNew, historySituationFlattenData, err := historyService.ExtractFactData(historyFactNew, s.Facts) - if err != nil { - t.Error(err) - } - for key, value := range parameters { - historySituationFlattenData[key] = value - } - for key, value := range expression.GetDateKeywords(ti) { - historySituationFlattenData[key] = value - } - t.Log("flatten data", historySituationFlattenData) - - // Evaluate expression facts - expressionFacts := EvaluateExpressionFacts(s.ExpressionFacts, historySituationFlattenData) - t.Log("expressionfacts", expressionFacts) - - // Build and insert HistorySituationV4 - historySituationNew := HistorySituationsV4{ - // ID: -1, - SituationID: historyFactNew.SituationID, - SituationInstanceID: historyFactNew.SituationInstanceID, - Ts: historyFactNew.Ts, - Parameters: parameters, - ExpressionFacts: expressionFacts, - Metadatas: make([]models.MetaData, 0), - } - historySituationNew.ID, err = historyService.HistorySituationsQuerier.Insert(historySituationNew) - if err != nil { - t.Error(err) - } - t.Log("historySituationNew", historySituationNew) - - // Build and insert HistorySituationFactsV4 - historySituationFactNew := make([]HistorySituationFactsV4, 0) - for _, historyFactNew := range historyFactsNew { - historySituationFactNew = append(historySituationFactNew, HistorySituationFactsV4{ // Replace entry for existing factID with new HistorySituationFactsV4{} - HistorySituationID: historySituationNew.ID, - HistoryFactID: historyFactNew.ID, - FactID: historyFactNew.FactID, - }) - } - - err = historyService.HistorySituationFactsQuerier.Execute(historyService.HistorySituationFactsQuerier.Builder.InsertBulk(historySituationFactNew)) - if err != nil { - t.Error(err) - } - t.Log("historySituationFactNew", historySituationFactNew) -} - -// func TestQuery4(t *testing.T) { -// t.Fail() -// db := tests.DBClient(t) -// historyService := New(db) -// situation.ReplaceGlobals(situation.NewPostgresRepository(tests.DBClient(t))) - -// /* -// Receive facts to calculate -// => 1, 2, 3 - -// Search associated situation from settings -// => 1, 5 - -// Check if template (and search for instances) or if simple situation - -// Calculate the fact from source (ES) -// => 1 = doc_count/value -// => 2 = doc_count/value -// => 3 = doc_count/value - -// !!!!!! Persist the fact in history table - -// !!!!!! CAN ALSO BE AN UPDATE ? => Retroactive calculation -// */ - -// /* -// Build a list of skeleton SituationHistoryRecord from the calculated facts -// (situationID + situationInstanceID + Flatten parameters (situation+instance) ) -// !! + FactIDS (Combo FactID + TS) - -// for each situation -// get all factIDS inside situation (from settings) - -// Search for the last-closest SituationHistoryRecord ?????? -// lastHistoryRecord, err := situation.GetFromHistory(record.ID, record.TS, record.TemplateInstanceID, true) - -// Merge old record with new calculated fact - -// Process ExpressionFacts - -// Persist situation -// */ - -// ti := time.Now() -// historyFactNew := HistoryFactsV4{ -// // ID: -1, -// //FactName: "", -// SituationID: 1, -// SituationInstanceID: 1, -// FactID: 1, -// Ts: ti, -// Result: reader.Item{Aggs: map[string]*reader.ItemAgg{"doc_count": {Value: 100}}}, -// } - -// var err error -// historyFactNew.ID, err = historyService.HistoryFactsQuerier.Insert(historyFactNew) -// if err != nil { -// t.Error(err) -// } -// t.Log(historyFactNew.ID) - -// // Flatten parameters from situation definition + situation instance definition -// situationInstanceName := "" -// parameters := make(map[string]interface{}) -// s, _, _ := situation.R().Get(historyFactNew.SituationID) // .SituationInstanceID -// for k, v := range s.Parameters { -// parameters[k] = v -// } -// if s.IsTemplate { -// si, _, _ := situation.R().GetTemplateInstance(historyFactNew.SituationInstanceID) -// situationInstanceName = si.Name -// for k, v := range si.Parameters { -// parameters[k] = v -// } -// } -// t.Log("parameters", parameters) - -// // TODO: Might be faster to just get last fact value ? What if no situation history is found ? -// // Flatten facts data (old facts + new fact) -// // Get last saved situation -// historySituationLasts, err := historyService.HistorySituationsQuerier.GetHistorySituationsIdsLast(GetHistorySituationsOptions{ -// SituationID: historyFactNew.SituationID, -// SituationInstanceID: historyFactNew.SituationInstanceID, -// ToTS: historyFactNew.Ts, -// }) -// if err != nil { -// t.Error(err) -// } -// t.Log("last situations", historySituationLasts) - -// historySituationIDs := make([]int64, 0) -// for _, historySituation := range historySituationLasts { -// historySituationIDs = append(historySituationIDs, historySituation.ID) -// } - -// // Get last saved facts for the old situation -// historyFactLasts, historySituationFactLasts, err := historyService.HistoryFactsQuerier.GetHistoryFactsFromSituationIds(historyService.HistorySituationFactsQuerier, historySituationIDs) -// if err != nil { -// t.Error(err) -// } -// t.Log("last facts", historyFactLasts) - -// // Flatten situation data (old and new facts + parameters) -// historySituationFlattenData := make(map[string]interface{}) -// for _, historyFactLast := range historyFactLasts { -// historyFactData, err := historyFactLast.Result.ToAbstractMap() -// if err != nil { -// t.Error(err) -// } -// historySituationFlattenData[historyFactLast.FactName] = historyFactData -// } -// historySituationFlattenData[historyFactNew.FactName] = historyFactNew // override datas with new fact data -// for key, value := range parameters { -// historySituationFlattenData[key] = value -// } -// for key, value := range expression.GetDateKeywords(ti) { -// historySituationFlattenData[key] = value -// } -// t.Log("flatten data", historySituationFlattenData) - -// // Evaluate expression facts -// expressionFacts := make(map[string]interface{}) -// for _, expressionFact := range s.ExpressionFacts { -// result, err := expression.Process(expression.LangEval, expressionFact.Expression, historySituationFlattenData) -// if err != nil { -// t.Error(err) -// continue -// } -// if expression.IsInvalidNumber(result) { -// continue -// } - -// historySituationFlattenData[expressionFact.Name] = result // Used for chaining expression facts -// expressionFacts[expressionFact.Name] = result -// } -// t.Log("expressionfacts", expressionFacts) - -// historySituationNew := HistorySituationsV4{ -// ID: -1, -// SituationName: s.Name, -// SituationInstanceName: situationInstanceName, -// SituationID: historyFactNew.SituationID, -// SituationInstanceID: historyFactNew.SituationInstanceID, -// Ts: historyFactNew.Ts, -// Parameters: parameters, -// ExpressionFacts: expressionFacts, -// Metadatas: make([]models.MetaData, 0), -// } -// historySituationNew.ID, err = historyService.HistorySituationsQuerier.Insert(historySituationNew) -// if err != nil { -// t.Error(err) -// } -// t.Log("historySituationNew", historySituationNew) - -// historySituationFactNewMap := make(map[int64]HistorySituationFactsV4) -// for _, historySituationFact := range historySituationFactLasts { -// historySituationFactNewMap[historySituationFact.FactID] = HistorySituationFactsV4{ -// HistorySituationID: historySituationNew.ID, // Replace HistorySituationID with new inserted situation -// HistoryFactID: historySituationFact.HistoryFactID, -// FactID: historySituationFact.FactID, -// } -// } -// historySituationFactNewMap[historyFactNew.FactID] = HistorySituationFactsV4{ // Replace entry for existing factID with new HistorySituationFactsV4{} -// HistorySituationID: historySituationNew.ID, -// HistoryFactID: historyFactNew.ID, -// FactID: historyFactNew.FactID, -// } - -// historySituationFactNew := make([]HistorySituationFactsV4, 0, len(historySituationFactNewMap)) -// for _, val := range historySituationFactNewMap { -// historySituationFactNew = append(historySituationFactNew, val) -// } - -// err = historyService.HistorySituationFactsQuerier.execute(historyService.HistorySituationFactsQuerier.Builder.InsertBulk(historySituationFactNew)) -// if err != nil { -// t.Error(err) -// } -// t.Log("historySituationFactNew", historySituationFactNew) - -// // override historySituationFacts all entry with new situationHistoryID -// // override historySituationFacts one entry with new fact - -// // insert situation -// // insert historySituationFacts (rewrited) - -// } - -func TestQuery3(t *testing.T) { - t.Fail() - db := tests.DBClient(t) - historyService := New(db) - - options := GetHistorySituationsOptions{ - SituationID: 4, - SituationInstanceID: -1, - FromTS: time.Date(2022, time.July, 1, 0, 0, 0, 0, time.UTC), - ToTS: time.Time{}, - } - interval := "day" - - // Fetch situations history - historySituations, err := historyService.HistorySituationsQuerier.GetHistorySituationsIdsByStandardInterval(options, interval) - if err != nil { - t.Error(err) - t.FailNow() - } - - // Fetch facts history - historySituationsIds := make([]int64, 0) - for _, item := range historySituations { - historySituationsIds = append(historySituationsIds, item.ID) - } - - historyFacts, historySituationFacts, err := historyService.HistoryFactsQuerier.GetHistoryFactsFromSituationIds(historyService.HistorySituationFactsQuerier, historySituationsIds) - if err != nil { - t.Error(err) - t.FailNow() - } - - // Extract results - result := ExtractHistoryDataSearch(historySituations, historySituationFacts, historyFacts) - b, _ := json.Marshal(result) - t.Log(string(b)) -} diff --git a/internals/notifier/notification/postgres_repository.go b/internals/notifier/notification/postgres_repository.go index c4761d17..606f5634 100644 --- a/internals/notifier/notification/postgres_repository.go +++ b/internals/notifier/notification/postgres_repository.go @@ -2,11 +2,11 @@ package notification import ( "errors" + "github.com/myrteametrics/myrtea-engine-api/v5/internals/utils/dbutils" "time" sq "github.com/Masterminds/squirrel" "github.com/jmoiron/sqlx" - "github.com/myrteametrics/myrtea-engine-api/v5/internals/dbutils" ) // PostgresRepository is a repository containing the Fact definition based on a PSQL database and diff --git a/internals/notifier/notification/repository.go b/internals/notifier/notification/repository.go index 89d95d44..2d5c72b0 100644 --- a/internals/notifier/notification/repository.go +++ b/internals/notifier/notification/repository.go @@ -2,10 +2,9 @@ package notification import ( sq "github.com/Masterminds/squirrel" + "github.com/myrteametrics/myrtea-engine-api/v5/internals/utils/dbutils" "sync" "time" - - "github.com/myrteametrics/myrtea-engine-api/v5/internals/dbutils" ) // Repository is a storage interface which can be implemented by multiple backend diff --git a/internals/reader/reader.go b/internals/reader/reader.go index a4a57b2a..33aef80e 100644 --- a/internals/reader/reader.go +++ b/internals/reader/reader.go @@ -147,7 +147,11 @@ func Parse(res *search.Response) (*WidgetData, error) { zap.L().Warn("Cannot unmarshall Source", zap.Any("source", hit.Source_)) continue } - hits = append(hits, Hit{ID: hit.Id_, Fields: fields}) + if hit.Id_ == nil { + zap.L().Warn("Cannot get ID", zap.Any("hit", hit)) + continue + } + hits = append(hits, Hit{ID: *hit.Id_, Fields: fields}) } widgetData := WidgetData{ diff --git a/internals/dbutils/dbutils.go b/internals/utils/dbutils/dbutils.go similarity index 100% rename from internals/dbutils/dbutils.go rename to internals/utils/dbutils/dbutils.go diff --git a/internals/queryutils/utils.go b/internals/utils/queryutils/utils.go similarity index 100% rename from internals/queryutils/utils.go rename to internals/utils/queryutils/utils.go diff --git a/internals/queryutils/utils_test.go b/internals/utils/queryutils/utils_test.go similarity index 100% rename from internals/queryutils/utils_test.go rename to internals/utils/queryutils/utils_test.go