A Smarter Counter That Uses the Cloud

Cloud Cached counter values are nice if tracking error counts between Operation executions — to limit possible alert storms.
There are other use cases that might exist so I offer this interesting design of a cloud-based counter.
We can use the existing Counter script in Data Validation and On-Failure Operations to increment and check for thresholds but use a Numerator script to initialize and set the counter and cloud value as needed.
Recall some significance to the first and last call to the Counter Script. The first call initialized the counter and the last call used would return the current counter value.
For example, I initialize and increment with the same counter script:
RunScript("../Handlers/Counter","myCounter"); //initalize $myCounter to 0 (return "") RunScript("../Handlers/Counter","myCounter",2); //incrment by 1 (to 1) and look for threshold of 2 (return 0) // next increment will trigger threshold alert on value of 2 RunScript("../Handlers/Counter","myCounter",2); //increment by 1 (to 2) and return 1 last=RunScript("../Handlers/Counter","myCounter"); //print last counter value (returns 2 but counter is now 3)
Rather than use the same Counter script for initialization and final value, we use a dedicated “Numerator” Script to read and write to the Cloud at these important times. Now when using a “myCounter” value in the cloud..
RunScript("../Handlers/Event",7,"Initial Counter value", RunScript("../Utilities/Numerator","myCounter",true,true) //initalize cloud's $myCounter to 0 ); RunScript("…/Handlers/Counter","myCounter",2); //incrment by 1 (to 1) and look for threshold of 2 (return 0) RunScript("…/Handlers/Counter","myCounter",2); //increment by 1 (to 2) and see the alert due to new value of 2 (return 1) RunScript("../Handlers/Event",7,"Final Counter value", RunScript("../Utilities/Numerator","myCounter",false,true) //print last counter value (return 2) );
Then, in a second run of the same operation…
RunScript("../Handlers/Event",7,"Initial Counter value", RunScript("../Utilities/Numerator","myCounter",false,true) //get cloud's $mycounter (issue warning, returns 2) ); RunScript("../Handlers/Counter","myCounter",2); //incrment by 1 (to 3) and look for threshold of 2 (return 2) RunScript("../Handlers/Counter","myCounter",2); //increment by 1 (to 4) (return 3) RunScript("../Handlers/Event",7,"Final Counter value", RunScript("../Utilities/Numerator","myCounter",false,true) //print last counter value: 4 );
Numerator Script
_counter_name = IfEmpty(_1,RaiseError("Counter script requires a counter name")); _reset = Bool(IfEmpty(_2,false)); _cloud = Bool(IfEmpty(_3,false)); _pretty = Bool(IfEmpty(_4,true)); If(IsNull(Get(_counter_name)) || _reset, // initialize the counter If(_cloud, If(_reset, value=RunScript("<TAG>Scripts/Utilities/Set Cloud Cache</TAG>", $jitterbit.operation.project_name+'!'+_counter_name,0); RunScript("<TAG>Scripts/Handlers/Event</TAG>",6,"NOTICE", "Cloud cached value of "+_counter_name+ " has been reset to 0"); InitCounter(_counter_name,value); , value = InitCounter(_counter_name, IfEmpty(RunScript("<TAG>Scripts/Utilities/Get Cloud Cache</TAG>", $jitterbit.operation.project_name+'!'+_counter_name),0)); ); , value = InitCounter(_counter_name,0); ); , value = Get(_counter_name); If(_cloud, // return current counter value with embedded commas but don't _increment the counter RunScript("<TAG>Scripts/Utilities/Set Cloud Cache</TAG>", $jitterbit.operation.project_name+'!'+_counter_name, value); ); ); // return current counter value (with commas if requested) If(_pretty,RegExReplace(value,"(\d)(?=(\d{3})+$)","\\1,"),value);
Note the use of InitCounter() for thread-safe counting.
No Comments