Skip to content

Commit

Permalink
Merge pull request #21 from SumoLogic/byi-fix-issue-20
Browse files Browse the repository at this point in the history
Byi fix issue 20
  • Loading branch information
Bin Yi authored Aug 4, 2018
2 parents af30a1c + 747e249 commit 02d72af
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 141 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 1.1.3 - 8/3/2018

- Fix #20 - Get-SearchResult

## 1.1.2 - 7/31/2018

- Fix #18 - Start-SearchJob
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.1.2-{build}-{branch}
version: 1.1.3-{build}-{branch}

environment:
appveyor_test_acc_id:
Expand Down
82 changes: 49 additions & 33 deletions src/main/SumoLogic-Core/Lib/Utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,29 @@ function invokeSumoAPI([SumoAPISession]$session,
[string]$body,
$cmdlet) {

$url = $session.Endpoint + $function
if ($query) {
$qStr = getQueryString($query)
$url += "?" + $qStr
}
if ($method -ne [Microsoft.PowerShell.Commands.WebRequestMethod]::Get) {
& $cmdlet -Uri $url -Headers $headers -Method $method -WebSession $session.WebSession -Body $body -ErrorVariable err
} else {
& $cmdlet -Uri $url -Headers $headers -Method $method -WebSession $session.WebSession -ErrorVariable err
}
if ($err) {
$err | ForEach-Object { Write-Error $_ }
}
$url = $session.Endpoint + $function
if ($query) {
$qStr = getQueryString($query)
$url += "?" + $qStr
}
if ($method -ne [Microsoft.PowerShell.Commands.WebRequestMethod]::Get) {
& $cmdlet -Uri $url -Headers $headers -Method $method -WebSession $session.WebSession -Body $body -ErrorVariable err
}
else {
& $cmdlet -Uri $url -Headers $headers -Method $method -WebSession $session.WebSession -ErrorVariable err
}
if ($err) {
$err | ForEach-Object { Write-Error $_ }
}
}

function invokeSumoWebRequest([SumoAPISession]$session,
[hashtable]$headers = $Script:defaultHeaders,
[Microsoft.PowerShell.Commands.WebRequestMethod]$method,
[string]$function,
[hashtable]$query,
[string]$body) {
invokeSumoAPI $session $headers $method $function $query $body (Get-Command Invoke-WebRequest -Module Microsoft.PowerShell.Utility)
invokeSumoAPI $session $headers $method $function $query $body (Get-Command Invoke-WebRequest -Module Microsoft.PowerShell.Utility)
}

function invokeSumoRestMethod([SumoAPISession]$session,
Expand All @@ -101,7 +102,7 @@ function invokeSumoRestMethod([SumoAPISession]$session,
[string]$function,
[hashtable]$query,
[string]$body) {
invokeSumoAPI $session $headers $method $function $query $body (Get-Command Invoke-RestMethod -Module Microsoft.PowerShell.Utility)
invokeSumoAPI $session $headers $method $function $query $body (Get-Command Invoke-RestMethod -Module Microsoft.PowerShell.Utility)
}

function getCollectorsByPage([SumoAPISession]$session, [int]$offset, [int]$limit) {
Expand All @@ -111,7 +112,8 @@ function getCollectorsByPage([SumoAPISession]$session, [int]$offset, [int]$limit
}
try {
(invokeSumoRestMethod -session $Session -method Get -function "collectors" -query $query).collectors
} catch {
}
catch {
@()
}
}
Expand Down Expand Up @@ -142,7 +144,7 @@ function startSearchJob ([SumoAPISession]$session, [string]$query, [datetime]$fr
invokeSumoRestMethod -session $session -method Post -function "search/jobs" -body ($q | ConvertTo-Json)
}

function getSearchResult ([SumoAPISession]$session, [string]$id, [int]$limit, [SumoSearchResultType]$type) {
function getSearchResult ([SumoAPISession]$session, [string]$id, [int]$limit, [SumoSearchResultType]$type, [int]$page) {
$status = invokeSumoRestMethod -session $session -method Get -function "search/jobs/$id"
if ($status.state -ne "DONE GATHERING RESULTS") {
throw "Result is not ready"
Expand All @@ -158,24 +160,35 @@ function getSearchResult ([SumoAPISession]$session, [string]$id, [int]$limit, [S
if ($limit -and ($limit -lt $total)) {
$total = $limit
}
$page = 100
$offset = 0
while ($offset -le $total) {
[array]$results = @()
while ($results.Count -lt $total) {
$func = "search/jobs/{0}/{1}" -f $id, $ttype
$lowerBound = $results.Count
$upperBound = if ($results.Count + $page -lt $total) {
$results.Count + $page
}
else {
$total
}
Write-Verbose "Requesting result $lowerBound - $upperBound ..."
$res = invokeSumoRestMethod -session $session -method Get -function $func -query @{
"offset" = $offset
"limit" = $page
offset = $lowerBound
limit = ($upperBound - $lowerBound)
}
if ([SumoSearchResultType]::Record -eq $type) {
$set = $res.records
}
else {
$set = $res.messages
}
$results = $set | ForEach-Object { $_.map }
$text = "Downloaded {0} of {1} {2}" -f $offset, $total, $ttype
Write-Progress -Activity "Downloading Result" -Status $text -PercentComplete ($offset / $total * 100)
$offset += $page
$set | ForEach-Object {
$results += $_.map
}
Write-Verbose "Got $($set.Count) results (Total: $($results.Count))"
if ($total -ne 0 -and $results.Count -lt $total) {
$text = "Downloaded {0} of {1} {2}" -f $results.Count, $total, $ttype
Write-Progress -Activity "Downloading Result" -Status $text -PercentComplete ($results.Count * 100 / $total)
}
}
$results
}
Expand Down Expand Up @@ -223,14 +236,15 @@ function writeCollectorUpgradeStatus($collector, $upgrade) {
getFullName $collector | Write-Host -NoNewline -ForegroundColor White
if ($collector.alive) {
"(alive) " | Write-Host -NoNewline -ForegroundColor Green
} else {
}
else {
"(connection lost) " | Write-Host -NoNewline -ForegroundColor Red
}
if ((get-host).UI.RawUI.MaxWindowSize.Width -ge 150) {
"on $($collector.osName)($($collector.osVersion)) $($collector.collectorVersion)=>$($upgrade.toVersion); " | Write-Host -NoNewline -ForegroundColor Gray
}
"STATUS: " | Write-Host -NoNewline -ForegroundColor White
switch($upgrade.status) {
switch ($upgrade.status) {
0 {
getStatusMessage $upgrade | Write-Host -NoNewline -ForegroundColor White
}
Expand Down Expand Up @@ -267,7 +281,7 @@ function getCollectorUpgradeStatus($collector, $upgrade) {
}

function getStatusMessage($upgrade) {
switch($upgrade.status) {
switch ($upgrade.status) {
0 {
"Not started"
}
Expand All @@ -281,7 +295,7 @@ function getStatusMessage($upgrade) {
"Upgrade failed ($($upgrade.message))"
}
6 {
"Working on upgrade collector "
"Working on upgrade collector "
}
}
}
Expand Down Expand Up @@ -318,7 +332,7 @@ function waitForMultipleUpgrades([SumoAPISession]$Session, [array]$UpgradeIds, [
$spinner = "|", "/", "-", "\"
do {
$counter++
foreach($upgradeId in $UpgradeIds) {
foreach ($upgradeId in $UpgradeIds) {
if ($completed -contains $upgradeId) {
continue
}
Expand All @@ -327,10 +341,12 @@ function waitForMultipleUpgrades([SumoAPISession]$Session, [array]$UpgradeIds, [
Write-Warning "Cannot get upgrade with id $upgradeId"
$completed += $upgradeId
$na++
} elseif ($upgrade.status -eq 2) {
}
elseif ($upgrade.status -eq 2) {
$completed += $upgradeId
$succeed++
} elseif ($upgrade.status -eq 3) {
}
elseif ($upgrade.status -eq 3) {
$completed += $upgradeId
$failed++
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/SumoLogic-Core/SearchJob/Get-SearchResult.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ function Get-SearchResult {
[int]$Limit
)

getSearchResult $Session $Id $Limit $Type
$page = 100

getSearchResult -session $Session -id $Id -limit $Limit -type $Type -page $page

}
2 changes: 1 addition & 1 deletion src/main/SumoLogic-Core/SumoLogic-Core.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'SumoLogic-Core.psm1'

# Version number of this module.
ModuleVersion = '1.1.2'
ModuleVersion = '1.1.3'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
Loading

0 comments on commit 02d72af

Please sign in to comment.