Compile subroutine MONA_SERVICES(@Service, @Params)
/***************************************************************
This subroutine is used to send messages to MonA directly from
within OpenInsight. It utilizes a C# DotNet DLL MonAOIApi.dll
which contains two classes, one for sending status updates, and
one for sending metrics. Also requires the Infineon.Monitoring.Mona.dll which contains the needed dotnet libraries.

Parameters:
Instruction: Used to determine which subroutine to execute.
Parm1:
    -For Status updates represents the name of the monitor
    -For Metrics updates represents the name of the monitor
Parm2:
    -For Status updates represents the name of the status
    -For Metric updates represents the name of the metric
Parm3:
    -For status updates this represents the state of the object you are sending(Ok, Warning, Critical)
    -For Metric updates this represents the numeric data that you wish to send.
***************************************************************/

Declare Subroutine Errmsg, Error_Services, Delay, Mona_Services
Declare function Get_Status, SRP_Datetime, Environment_Services, Httpclient_Services
#pragma precomp SRP_PreCompiler

$Insert LOGICAL
$Insert SERVICE_SETUP
$Insert REVDOTNETEQUATES

GoToService else
	Error_Services('Add', Service : ' is not a valid service request within the ' : ServiceModule : ' services module.')
end

return

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Service Parameter Options
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Options MONITORS     = 'SQL_BACKLOG', 'SQL_BACKLOG_WO_MAT', 'SQL_BACKLOG_WO_LOG'
Options STATES       = 'OK', 'WARNING', 'CRITICAL'

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Services
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Service SendStatus(MonitorName=MONITORS, StatusName, CurrentState=STATES)
	
	Mona_Services("SendBufferedStatus", MonitorName, StatusName, CurrentState)
	
end service


Service SendMetric(MonitorName=MONITORS, MetricName, Number)
	
	Mona_Services("SendBufferedAverageMetric", MonitorName, MetricName, Number)
	
end Service


Service SendBufferedStatus(MonaResource, StatusName, StatusValue)
	
	If MonaResource EQ '' then
		MonaResource = Environment_Services("GetMonaResource")
	end
	
	Prod = Environment_Services('IsProd')
	If Not(Prod) then GoSub SwapResourceNames
	
	DateTime = SRP_DateTime('Now')
	Year     = SRP_Datetime("Year", DateTime)
	Month    = SRP_Datetime("Month", DateTime)
	If Len(Month) EQ 1 then
		Month = '0':Month
	end
	Day = SRP_Datetime("Day", DateTime)
	If Len(Day) EQ 1 then
		Day = '0':Day
	end
	Hour = SRP_Datetime("Hour", DateTime)
	If Len(Hour) EQ 1 then
		Hour = '0':Hour
	end
	Minute = SRP_Datetime("Minute", DateTime)
	If Len(Minute) EQ 1 then
		Minute = '0':Minute
	end
	Second = SRP_Datetime("Second", DateTime)
	If Len(Second) EQ 1 then
		Second = '0':Second
	end
	
	RequestBodyJson = '{ "resource": "':MonaResource:'"'
	RequestBodyJson = RequestBodyJson:', "dateTime": "':Year:'-':Month:'-':Day:'T':Hour:':':Minute:':':Second:'Z"'
	RequestBodyJson = RequestBodyJson:', "statusName": "':StatusName:'"'
	RequestBodyJson = RequestBodyJson:', "statusValue": "':StatusValue:'" }'
	
	ApiUrl = Environment_Services("GetMonInBufferedWorkerApiUrl"):'/status'
	
	retries = 3
	backoffSeconds = 1
	isSuccessful = False$
	
	Loop
		while (isSuccessful EQ False$ and retries GT 0)
			waitSeconds = (3 - retries) * backoffSeconds
			Delay(waitSeconds)
			
			retries = retries - 1
			
			response = Httpclient_Services('SendHTTPRequest', 'POST', ApiUrl, 'Content-Type':@VM:'application/json':@FM:'Accept':@VM:'*/*', RequestBodyJson, '', '', False$, False$, '')

			If response EQ '"Request queued for processing"' then
				isSuccessful = True$
			end
	Repeat
	
end service


Service SendBufferedAverageMetric(MonaResource, MetricName, MetricValue)
	
	If MonaResource EQ '' then
		MonaResource = Environment_Services("GetMonaResource")
	end
	
	DateTime = SRP_DateTime('Now')
	Year = SRP_Datetime("Year", DateTime)
	Month = SRP_Datetime("Month", DateTime)
	If Len(Month) EQ 1 then
		Month = '0':Month
	end
	Day = SRP_Datetime("Day", DateTime)
	If Len(Day) EQ 1 then
		Day = '0':Day
	end
	Hour = SRP_Datetime("Hour", DateTime)
	If Len(Hour) EQ 1 then
		Hour = '0':Hour
	end
	Minute = SRP_Datetime("Minute", DateTime)
	If Len(Minute) EQ 1 then
		Minute = '0':Minute
	end
	Second = SRP_Datetime("Second", DateTime)
	If Len(Second) EQ 1 then
		Second = '0':Second
	end
	
	RequestBodyJson = '{ "resource": "':MonaResource:'"'
	RequestBodyJson = RequestBodyJson:', "dateTime": "':Year:'-':Month:'-':Day:'T':Hour:':':Minute:':':Second:'Z"'
	RequestBodyJson = RequestBodyJson:', "metricName": "':MetricName:'"'
	RequestBodyJson = RequestBodyJson:', "metricValue": "':MetricValue:'" }'
	
	ApiUrl = Environment_Services("GetMonInBufferedWorkerApiUrl"):'/average'
	
	retries = 3
	backoffSeconds = 1
	isSuccessful = False$
	
	Loop
		while (isSuccessful EQ False$ and retries GT 0)
			waitSeconds = (3 - retries) * backoffSeconds
			Delay(waitSeconds)
			
			retries = retries - 1

			response = Httpclient_Services('SendHTTPRequest', 'POST', ApiUrl, 'Content-Type':@VM:'application/json':@FM:'Accept':@VM:'*/*', RequestBodyJson, '', '', False$, False$, '')

			If response EQ '"Request queued for processing"' then
				isSuccessful = True$
			end
	Repeat
	
end service


SwapResourceNames:
	
	// Swap out prod resource name with dev resource name
	Begin Case
		Case ( (MonaResource EQ 'GRP_OPENINSIGHT_MES_OP_FE_SAP_OUTBOUND') and (StatusName EQ 'SFTP-Interface-Outbound') )
			MonaResource = 'GRP_OPENINSIGHT_MES_OP_FE_DEV_SAP_OUTBOUND'
			StatusName   = 'SFTP-Interface-Outbound-Dev'
			
	    Case ( (MonaResource EQ 'GRP_OPENINSIGHT_MES_OP_FE_SAP_OUTBOUND') and (StatusName EQ 'SetSAPBatchQueue') )
			MonaResource = 'GRP_OPENINSIGHT_MES_OP_FE_DEV_SAP_OUTBOUND'
			StatusName   = 'SetSAPBatchQueue-Dev'
			
		Case ( (MonaResource EQ 'GRP_OPENINSIGHT_MES_OP_FE_SAP_INBOUND') and (StatusName EQ 'SFTP-Interface-Inbound') )
			MonaResource = 'GRP_OPENINSIGHT_MES_OP_FE_DEV_SAP_INBOUND'
			StatusName   = 'SFTP-Interface-Inbound-Dev'
	End Case
	
return
