Skip to content

Commit

Permalink
feat: add function for check psql connection (#10)
Browse files Browse the repository at this point in the history
* feat: add function for check psql connection

* fix: fix swift lint

* tests: try fix tests. Bug with date format

* test: try fix test for date format

* refactor: refactor test and add extension

* refactor: delete print

* style: delete space

* docs: add documentation

* refactor: change date format

* tests: refactor mock data

* refactor: refactor functions, rename and add tls parameter

* refactor: change function checkConnection for result

* refactor: change struct response

* docs: change documentation

* refactor: rename file

* docs: add license documentation

* refactor: refactor method for get psql connection

* refactor: add test for func

* refactor: refactotr test

* refactor: rename func

* refactor: add field to struct

* refactor: rename func

* refactor: delete not use init

* refactor: refactor service

* refactor: refactor funcs

* docs: change documentation

* tests: fix tests

* tests: refactor tests

* refactor: change funcs

* tests: refactor tests

* refactor: change function
  • Loading branch information
gulivero1773 authored Feb 6, 2024
1 parent 8a32cba commit 4000843
Show file tree
Hide file tree
Showing 19 changed files with 613 additions and 19 deletions.
54 changes: 54 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@
"version" : "4.14.1"
}
},
{
"identity" : "fluent",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/fluent.git",
"state" : {
"revision" : "a586a5d4164f23a0ee4e02e1f467b9bbef0c9f1c",
"version" : "4.9.0"
}
},
{
"identity" : "fluent-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/fluent-kit.git",
"state" : {
"revision" : "6cef8533c9ab87865de58fa3c6e6317e3e09857a",
"version" : "1.45.1"
}
},
{
"identity" : "fluent-postgres-driver",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/fluent-postgres-driver.git",
"state" : {
"revision" : "a538fc647f82d915eb84e0a12ca9b08c513e57c4",
"version" : "2.8.0"
}
},
{
"identity" : "multipart-kit",
"kind" : "remoteSourceControl",
Expand All @@ -36,6 +63,24 @@
"version" : "4.6.0"
}
},
{
"identity" : "postgres-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/postgres-kit.git",
"state" : {
"revision" : "80ab7737dac4fccd4a8ad38743828dcb71ba7ac8",
"version" : "2.12.2"
}
},
{
"identity" : "postgres-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/postgres-nio.git",
"state" : {
"revision" : "fa3137d39bca84843739db1c5a3db2d7f4ae65e6",
"version" : "1.20.0"
}
},
{
"identity" : "routing-kit",
"kind" : "remoteSourceControl",
Expand All @@ -45,6 +90,15 @@
"version" : "4.9.0"
}
},
{
"identity" : "sql-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/sql-kit.git",
"state" : {
"revision" : "b2f128cb62a3abfbb1e3b2893ff3ee69e70f4f0f",
"version" : "3.28.0"
}
},
{
"identity" : "swift-algorithms",
"kind" : "remoteSourceControl",
Expand Down
10 changes: 8 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ let package = Package(
],
dependencies: [
// 💧 A server-side Swift web framework.
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0")
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
// 🖋 Swift ORM (queries, models, and relations) for NoSQL and SQL databases.
.package(url: "https://github.com/vapor/fluent.git", from: "4.1.0"),
// 🐘 Swift ORM (queries, models, relations, etc) built on PostgreSQL.
.package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.1.1")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "AppHealthChecks",
dependencies: [
.product(name: "Vapor", package: "vapor")
.product(name: "Vapor", package: "vapor"),
.product(name: "Fluent", package: "fluent"),
.product(name: "FluentPostgresDriver", package: "fluent-postgres-driver")
]
),
.testTarget(
Expand Down
33 changes: 33 additions & 0 deletions Sources/AppHealthChecks/ChecksProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// FS App Health Checks
// Copyright (C) 2024 FREEDOM SPACE, LLC

//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//
// ChecksProtocol.swift
//
//
// Created by Mykola Buhaiov on 06.02.2024.
//

import Vapor

/// Groups func for get health check
public protocol ChecksProtocol {
/// Check with setup options
/// - Parameter options: array of `MeasurementType`
/// - Returns: dictionary `[String: HealthCheckItem]`
func checkHealth(for options: [MeasurementType]) async -> [String: HealthCheckItem]
}
38 changes: 37 additions & 1 deletion Sources/AppHealthChecks/Extensions/Application+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension Application {
/// Less verbose typealias for `UUID`.
typealias Value = UUID
}

/// Setup `serviceId` in application storage
public var serviceId: UUID? {
get { storage[ServiceIdKey.self] }
Expand All @@ -49,4 +49,40 @@ extension Application {
get { storage[ReleaseIdKey.self] }
set { storage[ReleaseIdKey.self] = newValue }
}

/// A `psqlIdKey` conform to StorageKey protocol
private struct PsqlIdKey: StorageKey {
/// Less verbose typealias for `String`.
typealias Value = String
}

/// Setup `psqlIdKey` in application storage
public var psqlId: String? {
get { storage[PsqlIdKey.self] }
set { storage[PsqlIdKey.self] = newValue }
}
}

extension Application {
/// A `PostgresHealthChecksKey` conform to StorageKey protocol
public struct PostgresHealthChecksKey: StorageKey {
/// Less verbose typealias for `PostgresHealthChecksProtocol`.
public typealias Value = PostgresHealthChecksProtocol
}

/// Setup `psqlHealthChecks` in application storage
public var psqlHealthChecks: PostgresHealthChecksProtocol? {
get { storage[PostgresHealthChecksKey.self] }
set { storage[PostgresHealthChecksKey.self] = newValue }
}
}

extension Application {
/// Variable of date conform to DateFormatter protocol. ISO 8601 with date time format
/// Example: `2024-02-01T11:11:59.364`
public var dateTimeISOFormat: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = Constants.dateFormat
return formatter
}
}
49 changes: 49 additions & 0 deletions Sources/AppHealthChecks/Models/ComponentName.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// FS App Health Checks
// Copyright (C) 2024 FREEDOM SPACE, LLC

//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//
// ComponentName.swift
//
//
// Created by Mykola Buhaiov on 29.01.2024.
//

import Vapor

/// Human-readable name for the component
public enum ComponentName: String {
/// The Central Processing Unit (CPU) is the primary component of a computer that acts as its "control center."
case cpu
/// Memory, also known as random access memory (RAM), is a PC component that stores data while the computer runs
case memory
/// Redis is an open-source in-memory storage, used as a distributed, in-memory key–value database
case redis
/// PostgreSQL also known as Postgres, is a free and open-source relational database management system (RDBMS) emphasizing extensibility
case postgresql
/// MongoDB is a source-available, cross-platform, document-oriented database program.
case mongo
/// Distributed messaging system between server applications in real time
case kafka
/// Consul is a service networking solution to automate network configurations, discover services, and enable secure connectivity across any cloud or runtime.
case consul
/// gRPC is a modern open source high performance Remote Procedure Call (RPC) framework that can run in any environment.
case grpc
}

extension ComponentName: Content {}

extension ComponentName: CaseIterable {}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import Vapor

/// Human-readable type for the component.
public enum ComponentType: String {
/// A part that combines with other parts to form something bigger
case component
/// A datastore is a repository for storing, managing and distributing data sets on an enterprise level
case datastore
/// A set of connected things or devices that operate together
case system
}

Expand Down
59 changes: 59 additions & 0 deletions Sources/AppHealthChecks/Models/HealthCheckItem+Equatable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// FS App Health Checks
// Copyright (C) 2024 FREEDOM SPACE, LLC

//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//
// HealthCheckItem+Equatable.swift
//
//
// Created by Mykola Buhaiov on 06.02.2024.
//

import Vapor

/// Equatable protocol implementation for `HealthCheckItem` struct.
///
/// Two `HealthCheckItem` structs are considered equal if they have the same:
///
/// - componentId
/// - componentType
/// - observedValue
/// - observedUnit
/// - status
/// - affectedEndpoints
/// - time
/// - output
/// - links
/// - node
extension HealthCheckItem: Equatable {
/// Conform `HealthCheckItem` to `Equatable` protocol
/// - Parameters:
/// - lhs: `HealthCheckItem`
/// - rhs: `HealthCheckItem`
/// - Returns: `Bool`
public static func == (lhs: HealthCheckItem, rhs: HealthCheckItem) -> Bool {
return lhs.componentId == rhs.componentId &&
lhs.componentType == rhs.componentType &&
lhs.observedValue == rhs.observedValue &&
lhs.observedUnit == rhs.observedUnit &&
lhs.status == rhs.status &&
lhs.affectedEndpoints == rhs.affectedEndpoints &&
lhs.time == rhs.time &&
lhs.output == rhs.output &&
lhs.links == rhs.links &&
lhs.node == rhs.node
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ import Vapor

/// Name of the measurement type (a data point type) that the status is reported for
public enum MeasurementType: String {
/// An act or instance of making practical or profitable use of something
case utilization
/// The time lag between an electronic input and the output signal which depends upon the value of passive components used
case responseTime
/// The state of being connected
case connections
/// Uptime is a measure of system reliability, expressed as the percentage of time a machine
case uptime
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// FS App Health Checks
// Copyright (C) 2024 FREEDOM SPACE, LLC

//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//
// PostgresChecksProtocol.swift
//
//
// Created by Mykola Buhaiov on 31.01.2024.
//

import Vapor

/// Groups func for get psql health check
public protocol PostgresChecksProtocol {
/// Get Postgresql version
/// - Returns: `HealthCheckItem`
func connection() async -> HealthCheckItem

/// Get response time from postgresql
/// - Returns: `HealthCheckItem`
func getResponseTime() async -> HealthCheckItem

/// Get version from postgresql
/// - Returns: `String`
func getVersion() async -> String
}
Loading

0 comments on commit 4000843

Please sign in to comment.