Skip to content

Commit b9ed392

Browse files
committed
bugfix: coderabbit suggestion
1 parent 0f7c879 commit b9ed392

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

src/alerts/target.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,18 @@ impl TargetConfigs {
6565
}
6666

6767
pub async fn update(&self, target: Target) -> Result<(), AlertError> {
68-
let id = target.id;
69-
self.target_configs.write().await.insert(id, target.clone());
70-
let path = target_json_path(&id);
68+
let mut map = self.target_configs.write().await;
69+
if map.values().any(|t| {
70+
t.target == target.target
71+
&& t.timeout.interval == target.timeout.interval
72+
&& t.timeout.times == target.timeout.times
73+
&& t.id != target.id
74+
}) {
75+
return Err(AlertError::DuplicateTargetConfig);
76+
}
77+
map.insert(target.id, target.clone());
78+
79+
let path = target_json_path(&target.id);
7180

7281
let store = PARSEABLE.storage.get_object_store();
7382
let target_bytes = serde_json::to_vec(&target)?;
@@ -146,21 +155,6 @@ pub struct Target {
146155
}
147156

148157
impl Target {
149-
pub async fn validate(&self) -> Result<(), AlertError> {
150-
// just check for liveness
151-
// what if the target is not live yet but is added by the user?
152-
let targets = TARGETS.list().await?;
153-
for target in targets {
154-
if target.target == self.target
155-
&& target.timeout.interval == self.timeout.interval
156-
&& target.timeout.times == self.timeout.times
157-
{
158-
return Err(AlertError::DuplicateTargetConfig);
159-
}
160-
}
161-
Ok(())
162-
}
163-
164158
pub fn call(&self, context: Context) {
165159
trace!("target.call context- {context:?}");
166160
let timeout = &self.timeout;

src/handlers/http/targets.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ pub async fn post(
1515
Json(target): Json<Target>,
1616
) -> Result<impl Responder, AlertError> {
1717
// should check for duplicacy and liveness (??)
18-
target.validate().await?;
19-
2018
// add to the map
2119
TARGETS.update(target.clone()).await?;
2220

@@ -55,8 +53,6 @@ pub async fn update(
5553
target.id = target_id;
5654

5755
// should check for duplicacy and liveness (??)
58-
target.validate().await?;
59-
6056
// add to the map
6157
TARGETS.update(target.clone()).await?;
6258

0 commit comments

Comments
 (0)