-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add migration to setup alert config tables * chore: Add local dev seed data for a Slack alert * feat: Model alert configs * feat: Add `Error` for invalid alert configs * chore: Fix `seeder` command in `docker-compose.yml` * chore: Rename `docker-compose.yml` -> `compose.yaml` * refactor: `get_connection` out from repos and into free-standing func * feat: Add data model for reading alert configs * feat: `AlertConfigRepository` (only reading methods for now) * refactor: `AlertConfigReadData` -> `AlertConfigData` * test: Add some more test cases for converting `AlertConfigData` to `AlertConfig`s * feat: Support inserting, updating and deleting alert configs in `AlertConfigRepository` * feat: More control over JSON serialisation for `AlertConfig` * refactor: Test seed deletion * chore: Use `.contains_key` rather than `.get` and `.is_some` * test: Add test seeds for alert configs * test: Add integration tests for `AlertConfigRepository` * docs: Update `README` with info on env variables required to seed local DB * chore: Changing warning to note box, and fix formatting * chore: Try running `clippy` with `--verbose` to get more info * chore: No need for `--verbose` with `cargo clippy` * chore: Surpress `clippy` error * test: Add missing coverage * chore: Fix failing test
- Loading branch information
Showing
23 changed files
with
1,045 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ api/target | |
|
||
# Miscellaneous | ||
.DS_Store | ||
.env | ||
lcov.info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
use serde::Serialize; | ||
use uuid::Uuid; | ||
|
||
/// A domain model representing user configuration for alerts. | ||
#[derive(Clone, Debug, PartialEq, Serialize)] | ||
pub struct AlertConfig { | ||
/// The unique identifier for the alert configuration. | ||
pub alert_config_id: Uuid, | ||
/// The name of the alert configuration. | ||
pub name: String, | ||
/// The tenant that the alert configuration belongs to. | ||
pub tenant: String, | ||
/// Whether the alert configuration is active. | ||
pub active: bool, | ||
/// Whether to send alerts for late jobs. | ||
pub on_late: bool, | ||
/// Whether to send alerts for errored jobs. | ||
pub on_error: bool, | ||
/// The type of alert. | ||
#[serde(rename = "type")] | ||
pub type_: AlertType, | ||
} | ||
|
||
/// The different types of alerts that can be configured. | ||
#[derive(Clone, Debug, PartialEq, Serialize)] | ||
pub enum AlertType { | ||
/// An alert that sends a Slack message. | ||
#[serde(rename = "slack")] | ||
Slack(SlackAlertConfig), | ||
} | ||
|
||
/// Slack-specifc configuration for alerts. | ||
#[derive(Clone, Debug, PartialEq, Serialize)] | ||
pub struct SlackAlertConfig { | ||
/// The channel to send the alert to. | ||
pub channel: String, | ||
/// The Slack bot-user OAuth token (for use with chat.postMessage) | ||
pub token: String, | ||
} | ||
|
||
impl AlertConfig { | ||
/// Create a new `AlertConfig` for Slack. | ||
pub fn new_slack_config( | ||
name: String, | ||
tenant: String, | ||
active: bool, | ||
on_late: bool, | ||
on_error: bool, | ||
channel: String, | ||
token: String, | ||
) -> Self { | ||
Self { | ||
alert_config_id: Uuid::new_v4(), | ||
name, | ||
tenant, | ||
active, | ||
on_late, | ||
on_error, | ||
type_: AlertType::Slack(SlackAlertConfig { channel, token }), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use serde_json::json; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn new_slack_config() { | ||
let alert_config = AlertConfig::new_slack_config( | ||
"test-name".to_string(), | ||
"test-tenant".to_string(), | ||
true, | ||
true, | ||
true, | ||
"test-channel".to_string(), | ||
"test-token".to_string(), | ||
); | ||
|
||
// Cannot check the alert_config_id as it is randomly generated, but we know it'll be a Uuid | ||
// because of its type. | ||
assert_eq!(&alert_config.name, "test-name"); | ||
assert_eq!(&alert_config.tenant, "test-tenant"); | ||
assert!(alert_config.active); | ||
assert!(alert_config.on_late); | ||
assert!(alert_config.on_error); | ||
assert_eq!( | ||
alert_config.type_, | ||
AlertType::Slack(SlackAlertConfig { | ||
channel: "test-channel".to_string(), | ||
token: "test-token".to_string(), | ||
}) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_serialisation() { | ||
let alert_config = AlertConfig::new_slack_config( | ||
"test-name".to_string(), | ||
"test-tenant".to_string(), | ||
true, | ||
true, | ||
true, | ||
"test-channel".to_string(), | ||
"test-token".to_string(), | ||
); | ||
|
||
let value = serde_json::to_value(&alert_config).unwrap(); | ||
assert_eq!( | ||
value, | ||
json!({ | ||
"alert_config_id": alert_config.alert_config_id.to_string(), | ||
"name": "test-name", | ||
"tenant": "test-tenant", | ||
"active": true, | ||
"on_late": true, | ||
"on_error": true, | ||
"type": { | ||
"slack": { | ||
"channel": "test-channel", | ||
"token": "test-token" | ||
} | ||
} | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod alert_config; | ||
pub mod api_key; | ||
pub mod job; | ||
pub mod monitor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
api/src/infrastructure/migrations/2024-12-02-203820_add-alert-config-tables/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
DROP TABLE monitor_alert_config; | ||
DROP TABLE slack_alert_config; | ||
DROP TABLE alert_config; |
37 changes: 37 additions & 0 deletions
37
api/src/infrastructure/migrations/2024-12-02-203820_add-alert-config-tables/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
CREATE TABLE alert_config ( | ||
alert_config_id uuid PRIMARY KEY, | ||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
name VARCHAR NOT NULL, | ||
tenant VARCHAR NOT NULL, | ||
type VARCHAR NOT NULL, | ||
active BOOLEAN NOT NULL, | ||
on_late BOOLEAN NOT NULL, | ||
on_error BOOLEAN NOT NULL | ||
); | ||
|
||
CREATE TABLE slack_alert_config ( | ||
alert_config_id uuid PRIMARY KEY REFERENCES alert_config ON DELETE CASCADE, | ||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
-- These columns are intentionally overly verbose in how they are named, | ||
-- since we're using class table inheritance, and we fetch all alert configs | ||
-- in one query using left joins. If we weren't as verbose here, the columns | ||
-- could class (for example if we add support for Discord alerts, would | ||
-- 'channel' be for Discord or for Slack? | ||
slack_channel VARCHAR NOT NULL, | ||
slack_bot_oauth_token VARCHAR NOT NULL | ||
); | ||
|
||
-- This is an association table between alert_config and monitor. | ||
CREATE TABLE monitor_alert_config ( | ||
alert_config_id uuid REFERENCES alert_config ON DELETE CASCADE, | ||
monitor_id uuid REFERENCES monitor ON DELETE CASCADE, | ||
|
||
CONSTRAINT pk_monitor_alert_config PRIMARY KEY (alert_config_id, monitor_id) | ||
); | ||
|
||
SELECT diesel_manage_updated_at('alert_config'); | ||
SELECT diesel_manage_updated_at('slack_alert_config'); |
Oops, something went wrong.