#include <Array.au3>
#include <File.au3>
#include <Excel.au3>
$sPath = @ScriptDir
$sTotalName = "Итог.xlsx"
Local $iN = 0, $iP = 0
Dim $aFirstReport[2][8] = [['SLA' & @CRLF & '(30*24*60)', 'Точка', 'Время недоступности (мин)', _
'Время перегрузки', '% недоступность ', '% перегрузка', '% суммарная недоступность', '% суммарная доступность'], [43200]]
Dim $aSecondReport[2][6] = [['№', 'Начало', 'Конец', 'Продолжительность (мин)', 'Узел', 'Тип']]
$iCount = 1
$iTotalTime = 0
$aList = _FileListToArray($sPath, '*.csv', 1)
For $i = UBound($aList) -1 To 0 Step -1
If Not StringRegExp($aList[$i], '[P|N]\.csv') Then
_ArrayDelete($aList, $i)
EndIf
Next
$oExcel = _Excel_Open()
For $iFile = 0 To UBound($aList) -1
$oBook = _Excel_BookOpen($oExcel, $sPath & "\" & $aList[$iFile])
$aRange = _Excel_RangeRead($oBook)
$iTime = 0
$sType = StringRegExp($aList[$iFile], '(.)\.', 3)[0]
For $iRow = 1 To UBound($aRange) -1 Step 2
ReDim $aSecondReport[$iCount + 1][6]
If $aRange[$iRow] = "" Then ContinueLoop
$aCurrent = StringSplit($aRange[$iRow], ',', 3)
$aNext = StringSplit($aRange[$iRow+1], ',', 3)
$iTime += _ToSecond($aNext[4])
$aSecondReport[$iCount][0] = $iCount
$aSecondReport[$iCount][1] = $aNext[0]
$aSecondReport[$iCount][2] = $aCurrent[0]
$aSecondReport[$iCount][3] = _SecondToMinutes(_ToSecond($aNext[4]))
$aSecondReport[$iCount][4] = StringLeft($aList[$iFile], StringInStr($aList[$iFile], '-', 0, -1) -1)
If $sType = 'N' Then
$aSecondReport[$iCount][5] = 'Недоступность'
Else
$aSecondReport[$iCount][5] = 'Перегрузка'
EndIf
$iCount += 1
Next
$aFirstReport[$iFile + 1][1] = StringLeft($aList[$iFile], StringInStr($aList[$iFile], '-', 0, -1) -1)
If $sType = 'N' Then
$aFirstReport[$iFile + 1][2] = _SecondToMinutes($iTime)
$aFirstReport[$iFile + 1][3] = "0"
Else
$aFirstReport[$iFile + 1][2] = "0"
$aFirstReport[$iFile + 1][3] = _SecondToMinutes($iTime)
EndIf
$iN += StringReplace($aFirstReport[$iFile + 1][2], ',', '.')
$iP += StringReplace($aFirstReport[$iFile + 1][2], ',', '.')
ReDim $aFirstReport[UBound($aFirstReport) + 1][8]
_Excel_BookClose($oBook)
Next
For $i = 1 To UBound($aFirstReport) -1
$aFirstReport[$i][6] = Round(StringReplace($aFirstReport[$i][2], ',', '.') / $iN * 100, 2) & '%'
$aFirstReport[$i][7] = Round(StringReplace($aFirstReport[$i][3], ',', '.') / $iP * 100, 2) & '%'
Next
$oBook = _Excel_BookOpen($oExcel, $sPath & "\" & $sTotalName)
If @error Then $oBook = _Excel_BookNew($oExcel)
$oExcel.Columns('A:L').EntireColumn.Delete
_Excel_RangeWrite($oBook, Default, $aFirstReport, "D6")
_Excel_RangeWrite($oBook, Default, $aSecondReport, "D" & (6 + UBound($aSecondReport)))
$oExcel.Columns('A:L').EntireColumn.AutoFit
_Excel_BookSaveAs($oBook, $sPath & "\" & $sTotalName)
Func _SecondToMinutes($iSecond)
If $iSecond >= 60 Then
$iTmp = Floor($iSecond / 60) & ','
$iSecond = $iSecond - $iTmp * 60
Else
$iTmp = 0 & ','
EndIf
If $iSecond > 10 Then
Return $iTmp & $iSecond
Else
Return $iTmp & '0' & $iSecond
EndIf
EndFunc
Func _ToSecond($sTime)
$sTime = StringReplace($sTime, '"', '')
$aTime = StringSplit($sTime, ' ', 2)
$iValue = 0
For $iTime = 0 To UBound($aTime) -1
$sTmp = StringRight($aTime[$iTime], 1)
$aTime[$iTime] = StringTrimRight($aTime[$iTime], 1)
Switch $sTmp
Case 'd'
$iValue = $iValue + $aTime[$iTime] * 86400
Case 'h'
$iValue = $iValue + $aTime[$iTime] * 3600
Case 'm'
$iValue = $iValue + $aTime[$iTime] * 60
Case 's'
$iValue = $iValue + $aTime[$iTime]
EndSwitch
Next
Return $iValue
EndFunc