Working on historical data (incrementing properties) - complex-event-processing

I'm trying to write a CEP rule that would take all the existing ACTIVE alarms and increase a specific fragment bike_alarm.priority by 1 every minute. This is the whole structure of alarm:
{
"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 is what I managed to create (based mainly on 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))];
the problem is that not all alarms are founded and even those that are the incrementation don't work, priority is set to null.
Additionally, could you point me in direction of some better documentation? Is this something you use?

In general the esper documentation that you linked is the best place to look for the generic syntax.
In combination you probably sometimes also need the Cumulocity documentation for the specific stuff: http://cumulocity.com/guides/event-language/introduction
Coming to your problems:
You are miss-using a realtime processing engine to do cron-like batch operations. While it technically can be done this might not be the best approach and I will show you a different approach that you can take.
But first solving your approach:
The database queries like findAllAlarmByTimeBetween() will only return up to 2000 results and there is no way to get the next page like on the REST API of Cumulocity. Also if you say you want to handle only active alarms you should use a function that also filters for the status.
Getting null out of a function like getNumber() means the JsonPath wasn't found or the dataType is incorrect (using getNumber for a String). You can set a default value for that case as the third parameter. From you Json that you provided it looks correct though. The syntax errors in your code are copy paste errors I assume as otherwise you wouldn't have been able to deploy it.
In my opinion you should approach that differently:
On each incoming alarm raise the priority after one minute if it hasn't been cleared. Additionally trigger the 1 minute timer again. Like a loop until the alarm is cleared.
The pattern for this would like like that:
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))
];
You need one with AlarmCreated which will only cover the initial increase and a second statement that triggers on your own schema which is then run in a loop.
In general try to avoid as many database calls as you can. So keep the loop counter in your schema so you only always need to execute the update call of the alarm.

Related

How to get many issues with Jira REST API?

New to REST API, and struggling a little.
I learned recently from here and here that I can receive a JSON describing an issue by calling a REST API of the form <JIRA_BASE_URL>/rest/api/2/issue/{issueIdOrKey}, e.g.:
curl -s -X GET -u super_user:super_password https://jira.server.com/rest/api/2/issue/TEST-12
Is there a way I can query many issues at once if I have a list of issue-ids, e.g. ["TEST-12", "TEST-13", "TEST-14"]?
I'm specifically interested in getting the summary field of each issue in my list of issue-ids. I.e. I'm trying to create a map of [issue-id:summary]. I currently do this by invoking the above curl command in a loop for each issue-id in my list. But I observe that this takes a long time, and I wonder if performance might be improved if there's a way to do a "bulk get" -- if such a feature exists.
Give the JQL Search API endpoint a try:
https://jira-url/rest/api/latest/search?fields=summary&jql=key%20in%20(TEST-12,%20TEST-13)
The fields parameter limits the fields returned, and the jql parameter lists out an array of the issue keys you'd like to retrieve.
The response looks like this:
{
...
"startAt": 0,
"maxResults": 50,
"total": 2,
"issues": [
{
...
"key": "TEST-12",
"fields": {
"summary": "TEST-12 Summary"
}
},
{
...
"key": "TEST-13",
"fields": {
"summary": "TEST-13 Summary"
}
}
]
}

Arbitrary HTTP API Call to Enter Cell Value into a MUTL_PICKLIST Column

I am quite new new to Smartsheets and to programming.
I am using Integromat to update various stuff in Smartsheets - 99% operations are done via a nice interface for dummies.
But I have an issue with one column which is MULTI_PICKLIST and which cannot be processed with native dummy-friendly UI.
Basically, I'm adding a new row and one of the columns on the way is the MULTI_PICKLIST one. In order to enter value into this cell, I need to make an arbitrary HTTP API call.
I know row ID, I know column ID. I just need to construct the body of the HTTP request.
The possible picklist value are: John or Maya or Paul. Assume I need to enter "John" into the column.
Attached, you will find my "progress". I obviously, I'm stuck with the BODY part. Can someone give me a little push, please? I think it's gotta be like 5 lines of code.
This is what I have:
DZ
A few things...
First, the value that you're using for URL doesn't look quite right. It should be in the following format, where {sheetId} is replaced with the ID of the sheet you're updating:
sheets/{sheetId}/rows
Second, I don't think you need the key/value that you've specified for Query String -- I'd suggest that you delete this info.
Next, I'm not sure what the other possible values are for Type (based on your screenshot, it looks like a picklist) -- but if JSON is an option, I'd suggest choosing that option instead of Text.
Finally, here's any example of the correct structure/contents for Body to update a MULTI_PICKLIST cell with the value John -- replace the value of the id property (5225480965908356) with your Row ID and replace the value of the columnId property (8436269809198980) with your Column ID:
[
{
"id": "5225480965908356",
"cells": [
{
"columnId": "8436269809198980",
"objectValue": {
"objectType": "MULTI_PICKLIST",
"values": ["John"]
}
}
]
}
]
If you want to select multiple values for a MULTI_PICKLIST cell, here's an example that specifies two values for the cell (John and Maya):
[
{
"id": "5225480965908356",
"cells": [
{
"columnId": "8436269809198980",
"objectValue": {
"objectType": "MULTI_PICKLIST",
"values": ["John", "Maya"]
}
}
]
}
]
** UPDATE **
My initial answer answer above assumed you wanted to update a cell value in a MULTI-PICKLIST column (b/c you've selected PUT for the Method value in your screenshot -- which is the verb used to update a row). Having re-read your question just now though, it sounds like maybe you want to add a new row...is that correct? If so, then the value for Method should be POST (not PUT), and Body will need to include additional objects within the cells array to specify values of other cells in the new row. The following example request (when used with the verb POST) adds a new row and populates 3 cells in that row, the first of which is a MULTI_PICKLIST cell:
[
{
"cells": [
{
"columnId": "8436269809198980",
"objectValue": {
"objectType": "MULTI_PICKLIST",
"values": ["John"]
}
},
{
"columnId": 6101753539127172,
"value": "test value"
},
{
"columnId": 4055216160040836,
"value": 10
}
]
}
]
More info about the Add Rows request can be found in the Smartsheet API docs: Add Rows.

Sensu: best practices to only handle a check after n occurences

I've recently upgraded from Sensu 0.24 to 1.2 and noticed that checks where triggered immediately to the referenced handlers.
On the old checks (with v0.24), checks had an "occurences" attribute to filter out noise. I only want checks to be handled by handlers on every n occurences, i.e. a http check must fail 5 times before the pagerduty handler will be triggered. This behaviour seems to have changed with the sensu upgrade.
As I understand, a handler is supposed to include a filter to sort out events based on attributes. So suppose this is my check:
{
"checks": {
"examplecom_http": {
"command": "check-http.rb --url https://example.com -s -k -q 'Keyword'",
"handlers": ["default","pagerduty"],
"subscribers": ["network"],
"interval": 60,
"occurrences": 5
}
}
}
In previous versions (or at least that was my understanding), this check would only be handled after 5 minutes (5 occurences for 60 second intervals) of failure. This doesn't work anymore, so now the handler should include a filter to handle occurences:
{
"handlers": {
"pagerduty": {
"type": "pipe",
"command": "/etc/sensu/plugins/pagerduty.rb",
"severities": ["critical"],
"filter": "occurences"
}
}
And the "occurences" filter would look like this:
{
"filters": {
"occurences": {
"attributes": {
"occurrences": "eval: value >= 5"
}
}
}
}
However, whatever comes after the eval part, be it value >= 5 or value < 5, the effect is the same and the pagerduty handler gets executed. I've tried using the negate directive with true and false but it seems my understanding of how filtering and occurences work for checks is just not correct. Maybe checks don't count their occurences at all?
Can somebody help and explain this?
As it stands right now, it looks like occurrences in your handlers JSON block is spelled incorrectly as occurences.
You will want "negate": false the way that your eval is currently written. When negate is false, this means that the handler will only trigger when the filter evaluation is true - negate will simply reverse the result so handlers trigger when the filter evaluation is false.
Finally, if you are looking to trigger the handler "every n occurrences" instead of every occurrence after the 5th, consider this evaluation:
{
"filters": {
"every_five": {
"attributes": {
"occurrences": "eval: value % 5 == 0"
}
}
}
}
This way, when the number of occurrences is divisible by 5, the handler will trigger, but you will need to add the every_five filter to your handler definition. You can change filter to filters and pass an array instead, like so:
{
"handlers": {
"pagerduty": {
"type": "pipe",
"command": "/etc/sensu/plugins/pagerduty.rb",
"severities": ["critical"],
"filters": [
"occurrences",
"every_five"
}
}
}
This will allow you to use the built-in filter "occurrences" as well as your custom filter.

How to pass a parameter to dashDB query using Node-RED editor?

In Bluemix Node-RED application I use Cloudant and dashDB services. I replicated Cloudant database into dashDB which contains multiple values stored in a table like DALERT,DEVICE,ID etc.
I am trying to search records from the CLOUDANT table in my dashDB using DALERT column which value is equal to critical.
I am trying these way in Node-RED editor but unable to retrieve data from dashDB:
[
{"id":"9941f62b.66be08","type":"http in","name":"","url":"/get/specificcritical","method":"get","swaggerDoc":"","x":103.5,"y":409,"z":"c96fb1cb.36905","wires":[["b52196bf.4ade68","292d3e2e.d6d2c2"]]},
{"id":"b52196bf.4ade68","type":"function","name":"","func":"msg.Dalert=msg.payload.Dalert;\n\nreturn msg;","outputs":1,"noerr":0,"x":326,"y":353,"z":"c96fb1cb.36905","wires":[["457cf34.fba830c","d937915d.26c87"]]},
{"id":"457cf34.fba830c","type":"dashDB in","service":"dashDB-9a","query":"select * from XXXXX.CLOUDANT WHERE DALERT=?;","params":"msg.Dalert","name":"","x":510,"y":404,"z":"c96fb1cb.36905","wires":[["60d36407.9f2c9c","886c48df.7793b8"]]},
{"id":"d937915d.26c87","type":"debug","name":"","active":false,"console":"true","complete":"payload","x":599,"y":327,"z":"c96fb1cb.36905","wires":[]},
{"id":"60d36407.9f2c9c","type":"debug","name":"","active":true,"console":"false","complete":"false","x":758,"y":397,"z":"c96fb1cb.36905","wires":[]},
{"id":"886c48df.7793b8","type":"http response","name":"","x":771,"y":477,"z":"c96fb1cb.36905","wires":[]},
{"id":"292d3e2e.d6d2c2","type":"debug","name":"","active":true,"console":"true","complete":"payload","x":321,"y":483,"z":"c96fb1cb.36905","wires":[]}
]
Please let me know if there is any solution.
If I understood correct your question, you just need to modify your function node with something similar to this:
msg.dalert="critical";
return msg;
Assuming your DALERT column in the CLOUDANT table is of type VARCHAR. You may need to change it if is a different type on your database.
Running the application like:
http://yourappname.mybluemix.net/get/specificcritical
will result in output similar to this for my table:
[
{
"DALERT": "critical",
"DEVICE": "device1",
"ID": 1
},
{
"DALERT": "critical",
"DEVICE": "device3",
"ID": 3
},
{
"DALERT": "critical",
"DEVICE": "device5",
"ID": 5
}
]
Here is the new node flow I created with the changes (I added an input node with blank message just to test the flow in the editor):
[{"id":"c7468303.38b98","type":"http in","name":"","url":"/get/specificcritical","method":"get","swaggerDoc":"","x":125,"y":245,"z":"8e2ae4a.f71d518","wires":[["c685ce8c.397a3","20dfeeba.df2012"]]},{"id":"c685ce8c.397a3","type":"function","name":"","func":"msg.dalert=\"critical\";\nreturn msg;","outputs":1,"noerr":0,"x":347.5,"y":189,"z":"8e2ae4a.f71d518","wires":[["e1f8c153.1e074","1f2d6f8e.e0d29"]]},{"id":"e1f8c153.1e074","type":"dashDB in","service":"dashDB-0a","query":"select * from CLOUDANT WHERE DALERT=?;","params":"msg.dalert","name":"","x":531.5,"y":240,"z":"8e2ae4a.f71d518","wires":[["f1810e4c.0e7ef","1401dc1a.ebfe24"]]},{"id":"1f2d6f8e.e0d29","type":"debug","name":"","active":false,"console":"true","complete":"payload","x":620.5,"y":163,"z":"8e2ae4a.f71d518","wires":[]},{"id":"f1810e4c.0e7ef","type":"debug","name":"dashDB Output","active":true,"console":"false","complete":"payload","x":779.5,"y":233,"z":"8e2ae4a.f71d518","wires":[]},{"id":"1401dc1a.ebfe24","type":"http response","name":"","x":792.5,"y":313,"z":"8e2ae4a.f71d518","wires":[]},{"id":"20dfeeba.df2012","type":"debug","name":"","active":true,"console":"true","complete":"payload","x":342.5,"y":319,"z":"8e2ae4a.f71d518","wires":[]},{"id":"1f530672.e0acfa","type":"inject","name":"","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":122,"y":112,"z":"8e2ae4a.f71d518","wires":[["c685ce8c.397a3"]]}]
I got ans for question.
if we want to pass value as parameter just write msg.variable=msg.payload.variable; and return msg;
inside function node and msg.variable also declare in query parm inside dashDB IN node. eg:msg.Dalert=msg.payload.Dalert; and
critical value pass with url as http://yourappname.mybluemix.net/get/specificcritical?Dalert=critical
Its simple working node red flow
[
{"id":"9941f62b.66be08","type":"http in","name":"","url":"/get/specificcritical","method":"get","swaggerDoc":"","x":103.5,"y":409,"z":"c96fb1cb.36905","wires":[["b52196bf.4ade68","292d3e2e.d6d2c2"]]},
{"id":"b52196bf.4ade68","type":"function","name":"","func":"msg.Device=msg.payload.Device;\n\nreturn msg;","outputs":1,"noerr":0,"x":326,"y":353,"z":"c96fb1cb.36905","wires":[["457cf34.fba830c","d937915d.26c87"]]},
{"id":"457cf34.fba830c","type":"dashDB in","service":"dashDB-XX","query":"select * from XXXXX.CLOUDANT WHERE DEVICE=?","params":"msg.Device","name":"","x":510,"y":404,"z":"c96fb1cb.36905","wires":[["60d36407.9f2c9c","886c48df.7793b8"]]},
{"id":"d937915d.26c87","type":"debug","name":"","active":false,"console":"true","complete":"payload","x":599,"y":327,"z":"c96fb1cb.36905","wires":[]},
{"id":"60d36407.9f2c9c","type":"debug","name":"","active":true,"console":"false","complete":"false","x":758,"y":397,"z":"c96fb1cb.36905","wires":[]},
{"id":"886c48df.7793b8","type":"http response","name":"","x":771,"y":477,"z":"c96fb1cb.36905","wires":[]},
{"id":"292d3e2e.d6d2c2","type":"debug","name":"","active":true,"console":"true","complete":"payload","x":321,"y":483,"z":"c96fb1cb.36905","wires":[]}
]

RESTful master/detail

Having 3 dropdown pickers in a web application. The web application uses a Restful service to populate pickers data.
The two first pickers get their values from something like /years and /colors. The third one should get its values depending on the settings of the two.
So it could be something like /models?year=1&color=red.
The question is, how to make this HATEOAS-compliant (so that the dev does not have to know the way he should create an url to get the models).
The root / gets me a number of links, such as:
{
"_links": {
"colors": "/colors",
"years": "/years",
"models": "???" }
}
What should be instead of ???? If there was some kind of template /models?color={color}&year={year}, the dev would have to create the url. Is this OK?
Or there could be a link to list of years on each color got from /colors and then a link to list of models on each year got from /years?color=red, but i'd have to first choose color, then populate years and then populate models. Any idea if i want to have the model dependent on both color and year, not just the year populated from color?
Is it even possible in this situation to make it hateoas-compliant?
I have not heard of HATEOAS before, but based on what I just read about it, it seems that it supposed to return links to where the consumer of the service can go forward in the "state machine".
In your case that would translate to the links being "function calls". The first two (/colors and /years) are functions that take no parameters (and return "something" at this point), while the third is a function call that takes two parameters: one that is a representation of a color, the other a year. For the first two having a simple URL will suffice for the link, but for the third, you need to include the parameter name/type information as well. Something like:
{
"_links": {
"colors": "/colors",
"years": "/years",
"models": {
"url": "/models",
"param1": {"color"}
"param2": {"year"}
}
}
}
Note: you can use the same layout as "models" for "colors" and "years" as well.
At this point the client knows what the URL to access the functions are and what the parameter (if any) names are to be passed to the function.
One more thing is missing: types. Although you could just use "string", it will not be obvious that the "color" parameter is actually a value from what "/colors" returns. You can be introducing a "type" Color that describes a color (and any functions that operate on a color: give a displayable name, HTML color code, etc.)
The "beefed up" signature becomes:
{
"_links": {
"colors": {
"url": "/colors",
"return": "/type/List?type=/type/Color"
},
"years": {
"url": "/years",
"return": "/type/List?type=/type/Integer"
},
"models": {
"url": "/models",
"param1": {
"name": "color",
"type": "/type/Color"
},
"param2": {
"name": "year",
"type": "/type/Integer"
}
"return": "/type/List?type=/type/Model"
}
}
}
Note: the path "/type" is used just to separate the types from functions, but is not necessary.
This will interchangeably and discoverably describe the functions, what parameters they take, and what values they are returning, so you can use the right value at the right place.
Of course implementing this on the service end will not be easy (especially with parameterized types, like "/type/List" -- think Generics in Java or templates in C++), but this is the most "safe" and "portable" way you can describe your interface to your clients.