Getting number of deployments in octopus for a particular date

I am trying to script getting number of Octopus Deployments for each space for a specific date (say yesterday AddDays(-1)). Am I going about this the right way?

Snippet of code:

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $OctopusUrl,$ApiKey
$client = new-object Octopus.Client.OctopusClient $endpoint

$space = $client.ForSystem().Spaces.FindByName($space.Name)
$spaceRepository = $client.ForSpace($space)

$deployments = $spaceRepository.Deployments.Get($timeStamp.AddDays(-1))
$daysDeployments = $($deployments.Count)

Kind Regards,
Mike

Hey Mike,

I adapted some code here that should get the job done. (based on this code: https://github.com/OctopusDeploy/OctopusDeploy-Api/blob/master/Octopus.Client/PowerShell/Events/FindByDate.ps1)

Instead of going into the deployments repository, I instead went to the Audit log and filtered by date.

Add-Type -Path 'C:\temp\Octopus.Client.dll' 

$apikey = '' # Get this from your profile
$octopusURI = '' # Your Octopus Server address
$spaceName = "Default"


$endpoint = new-object Octopus.Client.OctopusServerEndpoint $OctopusURI, $APIKey
$client = new-object Octopus.Client.OctopusClient $endpoint
$space = $client.ForSystem().Spaces.FindByName($spaceName)
$spaceRepository = $client.ForSpace($space)

[System.DateTimeOffset]$after = "9/8/2020"
[System.DateTimeOffset]$before = "9/9/2020"
$deploymentcount = 0

#return the all of the events that match the parameters
$events = $spacerepository.Events.FindMany(
{param($e) if(($e.Category -match "DeploymentStarted") -and ($e.Occurred -gt $after) -and ($e.Occurred -lt $before))
    {
    $True 
    
    }
})
#if you want to do more logic, you could implement it in the for loop
foreach ($event in $events){
    $deploymentcount++
}
#or alternatively just do a count
#$events.Count

write-host $deploymentcount

You could also add some extra logic for DeploymentSucceeded or DeploymentFailed or other categories to get more advanced stats/numbers.

Please let me know if this works for you or if we need to find a different solution.

Thanks,
Jeremy

Hi Jeremy,
That looks good. The only thing is that I will be running this once daily and the date is hardcoded, so if I wanted to get yesterdays deployments.

So would this work:
[System.DateTimeOffset]$after = (Get-Date).AddDays(-1) (ie: yesterday)
[System.DateTimeOffset]$before = Get-Date (ie: today)

Thanks,
Mike

Hey Mike,

I changed the code to be :

[System.DateTimeOffset]$after = (Get-Date).AddDays(-1) 
[System.DateTimeOffset]$before = (Get-Date)

It produced the same number of deployments on my side as when hardcoded.

I think that would work fine for you, but as always, please do testing as my setup was very minimal. It’s not guaranteed to work in every scenario.

Please let me know how it goes or if we need to work on it some more.

Thanks,
Jeremy

Thats great thanks for your help.

Hi Jeremy,
I have implemented the code and it works fine. For one of my spaces which do alot of deployments the script runs for along time and still dont get a number back for the number of deployments. Is there a way to improve the search to make it faster?
The only property I can use is the Occured which I have narrowed down using the dates $after and $before.
I get this error:
Exception calling “FindMany” with “1” argument(s): “Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.”

Thanks,
Mike

Hey Mike,

How long of a time span are you trying to search on that’s causing the timeout? How short of a time span does it work with?

Thanks,
Jeremy

I dont have a time span set.

Hi Mike,

What I meant by that is what is the before/after that you’re running that times out?

And you’re saying it works with 1 space but not another? (and the one that doesnt work has tons of deployments?)

Thanks,
Jeremy

Yes exactly. Doesnt work on the busiest space.

Hi Mike,

And what is the before/after you’re using when doing the query? Just a day?

Thanks,
Jeremy

Yes just one day.

Hey Mike,

Yeah, that’s a pretty expensive API call because it’s hitting every row in your event table in your SQL database.

Let’s go back to working with deployments. A colleague of mine figured out that we should be using FindAll instead of GetAll, and then filter on the date. GetAll not working is why I moved to the event table for the solution.

This code works for me in testing.

Please test it and let me know if it works for you.

Add-Type -Path 'C:\temp\Octopus.Client.dll' 

$apikey = '' # Get this from your profile
$octopusURI = '' # Your Octopus Server address
$spaceName = "Default"


$endpoint = new-object Octopus.Client.OctopusServerEndpoint $OctopusURI, $APIKey
$client = new-object Octopus.Client.OctopusClient $endpoint
$space = $client.ForSystem().Spaces.FindByName($spaceName)
$spaceRepository = $client.ForSpace($space)

[System.DateTimeOffset]$after = (Get-Date).AddDays(-1) 

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

There is also a reporting xml endpoint you can hit but you can only use REST API for that.

Thanks,
Jeremy

Hi Jeremy,
Thanks again for your response. Still no luck. I will try the REST API option I think.

Kind Regards,
Mike

Hey Mike,

I’m kind of surprised that the deployments section is also timing out as that gets cleaned up by retention policies etc. What do your sql server metrics look like when you’re running the query? How long does it run before it times out?

Thanks,
Jeremy

Actually I have run it again and it does work. It does take a bit of time but it works.
Thanks for all your help.

1 Like

Hey Mike,

I’m glad to hear its working. If it stops working again I would check your SQL metrics and see if the server is maxed out. You could also move the code to REST API and do a take command and only take the last X number of deployments. That would likely be much quicker, but it also has the potential for missing some if you set the number too low. For example, if you had 105 deployments and you only take the last 100, you would just get 100 as your number. You could definitely code it in to say, if deployment count -eq takevalue, add 50 to take value and rerun script or something like that.

If you try to implement the above and hit a roadblock please let me know.

Thanks,
Jeremy