Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hoco's answer for it-support-engineer #1990

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions it-support-engineer/hoco-homework-Powershell.ps1
Original file line number Diff line number Diff line change
@@ -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"
45 changes: 45 additions & 0 deletions it-support-engineer/hoco-homework-python.py
Original file line number Diff line number Diff line change
@@ -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)