-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add heartbeat notification. #45
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,127 @@ | ||
use anyhow::Result; | ||
use uuid::Uuid; | ||
|
||
pub struct HeartbeatRepository<'a> { | ||
conn: &'a libsql::Connection, | ||
} | ||
|
||
impl<'a> HeartbeatRepository<'a> { | ||
pub fn new(conn: &'a libsql::Connection) -> Self { | ||
Self { conn } | ||
} | ||
|
||
/// Creates a new heartbeat notification record | ||
pub async fn create_notification(&self, pubkey: &str) -> Result<String> { | ||
let notification_id = Uuid::new_v4().to_string(); | ||
|
||
self.conn | ||
.execute( | ||
"INSERT INTO heartbeat_notifications (pubkey, notification_id, status) VALUES (?, ?, 'pending')", | ||
libsql::params![pubkey, notification_id.clone()], | ||
) | ||
.await?; | ||
|
||
Ok(notification_id) | ||
} | ||
|
||
/// Marks a heartbeat notification as responded | ||
pub async fn mark_as_responded(&self, notification_id: &str) -> Result<bool> { | ||
let result = self.conn | ||
.execute( | ||
"UPDATE heartbeat_notifications SET responded_at = CURRENT_TIMESTAMP, status = 'responded' WHERE notification_id = ? AND status = 'pending'", | ||
libsql::params![notification_id], | ||
) | ||
.await?; | ||
|
||
Ok(result > 0) | ||
} | ||
|
||
/// Counts consecutive missed heartbeats for a user (most recent first) | ||
pub async fn count_consecutive_missed(&self, pubkey: &str) -> Result<i32> { | ||
let mut rows = self.conn | ||
.query( | ||
"SELECT status FROM heartbeat_notifications WHERE pubkey = ? ORDER BY sent_at DESC LIMIT 10", | ||
libsql::params![pubkey], | ||
) | ||
.await?; | ||
|
||
let mut consecutive_missed = 0; | ||
while let Some(row) = rows.next().await? { | ||
let status: String = row.get(0)?; | ||
if status == "pending" { | ||
consecutive_missed += 1; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
Ok(consecutive_missed) | ||
} | ||
niteshbalusu11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Gets all users who have push tokens (active users) | ||
pub async fn get_active_users(&self) -> Result<Vec<String>> { | ||
let mut rows = self.conn | ||
.query( | ||
"SELECT DISTINCT pt.pubkey FROM push_tokens pt INNER JOIN users u ON pt.pubkey = u.pubkey", | ||
(), | ||
) | ||
.await?; | ||
|
||
let mut pubkeys = Vec::new(); | ||
while let Some(row) = rows.next().await? { | ||
let pubkey: String = row.get(0)?; | ||
pubkeys.push(pubkey); | ||
} | ||
|
||
Ok(pubkeys) | ||
} | ||
|
||
/// Cleans up old heartbeat notifications (keeps only last 15 per user) | ||
pub async fn cleanup_old_notifications(&self) -> Result<()> { | ||
self.conn | ||
.execute( | ||
"DELETE FROM heartbeat_notifications WHERE id NOT IN ( | ||
SELECT id FROM ( | ||
SELECT id, ROW_NUMBER() OVER (PARTITION BY pubkey ORDER BY sent_at DESC) as rn | ||
FROM heartbeat_notifications | ||
) ranked WHERE rn <= 15 | ||
)", | ||
(), | ||
) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Gets users who have missed 10 or more consecutive heartbeats | ||
pub async fn get_users_to_deregister(&self) -> Result<Vec<String>> { | ||
let mut rows = self | ||
.conn | ||
.query( | ||
"WITH recent_heartbeats AS ( | ||
SELECT pubkey, status, sent_at, | ||
ROW_NUMBER() OVER (PARTITION BY pubkey ORDER BY sent_at DESC) as rn | ||
FROM heartbeat_notifications | ||
), | ||
consecutive_missed AS ( | ||
SELECT pubkey, | ||
COUNT(*) as missed_count | ||
FROM recent_heartbeats | ||
WHERE rn <= 10 AND status = 'pending' | ||
GROUP BY pubkey | ||
HAVING COUNT(*) = 10 | ||
) | ||
SELECT pubkey FROM consecutive_missed", | ||
(), | ||
) | ||
.await?; | ||
|
||
let mut pubkeys = Vec::new(); | ||
while let Some(row) = rows.next().await? { | ||
let pubkey: String = row.get(0)?; | ||
pubkeys.push(pubkey); | ||
} | ||
|
||
Ok(pubkeys) | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.