diff --git a/it-support-engineer/hoco-homework-Powershell.ps1 b/it-support-engineer/hoco-homework-Powershell.ps1 new file mode 100644 index 000000000..a7dbf14b6 --- /dev/null +++ b/it-support-engineer/hoco-homework-Powershell.ps1 @@ -0,0 +1,57 @@ +$ErrorActionPreference = "Stop" +$url = 'https://foo.com/bar' +$logfile = '.\interview_data_set.gz' + + +$lineObjectArray = @() + + +foreach ($line in Get-Content -Path $logfile) { + if ($line -match ':') { + $lineArray = $line -split ' ' + if ($lineArray[3] -eq 'BBAOMACBOOKAIR2') { + $obj = New-Object PSCustomObject + $obj | Add-Member -MemberType NoteProperty -Name "deviceName" -Value $lineArray[3] + $obj | Add-Member -MemberType NoteProperty -Name "processId" -Value ($lineArray[4] -replace '\D+\[(\d+)\]', '$1').Trim(':') + $obj | Add-Member -MemberType NoteProperty -Name "processName" -Value ($lineArray[4] -replace '(\D+)\[\d+\]', '$1').Trim(':') + $obj | Add-Member -MemberType NoteProperty -Name "description" -Value ($lineArray[5 .. ($lineArray.length-1)] -join ' ') + $obj | Add-Member -MemberType NoteProperty -Name "hour" -Value ($lineArray[2] -split ':')[0] + + $lineObjectArray += $obj + } + } +} + + +$groupCounter = @{} + + +$lineObjectArray | ForEach-Object { + $key = $_.deviceName + $_.processId + $_.processName + $_.description + $_.hour + if ($groupCounter.Contains($key)) { + $groupCounter[$key].numberOfOccurrence++ + } else { + $timeWindow = '{0:D2}:00 - {1:D2}:00' -f $_.hour, (([int]$_.hour + 1) % 24) + $obj = New-Object PSCustomObject + $obj | Add-Member -MemberType NoteProperty -Name "deviceName" -Value $_.deviceName + $obj | Add-Member -MemberType NoteProperty -Name "processId" -Value $_.processId + $obj | Add-Member -MemberType NoteProperty -Name "processName" -Value $_.processName + $obj | Add-Member -MemberType NoteProperty -Name "description" -Value $_.description + $obj | Add-Member -MemberType NoteProperty -Name "timeWindow" -Value $timeWindow + $obj | Add-Member -MemberType NoteProperty -Name "numberOfOccurrence" -Value 1 + + $groupCounter[$key] = $obj + } +} + + +$sortedOutput = $groupCounter.Values | Sort-Object -Property timeWindow +$jsonPayload = $sortedOutput | ConvertTo-Json + + +Write-Output $jsonPayload + +$jsonPayload | Set-Content -Path ".\output-2.json" + + +Invoke-RestMethod -Uri $url -TimeoutSec 5 -Method Post -InFile ".\output-2.json" diff --git a/it-support-engineer/hoco-homework-python.py b/it-support-engineer/hoco-homework-python.py new file mode 100644 index 000000000..d7a4f8729 --- /dev/null +++ b/it-support-engineer/hoco-homework-python.py @@ -0,0 +1,45 @@ +import collections +import json +import requests +import re + +url = 'https://foo.com/bar' +logfile = '.\interview_data_set.gz' +lineObjectArray = [] + +for line in open(logfile): + lineArray = line.strip('\n').split(' ') + if lineArray[3] == 'BBAOMACBOOKAIR2': + lineObjectArray.append({ + "deviceName": lineArray[3], + "processId": re.search('\[(\d+)]', line).group(0)[1:-1], + "processName": ' '.join(lineArray[4:6]).replace('(\D+)\[\d+\]', '$1').strip(':'), + "description": ' '.join(lineArray[6:]), + "hour": lineArray[2].split(':')[0] + }) + +groupCounter = {} + +for obj in lineObjectArray: + key = obj["deviceName"] + obj["processId"] + obj["processName"] + obj["description"] + obj["hour"] + if key in groupCounter: + numberOfOccurrence = groupCounter[key]["numberOfOccurrence"] + numberOfOccurrence = numberOfOccurrence + 1 + groupCounter[key]["numberOfOccurrence"] = numberOfOccurrence + else: + groupCounter[key] = collections.OrderedDict( + deviceName=obj["deviceName"], + processId=obj["processId"], + processName=obj["processName"], + description=obj["description"], + timeWindow='{0}:00 - {1}:00'.format(obj["hour"], str((int(obj["hour"]) + 1) % 24).rjust(2, '0')), + numberOfOccurrence=1 + ) + + +row_list = list(item[1] for item in groupCounter.items()) +sorted(row_list, key=lambda item: item['timeWindow']) + +with open('py-payload-v2.json', 'wt') as file: + file.write(json.dumps(row_list)) +x = requests.post(url, json=row_list, timeout=5) \ No newline at end of file