Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ itertools = "0.13.0"
chrono = "0.4.38"
tracing-subscriber = "0.3.19"
tower-http = { version = "0.6.2", features = ["trace"] }
probability = "0.20.3"
61 changes: 61 additions & 0 deletions migrations/20241122221757_init_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,67 @@ create table if not exists item (
, created_at integer not null default (unixepoch('subsec') * 1000)
) strict;

create table lineage (
ancestor_id integer not null references item(item_id)
, descendant_id integer not null references item(item_id)
, separation integer not null
, primary key(ancestor_id, descendant_id)
) strict;

create trigger after_insert_item
after insert on item when new.parent_id is not null
begin
-- Insert a lineage record for parent
insert into lineage (
ancestor_id
, descendant_id
, separation
)
values(
new.parent_id
, new.item_id
, 1
)
on conflict do nothing;

-- Insert a lineage record for all ancestors of this parent
insert into lineage
select
ancestor_id
, new.item_id as descendant_id
, 1 + separation as separation
from lineage ancestor
where ancestor.descendant_id = new.parent_id;

-- It is possible that this item is inserted *after* some of its children
-- In that case, the ancestry for those children (and their children) will
-- be incomplete.

-- Insert a lineage record for all children joined to all ancestors
insert into lineage
select
ancestor.ancestor_id
, child.item_id descendant_id
, ancestor.separation + 1 as separation
from item child
join lineage ancestor
on ancestor.descendant_id = new.item_id
where child.parent_id = new.item_id;

-- Insert a lineage record for all descendants joined to all ancestors
insert into Lineage
select
ancestor.ancestor_id
, descendant.descendant_id
, ancestor.separation + descendant.separation + 1 as separation
from item child
join Lineage descendant
on descendant.ancestor_id = child.item_id
join Lineage ancestor
on ancestor.descendant_id = new.item_id
where child.parent_id = new.item_id;
end;

create table if not exists vote_event (
vote_event_id integer not null primary key autoincrement
, item_id integer not null references item(item_id)
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod database;
mod http_server;
mod scheduler;
mod util;
mod probability;

#[tokio::main]
async fn main() -> Result<(), common::error::AppError> {
Expand Down
1 change: 1 addition & 0 deletions src/probability/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod probability_models;
24 changes: 24 additions & 0 deletions src/probability/probability_models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use probability::prelude::*;

#[allow(dead_code)]
#[derive(Debug)]
struct Tally {
upvotes: i32,
total_votes: i32,
}

#[allow(dead_code)]
trait Update<T> {
type Output: Update<T>;
fn update(&self, new_data: &T) -> Self::Output;
}

impl Update<Tally> for Beta {
type Output = Beta;
fn update(&self, new_data: &Tally) -> Beta {
let new_upvotes = new_data.upvotes as f64;
let new_downvotes = (new_data.total_votes - new_data.upvotes) as f64;
Beta::new(self.alpha() + new_upvotes, self.beta() + new_downvotes, self.a(), self.b())
}
}