Getting daily deployments counts

Hi,
I am running a Powershell script to get our daily deployments from each space in Octopus Deploy.
For one of our spaces the script just takes forever and fails due to the sheer volume of deployments in that space.

I am using the Octopus.client in my script. I am getting all of yesterdays deployments and using the .FindAll(), which searches all deployments so that’s why my script takes so long. Is there a better way to search yesterdays deployments so then my scripts wont take so long to run. I know there is a .FindBy(), but cant get that to work.

#return the all of the deployments that match the parameters

 $deployments = $Spacerepository.Deployments.FindAll() | Where-Object {$_.Created -ge $after - 
   and $_.Created -lt $before}

Any help would be much appreciated.

Kind Regards,
Micheal Power

Hi @mikepower79

As you’ve found the FindAll() method on the client doesnt allow for easily filtering on dates on the server side. That’s because the underlying API call (/api/deployments) doesnt support it.

To get an efficient list of yesterdays deployments, I’d use the Events API (/api/events) endpoint instead as it’s one of the only endpoints that allows you to filter on date on the server side, much more efficient than pulling all data back and then filtering on the client side.

With it, you can filter on different types of deployment event. For example: “DeploymentQueued”, “DeploymentStarted”, “DeploymentSucceeded”. Which you choose is up to your own requirements.

Using DeploymentQueued as an example, here is what a script for yesterdays queued deployments might look like for the Default space:

# Load octopus.client assembly
Add-Type -Path "C:\Octo\Octopus.Client.dll"

$octopusURL = "https://your.octopus.app"
$octopusAPIKey = "API-YOURKEY"
$spaceName = "Default"

$Today = Get-Date
$StartOfYesterday = $Today.AddDays(-1).Date
$EndOfYesterday = $Today.Date.AddMilliseconds(-1)

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURL, $octopusAPIKey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$client = New-Object Octopus.Client.OctopusClient $endpoint

try
{
    # Get space
    $space = $repository.Spaces.FindByName($spaceName)
    $repositoryForSpace = $client.ForSpace($space)

    # Required Parameters
    $from = $StartOfYesterday.ToString("yyyy-MM-dd HH:mm:ss")
    $to = $EndOfYesterday.ToString("yyyy-MM-dd HH:mm:ss")
    $eventGroups = "Deployment"
    $eventCategories = "DeploymentQueued"
    
    # Other Parameters
    
    $skip = 0
    $take = $null
    
    $regarding = $null
    $regardingAny = $null
    $includeInternalEvents = $True
    $user = $null
    $users = $null
    $projects = $null
    $environments = $null
    $tenants = $null
    $tags = $null
    $fromAutoId = $null
    $toAutoId = $null
    $documentTypes = $null
    $eventAgents = $null
    $projectGroups = $null
    
    # Get Deployment events queued yesterday
    $deployments = $repositoryForSpace.Events.List($skip,$take, $from, $to, $regarding, $regardingAny, $includeInternalEvents, $user, $users, $projetcs, $environments, $eventGroups, $eventCategories, $tenants, $tags, $fromAutoId, $toAutoId, $documentTypes, $eventAgents, $projectGroups)

    Write-Host "Found $($deployments.Items.Count) deployments queued between $($from) and $($to)"
}
catch
{
    Write-Host $_.Exception.Message
}

Note: If you add multiple event categories the Items count will need to be filtered to identify unique deployments :slight_smile:

I hope that helps!

Best regards,
Mark

Hi @mark.harrison
Thanks for the feedback.
I will give that a go and see how I get on.

Kind Regards,
Micheál Power

Hey @mikepower79

You’re very welcome :slight_smile:

Best,
Mark

Hi @mark.harrison,
I have made a slight modification to your script:

$eventCategories = "Deployment succeeded" also tried DeploymentSucceeded

But it doesnt seem to work.

image

Kind Regards,
Micheál

Hi @mikepower79

I’d expect DeploymentSucceeded to work. When looking in the audit screen the event category has a space in it for readability.

If you open the developer tools in your browser (I’m using Firefox) you can see the call made to the event API:

From the original script I provided, I changed the section of “Required Parameters” to:

 # Required Parameters
$from = $StartOfYesterday.ToString("yyyy-MM-dd HH:mm:ss")
$to = $EndOfYesterday.ToString("yyyy-MM-dd HH:mm:ss")
$eventGroups = "Deployment"
$eventCategories = "DeploymentSucceeded"

And it worked successfully.

I’d recommend using a traffic capture tool if you’re using PowerShell ISE or another tool (such as Telerik Fiddler) to see what request is being made to the Octopus API to compare it against the Octopus Portal in your browser developer tools.

I hope that helps!

Hi @mark.harrison,
Quick update, the script works now so it does. Thanks for all your help.

Kind REgards,
Micheál

Hi @mikepower79

Apologies for the delay, but that’s great it’s working!

Best regards,