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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use crate::error::Result;
use crate::script::{IntoScriptName, ScriptInfo, ScriptName};
use crate::tag::{Tag, TagFilterGroup};
use crate::Either;
use chrono::{Duration, Utc};
use fxhash::FxHashMap as HashMap;
use hyper_scripter_historian::{Event, EventData, Historian};
use sqlx::SqlitePool;
use std::collections::hash_map::Entry::{self, *};
pub mod helper;
pub use helper::RepoEntry;
use helper::*;
#[derive(Clone, Debug)]
pub struct RecentFilter {
pub recent: u32,
pub archaeology: bool,
}
#[derive(Debug)]
enum TraceOption {
Normal,
NoTrace,
Humble,
}
#[derive(Debug)]
pub struct DBEnv {
trace_opt: TraceOption,
info_pool: SqlitePool,
historian: Historian,
modifies_script: bool,
}
pub struct RepoEntryOptional<'b> {
entry: Entry<'b, String, ScriptInfo>,
env: &'b DBEnv,
}
impl<'b> RepoEntryOptional<'b> {
pub fn into_either(self) -> Either<RepoEntry<'b>, Self> {
match self.entry {
Occupied(entry) => Either::One(RepoEntry::new(entry.into_mut(), self.env)),
_ => Either::Two(self),
}
}
pub async fn or_insert(self, info: ScriptInfo) -> Result<RepoEntry<'b>> {
let exist = matches!(&self.entry, Occupied(_));
let info = self.entry.or_insert(info);
if !exist {
log::debug!("往資料庫塞新腳本 {:?}", info);
let id = self.env.handle_insert(info).await?;
log::debug!("往資料庫新增腳本成功,得 id = {}", id);
info.set_id(id as i64);
}
Ok(RepoEntry::new(info, self.env))
}
}
impl DBEnv {
pub async fn handle_neglect(&self, id: i64) -> Result {
let time = Utc::now().naive_utc();
sqlx::query!(
"UPDATE last_events SET neglect = ? WHERE script_id = ?",
time,
id
)
.execute(&self.info_pool)
.await?;
Ok(())
}
async fn update_last_time(&self, info: &ScriptInfo) -> Result {
if !matches!(self.trace_opt, TraceOption::Normal) {
return Ok(());
}
let last_time = info.last_time();
let exec_time = info.exec_time.as_ref().map(|t| **t);
let exec_done_time = info.exec_done_time.as_ref().map(|t| **t);
let neglect_time = info.neglect_time.as_ref().map(|t| **t);
let miss_time = info.miss_time.as_ref().map(|t| **t);
let exec_count = info.exec_count as i32;
sqlx::query!(
"
INSERT OR REPLACE INTO last_events
(script_id, last_time, read, write, miss, exec, exec_done, neglect, exec_count)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)
",
info.id,
last_time,
*info.read_time,
*info.write_time,
miss_time,
exec_time,
exec_done_time,
neglect_time,
exec_count
)
.execute(&self.info_pool)
.await?;
Ok(())
}
async fn handle_delete(&self, id: i64) -> Result {
assert!(self.modifies_script);
self.historian.remove(id).await?;
log::debug!("清理腳本 {:?} 的最新事件", id);
sqlx::query!("DELETE FROM last_events WHERE script_id = ?", id)
.execute(&self.info_pool)
.await?;
sqlx::query!("DELETE from script_infos where id = ?", id)
.execute(&self.info_pool)
.await?;
Ok(())
}
async fn handle_insert(&self, info: &ScriptInfo) -> Result<i64> {
assert!(self.modifies_script);
let name_cow = info.name.key();
let name = name_cow.as_ref();
let ty = info.ty.as_ref();
let tags = join_tags(info.tags.iter());
sqlx::query!(
"
INSERT INTO script_infos (name, ty, tags)
VALUES(?, ?, ?)
",
name,
ty,
tags,
)
.execute(&self.info_pool)
.await?;
let id = sqlx::query!("SELECT last_insert_rowid() as id")
.fetch_one(&self.info_pool)
.await?
.id;
Ok(id as i64)
}
async fn handle_change(&self, info: &ScriptInfo) -> Result<i64> {
log::debug!("開始修改資料庫 {:?}", info);
if info.changed {
assert!(self.modifies_script);
let name = info.name.key();
let name = name.as_ref();
let tags = join_tags(info.tags.iter());
let ty = info.ty.as_ref();
sqlx::query!(
"UPDATE script_infos SET name = ?, tags = ?, ty = ? where id = ?",
name,
tags,
ty,
info.id,
)
.execute(&self.info_pool)
.await?;
}
if matches!(self.trace_opt, TraceOption::NoTrace) {
return Ok(0);
}
let mut last_event_id = 0;
macro_rules! record_event {
($time:expr, $data:expr) => {
self.historian.record(&Event {
script_id: info.id,
humble: matches!(self.trace_opt, TraceOption::Humble),
time: $time,
data: $data,
})
};
}
if let Some(time) = info.exec_done_time.as_ref() {
if let Some(&(code, main_event_id)) = time.data() {
log::debug!("{:?} 的執行完畢事件", info.name);
last_event_id = record_event!(
**time,
EventData::ExecDone {
code,
main_event_id,
}
)
.await?;
if last_event_id != 0 {
self.update_last_time(info).await?;
} else {
log::info!("{:?} 的執行完畢事件被忽略了", info.name);
}
return Ok(last_event_id);
}
}
self.update_last_time(info).await?;
if info.read_time.has_changed() {
log::debug!("{:?} 的讀取事件", info.name);
last_event_id = record_event!(*info.read_time, EventData::Read).await?;
}
if info.write_time.has_changed() {
log::debug!("{:?} 的寫入事件", info.name);
last_event_id = record_event!(*info.write_time, EventData::Write).await?;
}
if let Some(time) = info.miss_time.as_ref() {
if time.has_changed() {
log::debug!("{:?} 的錯過事件", info.name);
last_event_id = record_event!(**time, EventData::Miss).await?;
}
}
if let Some(time) = info.exec_time.as_ref() {
if let Some((content, args, dir)) = time.data() {
log::debug!("{:?} 的執行事件", info.name);
last_event_id = record_event!(
**time,
EventData::Exec {
content,
args,
dir: dir.as_deref(),
}
)
.await?;
}
}
Ok(last_event_id)
}
}
fn join_tags<'a, I: Iterator<Item = &'a Tag>>(tags: I) -> String {
let tags_arr: Vec<&str> = tags.map(|t| t.as_ref()).collect();
tags_arr.join(",")
}
#[derive(Debug)]
pub struct ScriptRepo {
map: HashMap<String, ScriptInfo>,
hidden_map: HashMap<String, ScriptInfo>,
latest_name: Option<String>,
db_env: DBEnv,
}
impl ScriptRepo {
pub fn iter(&self) -> impl Iterator<Item = &ScriptInfo> {
self.map.iter().map(|(_, info)| info)
}
pub fn iter_mut(&mut self, all: bool) -> Iter<'_> {
Iter {
iter: self.map.iter_mut(),
env: &self.db_env,
iter2: if all {
Some(self.hidden_map.iter_mut())
} else {
None
},
}
}
pub fn iter_hidden_mut(&mut self) -> Iter<'_> {
Iter {
iter: self.hidden_map.iter_mut(),
iter2: None,
env: &self.db_env,
}
}
pub fn historian(&self) -> &Historian {
&self.db_env.historian
}
pub async fn new(
pool: SqlitePool,
recent: Option<RecentFilter>,
historian: Historian,
modifies_script: bool,
) -> Result<ScriptRepo> {
let mut hidden_map = HashMap::<String, ScriptInfo>::default();
let time_bound = recent.map(|r| {
let mut time = Utc::now().naive_utc();
time -= Duration::days(r.recent.into());
(time, r.archaeology)
});
let scripts = sqlx::query!(
"SELECT * FROM script_infos si LEFT JOIN last_events le ON si.id = le.script_id"
)
.fetch_all(&pool)
.await?;
let mut map: HashMap<String, ScriptInfo> = Default::default();
for record in scripts.into_iter() {
let name = record.name;
log::trace!("載入腳本:{} {} {}", name, record.ty, record.tags);
let script_name = name.clone().into_script_name()?;
let mut builder = ScriptInfo::builder(
record.id,
script_name,
record.ty.into(),
record.tags.split(',').filter_map(|s| {
if s.is_empty() {
None
} else {
s.parse().ok()
}
}),
);
builder.created_time(record.created_time);
builder.exec_count(record.exec_count as u64);
if let Some(time) = record.write {
builder.write_time(time);
}
if let Some(time) = record.read {
builder.read_time(time);
}
if let Some(time) = record.miss {
builder.miss_time(time);
}
if let Some(time) = record.exec {
builder.exec_time(time);
}
if let Some(time) = record.exec_done {
builder.exec_done_time(time);
}
if let Some(time) = record.neglect {
builder.neglect_time(time);
}
let script = builder.build();
let hide_by_time = if let Some((mut time_bound, archaeology)) = time_bound {
if let Some(neglect) = record.neglect {
log::debug!("腳本 {} 曾於 {} 被忽略", script.name, neglect);
time_bound = std::cmp::max(neglect, time_bound);
}
let overtime = time_bound > script.last_major_time();
archaeology ^ overtime
} else {
false
};
if hide_by_time {
hidden_map.insert(name, script);
} else {
map.insert(name, script);
}
}
Ok(ScriptRepo {
map,
hidden_map,
latest_name: None,
db_env: DBEnv {
trace_opt: TraceOption::Normal,
info_pool: pool,
historian,
modifies_script,
},
})
}
pub fn no_trace(&mut self) {
self.db_env.trace_opt = TraceOption::NoTrace;
}
pub fn humble(&mut self) {
self.db_env.trace_opt = TraceOption::Humble;
}
pub fn latest_mut(&mut self, n: usize, all: bool) -> Option<RepoEntry<'_>> {
let mut v: Vec<_> = if all {
self.map
.iter_mut()
.chain(self.hidden_map.iter_mut())
.map(|(_, s)| s)
.collect()
} else {
self.map.iter_mut().map(|(_, s)| s).collect()
};
v.sort_by_key(|s| s.last_time());
if v.len() >= n {
let t = unsafe { std::ptr::read(&v[v.len() - n]) };
Some(RepoEntry::new(t, &self.db_env))
} else {
None
}
}
pub fn get_mut(&mut self, name: &ScriptName, all: bool) -> Option<RepoEntry<'_>> {
let map = &mut self.map as *mut HashMap<String, ScriptInfo>;
let map = unsafe { &mut *map };
match (all, map.get_mut(&*name.key())) {
(false, None) => None,
(true, None) => self.get_hidden_mut(name),
(_, Some(info)) => Some(RepoEntry::new(info, &self.db_env)),
}
}
pub fn get_hidden_mut(&mut self, name: &ScriptName) -> Option<RepoEntry<'_>> {
let db_env = &self.db_env;
self.hidden_map
.get_mut(&*name.key())
.map(|info| RepoEntry::new(info, db_env))
}
pub fn get_mut_by_id(&mut self, id: i64) -> Option<RepoEntry<'_>> {
self.iter_mut(true).find(|e| e.id == id)
}
pub async fn remove(&mut self, id: i64) -> Result {
log::debug!("從資料庫刪除腳本 {:?}", id);
self.db_env.handle_delete(id).await?;
Ok(())
}
pub fn entry(&mut self, name: &ScriptName) -> RepoEntryOptional<'_> {
let entry = self.map.entry(name.key().into_owned());
RepoEntryOptional {
entry,
env: &self.db_env,
}
}
pub fn filter_by_tag(&mut self, filter: &TagFilterGroup) {
log::debug!("根據標籤 {:?} 進行篩選", filter);
let drain = self.map.drain();
let mut map = HashMap::default();
for (key, info) in drain {
let tags_arr: Vec<_> = info.tags.iter().collect();
if filter.filter(&tags_arr) {
log::trace!("腳本 {:?} 通過篩選", info.name);
map.insert(key, info);
} else {
log::trace!("掰掰,{:?}", info.name);
self.hidden_map.insert(key, info);
}
}
self.map = map;
}
}