mirror of
https://github.com/hanzoai/jube-fork.git
synced 2026-10-30 18:33:33 +00:00
* Pushed Case Workflow Display HTML rendering logic down into a controller for improved security and consistent tokenisation of the Jube token format against context. Included a migration to update the default demo entry to use proper tokenisation. Updated the repository to support fetching CaseWorkflowDisplay with appropriate security. * Implemented improved tokenisation of the EntityAnalysisModelInstanceEntryPayload model via a common extension method on EntityAnalysisModelInstanceEntryPayload, used consistently across the system for serialisation and storage. * Included a migration to update the tokenisation format for full CaseWorkflowDisplay support. * Fixed an issue where CaseWorkflowFilter was incorrectly filtering on CaseWorkflowStatus.Guid instead of CaseWorkflowStatus.Name, causing the GUID to be returned rather than the display name on the template/demo model. (The remainder was fixed in GitHub issue #6.) Included a migration with a corrective SQL statement. * Fixed an issue where tokens were not being stored alongside CaseWorkflowFilter definition configuration. It now behaves identically to CaseSearch. Note: migrating missing tokens is not feasible as it requires JS-side parsing via JQueryBuilder. * Reduced the size of the CaseWorkflowForm invocation to include only the required fields given the new POST body context being rendered server side. Improved API serialisation by using a dictionary for form payload keys and values. * Consolidated JSON serialisation settings into DI and updated usages throughout. * Updated EntityAnalysisModelInstanceEntryPayload write serialisers to handle deserialisation, which is required to support tokenisation of new Case Automation POST bodies. * Created an extension method on the EntityAnalysisModelInstanceEntryPayload to centralise tokenisation of strings across Activation and Case Automation on a consistent basis. Simplified Case Notification and Case HTTP dispatch by removing tokenisation logic from those classes. * Removed a redundant field from the CaseWorkflowFormEntry POCO (database left unchanged for now). * Fixed a bug in Case session SQL rebuilding where the rebuild date was not being correctly checked for null or default value, which caused a failure in the last migration. * Made minor modifications to the Create Case process and method signature to skip serialisation when the payload object required for tokenisation is already available on the thread (e.g. during synchronous reprocessing). * Updated Case Workflow Macro processing to fetch the archive payload for the case from the database, improving security of that invocation endpoint. * Removed what appeared to be a duplicate EntityAnalysisModelInstanceEntryPayload model introduced during refactoring. * Fixed a but in Case Worfkflow Display Role assigment owing to volatile Guid in its update.
229 lines
14 KiB
C#
229 lines
14 KiB
C#
/* Copyright (C) 2022-present Jube Holdings Limited.
|
|
*
|
|
* This file is part of Jube™ software.
|
|
*
|
|
* Jube™ is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License
|
|
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
* Jube™ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU Affero General Public License along with Jube™. If not,
|
|
* see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
namespace Jube.Engine.EntityAnalysisModelInvoke.Context.Extensions.ActivationRules
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Cache;
|
|
using Data.Poco;
|
|
using EntityAnalysisModelManager.EntityAnalysisModel.Models.Models.EntityAnalysisModelInlineScript;
|
|
using Models.CaseManagement;
|
|
using Models.Payload.EntityAnalysisModelInstanceEntryPayload;
|
|
using RabbitMQ.Client;
|
|
using ReflectionHelpers;
|
|
using EntityAnalysisModel=EntityAnalysisModelManager.EntityAnalysisModel.EntityAnalysisModel;
|
|
|
|
public static class IterateActivationRulesExtensions
|
|
{
|
|
public static async Task<(int activationRuleCount, CreateCase createCase, int? prevailingActivationRuleId)> IterateAndProcessAsync(this Context context,
|
|
CacheService cacheService, Dictionary<int, EntityAnalysisModel> availableModels,
|
|
IModel rabbitMqChannel)
|
|
{
|
|
var rulesCount = context.EntityAnalysisModel.Collections.ModelActivationRules.Count;
|
|
var prevailingActivationRuleName = String.Empty;
|
|
var responseElevationHighWaterMark = 0d;
|
|
var suppressedActivationRules = new List<string>(rulesCount);
|
|
var activationRuleCount = 0;
|
|
CreateCase createCase = null;
|
|
int? prevailingActivationRuleId = null;
|
|
|
|
foreach (var evaluateActivationRule in context.EntityAnalysisModel.Collections.ModelActivationRules)
|
|
{
|
|
try
|
|
{
|
|
var suppressed = false;
|
|
if (context.ActivationRuleGetSuppressedModel(ref suppressedActivationRules) || context.CheckSuppressedResponseElevation())
|
|
{
|
|
suppressed = true;
|
|
|
|
if (context.Log.IsInfoEnabled)
|
|
{
|
|
context.Log.Info(
|
|
$"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} activation rule {evaluateActivationRule.Id} is suppressed at the model level or has exceeded response elevation counter at {context.EntityAnalysisModel.ConcurrentQueues.BillingResponseElevationBalanceEntries.Count} or {context.EntityAnalysisModel.Counters.BillingResponseElevationBalance}.");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (context.Log.IsInfoEnabled)
|
|
{
|
|
context.Log.Info(
|
|
$"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} activation rule {evaluateActivationRule.Id} is not suppressed at the model level, will test at rule level.");
|
|
}
|
|
|
|
if (!evaluateActivationRule.EnableReprocessing && context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelReprocessingRuleInstanceId.HasValue)
|
|
{
|
|
suppressed = true;
|
|
|
|
if (context.Log.IsInfoEnabled)
|
|
{
|
|
context.Log.Info(
|
|
$"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} activation rule {evaluateActivationRule.Id} is suppressed at the activation rule level because of reprocessing.");
|
|
}
|
|
}
|
|
else if (suppressedActivationRules is { Count: > 0 })
|
|
{
|
|
suppressed = suppressedActivationRules.Contains(evaluateActivationRule.Name);
|
|
|
|
if (context.Log.IsInfoEnabled)
|
|
{
|
|
context.Log.Info(
|
|
suppressed
|
|
? $"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} activation rule {evaluateActivationRule.Id} is suppressed at the activation rule level."
|
|
: $"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} activation rule {evaluateActivationRule.Id} is not suppressed at the activation rule level.");
|
|
}
|
|
}
|
|
}
|
|
|
|
var activationSample = evaluateActivationRule.ActivationSample >= context.Random.NextDouble();
|
|
if (!activationSample)
|
|
{
|
|
if (context.Log.IsInfoEnabled)
|
|
{
|
|
context.Log.Info(
|
|
$"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} has failed in sampling so certain activations will not take place even if there is a match on the activation rule.");
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
if (context.Log.IsInfoEnabled)
|
|
{
|
|
context.Log.Info(
|
|
$"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} has passed sampling and is eligible for activation.");
|
|
}
|
|
|
|
var matched = ReflectRuleHelper.Execute(
|
|
evaluateActivationRule,
|
|
context.EntityAnalysisModel,
|
|
context.EntityAnalysisModelInstanceEntryPayload,
|
|
context.EntityAnalysisModelInstanceEntryPayload.Dictionary,
|
|
context.Log);
|
|
|
|
if (context.Log.IsInfoEnabled)
|
|
{
|
|
context.Log.Info(
|
|
$"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} has finished testing the activation rule and it has a matched status of {matched}.");
|
|
}
|
|
|
|
if (context.Log.IsInfoEnabled)
|
|
{
|
|
context.Log.Info(
|
|
$"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} is checking for Activation Rule Evaluation InlineScripts looking for context Guid:{evaluateActivationRule.Guid} and or Name:{evaluateActivationRule.Name}.");
|
|
}
|
|
|
|
foreach (var inlineScript in context.EntityAnalysisModel.Collections.EntityAnalysisModelInlineScripts.Where(s => s.EntityAnalysisModelInlineScriptEvents
|
|
.Any(e => e.EntityAnalysisModelInlineScriptEventType == EntityAnalysisModelInlineScriptEventTypeEnum.AbstractionRuleOverride
|
|
&& (e.Guid == evaluateActivationRule.Guid || e.Guid == Guid.Empty)
|
|
&& (e.Name == evaluateActivationRule.Name || String.IsNullOrEmpty(e.Name))
|
|
)))
|
|
{
|
|
if (context.Log.IsInfoEnabled)
|
|
{
|
|
context.Log.Info(
|
|
$"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} is about to execute Inline Script Id: {inlineScript.Id}.");
|
|
}
|
|
|
|
if (await ReflectInlineScriptHelper.ExecuteAsync(inlineScript, context).ConfigureAwait(false))
|
|
{
|
|
if (context.Log.IsInfoEnabled)
|
|
{
|
|
context.Log.Info(
|
|
$"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} matched Inline Script Id: {inlineScript.Id}.");
|
|
}
|
|
|
|
matched = true;
|
|
}
|
|
else
|
|
{
|
|
if (context.Log.IsInfoEnabled)
|
|
{
|
|
context.Log.Info(
|
|
$"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} did not match Inline Script Id: {inlineScript.Id}.");
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!matched)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (context.EntityAnalysisModelInstanceEntryPayload.Activation.ContainsKey(evaluateActivationRule.Name))
|
|
{
|
|
if (context.Log.IsInfoEnabled)
|
|
{
|
|
context.Log.Info(
|
|
$"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} and has already added the activation rule {evaluateActivationRule.Id} on {evaluateActivationRule.Name} for processing.");
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
context.EntityAnalysisModelInstanceEntryPayload.Activation.Add(
|
|
evaluateActivationRule.Name,
|
|
new EntityModelActivationRulePayload
|
|
{
|
|
Visible = evaluateActivationRule.Visible
|
|
});
|
|
|
|
if (context.Log.IsInfoEnabled)
|
|
{
|
|
context.Log.Info(
|
|
$"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} and has added the activation rule {evaluateActivationRule.Id} flag on {evaluateActivationRule.Name} to the activation buffer for processing.");
|
|
}
|
|
|
|
if (evaluateActivationRule.ReportTable)
|
|
{
|
|
context.EntityAnalysisModelInstanceEntryPayload.ArchiveKeys.Add(new ArchiveKey
|
|
{
|
|
ProcessingTypeId = 11,
|
|
Key = evaluateActivationRule.Name,
|
|
KeyValueBoolean = 1,
|
|
EntityAnalysisModelInstanceEntryGuid =
|
|
context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid
|
|
});
|
|
|
|
if (context.Log.IsInfoEnabled)
|
|
{
|
|
context.Log.Info(
|
|
$"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} and has added the activation rule {evaluateActivationRule.Id} flag to the response payload also.");
|
|
}
|
|
}
|
|
|
|
context.ProcessResponseElevation(evaluateActivationRule, ref responseElevationHighWaterMark, suppressed);
|
|
context.ActivationRuleNotification(evaluateActivationRule, suppressed, rabbitMqChannel);
|
|
|
|
context.ActivationRuleCountsAndArchiveHighWatermark(evaluateActivationRule, suppressed, ref activationRuleCount,
|
|
ref prevailingActivationRuleId, ref prevailingActivationRuleName);
|
|
|
|
context.ActivationRuleActivationWatcher(evaluateActivationRule, suppressed, rabbitMqChannel);
|
|
|
|
createCase ??= context.ActivationRuleCreateCaseObject(evaluateActivationRule, suppressed);
|
|
|
|
context.ActivationRuleTtlCounter(evaluateActivationRule, availableModels, cacheService);
|
|
}
|
|
catch (Exception ex) when (ex is not OperationCanceledException)
|
|
{
|
|
context.Log.Error(
|
|
$"Entity Invoke: GUID {context.EntityAnalysisModelInstanceEntryPayload.EntityAnalysisModelInstanceEntryGuid} and model {context.EntityAnalysisModel.Instance.Id} error in TTL Counter processing as {ex} .");
|
|
}
|
|
}
|
|
|
|
return (activationRuleCount, createCase, prevailingActivationRuleId);
|
|
}
|
|
}
|
|
}
|