我正在尝试编写一个CEP规则,该规则将接收所有现有的ACTIVE警报,并将特定片段 bike_alarm.priority 每分钟增加1。这是警报的整体结构:
{
"count": 1,
"creationTime": "2018-07-09T15:30:20.790+02:00",
"time": "2014-03-03T12:03:27.845Z",
"history": {
"auditRecords": [],
"self": "https://cumulocity.com/audit/auditRecords"
},
"id": "1024",
"self": "https://cumulocity.com/alarm/alarms/1024",
"severity": "WARNING",
"source": {
"id": "22022",
"name": "01BIKE_STATION_NORTH",
"self": "https://cumulocity.com/inventory/managedObjects/22022"
},
"status": "ACTIVE",
"text": "Bike disconnected",
"type": "bike_error_01",
"bike_alarm" {
"priority": 10
}
}
这是我设法创建的(主要基于this question):
create schema Alarm as Alarm;
create schema CollectedAlarms(
alarms List
);
create schema SingleAlarm(
alarm Alarm
);
@Name("Collecting alarms")
insert into CollectedAlarms
select findAllAlarmByTimeBetween(
current_timestamp().minus(100 day).toDate(),
current_timestamp().toDate()
) as alarms
from pattern[every timer:interval(30 sec)];
@Name("Splitting alarms")
insert into SingleAlarm
select
singleAlarm as alarm
from
CollectedAlarms as alarms unidirectional,
CollectedAlarms[alarms@type(Alarm)] as singleAlarm;
@Name("Rising priority")
insert into UpdateAlarm
select
sa.alarm.id as id,
{
"bike_alarm.priority", GetNumber(sa.alarm, "bike_alarm.priority". 0) + 1
} as fragments
from pattern [every sa = SingleAlarm -> (timer:interval(1 minutes))];
问题在于并非所有警报都已建立,即使是增量警报也不起作用,因此将优先级设置为 null 。
此外,您能指出一些更好的文档吗?是this something you use吗?
答案 0 :(得分:3)
通常,您链接的esper文档是查找通用语法的最佳位置。 结合起来,您有时可能还需要针对特定内容的Cumulocity文档:http://cumulocity.com/guides/event-language/introduction
遇到您的问题: 您错过了使用实时处理引擎执行类似cron的批处理操作的机会。尽管从技术上讲可以做到这一点,但这可能不是最佳方法,我将向您展示可以采用的另一种方法。
但是首先解决您的方法:
我认为您应该采取不同的方法: 如果尚未清除,则对每一个进入的警报在1分钟后提高优先级。另外,再次触发1分钟计时器。就像一个循环,直到警报被清除。
此模式想要这样:
from pattern [
every e = AlarmCreated(alarm.status = CumulocityAlarmStatuses.ACTIVE)
-> (timer:interval(1 minutes)
and not AlarmUpdated(alarm.status != CumulocityAlarmStatuses.ACTIVE, alarm.id.value = e.alarm.id.value))
];
您需要一个带有AlarmCreated的语句,该语句只能覆盖最初的增加,而第二个语句将在您自己的模式上触发,然后在循环中运行。
通常,请尝试避免尽可能多的数据库调用。因此,请将循环计数器保留在您的架构中,以便您始终只需要执行警报的更新调用即可。