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

EQU ReportPeriodStartDtm$       To 1
EQU ReportPeriodEndDtm$         To 2
EQU ReportAllUpTimePercent$     To 3
EQU ReportAllUpTimeMinutes$     To 4
EQU ReportAllAvailMinutes$      To 5
EQU ReportASMUpTimePercent$     To 6
EQU ReportASMUpTimeMinutes$     To 7
EQU ReportASMAvailMinutes$      To 8
EQU ReportHTRUpTimePercent$     To 9
EQU ReportHTRUpTimeMinutes$     To 10
EQU ReportHTRAvailMinutes$      To 11
EQU ReportEPPUpTimePercent$     To 12
EQU ReportEPPUpTimeMinutes$     To 13
EQU ReportEPPAvailMinutes$      To 14

Declare function SRP_Datetime, Database_Services, SRP_Math, SRP_Json, Date_Services
Declare subroutine SRP_Json

GoToService

Return Response or ""

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Service Parameter Options
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Options BOOLEAN     = True$, False$
Options RESPONSE_OUTPUT = 'CSV', 'JSON', 'INTERNAL'

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

Service GetReactorClassUpTimeMetricsByTimePeriod(StartDtm, EndDtm, ResponseType=RESPONSE_OUTPUT)
    
    MetricsData = ''
    StartDate = SRP_Datetime('Date', StartDtm)
    EndDate = SRP_Datetime('Date', EndDtm)
    AllReportMinutes = 0
    AllUptimeMinutes = 0
    AllUptimePercentage = 0
    ASMReportMinutes = 0
    ASMUptimeMinutes = 0
    ASMUptimePercentage = 0
    HTRReportMinutes = 0
    HTRUptimeMinutes = 0
    HTRUptimePercentage = 0
    EPPReportMinutes = 0
    EPPUptimeMinutes = 0
    EPPUptimePercentage = 0
    
    for ThisDate = StartDate to EndDate
        ASMRecordId = 'ASM*':ThisDate
        HTRRecordId = 'HTR*':ThisDate
        EPPRecordId = 'EPP*':ThisDate
        AllRecordId = 'ALL*':ThisDate
        
        ASMRecord = Database_Services('ReadDataRow', 'REACTOR_DAILY_UPTIME', ASMRecordId, True$, 0, False$)
        ThisASMReportMinutes = ASMRecord<REACTOR_DAILY_UPTIME_TOTAL_AVAIL_MINUTES$>
        ThisASMUptimeMinutes = ASMRecord<REACTOR_DAILY_UPTIME_TOTAL_UPTIME_MINUTES$>
        ASMReportMinutes += ThisASMReportMinutes
        ASMUptimeMinutes += ThisASMUptimeMinutes
        
        HTRRecord = Database_Services('ReadDataRow', 'REACTOR_DAILY_UPTIME', HTRRecordId, True$, 0, False$)
        ThisHTRReportMinutes = HTRRecord<REACTOR_DAILY_UPTIME_TOTAL_AVAIL_MINUTES$>
        ThisHTRUptimeMinutes = HTRRecord<REACTOR_DAILY_UPTIME_TOTAL_UPTIME_MINUTES$>
        HTRReportMinutes += ThisHTRReportMinutes
        HTRUptimeMinutes += ThisHTRUptimeMinutes
        
        EPPRecord = Database_Services('ReadDataRow', 'REACTOR_DAILY_UPTIME', EPPRecordId, True$, 0, False$)
        ThisEPPReportMinutes = EPPRecord<REACTOR_DAILY_UPTIME_TOTAL_AVAIL_MINUTES$>
        ThisEPPUptimeMinutes = EPPRecord<REACTOR_DAILY_UPTIME_TOTAL_UPTIME_MINUTES$>
        EPPReportMinutes += ThisEPPReportMinutes
        EPPUptimeMinutes += ThisEPPUptimeMinutes
        
        AllRecord = Database_Services('ReadDataRow', 'REACTOR_DAILY_UPTIME', AllRecordId, True$, 0, False$)
        ThisALLReportMinutes = AllRecord<REACTOR_DAILY_UPTIME_TOTAL_AVAIL_MINUTES$>
        ThisALLUptimeMinutes = AllRecord<REACTOR_DAILY_UPTIME_TOTAL_UPTIME_MINUTES$>
        AllReportMinutes += ThisAllReportMinutes
        AllUptimeMinutes += ThisAllUptimeMinutes
    Next ThisDate
    
    if AllReportMinutes > 0 then AllUptimePercentage = SRP_Math('ROUND', AllUptimeMinutes, 0) / SRP_Math('ROUND', AllReportMinutes, 0)

    if ASMReportMinutes > 0 then ASMUptimePercentage = SRP_Math('ROUND', ASMUptimeMinutes, 0) / SRP_Math('ROUND', ASMReportMinutes, 0)

    if HTRReportMinutes > 0 then HTRUptimePercentage = SRP_Math('ROUND', HTRUptimeMinutes, 0) / SRP_Math('ROUND', HTRReportMinutes, 0)
        
    if EPPReportMinutes > 0 then EPPUptimePercentage = SRP_Math('ROUND', EPPUptimeMinutes, 0) / SRP_Math('ROUND', EPPReportMinutes, 0)
        
    Begin Case
        Case ResponseType = 'JSON'
            objJson = ''
            If SRP_Json(objJson, 'New') then
                StartDtmString = Date_Services('ConvertDateTimeToISO8601', StartDtm)
                StopDtmString = Date_Services('ConvertDateTimeToISO8601', EndDtm)
                SRP_Json(objJson, "SetValue", "PeriodStartDtm", StartDtmString)
                SRP_Json(objJson, "SetValue", "PeriodEndDtm", StopDtmString)
                SRP_Json(objJson, "SetValue", "AllUptimePercentage", AllUptimePercentage)
                SRP_Json(objJson, "SetValue", "AllUptimeMinutes", AllUptimeMinutes)
                SRP_Json(objJson, "SetValue", "AllAvailMinutes", AllReportMinutes)
                SRP_Json(objJson, "SetValue", "ASMUptimePercentage", ASMUptimePercentage)
                SRP_Json(objJson, "SetValue", "ASMUptimeMinutes", ASMUptimeMinutes)
                SRP_Json(objJson, "SetValue", "ASMAvailMinutes", ASMReportMinutes)
                SRP_Json(objJson, "SetValue", "HTRUptimePercentage", HTRUptimePercentage)
                SRP_Json(objJson, "SetValue", "HTRUptimeMinutes", HTRUptimeMinutes)
                SRP_Json(objJson, "SetValue", "HTRAvailMinutes", HTRReportMinutes)
                SRP_Json(objJson, "SetValue", "EPPUptimePercentage", EPPUptimePercentage)
                SRP_Json(objJson, "SetValue", "EPPUptimeMinutes", EPPUptimeMinutes)
                SRP_Json(objJson, "SetValue", "EPPAvailMinutes", EPPReportMinutes)
                MetricsData = SRP_Json(objJson, "Stringify", "STYLED")
                SRP_Json(objJson, "Release")
            end
        Case Otherwise$
            MetricsData<ReportPeriodStartDtm$>      = StartDtm
            MetricsData<ReportPeriodEndDtm$>        = EndDtm
            MetricsData<ReportAllUpTimePercent$>    = AllUptimePercentage
            MetricsData<ReportAllUpTimeMinutes$>    = AllUptimeMinutes
            MetricsData<ReportAllAvailMinutes$>     = AllReportMinutes
            MetricsData<ReportASMUpTimePercent$>    = ASMUptimePercentage
            MetricsData<ReportASMUpTimeMinutes$>    = ASMUptimeMinutes
            MetricsData<ReportASMAvailMinutes$>     = ASMReportMinutes
            MetricsData<ReportHTRUpTimePercent$>    = HTRUptimePercentage
            MetricsData<ReportHTRUpTimeMinutes$>    = HTRUptimeMinutes
            MetricsData<ReportHTRAvailMinutes$>     = HTRReportMinutes
            MetricsData<ReportEPPUpTimePercent$>    = EPPUptimePercentage
            MetricsData<ReportEPPUpTimeMinutes$>    = EPPUptimeMinutes
            MetricsData<ReportEPPAvailMinutes$>     = EPPReportMinutes
    End Case
    
    Response = MetricsData

End Service

