Matching the absence of events - wso2

I have a simple stream of status of objects :
define stream statusStream (id string, success bool);
I want to query all of objects which are "failed" (success=false) since 5 minutes : all event of statusStream (, false) where there are no event (same id, true) within 5 minutes.
What is the simplest siddhi query for this kind of job ?
Currently I have :
define stream statusStream (id string, success bool);
from statusStream[ success == false ]#window.time(5 minutes)
insert expired events into expiredStatusStream;
from every status = statusStream[ success == false ]
-> ackStatus = statusStream[ success == true and id == status.id]
or expiredStatus = expiredStatusStream[ id == status.id ]
select status.id, ackStatus.id as ackId
into filteredStatusStream;
from filteredStatusStream[ ackStatus.id is null ]
insert into failedStatusStream;

In case you are getting 'success == false' messages to indicate failure, then try below execution plan:
#Import('statusStream:1.0.0')
define stream statusStream (id string, success bool);
#Export('alertStream:1.0.0')
define stream alertStream (alert string);
from statusStream
select id, success, time:timestampInMilliseconds() as ts
insert into statusStreamWithTS;
from every e1=statusStreamWithTS[success==false], statusStreamWithTS[success==false]*, e2=statusStreamWithTS[success==false AND (e2.ts - e1.ts) >= 500000]
select 'some message' as alert
insert into alertStream;
Here, an alert will be generated if we continue to get 'success==false' messages for 5 mins.

Related

How do I restrict hours of operation in a rest-based API function?

I have an API integration for our web store running on AWS Lambda to return live delivery quotes based on customer address, and then create the delivery order to a third party delivery as a service provider when the invoice is completed (paid).
I was able to add a time restriction for Monday-Saturday but Sunday has different hours and is not working. Here is the relevant code:
'use strict'
/**
* This function is use to generate qoutes to client 1st warehouse
*/
exports.handler = function (event, context, callback) {
console.log('-------------------EVENT OBJECT--------------------------')
// console.log(event.body.shipping_address)
console.log(event)
try {
const app = require('./app')
const EventEmitter = require('events').EventEmitter
const _bus = new EventEmitter()
let date = new Date()
if (date.getDay() == 0) {
if (!(date.getHours() >= 17 && date.getHours() <= 22) || !(date.getHours() < 3)) {
callback(null, {
message: 'The store is closed'
})
}
} else {
if (date.getHours() >= 3 && date.getHours() <= 15) {
callback(null, {
message: 'The store is closed'
})
}
}
let _shipmentReturn = []
let _shipmentReturnError = []
}
catch(e) {
}
}
Be very careful when using NOT logic.
Your 'normal' days have the store closed from 3am to 4pm. (Yes, 4pm. That's because you only check hours, so 3:59pm is still an 'hour' of 3, so it would be closed.)
On Sunday, the store is closed from midnight to 4:59pm, and also 10pm to midnight.
Take a look at this line:
if (!(date.getHours() >= 17 && date.getHours() <= 22) || !(date.getHours() < 3)) {
Let's pick a time of 2am. It equates to:
if (!(FALSE) || !(TRUE))
This equals TRUE, so the store is closed.
Same for 4am: if (!(FALSE) || !(FALSE)) also equals TRUE
You possibly want an AND rather than an OR in those logic statements.
I would also recommend that you convert the UTC times into your "local" times, which would make it easier for you to write the logic. This will avoid errors where UTC Sunday does not actually align to your 'local' Sunday. For example, if you are UTC-6, then 2am UTC Sunday is not Sunday in your timezone.

Exit Pipeline if user did not enter values for Active Choice Parameterized pipeline build

I wish to abort the pipeline if the user did not select any value for Active Choice parameter for single/multi choice/string pipeline parameter.
For example I have Active Choices Reactive Parameter Named "IPAddress" of Type "Multi Select" with Groovy Script as below:
if (Location.equals("MyTown")) {
return["DDL1", "DDL2", "DDL3", "DDL4"]
} else if (Location.equals("Your Town")) {
return["DDP1", "DDP2"]
} else {
return ["Select an IP from the drop-down"]
}
Thus, once I run the pipeline i see "Select an IP from the drop-down" for IPAddress.
Now, If the user does not select anything from the dropdown the pipeline should fail & abort.
In the pipeline script I have written the below condition check which fails to check the condition despite user ignoring to select any IPAddress.
def ex(param){
currentBuild.result = 'ABORTED'
error('BAD PARAM: ' + param)
}
pipeline {
agent any
stages {
stage ("Pre-Check Parameters") {
steps {
echo "Pre-Check called in pipeline"
script {
if ("${params.IPAddress}" == null) {ex("IPAddress")}
//if ("${params.myEnv}" == null) {ex("b")}
//if ("${params.myLoc}" == null) {ex("c")}
}
}
}
}
}
Can you please suggest what could be the issue here ?
Do you have any constraint against using the input step?
def days=''
pipeline{
agent any;
stages {
stage('master'){
steps{
script {
try {
timeout(time:10, unit:'SECONDS') {
days = input message: 'Please enter the time window in number of days', ok: 'Fetch Statistics', parameters: [string(defaultValue: '90', description: 'Number of days', name: 'days', trim: true)]
}
}
catch (err){
error("No custom value has been entered for number of days.")
}
}
}
}
}
}
To determine if your string is empty you can use the method .trim(). It will remove leading and trailing spaces from your string. The two magic words are "Groovy Truth". An empty string is false in Groovy. This makes it easier to evaluate conditional expressions. Means in your case, if you use .trim() in combination with an if conditional then the Groovy Truth value of the string will be used for the evaluation.
Your pipeline should work if you change it to the following. It will check if your variable is null or empty:
script {
if (!params.IPAddress?.trim()) {
ex("IPAddress")
}
}

DynamoDB multiple filter conditions, gives error - buildTree error: unset parameter: ConditionBuilder

I am building REST APIs, using Lambda and DynamoDB in GO.
I need to query the data based on multiple filters.
The number of filters can be varying based on the number of query parameters user has provided, while calling the REST API.
As per the below post, I had developed the code to add multiple conditions.
AWS SDK for Go - DynamoDb - Add multiple conditions to FilterExpression
But when I invoke the function, I get below error, in the logs.-
buildTree error: unset parameter: ConditionBuilder
The Filter expression is not applied and the scan returns all the results.
Here is the code snippet.
for queryParam, queryParamValue := range searchParams {
fmt.Println("queryParam:", queryParam, "=>", "queryParamValue:", queryParamValue)
if queryParam == “param1” {
param1Condition = expression.Name(“param1”).Equal(expression.Value(queryParamValue))
}
if queryParam == “param2” {
param2Condition = expression.Name(“param2”).Equal(expression.Value(queryParamValue))
}
}
sampleExpr, errSample := expression.NewBuilder().
WithCondition(param1Condition.Or(param2Condition)).
Build()
if errSample != nil {
fmt.Println("Error in building Sample Expr ", errSample)
} else {
fmt.Println("sampleExpr ", sampleExpr)
}
input := &dynamodb.ScanInput{
ExpressionAttributeNames: sampleExpr.Names(),
ExpressionAttributeValues: sampleExpr.Values(),
FilterExpression: sampleExpr.Filter(),
TableName: aws.String(deviceInfotable),
}
But if I create the expression in different way, it works.
filt := expression.Name("param1").Equal(expression.Value("valu1")).Or(expression.Name("param2").Equal(expression.Value("value2")))
ConditionBuilder has mode field
type ConditionBuilder struct {
operandList []OperandBuilder
conditionList []ConditionBuilder
mode conditionMode
}
The zero value of mode is unsetCond. When build condition, unsetCond raises the error.
https://github.com/aws/aws-sdk-go/blob/7798c2e0edc02ba058f7672d32f4ebf6603b5fc6/service/dynamodb/expression/condition.go#L1415
case unsetCond:
return exprNode{}, newUnsetParameterError("buildTree", "ConditionBuilder")
In your code, if queryParam != “param1” and queryParam != “param2”, the param1Condition and param2Condition is zero value of ConditionBuilder, which fails on build.

Pubnub History Api

I am trying to extract the message from my pubnub history, where I wish to compare any change of messages. For the same I am calling pubnub.history in my main callback function (used by pubnub.subscribe) -
pubnub.history(channel=channel, count = 1, callback =_callback,error=_error)
and I define my _callback as:
def _callback(message):
_type = message[0]
_type_ = _type[0]
prevMess = _type_['command']
if prevMess == "..":
flag = 1
elif prevMess == "...":
flag = 4
elif prevMess == "....":
flag = 7
print flag
How can I successfully 'return' the flag value from the _callback() to my callback()? Or in that case return "prevMess" value from _callback() to callback()?

Grails Unit Testing with Composite Keys

I'm trying to unit test a method where an object with a composite key is being inserted into the database. Whenever I run my unit test for this, I get the following.
Failure: |
testTransaction(com.myapp.foo.TestServiceSpec)
|
Condition not satisfied:
Transaction.count() == 1
| |
0 false
at com.myapp.foo.TestServiceSpec.testTransaction(TestServiceSpec.groovy:166)
If I remove the composite key code and nothing else from my domain class, the test passes.
This is my Domain Class, Transaction.groovy:
class Transaction implements Serializable {
String timestamp
String source
String tableName
String fieldName
Integer changeNumber
String fieldValue
String objectId
static mapping = {
id composite: ["timestamp", "source", "tableName", "fieldName", "changeNumber"], generator: 'assigned'
}
boolean equals(other) {
if (!(other instanceof Transaction)) {
return false
}
other.timestamp == timestamp && other.source == source && other.id == id && other.tableName == tableName && other.fieldName == fieldName && other.changeNumber == changeNumber
}
int hashCode() {
def builder = new HashCodeBuilder()
builder.append timestamp
builder.append source
builder.append tableName
builder.append fieldName
builder.append changeNumber
builder.toHashCode()
}
}
This is the code that's being tested:
def response = [code: 'OK']
def transaction = new Transaction()
transaction.timestamp = (new Date()).format("yyyy-MM-dd HH:mm:ss.SSS")
transaction.source = "APP"
transaction.tableName = "TABLE_NAME"
transaction.fieldName = "FIELD_NAME"
transaction.fieldValue = "FIELD_VALUE"
transaction.objectId = "OBJECT_ID"
def changeNumber = Transaction.createCriteria().get {
eq("objid", currentTransaction.objid)
eq("tableName", currentTransaction.tableName)
projections {
max("changeNumber")
}
}
currentTransaction.changeNumber = (changeNumber ?: 0) + 1
if(!transaction.save()) {
transaction.errors.each {
println it
}
response = [code: 'error transaction', status: 500]
}
return response
And finally, here's my unit test code:
void testTransaction() {
when:
def response = testService.loadTransaction() // Creates transaction row
then:
assert response == [code: 'OK']
assert Transaction.count() == 1
}
The domain structure was defined by another party, and I can't change it in any way, so the composite key is a must. Unfortunately many classes in this app use composite keys, so if I can't unit test them, a lot of my code can't be covered by unit testing. Any info to get me going in the right direction would be great.
Don't use unit tests to test persistence.
Unit tests have a GORM implementation, but it isn't backed by a database, only a ConcurrentHashMap. It's pretty good, but it should only ever be used to avoid having to mock the persistence layer when unit testing other artifact types like controllers. If you want to test persistence, use a database.
Otherwise, you'll see funky issues like this, and similar issues like false negatives or worse - false positives where a test passes that shouldn't, leaving bugs in your "well-tested" code.