Skip to content

Commit 88a50e8

Browse files
authored
[Breaking Change] feat: Implement multiple sending of events (#479)
* implement sending events * mod exmples * change example file names
1 parent 43af4c4 commit 88a50e8

File tree

8 files changed

+44
-33
lines changed

8 files changed

+44
-33
lines changed

agent/src/controllers/public/send_event.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,32 @@ pub struct MessageContainer {
1717
occurred_at: u64,
1818
}
1919

20-
pub async fn handler(Json(json): Json<MessageContainer>) -> Result<StatusCode, AgentErrorCode> {
21-
if json.key.is_empty() {
22-
Err(AgentErrorCode::SendEventNoKey)?
23-
}
24-
if json.detail.is_empty() {
25-
Err(AgentErrorCode::SendEventNoDetail)?
26-
}
20+
pub async fn handler(
21+
Json(json): Json<Vec<MessageContainer>>,
22+
) -> Result<StatusCode, AgentErrorCode> {
23+
let events = json
24+
.iter()
25+
.map(|m| {
26+
if m.key.is_empty() {
27+
return Err(AgentErrorCode::SendEventNoKey);
28+
}
29+
if m.detail.is_empty() {
30+
return Err(AgentErrorCode::SendEventNoDetail);
31+
}
2732

28-
let occurred_at =
29-
milliseconds_to_time(json.occurred_at).ok_or(AgentErrorCode::SendEventInvalidOccurredAt)?;
33+
let occurred_at = milliseconds_to_time(m.occurred_at)
34+
.ok_or(AgentErrorCode::SendEventInvalidOccurredAt)?;
3035

31-
let usecase = EventUsecase::new();
32-
match usecase
33-
.save(EventStoreRequest {
34-
key: json.key,
35-
detail: json.detail,
36-
occurred_at,
36+
Ok(EventStoreRequest {
37+
key: m.key.clone(),
38+
detail: m.detail.clone(),
39+
occurred_at,
40+
})
3741
})
38-
.await
39-
{
42+
.collect::<Result<Vec<EventStoreRequest>, AgentErrorCode>>()?;
43+
44+
let usecase = EventUsecase::new();
45+
match usecase.save(events).await {
4046
Ok(_) => {
4147
log::info!("save event");
4248
Ok(StatusCode::NO_CONTENT)

agent/src/repository/event_repository.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ pub struct EventStoreRequest {
1010

1111
#[trait_variant::make(Send)]
1212
pub trait EventStoreRepository {
13-
async fn save(&self, request: EventStoreRequest) -> anyhow::Result<()>;
13+
async fn save(&self, request: Vec<EventStoreRequest>) -> anyhow::Result<()>;
1414
}

agent/src/services/studio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ impl MetricStoreRepository for Studio {
464464
}
465465

466466
impl EventStoreRepository for Studio {
467-
async fn save(&self, request: EventStoreRequest) -> anyhow::Result<()> {
467+
async fn save(&self, request: Vec<EventStoreRequest>) -> anyhow::Result<()> {
468468
self.relay_to_studio("/v1/events", request).await
469469
}
470470
}

agent/src/usecase/event_usecase.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl EventUsecase<Studio> {
1919
}
2020

2121
impl<R: EventStoreRepository> EventUsecase<R> {
22-
pub async fn save(&self, request: EventStoreRequest) -> anyhow::Result<()> {
22+
pub async fn save(&self, request: Vec<EventStoreRequest>) -> anyhow::Result<()> {
2323
match self.repository.save(request).await {
2424
Ok(_) => {
2525
log::info!("save event");
@@ -43,7 +43,7 @@ mod tests {
4343
pub struct MockEventStoreRepository {}
4444

4545
impl EventStoreRepository for MockEventStoreRepository {
46-
async fn save(&self, _: EventStoreRequest) -> anyhow::Result<()> {
46+
async fn save(&self, _: Vec<EventStoreRequest>) -> anyhow::Result<()> {
4747
Ok(())
4848
}
4949
}
@@ -54,11 +54,11 @@ mod tests {
5454
repository: MockEventStoreRepository {},
5555
};
5656
let _ = usecase
57-
.save(EventStoreRequest {
57+
.save(vec![EventStoreRequest {
5858
key: "test".to_string(),
5959
detail: "test".to_string(),
6060
occurred_at: chrono::Utc::now(),
61-
})
61+
}])
6262
.await
6363
.map_err(|e| panic!("{:?}", e));
6464
}
File renamed without changes.

examples/nodejs/src/generateEvent.js renamed to examples/nodejs/src/generateEvents.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { post } from "./lib/sock.js";
22

33
(async () => {
4-
const payload = {
5-
key: "test-key",
6-
detail: "test-detail",
7-
occurred_at: Date.now(),
8-
};
4+
const payload = Array.from({ length: 10 }, (_, i) => ({
5+
key: `test-key${i}`,
6+
detail: `test-detail${i}`,
7+
occurred_at: Date.now() + i,
8+
}));
99
const response = await post("/events", payload);
1010

1111
console.log("The response is as follows.\n");
File renamed without changes.

examples/python/src/generate_event.py renamed to examples/python/src/generate_events.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
else:
77
from sock import post
88

9+
events = [
10+
{
11+
"key": "test-key" + str(x + 10),
12+
"detail": "test-detail" + str(x),
13+
"occurred_at": int(time.time() * 1000 + x),
14+
}
15+
for x in range(10)
16+
]
17+
918
json_response = post(
1019
"/events",
11-
{
12-
"key": "test-key",
13-
"detail": "test-detail",
14-
"occurred_at": int(time.time() * 1000),
15-
},
20+
events,
1621
)
1722

1823
print("The response is as follows.\n")

0 commit comments

Comments
 (0)