Name

sn_itom_pattern.EventsTimestampHandler

Description

A script which support data handling on the sa_hash table storing the last event s run per account.

Script

var EventsTimestampHandler = Class.create();
EventsTimestampHandler.prototype = {
  	EVENT_NAME_PRE:"events.timestamp.",
  initialize: function() {
  },
  
  _runGlideQuery : function(accountID)
  {
  	var saHash = new GlideRecord('sa_hash');
  	saHash.addQuery('name', this.EVENT_NAME_PRE +accountID);
  	saHash.query();
  	return saHash;
  },
  
  getReducedTime : function()
  {
  	var currentDate = new Date();
  	var currentTime = currentDate.getTime();
  	var newTime = currentTime - 5*60*1000; // we want to go back in time 5 minuets
  	currentDate.setTime(newTime);
  	return currentDate.toISOString();
  },
  
  /***
   * Get the timestamp of last events run per account
   * In case no record exist for this account - return -1
   */
  getLatestTimeStampForAccount : function(accountID){
  	saHash = this._runGlideQuery(accountID);
  	var rtrn = -1;
  	if(saHash.next()){
  		rtrn = saHash.hash;
  	}
  	else
  	{
  		rtrn = this.getReducedTime();
  	}
  	return rtrn;
  },

  /***
   * Update the timestamp of last events run per account
   * In case no record exist for this account - create one
   */
  updateSaTableForAccountWithLatestEvent : function(accountID, latestTS){
  	saHash = this._runGlideQuery(accountID);
  	if(!saHash.next()){
  		saHash.name = this.EVENT_NAME_PRE + accountID;
  	}
  	saHash.hash = latestTS;
  	saHash.update();
  },
  
  type: 'EventsTimestampHandler'
};

Sys ID

0e0f0d641b9977807637a821ec4bcb8e

Offical Documentation

Official Docs: