Compile function System_Healthcheck_Services(@Service, @Params)
#pragma precomp SRP_PreCompiler
$insert LOGICAL
$Insert ENGINE_HEALTH_EQUATES

Declare Subroutine Error_Services, SRP_Json
Declare function System_Healthcheck_Services, Error_Services, SRP_Datetime, SRP_Json

GoToService

Return Response or ""

//-----------------------------------------------------------------------------
// SERVICES
//-----------------------------------------------------------------------------

Service GetEngineLastRunDTM(EngineID)
    If EngineID NE '' then
    	If RowExists('APP_INFO', EngineID) then
    		Open "APP_INFO" To AITable then
    			LastRunDTM = ''
    			ReadV LastRunDTM From AITable, EngineID, 1 Then
    				If LastRunDTM NE '' then
						Response = LastRunDTM
					end else
						Error_Services('Add', 'Error in System_Healthcheck_Services -> GetEngineLastRunDTM: Last Run Time not found')
					end
    			end else
    				Error_Services('Add', 'Error in System_Healthcheck_Services -> GetEngineLastRunDTM: Error reading specified engine record')
    			end
    		end else
    			Error_Services('Add', 'Error in System_Healthcheck_Services -> GetEngineLastRunDTM: Unable to open APP_INFO table')
    		end
    	end else
    		Error_Services('Add', 'Error in System_Healthcheck_Services -> GetEngineLastRunDTM: No engine record found')
    	end
    end else
    	Error_Services('Add', 'Error in System_Healthcheck_Services -> GetEngineLastRunDTM: No Engine ID entered')
    end
End Service

Service GetEngineHealthInfo(EngineID)
	EngineHealthInfo = ''
	If EngineID NE '' then
    	If RowExists('APP_INFO', EngineID) then
    		LastRunDTM = System_HealthCheck_Services('GetEngineLastRunDTM', EngineID)
    		If Error_Services('NoError') then
    			CurrDTM = SRP_Datetime('Now')
    			EngineHealthThreshold = XLATE('APP_INFO', 'ENGINE_HEALTH_THRESHOLD_MINUTES', 1, 'X')
    			MinutesSinceLastRun = SRP_Datetime('MinuteSpan', LastRunDTM, CurrDTM)
    			Healthy = MinutesSinceLastRun LT EngineHealthThreshold
    			EngineHealthInfo<ENGINE_HEALTH_ENGINE_ID$> = EngineID
    			EngineHealthInfo<ENGINE_HEALTH_LAST_RUN_DTM$> = LastRunDTM
    			EngineHealthInfo<ENGINE_HEALTH_MINUTES_SINCE_LAST_RUN$> = MinutesSinceLastRun
    			EngineHealthInfo<ENGINE_HEALTH_HEALTH_STATUS$> = Healthy
    			Response = EngineHealthInfo
    		end else
    			Error_Services('Add', Error_Services('GetMessage'))
    		end
    	end else
    		Error_Services('Add', 'Error in System_Healthcheck_Services -> GetEngineHealth: No engine record found')
    	end
    end else
    	Error_Services('Add', 'Error in System_Healthcheck_Services -> GetEngineHealth: No Engine ID entered')
    end
end service

Service ConvertEngineHealthInfoToJSON(EngineHealthInfo, ItemURL)
	objJSON = ''
	If SRP_JSON(objJSON, 'New', 'Object') then
		objEngine = ''
		If SRP_JSON(objEngine, 'New', 'Object') then
			SRP_JSON(objEngine, 'SetValue', 'EngineID', EngineHealthInfo<ENGINE_HEALTH_ENGINE_ID$>)
			SRP_JSON(objEngine, 'SetValue', 'LastRunDTM', EngineHealthInfo<ENGINE_HEALTH_LAST_RUN_DTM$>)
			SRP_JSON(objEngine, 'SetValue', 'MinutesSinceLastRun', EngineHealthInfo<ENGINE_HEALTH_MINUTES_SINCE_LAST_RUN$>)
			SRP_JSON(objEngine, 'SetValue', 'Healthy', EngineHealthInfo<ENGINE_HEALTH_HEALTH_STATUS$>)
			SRP_JSON(objJSON, 'Set', 'Engine', objEngine)
			SRP_JSON(objEngine, 'Release')
		end
		
		If itemURL NE '' then
			objLinks = ''
			If SRP_JSON(objLinks, 'New', 'Object') then
				objLink = ''
				If SRP_JSON(objLink, 'New', 'Object') then
					SRP_JSON(objLink, 'SetValue', 'href', ItemURL, 'String')
					SRP_JSON(objLink, 'SetValue', 'title', 'Self', 'String')
					SRP_JSON(objLinks, 'Set', 'self', objLink)
					SRP_JSON(objLink, 'Release')
				end
				SRP_JSON(objJSON, 'Set', '_links', objLinks)
				SRP_JSON(objLinks, 'Release')
			end
			SRP_JSON(objJSON, 'SetValue', '_class', 'resource')
		end  
		JSON = SRP_JSON(objJSON, 'Stringify', 'Styled')
		SRP_JSON(objJSON, 'Release')
	end else
		Error_Services('Add', 'Error creating JSON Object')
	end
	Response = JSON
end service




