gorules / zen

Open-source Business Rules Engine for your Rust, NodeJS, Python or Go applications.
https://gorules.io
MIT License
759 stars 73 forks source link

getDecision method in ZenEngine #233

Open suraj-sundariya opened 3 weeks ago

suraj-sundariya commented 3 weeks ago

Hi Team, I want to fetch rules directly from the goRules board without exporting them in a file, etc.

I was looking the constructor of ZenEngine and found a method called getDecision which requires a key.

Please answer the following question

  1. what is the purpose of this method
  2. How can I get this decision key from goRule dashboard
  3. If not by using this method then how?
justin0108 commented 3 weeks ago

Hi @suraj-sundariya ,

Taking reference from the JS code: getDecision is actually making use of the loader that you pass in during Engine initialisation. ZenEngine constructor is able to take in ZenEngineOptions and ZenEngineOptions is able to take in a function which is expecting a key and return decision content.

export class ZenEngine {
  constructor(options?: ZenEngineOptions | undefined | null)
  ...
}

export interface ZenEngineOptions {
  loader?: (key: string) => Promise<Buffer | ZenDecisionContent>
  customHandler?: (request: ZenEngineHandlerRequest) => Promise<ZenEngineHandlerResponse>
}

So this means that you have the flexibility to indicate to ZenEngine how to fetch the decision content. For example, with this code I will be able to fetch the decision content from filesystem

const loader = async (filename: string) => fs.readFile(filename);
...
const engine = new ZenEngine({ loader });
const decision = await engine.getDecision("YourDecisionFilename.json");

If you are using Rust, this code snippet is similar to the above JS and it already provided a FilesystemLoader for you

let engine = DecisionEngine::new(FilesystemLoader::new(FilesystemLoaderOptions {
    keep_in_memory: true, // optionally, keep in memory for increase performance
    root: root_path
}));
let decision = match engine.get_decision("YourDecisionFilename.json").await {
    ...
};
suraj-sundariya commented 3 weeks ago

@justin0108 Thanks for the detailed answer but my problem is different

  const loader = async (filename: string) => fs.readFile(filename);
  ...
  const engine = new ZenEngine({ loader });
  const decision = await engine.getDecision("YourDecisionFilename.json");

in this case, we have to export a file(YourDecisionFilename) from GoRules dashboard and have to store this somewhere in the file system or s3.

I'm looking for something in which I don't have to export this file and store it somewhere. As GoRules already have the data stored, I want to access that data directly by using an API call or by passing the access key in one of the methods.

like below

  const query = { country: "USA" }
  const engine = new ZenEngine();
  const rules = engine.fetchRulesByAccessKey(key) // Something like this 
  const decision = engine.createDecision(rules);
  const result = await decision.evaluate(query);
  engine.dispose();
  return result;
justin0108 commented 3 weeks ago

I see. It seems that you are using their BRMS and wana access the rules store in their system. For my case I am only using their BRE.

Guess I cannot help much then 🙏

stefan-gorules commented 3 weeks ago

@justin0108 Thanks a ton for providing community support!

Regarding accessing GoRules files, we have details about our API exposed through the swagger which can be accessed here: https://initial.gorules.io/api/docs.

Most of the endpoints should be accessible using PAT which you can generate from the Settings page. The endpoints you are interested in are tagged as document. Let me know if this works.

justin0108 commented 3 weeks ago

@stefan-gorules you guys have did such great work in providing an open source engine with exceptional features and editor.

it's my pleasure to contribute my knowledge back to the community

suraj-sundariya commented 3 weeks ago

@stefan-gorules Thank you so much, the mentioned document is really helpful and resolves all of my queries.

This URL was not listed in the public document, it would be great if you could list it there.

Also @justin0108 Thanks for your contribution too.

suraj-sundariya commented 6 days ago

@stefan-gorules All of a sudden the API is giving 400(bad request) which was working earlier.

Error: " Invalid enum value. Expected 'documents' | 'releases' | 'releases:manage' | 'releases:delete' | 'integrations:manage', received 'releases:view'"

Could you please check what is wrong with my request?

curl 'https://initial.gorules.io/api/projects/{{projectId}}/documents/{{documentId}}' \ -H 'accept: application/json, text/plain, /' \ -H 'accept-language: en-GB,en-US;q=0.9,en;q=0.8' \ -H 'authorization: Bearer {{token}}' \ -H 'cache-control: no-cache' \ -H 'cookie: cookieyes-consent=consentid:YTdsN2VZbTNQZ3RJU3N2RU9zRlVGYVh4SXB4OHdEZkY,consent:yes,action:yes,necessary:yes,functional:yes,analytics:yes,performance:yes,advertisement:yes,other:yes; _gcl_au=1.1.724081848.1725251189; _ga=GA1.1.1417631158.1725251189; _ga_81N81MPDXW=GS1.1.1725343909.2.1.1725343936.33.0.0' \ -H 'pragma: no-cache' \ -H 'priority: u=1, i' \ -H 'referer: https://initial.gorules.io/api/docs' \ -H 'sec-ch-ua: "Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"' \ -H 'sec-ch-ua-mobile: ?0' \ -H 'sec-ch-ua-platform: "macOS"' \ -H 'sec-fetch-dest: empty' \ -H 'sec-fetch-mode: cors' \ -H 'sec-fetch-site: same-origin' \ -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'

ivanmiletic commented 6 days ago

Taking a look

ivanmiletic commented 6 days ago

@suraj-sundariya please try it now