Integration - Octopus.Client - Efficiently getting most recent successful deployment for a tenant

Is there a better way to optimize this code? Currently with only a few hundred releases in the system (Cloud) this code block takes over 30 seconds to complete. Using my original flow that used the repos instead of the client get the same thing would take minutes.

The intent is to get the most recent successful deployment of every project in every environment for a given tenantid, currently this does that except it gets all deployments not just the most recent. I cannot figure out how to do that in the get query.

C#

var space = await client.Repository.Spaces.FindOne(x => x.Id == spaceId);
var repos = client.ForSpace(space);
var envs = await repos.Environments.GetAll();
var tenant = await repos.Tenants.FindOne(x => x.Id == tenantId);
var root = await repos.LoadSpaceRootDocument();

//under 30 projects, no need to use get
var projects = await repos.Projects.FindMany(x => x.TenantedDeploymentMode != TenantedDeploymentMode.Untenanted && tenant.ProjectEnvironments.ContainsKey(x.Id) );

var deployments = await repos.Client.Get<ResourceCollection< DeploymentResource>>(root.Links[“Deployments”] + $"?take=100000&tenants={tenantId}");

var.releases = await repos.Client.Get<ResourceCollection< ReleaseResource>>(root.Links[“Releases”] + $"?take=100000&ids={deployments.Items.Select(x => x.ReleaseId).Aggregate((a,b) => a + “,” + b)}");

//no links available in root document for tasks.
var tasks = await repos.Tasks.FindMany(x => x.IsCompleted && x.FinishedSuccessfully && deployments.Items.Select(d => d.TaskId).Contains(x.Id));

Also this text editor does strang things when something is formatted like
<word< anotherword>> without the white space the inner <> is removed.

Hi @PMSwetz,

Thanks for getting in touch! I had a look to see if there was an example I could provide you but we don’t seem to have one. However, if you were to do this, the best way would be to use the Dashboard endpoint.

It contains all currently deployed projects and their status. You should be able to work with the dashboard to build this us and save time over getting each object the way you currently are. With your current method, your script is only going to get slower as you deploy.

I thought I had a similar script laying around but was unable to locate it unfortunately. :frowning:

We have an opensource repository of PowerShell scripts with the Octopus.Client which you are welcome to reference. If you find a good solution and feel like posting it here, I would be happy to upload it to the repository to help out others in the future.

If you have any further thoughts or questions on this, please don’t hesitate to let me know.

Best regards,
Daniel

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.