-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
186 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,19 @@ | ||
use chrono::NaiveDateTime; | ||
use sea_orm::entity::prelude::*; | ||
use sea_orm::{ActiveModelBehavior, DeriveEntityModel, DeriveRelation, EnumIter}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)] | ||
#[sea_orm(table_name = "access_log")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: u64, | ||
pub short_id: String, | ||
pub req_headers: String, | ||
pub create_time: NaiveDateTime, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains 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 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod access_log; | ||
pub mod link; |
This file contains 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,58 @@ | ||
use crate::models::access_log; | ||
use log::error; | ||
use sea_orm::{ActiveModelTrait, DatabaseBackend, DbConn, DbErr, FromQueryResult, Set, Statement}; | ||
use std::collections::HashMap; | ||
|
||
pub struct AccessLogService; | ||
|
||
impl AccessLogService { | ||
// 添加 | ||
pub async fn add( | ||
db: &DbConn, | ||
data: access_log::Model, | ||
) -> Result<access_log::ActiveModel, DbErr> { | ||
access_log::ActiveModel { | ||
short_id: Set(data.short_id.to_owned()), | ||
req_headers: Set(data.req_headers.to_owned()), | ||
..Default::default() | ||
} | ||
.save(db) | ||
.await | ||
} | ||
|
||
pub async fn batch_query_pv(db: &DbConn, short_ids: Vec<String>) -> HashMap<String, i64> { | ||
#[derive(Debug, FromQueryResult)] | ||
struct GroupByResult { | ||
short_id: String, | ||
total: i64, | ||
} | ||
|
||
let mut pv_map: HashMap<String, i64> = HashMap::new(); | ||
if short_ids.is_empty() { | ||
return pv_map; | ||
} | ||
let id_string = short_ids | ||
.iter() | ||
.map(|s| format!("'{}'", s)) | ||
.collect::<Vec<_>>() | ||
.join(","); | ||
|
||
let result = GroupByResult::find_by_statement(Statement::from_sql_and_values( | ||
DatabaseBackend::MySql, | ||
format!("SELECT short_id, COUNT(1) AS total FROM `access_log` WHERE `short_id` IN ({}) GROUP BY `short_id`", id_string), | ||
[], | ||
)) | ||
.all(db) | ||
.await; | ||
|
||
if result.is_err() { | ||
error!("batch_query_pv error: {:?}", result.err()); | ||
return pv_map; | ||
} | ||
|
||
for row in result.unwrap() { | ||
pv_map.insert(row.short_id, row.total); | ||
} | ||
pv_map | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod access_log; | ||
pub mod link; |
This file contains 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 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