Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add function for check psql connection #10

Merged
merged 32 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3890d1a
feat: add function for check psql connection
gulivero1773 Feb 1, 2024
587f4c7
fix: fix swift lint
gulivero1773 Feb 1, 2024
68c3234
tests: try fix tests. Bug with date format
gulivero1773 Feb 1, 2024
d415791
test: try fix test for date format
gulivero1773 Feb 1, 2024
55d8a3b
refactor: refactor test and add extension
gulivero1773 Feb 1, 2024
296b787
refactor: delete print
gulivero1773 Feb 1, 2024
5c78c25
style: delete space
gulivero1773 Feb 1, 2024
cd26e15
docs: add documentation
gulivero1773 Feb 1, 2024
0ba455b
refactor: change date format
gulivero1773 Feb 1, 2024
a1712e9
tests: refactor mock data
gulivero1773 Feb 1, 2024
afae7d6
refactor: refactor functions, rename and add tls parameter
gulivero1773 Feb 1, 2024
bc4c627
refactor: change function checkConnection for result
gulivero1773 Feb 1, 2024
5748127
refactor: change struct response
gulivero1773 Feb 1, 2024
bf513f0
docs: change documentation
gulivero1773 Feb 1, 2024
71d5a4d
refactor: rename file
gulivero1773 Feb 1, 2024
83f125c
docs: add license documentation
gulivero1773 Feb 1, 2024
b72b548
Merge remote-tracking branch 'refs/remotes/origin/main'
gulivero1773 Feb 1, 2024
c095295
refactor: refactor method for get psql connection
gulivero1773 Feb 1, 2024
266e104
refactor: add test for func
gulivero1773 Feb 2, 2024
60674cd
refactor: refactotr test
gulivero1773 Feb 2, 2024
c5b448e
refactor: rename func
gulivero1773 Feb 2, 2024
c8655ec
refactor: add field to struct
gulivero1773 Feb 5, 2024
2da4f43
refactor: rename func
gulivero1773 Feb 5, 2024
561614a
refactor: delete not use init
gulivero1773 Feb 5, 2024
c81a5fb
refactor: refactor service
gulivero1773 Feb 6, 2024
fdb632f
refactor: refactor funcs
gulivero1773 Feb 6, 2024
5d6ca70
docs: change documentation
gulivero1773 Feb 6, 2024
484ba42
tests: fix tests
gulivero1773 Feb 6, 2024
9353267
tests: refactor tests
gulivero1773 Feb 6, 2024
d3c7170
refactor: change funcs
gulivero1773 Feb 6, 2024
611be4f
tests: refactor tests
gulivero1773 Feb 6, 2024
0d89dd7
refactor: change function
gulivero1773 Feb 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"),
gulivero1773 marked this conversation as resolved.
Show resolved Hide resolved
// 🖋 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 {
gulivero1773 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading