Add global, conf stat.

This commit is contained in:
Alexandr Bogomyakov
2022-11-23 17:53:56 +02:00
parent 55a7dd6a67
commit 2fb5b03fbf
3 changed files with 153 additions and 6 deletions

View File

@ -32,6 +32,12 @@ pub struct Here {
pub struct Top {
pub data: String,
}
pub struct ConfTop {
pub data: String,
}
pub struct GlobalTop {
pub data: String,
}
pub struct MarkovAll {
pub data: String,
}
@ -347,6 +353,84 @@ impl Execute for Top {
}
}
#[async_trait]
impl Execute for GlobalTop {
async fn exec(&self, api: &Api, message: &Message) -> Result<(), Error> {
let top = db::get_global_top().await?;
let mut msg = "<b>Global top words:</b>\n<pre>".to_string();
let mut counter = 1;
for word in top.iter() {
msg = format!(
"{} <b>{}</b> {} - {}\n",
msg, counter, word.word, word.count
);
counter += 1;
}
msg = format!("{}{}", msg, "</pre>");
match api
.send(message.text_reply(msg).parse_mode(ParseMode::Html))
.await
{
Ok(_) => debug!("/global_top command sent to {}", message.chat.id()),
Err(_) => warn!("/global_top command sent failed to {}", message.chat.id()),
}
Ok(())
}
async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> {
unimplemented!()
}
#[allow(unused_variables)]
async fn exec_mystem(
&self,
api: &Api,
message: &Message,
mystem: &mut MyStem,
) -> Result<(), Error> {
unimplemented!()
}
}
#[async_trait]
impl Execute for ConfTop {
async fn exec(&self, api: &Api, message: &Message) -> Result<(), Error> {
let top = db::get_conf_top(&message).await?;
let mut msg = "<b>Conf top words:</b>\n<pre>".to_string();
let mut counter = 1;
for word in top.iter() {
msg = format!(
"{} <b>{}</b> {} - {}\n",
msg, counter, word.word, word.count
);
counter += 1;
}
msg = format!("{}{}", msg, "</pre>");
match api
.send(message.text_reply(msg).parse_mode(ParseMode::Html))
.await
{
Ok(_) => debug!("/conf_top command sent to {}", message.chat.id()),
Err(_) => warn!("/conf_top command sent failed to {}", message.chat.id()),
}
Ok(())
}
async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> {
unimplemented!()
}
#[allow(unused_variables)]
async fn exec_mystem(
&self,
api: &Api,
message: &Message,
mystem: &mut MyStem,
) -> Result<(), Error> {
unimplemented!()
}
}
#[async_trait]
impl Execute for MarkovAll {
async fn exec(&self, api: &Api, message: &Message) -> Result<(), Error> {
@ -676,7 +760,7 @@ impl Execute for Code {
.collect();
let theme = if theme.len() != 1 {
ts.themes.get("Dracula").unwrap()
ts.themes.get("gruvbox").unwrap()
} else {
theme[0]
};

View File

@ -512,3 +512,56 @@ pub(crate) async fn get_top(
}
Ok(top)
}
pub(crate) async fn get_global_top() -> Result<Vec<TopWord>, errors::Error> {
let conn = open()?;
let mut stmt = conn.prepare_cached(
"
SELECT w.word, COUNT(*) as count FROM relations r
LEFT JOIN word w ON w.id = r.word_id
GROUP BY w.word
ORDER BY count DESC
LIMIT 50
",
)?;
let mut rows = stmt.query_named(named_params! {})?;
let mut top = Vec::new();
while let Some(row) = rows.next()? {
top.push(TopWord {
word: row.get(0)?,
count: row.get(1)?,
})
}
Ok(top)
}
pub(crate) async fn get_conf_top(
message: &telegram_bot::Message,
) -> Result<Vec<TopWord>, errors::Error> {
let conf_id = i64::from(message.chat.id());
let conn = open()?;
let mut stmt = conn.prepare_cached(
"
SELECT w.word, COUNT(*) as count FROM relations r
LEFT JOIN word w ON w.id = r.word_id
WHERE r.conf_id = :conf_id
GROUP BY w.word
ORDER BY count DESC
LIMIT 10
",
)?;
let mut rows = stmt.query_named(named_params! {":conf_id": conf_id})?;
let mut top = Vec::new();
while let Some(row) = rows.next()? {
top.push(TopWord {
word: row.get(0)?,
count: row.get(1)?,
})
}
Ok(top)
}

View File

@ -1,5 +1,7 @@
//use crate::commands::Command;
use crate::commands::{Code, Execute, Here, Markov, MarkovAll, Omedeto, Scheme, Sql, Top};
use crate::commands::{
Code, ConfTop, Execute, GlobalTop, Here, Markov, MarkovAll, Omedeto, Scheme, Sql, Top,
};
use crate::db;
use crate::errors;
use crate::utils;
@ -77,6 +79,7 @@ pub async fn handler(
|| s.contains("/хере")
|| s.contains("@хере")
|| s.contains("@all")
|| s.contains("\"руку")
|| s.contains("\"хере") =>
{
db::add_sentence(&message, mystem).await?;
@ -108,15 +111,22 @@ pub async fn handler(
.await?;
}
},
"/top" => {
"/top" | "/stat" => {
Top {
data: "".to_string(),
}
.exec(&api, &message)
.await?
}
"/stat" => {
Top {
"/global_top" | "/global_stat" => {
GlobalTop {
data: "".to_string(),
}
.exec(&api, &message)
.await?
}
"/conf_stat" | "/conf_top" => {
ConfTop {
data: "".to_string(),
}
.exec(&api, &message)
@ -136,7 +146,7 @@ pub async fn handler(
.exec(&api, &message)
.await?
}
s if s =="/scheme" || s == "/schema" => {
s if s == "/scheme" || s == "/schema" => {
Scheme {
data: "".to_string(),
}