1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use futures::stream::StreamExt;
use mongodb::{
bson, bson::document::Document, options::UpdateModifications, Client, Collection, Cursor,
Database,
};
use regex::Regex;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct ModelState {
pub database: String,
pub collection: String,
pub fields: Vec<String>,
pub status: bool,
}
pub struct Monitor<'a> {
pub keyword: &'a str,
pub client: &'a Client,
}
impl<'a> Monitor<'a> {
pub async fn refresh(&self) {
let re = Regex::new(r"^[_a-zA-Z\d]{8,16}$").unwrap();
if !re.is_match(self.keyword) {
panic!("Keyword - Valid characters: _|a-z|A-Z|0-9 ; Size: 8-16.");
}
let mango_orm_keyword: String = format!("mango_orm_{}", self.keyword);
let collection_name: &'static str = "models";
let database_names: Vec<String> =
self.client.list_database_names(None, None).await.unwrap();
if !database_names.contains(&mango_orm_keyword) {
self.client
.database(&mango_orm_keyword)
.create_collection(collection_name, None)
.await
.unwrap();
} else {
let mango_orm_db: Database = self.client.database(&mango_orm_keyword);
let mango_orm_collection: Collection = mango_orm_db.collection(collection_name);
let mut cursor: Cursor = mango_orm_collection.find(None, None).await.unwrap();
while let Some(result) = cursor.next().await {
match result {
Ok(document) => {
let mut model_state: ModelState =
bson::de::from_document(document).unwrap();
model_state.status = false;
let query: Document = bson::doc! {
"database": &model_state.database,
"collection": &model_state.collection
};
let update: UpdateModifications = UpdateModifications::Document(
bson::ser::to_document(&model_state).unwrap(),
);
mango_orm_collection
.update_one(query, update, None)
.await
.unwrap();
}
Err(err) => panic!("{}", err),
}
}
}
}
pub async fn napalm(&self) {
let mango_orm_keyword: String = format!("mango_orm_{}", self.keyword);
let collection_name: &'static str = "models";
let mango_orm_db: Database = self.client.database(&mango_orm_keyword);
let mango_orm_collection: Collection = mango_orm_db.collection(collection_name);
let cursor: Cursor = mango_orm_collection.find(None, None).await.unwrap();
let results: Vec<Result<Document, mongodb::error::Error>> = cursor.collect().await;
for result in results {
match result {
Ok(document) => {
let model_state: ModelState = bson::de::from_document(document).unwrap();
if !model_state.status {
self.client
.database(&model_state.database)
.collection(&model_state.collection)
.drop(None)
.await
.unwrap();
let query: Document = bson::doc! {
"database": &model_state.database,
"collection": &model_state.collection
};
mango_orm_collection.delete_one(query, None).await.unwrap();
}
}
Err(err) => panic!("{}", err),
}
}
}
}
#[cfg(test)]
mod tests {
}