Skip to content

Commit

Permalink
⚗️ (stop): 继续优化查询
Browse files Browse the repository at this point in the history
  • Loading branch information
czy-29 committed Jan 13, 2025
1 parent 6fd7913 commit 1a6f80a
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions tracing-surreal/src/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl<C: Connection> Stop<C> {
})
}

pub async fn query_last_n(&self, n: u8) -> Result<Vec<String>, surrealdb::Error> {
pub async fn query_last_n(&self, n: u8) -> Result<Vec<(String, String)>, surrealdb::Error> {
#[derive(Serialize)]
struct Bind {
table_name: String,
Expand All @@ -250,14 +250,21 @@ impl<C: Connection> Stop<C> {

let table_name = format!("{}-msg", self.formatted_timestamp);

#[derive(Deserialize)]
struct ClientInfo {
c_client_name: String,
}

#[derive(Deserialize)]
struct Msg {
message: String,
client_id: ClientInfo,
}

// last_id: 01JH8CBYAKFSRK8Y9HY02QVTDS
// SELECT * FROM (SELECT *, client_id[*] FROM type::thing($table_name, ..=$last_id) ORDER BY id DESC LIMIT $n) ORDER BY id
let query = "SELECT * FROM type::thing($table_name, ..) ORDER BY id DESC LIMIT $n";
let query =
"SELECT *, client_id[*] FROM type::thing($table_name, ..) ORDER BY id DESC LIMIT $n";
let msgs: Vec<Msg> = self
.db
.query(query)
Expand All @@ -267,7 +274,11 @@ impl<C: Connection> Stop<C> {

// clients + disconnects & merge

Ok(msgs.iter().rev().map(|m| m.message.clone()).collect())
Ok(msgs
.iter()
.rev()
.map(|m| (m.message.clone(), m.client_id.c_client_name.clone()))
.collect())
}

pub async fn print(&self) {
Expand Down

0 comments on commit 1a6f80a

Please sign in to comment.