radicle_cli/commands/
stats.rs1use std::ffi::OsString;
2use std::path::Path;
3
4use localtime::LocalDuration;
5use localtime::LocalTime;
6use radicle::git;
7use radicle::issue::cache::Issues as _;
8use radicle::node::address;
9use radicle::node::routing;
10use radicle::patch::cache::Patches as _;
11use radicle::storage::{ReadRepository, ReadStorage, WriteRepository};
12use radicle_term::Element;
13use serde::Serialize;
14
15use crate::terminal as term;
16use crate::terminal::args::{Args, Error, Help};
17
18pub const HELP: Help = Help {
19 name: "stats",
20 description: "Displays aggregated repository and node metrics",
21 version: env!("RADICLE_VERSION"),
22 usage: r#"
23Usage
24
25 rad stats [<option>...]
26
27Options
28
29 --help Print help
30"#,
31};
32
33#[derive(Default, Serialize)]
34#[serde(rename_all = "camelCase")]
35struct NodeStats {
36 all: usize,
37 public_daily: usize,
38 online_daily: usize,
39 online_weekly: usize,
40 seeding_weekly: usize,
41}
42
43#[derive(Default, Serialize)]
44#[serde(rename_all = "camelCase")]
45struct LocalStats {
46 repos: usize,
47 issues: usize,
48 patches: usize,
49 pushes: usize,
50 forks: usize,
51}
52
53#[derive(Default, Serialize)]
54#[serde(rename_all = "camelCase")]
55struct RepoStats {
56 unique: usize,
57 replicas: usize,
58}
59
60#[derive(Default, Serialize)]
61#[serde(rename_all = "camelCase")]
62struct Stats {
63 local: LocalStats,
64 repos: RepoStats,
65 nodes: NodeStats,
66}
67
68#[derive(Default, Debug, Eq, PartialEq)]
69pub struct Options {}
70
71impl Args for Options {
72 fn from_args(args: Vec<OsString>) -> anyhow::Result<(Self, Vec<OsString>)> {
73 use lexopt::prelude::*;
74
75 let mut parser = lexopt::Parser::from_args(args);
76
77 #[allow(clippy::never_loop)]
78 while let Some(arg) = parser.next()? {
79 match arg {
80 Long("help") | Short('h') => {
81 return Err(Error::Help.into());
82 }
83 _ => return Err(anyhow::anyhow!(arg.unexpected())),
84 }
85 }
86
87 Ok((Options {}, vec![]))
88 }
89}
90
91pub fn run(_options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
92 let profile = ctx.profile()?;
93 let storage = &profile.storage;
94 let mut stats = Stats::default();
95
96 for repo in storage.repositories()? {
97 let repo = storage.repository(repo.rid)?;
98 let issues = term::cob::issues(&profile, &repo)?.counts()?;
99 let patches = term::cob::patches(&profile, &repo)?.counts()?;
100
101 stats.local.issues += issues.total();
102 stats.local.patches += patches.total();
103 stats.local.repos += 1;
104
105 for remote in repo.remote_ids()? {
106 let remote = remote?;
107 let sigrefs = repo.reference_oid(&remote, &git::refs::storage::SIGREFS_BRANCH)?;
108 let mut walk = repo.raw().revwalk()?;
109 walk.push(*sigrefs)?;
110
111 stats.local.pushes += walk.count();
112 stats.local.forks += 1;
113 }
114 }
115
116 let now = LocalTime::now();
117 let db = profile.database()?;
118 stats.nodes.all = address::Store::nodes(&db)?;
119 stats.repos.replicas = routing::Store::len(&db)?;
120
121 {
122 let row = db
123 .db
124 .prepare("SELECT COUNT(DISTINCT repo) FROM routing")?
125 .into_iter()
127 .next()
128 .unwrap()?;
129 let count = row.read::<i64, _>(0) as usize;
130
131 stats.repos.unique = count;
132 }
133
134 {
135 let since = now - LocalDuration::from_mins(60 * 24); let mut stmt = db.db.prepare(
137 "SELECT COUNT(DISTINCT node) FROM announcements WHERE timestamp >= ?1 AND timestamp < ?2",
138 )?;
139 stmt.bind((1, since.as_millis() as i64))?;
140 stmt.bind((2, now.as_millis() as i64))?;
141
142 let row = stmt.iter().next().unwrap()?;
144 stats.nodes.online_daily = row.read::<i64, _>(0) as usize;
145
146 let since = now - LocalDuration::from_mins(60 * 24 * 7); stmt.reset()?;
148 stmt.bind((1, since.as_millis() as i64))?;
149 stmt.bind((2, now.as_millis() as i64))?;
150
151 let row = stmt.iter().next().unwrap()?;
152 stats.nodes.online_weekly = row.read::<i64, _>(0) as usize;
153 }
154
155 {
156 let since = now - LocalDuration::from_mins(60 * 24); let mut stmt = db.db.prepare(
158 "SELECT COUNT(DISTINCT ann.node) FROM announcements as ann
159 JOIN addresses AS addr
160 ON ann.node == addr.node
161 WHERE ann.timestamp >= ?1 AND ann.timestamp < ?2",
162 )?;
163 stmt.bind((1, since.as_millis() as i64))?;
164 stmt.bind((2, now.as_millis() as i64))?;
165
166 let row = stmt
167 .into_iter()
168 .next()
169 .unwrap()?;
171 let count = row.read::<i64, _>(0) as usize;
172
173 stats.nodes.public_daily = count;
174 }
175
176 {
177 let since = now - LocalDuration::from_mins(60 * 24 * 7); let mut stmt = db.db.prepare(
179 "SELECT COUNT(DISTINCT node) FROM routing
180 WHERE timestamp >= ?1 AND timestamp < ?2",
181 )?;
182 stmt.bind((1, since.as_millis() as i64))?;
183 stmt.bind((2, now.as_millis() as i64))?;
184
185 let row = stmt
186 .into_iter()
187 .next()
188 .unwrap()?;
190 let count = row.read::<i64, _>(0) as usize;
191
192 stats.nodes.seeding_weekly = count;
193 }
194
195 let output = term::json::to_pretty(&stats, Path::new("stats.json"))?;
196 output.print();
197
198 Ok(())
199}