Skip to content

Commit

Permalink
Refactor rank_vec sorting in home_page function and add secondary sor…
Browse files Browse the repository at this point in the history
…ting by unique visitors in statistics_model
  • Loading branch information
naiba committed Dec 31, 2023
1 parent 5431c1d commit e4850e2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/app_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub async fn home_page(
let uv_read = ctx.unique_visitor.read().await;

let mut level: HashMap<i64, i64> = HashMap::new();
let mut rank_vec: Vec<(i64, NaiveDateTime)> = Vec::new();
let mut rank_vec: Vec<(i64, NaiveDateTime, i64)> = Vec::new();

for k in ctx.id2member.keys() {
let uv = uv_read
Expand All @@ -161,12 +161,15 @@ pub async fn home_page(
.unwrap_or(&(0, NaiveDateTime::from_timestamp(0, 0)))
.to_owned();
if uv.0 > 0 || rv.0 > 0 {
rank_vec.push((k.to_owned(), rv.1));
rank_vec.push((k.to_owned(), rv.1, uv.0));
level.insert(k.to_owned(), ctx.get_tend_from_uv_and_rv(uv.0, rv.0).await);
}
}

rank_vec.sort_by(|a, b| b.1.cmp(&a.1));
rank_vec.sort_by(|a, b| match b.1.cmp(&a.1) {
std::cmp::Ordering::Equal => b.2.cmp(&a.2),
_ => b.1.cmp(&a.1),
});

let mut membership = Vec::new();
for v in rank_vec {
Expand Down
3 changes: 2 additions & 1 deletion src/statistics_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ impl Statistics {
))
.filter(created_at.between(start, end))
.group_by(membership_id)
.order(sql::<diesel::sql_types::BigInt>("s_referrer DESC"))
.order_by(sql::<diesel::sql_types::BigInt>("s_referrer DESC"))
.then_order_by(sql::<diesel::sql_types::BigInt>("s_unique_visitor DESC"))
.load::<(i64, NaiveDateTime, i64, i64)>(&mut conn);

let updated_at_list = statistics
Expand Down
4 changes: 2 additions & 2 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ <h2 class="font-size-18 text-center">即将移除</h2>
<table class="table">
<thead>
<tr>
<th scope="col">编号</th>
<th scope="col">ID</th>
<th scope="col">站点</th>
<th scope="col">UV
<p class="d-inline font-size-12 m-0">独立访客</p>
Expand All @@ -99,7 +99,7 @@ <h2 class="font-size-18 text-center">即将移除</h2>
<tr>
<th scope="row">
<del>
{{ loop.index }}
{{ r.membership.id }}
</del>
</th>
<td>
Expand Down
4 changes: 2 additions & 2 deletions templates/rank.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h2 class="font-size-18 text-center">即将移除</h2>
<table class="table">
<thead>
<tr>
<th scope="col">编号</th>
<th scope="col">ID</th>
<th scope="col">站点</th>
<th scope="col">UV
<p class="d-inline font-size-12 m-0">独立访客</p>
Expand All @@ -67,7 +67,7 @@ <h2 class="font-size-18 text-center">即将移除</h2>
<tr>
<th scope="row">
<del>
{{ loop.index }}
{{ r.membership.id }}
</del>
</th>
<td>
Expand Down

0 comments on commit e4850e2

Please sign in to comment.