Skip to content

Instantly share code, notes, and snippets.

@smarigowda
Last active January 23, 2017 15:01
Show Gist options
  • Save smarigowda/f0ba51e78564a70cd588 to your computer and use it in GitHub Desktop.
Save smarigowda/f0ba51e78564a70cd588 to your computer and use it in GitHub Desktop.
SOASTA JavaScript Snippets
// ------------------------------------------------------------------------------------------------------
// find previous message
// ------------------------------------------------------------------------------------------------------
function findPriorMessage() {
var priorMessage = $context.currentItem.previousItem;
for (;;) {
if ( priorMessage == null ) {
throw "No Prior Message";
}
if ( priorMessage.type == "Message") {
break;
}
priorMessage = priorMessage.previousItem;
}
return priorMessage;
}
// Create property on the fly
var mylib = $context.currentClip;
// at least one property should be created on the clip manually
// else it throws error
var plist = $context.currentClip.propertyList;
// Setup properties
if ( plist.propertyNames.indexOf("XDATE") != -1 ) {
// property exists, do nothing
} else {
plist.createProperty("XDATE"); // create the property
}
// adding javascript functions into current clip context
function getSig() { // }
var addMethods = $context.currentClip;
addMethods.getSig = getSig;
// ------------------------------------------------------------------------------------------------------
// Set a Cookie
// ------------------------------------------------------------------------------------------------------
// Getting all the clip Targets
var targets = $context.currentClip.targets;
// Proceed if some targets exist in the clip
if (targets != null)
{
// Loop through all targets
for (var i=0; i<targets.length; i++) {
// Extract HostName
var hostName = targets[i].systemPropertyList.getPropertyValue("HostName");
var vURL = targets[i].systemPropertyList.getPropertyValue("URL");
if (hostName == "10.65.87.101") {
$context.result.postMessage($context.result.LEVEL_INFO, "Host Name: " + hostName);
$context.result.postMessage($context.result.LEVEL_INFO, "URL: " + vURL);
var list = targets[i].cookies;
if (list == null)
list = new Array();
var cookie = new Object();
cookie.name = "SSO";
cookie.domain = "10.65.87.101";
cookie.path = "/";
cookie.value = "cookie-value";
cookie.expirationDate = new Date("05 Aug 2030 00:00:00 GMT");
cookie.secure = false;
list[list.length] = cookie;
$context.currentClip.targets[i].cookies = list;
}
}
}
------------------------------------
Create composition level properties
------------------------------------
var pList = $context.composition.propertyList; // Get the prop list on composition
var pListTrack = $context.currentTrack.propertyList; // get the prop list on current track
var pIndex = -1; // Initialize index (-1 means not found)
// If there is a property list - search for prop name
if(!!pList.propertyNames) {
// Update pIndex (will update to -1 if not found)
pIndex = pList.propertyNames.indexOf("MyPropertyName");
}
// If index is -1, create property
if(pIndex == -1) {
$context.result.postMessage($context.result.LEVEL_INFO, "Creating Property");
pList.createProperty("MyPropertyName");
}
----
// dynamic ramp using script
var adjustments = new Array();
var adjustment = new Object();
adjustment.type = "rampto";
adjustment.trackName = "Track 2";
adjustment.distributionName = "Distribution 1";
adjustment.userCount = 10 * numLoops ;
adjustment.time = 60000 ; // 60 seconds
adjustments[0] = adjustment;
$context.composition.rampAdjust(null, adjustments) ;
------------------------------------------------------
rename transaction of next item
-----------------------------------------------------
// if there is a property by name TRANS_PREFIX on the track then append it to the transaction name
var pIndex = -1; // Initialize index (-1 means not found)
var pList = $context.currentTrack.propertyList; // get the prop list on current track
if(!!pList.propertyNames) {
// pIndex will update to -1 if not found)
pIndex = pList.propertyNames.indexOf("TRANS_PREFIX");
}
if ( pIndex != -1) {
var tname = $context.currentItem.nextItem.name
var tranprefix = $prop.value("track", "TRANS_PREFIX");
$context.currentItem.nextItem.name = tname + '_' + tranprefix;
}
// -----------------------------------------------------------------
// You can override with a list and 1 target. I've done it by percentages before, using something like this:
var number = $context.currentTrack.playNumber;
var vunum = parseInt($sysprop.value("Track","VUNumber"))+1;
number = (vunum+number) % 100;
if(number > 98) {
var newHostName = "store.website.com";
} else if(number > 96) {
var newHostName = "shop.website.com";
} else if(number > 91) {
var newHostName = "ship.website.com";
} else if(number > 80) {
var newHostName = "www.website.com";
} else if(number > 67) {
var newHostName = "sports.website.com";
} else if(number > 53) {
var newHostName = "school.website.com";
} else if(number > 35) {
var newHostName = "www2.website.com";
} else {
var newHostName = "save.website.com";
}
var clipTargets = $context.currentClip.targets;
for(var i = 0 ; i < clipTargets.length ; i++) {
clipTargets[i].systemPropertyList.setPropertyValue("HostName",newHostName);
clipTargets[i].systemPropertyList.setPropertyValue("HttpHostOverride",newHostName);
}
// -----------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment