Skip to content

Commit

Permalink
Add provisional support for Agent Metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronsky committed Jun 7, 2022
1 parent 868d5dd commit 2279a1e
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/Buildkite/Buildkite.docc/Buildkite.md
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down
38 changes: 38 additions & 0 deletions Sources/Buildkite/Models/AgentMetric.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
42 changes: 42 additions & 0 deletions Sources/Buildkite/Resources/Agent/AgentMetrics.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}
50 changes: 50 additions & 0 deletions Tests/BuildkiteTests/Resources/Agent/AgentMetricsTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 2279a1e

Please sign in to comment.