mycommon_utils/log/
push.rs1
2use sea_orm::prelude::DateTime;
3use redis::{FromRedisValue, RedisWrite, ToRedisArgs, Value as RedisValue};
4use serde::{Deserialize, Serialize};
5use crate::database::BigIntPrimaryKey;
6use crate::database::redis_util::RedisAsync;
7use crate::error::{Error, Result};
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
11pub enum MessagePushCategory {
12 LOG(SysOpeLogCache)
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize, Default)]
16pub struct SysOpeLogCache {
17 pub store_id: Option<BigIntPrimaryKey>,
18 pub title: Option<String>,
19 pub business_type: Option<i32>,
20 pub method: Option<String>,
21 pub request_method: Option<String>,
22 pub operator_type: Option<i32>,
23 pub oper_name: Option<String>,
24 pub dept_name: Option<String>,
25 pub oper_url: Option<String>,
26 pub oper_ip: Option<String>,
27 pub oper_location: Option<String>,
28 pub oper_param: Option<String>,
29 pub json_result: Option<String>,
30 pub status: Option<i32>,
31 pub error_msg: Option<String>,
32 pub oper_time: Option<DateTime>,
33 pub cost_time: Option<i64>,
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct MessageRedisCache {
38 pub content: MessagePushCategory,
39}
40
41impl ToRedisArgs for MessageRedisCache {
42 fn write_redis_args<W>(&self, out: &mut W) where W: ?Sized + RedisWrite,
43 {
44 let result = serde_json::to_string(self);
45 if let Ok(result) = result {
46 out.write_arg(result.as_bytes())
47 }else {
48 tracing::error!("TemplateMessageRedisCache::write_redis_args() failed");
49 }
50 }
51}
52impl FromRedisValue for MessageRedisCache {
53 fn from_redis_value(v: RedisValue) -> std::result::Result<Self, redis::ParsingError> {
54 match v {
55 RedisValue::BulkString(bytes) => {
56 let data = String::from_utf8(bytes.to_vec())?;
57 serde_json::from_str::<MessageRedisCache>(&data)
58 .map_err(|_| redis::ParsingError::from("JSON parse error"))
59 }
60 _ => Err(redis::ParsingError::from("Unexpected value type")),
61 }
62 }
63}
64
65
66#[allow(dead_code)]
67#[derive(Default,Clone)]
68pub struct MessagePushStream{
69 pub cache_name :String,
70}
71
72#[allow(dead_code)]
73impl MessagePushStream {
74 pub fn new(cache_name: String) -> Self {
75 MessagePushStream{
76 cache_name,
77 }
78 }
79 fn keys(&self) ->String {
81 format!("{}",self.cache_name)
82 }
83
84 pub async fn publish(&self, item: MessageRedisCache) -> Result<String> {
86 let queue_name = self.keys();
87 RedisAsync::xadd_maxlen(&queue_name, 100000, "message", &item).await
88 .map_err(|e| {
89 tracing::error!("Failed to publish message: {:?}", e);
90 Error::ErrorRedisConnectError()
91 })
92 }
93
94}