kg-cli 0.2.16

A knowledge graph CLI tool for managing structured information
Documentation
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 std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::Instant;

use anyhow::Result;

#[derive(Debug, Clone)]
pub struct AccessLogEntry {
    pub timestamp: String,
    pub query: String,
    pub results: usize,
    pub duration_ms: u128,
    pub node_get_id: Option<String>,
}

impl AccessLogEntry {
    pub fn new(query: String, results: usize, duration_ms: u128) -> Self {
        Self {
            timestamp: chrono_now(),
            query,
            results,
            duration_ms,
            node_get_id: None,
        }
    }

    pub fn node_get(id: String, duration_ms: u128) -> Self {
        Self {
            timestamp: chrono_now(),
            query: format!("GET {}", id),
            results: 1,
            duration_ms,
            node_get_id: Some(id),
        }
    }

    fn to_line(&self) -> String {
        if let Some(ref id) = self.node_get_id {
            format!(
                "{}\tGET\t{}\t1\t{}ms\n",
                self.timestamp, id, self.duration_ms
            )
        } else {
            format!(
                "{}\tFIND\t{}\t{}\t{}ms\n",
                self.timestamp, self.query, self.results, self.duration_ms
            )
        }
    }
}

fn chrono_now() -> String {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap();
    let secs = now.as_secs();
    let remaining = secs % 86400;
    let hours = remaining / 3600;
    let minutes = (remaining % 3600) / 60;
    let seconds = remaining % 60;
    let millis = now.subsec_millis();

    let days_since_epoch = secs / 86400;
    let (year, month, day) = days_to_date(days_since_epoch as i64);

    format!(
        "{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}",
        year, month, day, hours, minutes, seconds, millis
    )
}

fn days_to_date(days: i64) -> (i64, u32, u32) {
    let mut year = 1970;
    let mut remaining_days = days;

    loop {
        let days_in_year = if is_leap_year(year) { 366 } else { 365 };
        if remaining_days < days_in_year {
            break;
        }
        remaining_days -= days_in_year;
        year += 1;
    }

    let month_days = if is_leap_year(year) {
        [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    } else {
        [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    };

    let mut month = 1;
    for &days_in_month in &month_days {
        if remaining_days < days_in_month as i64 {
            break;
        }
        remaining_days -= days_in_month as i64;
        month += 1;
    }

    (year, month, (remaining_days + 1) as u32)
}

fn is_leap_year(year: i64) -> bool {
    (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}

pub fn access_log_path(graph_path: &Path) -> PathBuf {
    let stem = graph_path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("graph");
    let ext = graph_path
        .extension()
        .and_then(|s| s.to_str())
        .unwrap_or("json");
    crate::cache_paths::cache_root_for_graph(graph_path).join(format!("{stem}.{ext}.access.log"))
}

fn legacy_access_log_path(graph_path: &Path) -> PathBuf {
    let mut path = graph_path.to_path_buf();
    let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("graph");
    let ext = path.extension().and_then(|s| s.to_str()).unwrap_or("json");
    path.set_file_name(format!("{stem}.{ext}.access.log"));
    path
}

fn access_log_fallback_paths(graph_path: &Path) -> Vec<PathBuf> {
    let mut paths = vec![
        access_log_path(graph_path),
        legacy_access_log_path(graph_path),
    ];
    match graph_path.extension().and_then(|ext| ext.to_str()) {
        Some("kg") => {
            paths.push(access_log_path(&graph_path.with_extension("json")));
            paths.push(legacy_access_log_path(&graph_path.with_extension("json")));
        }
        Some("json") => {
            paths.push(access_log_path(&graph_path.with_extension("kg")));
            paths.push(legacy_access_log_path(&graph_path.with_extension("kg")));
        }
        _ => {}
    }
    paths
}

pub fn first_existing_access_log_path(graph_path: &Path) -> Option<PathBuf> {
    access_log_fallback_paths(graph_path)
        .into_iter()
        .find(|path| path.exists())
}

pub fn append_entry(graph_path: &Path, entry: &AccessLogEntry) -> Result<()> {
    let log_path = access_log_path(graph_path);
    if let Some(parent) = log_path.parent() {
        fs::create_dir_all(parent)?;
    }
    let mut file = OpenOptions::new()
        .create(true)
        .append(true)
        .open(&log_path)?;
    file.write_all(entry.to_line().as_bytes())?;
    Ok(())
}

pub fn append_hit(graph_path: &Path, user_short_uid: &str, node_id: &str) -> Result<()> {
    crate::kg_sidecar::append_hit_with_uid(graph_path, user_short_uid, node_id)
}

pub fn read_log(graph_path: &Path, limit: usize, show_empty: bool) -> Result<String> {
    let Some(log_path) = first_existing_access_log_path(graph_path) else {
        return Ok(String::from("= access-log\nempty: no entries yet\n"));
    };

    let content = fs::read_to_string(&log_path)?;
    let mut lines: Vec<&str> = content.lines().collect();

    if !show_empty {
        lines.retain(|l| {
            let parts: Vec<&str> = l.split('\t').collect();
            parts.len() >= 4 && parts[3] != "0"
        });
    }

    lines.reverse();
    let taken: Vec<&str> = lines.iter().take(limit).cloned().collect();

    let mut output = vec![String::from("= access-log")];
    output.push(format!("total_entries: {}", lines.len()));
    output.push(format!("showing: {}", taken.len()));

    if !show_empty {
        output.push(String::from(
            "(filtering: showing only queries with results)",
        ));
    }

    output.push("recent_entries:".to_owned());
    for line in &taken {
        let parts: Vec<&str> = line.split('\t').collect();
        if parts.len() >= 5 {
            let timestamp = parts[0];
            let op = parts[1];
            let query = parts[2];
            let results = parts[3];
            let duration = parts[4];
            output.push(format!(
                "- {} | {} | {} | {} results | {}",
                timestamp, op, query, results, duration
            ));
        }
    }

    Ok(output.join("\n"))
}

pub fn log_stats(graph_path: &Path) -> Result<String> {
    let Some(log_path) = first_existing_access_log_path(graph_path) else {
        return Ok(String::from("= access-stats\nno access log found\n"));
    };

    let content = fs::read_to_string(&log_path)?;
    let lines: Vec<&str> = content.lines().collect();

    if lines.is_empty() {
        return Ok(String::from("= access-stats\nno entries\n"));
    }

    let mut total_finds = 0;
    let mut total_gets = 0;
    let mut empty_finds = 0;
    let mut total_duration_ms: u128 = 0;
    let mut find_queries: std::collections::HashMap<String, usize> =
        std::collections::HashMap::new();

    for line in &lines {
        let parts: Vec<&str> = line.split('\t').collect();
        if parts.len() >= 5 {
            let op = parts[1];
            let results: usize = parts[3].parse().unwrap_or(0);
            let duration: u128 = parts[4].trim_end_matches("ms").parse().unwrap_or(0);
            total_duration_ms += duration;

            if op == "FIND" {
                total_finds += 1;
                let query = parts[2];
                *find_queries.entry(query.to_string()).or_insert(0) += 1;
                if results == 0 {
                    empty_finds += 1;
                }
            } else if op == "GET" {
                total_gets += 1;
            }
        }
    }

    let mut output = vec![String::from("= access-stats")];
    output.push(format!("total_operations: {}", lines.len()));
    output.push(format!("find_operations: {}", total_finds));
    output.push(format!("get_operations: {}", total_gets));
    output.push(format!(
        "empty_queries: {} ({:.1}%)",
        empty_finds,
        if total_finds > 0 {
            (empty_finds as f64 / total_finds as f64) * 100.0
        } else {
            0.0
        }
    ));
    output.push(format!(
        "avg_duration_ms: {:.1}",
        if !lines.is_empty() {
            total_duration_ms as f64 / lines.len() as f64
        } else {
            0.0
        }
    ));

    if !find_queries.is_empty() {
        output.push("top_queries:".to_owned());
        let mut sorted: Vec<_> = find_queries.iter().collect();
        sorted.sort_by(|a, b| b.1.cmp(a.1));
        for (query, count) in sorted.iter().take(10) {
            output.push(format!("- {} ({}x)", query, count));
        }
    }

    Ok(output.join("\n"))
}

pub fn detect_paths(graph_path: &Path, time_window_minutes: usize) -> Result<String> {
    let Some(log_path) = first_existing_access_log_path(graph_path) else {
        return Ok(String::from("= access-paths\nno access log found\n"));
    };

    let content = fs::read_to_string(&log_path)?;
    let lines: Vec<&str> = content.lines().collect();

    if lines.is_empty() {
        return Ok(String::from("= access-paths\nno entries\n"));
    }

    #[derive(Clone)]
    struct LogEntry {
        timestamp: String,
        op: String,
        query: String,
        results: usize,
        duration_ms: u128,
    }

    fn parse_timestamp(ts: &str) -> i64 {
        let parts: Vec<&str> = ts.split(|c| c == ' ' || c == ':' || c == '.').collect();
        if parts.len() >= 7 {
            let year: i64 = parts[0].parse().unwrap_or(0);
            let month: i64 = parts[1].parse().unwrap_or(0);
            let day: i64 = parts[2].parse().unwrap_or(0);
            let hour: i64 = parts[3].parse().unwrap_or(0);
            let min: i64 = parts[4].parse().unwrap_or(0);
            let sec: i64 = parts[5].parse().unwrap_or(0);
            let ms: i64 = parts[6].parse().unwrap_or(0);
            (((((year * 12 + month) * 31 + day) * 24 + hour) * 60 + min) * 60 + sec) * 1000 + ms
        } else {
            0
        }
    }

    fn tokens(query: &str) -> Vec<String> {
        let mut toks: Vec<String> = query
            .to_lowercase()
            .split(|c: char| !c.is_alphanumeric() && c != '_' && c != '-')
            .filter(|s| !s.is_empty())
            .map(|s| s.to_string())
            .collect();
        toks.sort();
        toks.dedup();
        toks
    }

    fn similarity(a: &str, b: &str) -> f64 {
        let ta = tokens(a);
        let tb = tokens(b);
        if ta.is_empty() || tb.is_empty() {
            return 0.0;
        }
        let common: usize = ta.iter().filter(|t| tb.contains(t)).count();
        let total = ta.len() + tb.len();
        if total == 0 {
            return 0.0;
        }
        2.0 * common as f64 / total as f64
    }

    let mut entries: Vec<LogEntry> = Vec::new();
    for line in &lines {
        let parts: Vec<&str> = line.split('\t').collect();
        if parts.len() >= 5 {
            let results: usize = parts[3].parse().unwrap_or(0);
            let duration: u128 = parts[4].trim_end_matches("ms").parse().unwrap_or(0);
            entries.push(LogEntry {
                timestamp: parts[0].to_string(),
                op: parts[1].to_string(),
                query: parts[2].to_string(),
                results,
                duration_ms: duration,
            });
        }
    }

    entries.reverse();

    let time_window_ms = (time_window_minutes as i64) * 60 * 1000;
    let mut paths: Vec<Vec<LogEntry>> = Vec::new();
    let mut current_path: Vec<LogEntry> = Vec::new();
    let mut last_ts: i64 = 0;

    for entry in &entries {
        let ts = parse_timestamp(&entry.timestamp);
        if last_ts == 0 {
            last_ts = ts;
            current_path.push(entry.clone());
        } else if ts - last_ts <= time_window_ms {
            current_path.push(entry.clone());
            last_ts = ts;
        } else {
            if current_path.len() > 1 {
                paths.push(current_path.clone());
            }
            current_path.clear();
            current_path.push(entry.clone());
            last_ts = ts;
        }
    }
    if current_path.len() > 1 {
        paths.push(current_path);
    }

    let mut output = vec![String::from("= access-paths")];
    output.push(format!("time_window: {} minutes", time_window_minutes));
    output.push(format!("detected_paths: {}", paths.len()));

    if paths.is_empty() {
        output.push(String::from("(no sequential query paths found)"));
        return Ok(output.join("\n"));
    }

    output.push(String::from("\npaths:").to_owned());

    for (i, path) in paths.iter().enumerate() {
        output.push(format!("\n--- path {} ({} queries) ---", i + 1, path.len()));

        let mut simplified_path: Vec<String> = Vec::new();
        for (j, entry) in path.iter().enumerate() {
            if j == 0 {
                output.push(format!(
                    "1. {} [{} results, {}]",
                    entry.query, entry.results, entry.duration_ms
                ));
                simplified_path.push(entry.query.clone());
            } else {
                let prev = &simplified_path[j - 1];
                let sim = similarity(prev, &entry.query);
                if sim > 0.3 {
                    output.push(format!(
                        "{} [{} results, {}] (sim: {:.0}%)",
                        entry.query, entry.results, entry.duration_ms, sim * 100.0
                    ));
                } else {
                    output.push(format!(
                        "2. {} [{} results, {}]",
                        entry.query, entry.results, entry.duration_ms
                    ));
                }
                simplified_path.push(entry.query.clone());
            }
        }
    }

    Ok(output.join("\n"))
}

pub struct Timer {
    start: Instant,
}

impl Timer {
    pub fn new() -> Self {
        Self {
            start: Instant::now(),
        }
    }

    pub fn elapsed_ms(&self) -> u128 {
        self.start.elapsed().as_millis()
    }
}