From 2279a1e98333c4ba3f32323b1a81385bc57ba1de Mon Sep 17 00:00:00 2001 From: Aaron Sky Date: Mon, 6 Jun 2022 20:09:08 -0400 Subject: [PATCH] Add provisional support for Agent Metrics --- Sources/Buildkite/Buildkite.docc/Buildkite.md | 1 + Sources/Buildkite/Models/AgentMetric.swift | 38 ++++++++++++++ .../Resources/Agent/AgentMetrics.swift | 42 ++++++++++++++++ .../Resources/Agent/AgentMetricsTests.swift | 50 +++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 Sources/Buildkite/Models/AgentMetric.swift create mode 100644 Sources/Buildkite/Resources/Agent/AgentMetrics.swift create mode 100644 Tests/BuildkiteTests/Resources/Agent/AgentMetricsTests.swift diff --git a/Sources/Buildkite/Buildkite.docc/Buildkite.md b/Sources/Buildkite/Buildkite.docc/Buildkite.md index 3e26329..e151b3e 100644 --- a/Sources/Buildkite/Buildkite.docc/Buildkite.md +++ b/Sources/Buildkite/Buildkite.docc/Buildkite.md @@ -17,6 +17,7 @@ The entire publicly documented REST API surface is supported by this package. - ``AccessToken/Resources`` - ``Agent/Resources`` +- ``AgentMetrics/Resources`` - ``Annotation/Resources`` - ``Artifact/Resources`` - ``Build/Resources`` diff --git a/Sources/Buildkite/Models/AgentMetric.swift b/Sources/Buildkite/Models/AgentMetric.swift new file mode 100644 index 0000000..73bbf67 --- /dev/null +++ b/Sources/Buildkite/Models/AgentMetric.swift @@ -0,0 +1,38 @@ +// +// AgentMetrics.swift +// Buildkite +// +// Created by Aaron Sky on 6/5/22. +// Copyright © 2022 Aaron Sky. All rights reserved. +// + +import Foundation + +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + +public struct AgentMetrics: Codable, Equatable { + public var agents: AgentTotals + public var jobs: JobTotals + public var organization: Organization + + public struct AgentTotals: Codable, Equatable { + public var idle: Int + public var busy: Int + public var total: Int + public var queues: [String: AgentTotals] = [:] + } + + public struct JobTotals: Codable, Equatable { + public var scheduled: Int + public var running: Int + public var waiting: Int + public var total: Int + public var queues: [String: JobTotals] = [:] + } + + public struct Organization: Codable, Equatable { + public var slug: String + } +} diff --git a/Sources/Buildkite/Resources/Agent/AgentMetrics.swift b/Sources/Buildkite/Resources/Agent/AgentMetrics.swift new file mode 100644 index 0000000..e337d9c --- /dev/null +++ b/Sources/Buildkite/Resources/Agent/AgentMetrics.swift @@ -0,0 +1,42 @@ +// +// AgentMetrics.swift +// Buildkite +// +// Created by Aaron Sky on 6/5/22. +// Copyright © 2022 Aaron Sky. All rights reserved. +// + +import Foundation + +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + +extension AgentMetrics { + /// Resources for requesting information about your organization's Buildkite agents. + public enum Resources {} +} + +extension AgentMetrics.Resources { + /// Get metrics about agents active with the current organization. + public struct Get: Resource { + public typealias Content = AgentMetrics + + public var version: APIVersion { + APIVersion.Agent.v3 + } + + public let path = "metrics" + + public init() {} + } +} + +extension Resource where Self == AgentMetrics.Resources.Get { + /// Get an object with properties describing Buildkite + /// + /// Returns meta information about Buildkite. + public static var agentMetrics: Self { + Self() + } +} diff --git a/Tests/BuildkiteTests/Resources/Agent/AgentMetricsTests.swift b/Tests/BuildkiteTests/Resources/Agent/AgentMetricsTests.swift new file mode 100644 index 0000000..e72ce02 --- /dev/null +++ b/Tests/BuildkiteTests/Resources/Agent/AgentMetricsTests.swift @@ -0,0 +1,50 @@ +// +// AgentMetricsTests.swift +// Buildkite +// +// Created by Aaron Sky on 6/5/22. +// Copyright © 2022 Aaron Sky. All rights reserved. +// + +import Foundation +import XCTest + +@testable import Buildkite + +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + +extension AgentMetrics { + init() { + self.init( + agents: .init( + idle: 0, + busy: 0, + total: 0, + queues: [:] + ), + jobs: .init( + scheduled: 0, + running: 0, + waiting: 0, + total: 0, + queues: [:] + ), + organization: .init( + slug: "buildkite" + ) + ) + } +} + +class AgentMetricsTests: XCTestCase { + func testAgentMetricsGet() async throws { + let expected = AgentMetrics() + let context = try MockContext(content: expected) + + let response = try await context.client.send(.agentMetrics) + + XCTAssertEqual(expected, response.content) + } +}