gitstats 0.0.1-alpha7

fetch stats from a git repository
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#[cfg(test)]
mod test {
	use std::env::current_dir;
	use std::ops::Deref;
	use std::time::{Duration, Instant};

	use chrono::{DateTime, Months, Utc, Weekday};
	use comfy_table::Table;
	use humansize::{BaseUnit, FormatSizeOptions};
	use itertools::Itertools;
	use lazy_static::lazy_static;
	use num_traits::cast::FromPrimitive;
	use textplots::{AxisBuilder, LabelBuilder, LabelFormat, LineStyle, Plot, Shape, TickDisplay, TickDisplayBuilder};

	use crate::traits::CommitStatsExt;
	use crate::{Author, CommitArgs, CommitDetail, CommitHash, Repo, SortStatsBy};

	lazy_static! {
		static ref SINCE: DateTime<Utc> = Utc::now().checked_sub_months(Months::new(6)).unwrap();
		static ref UNTIL: DateTime<Utc> = Utc::now();
		static ref COMMIT_ARGS: CommitArgs = CommitArgs::builder()
			.since(SINCE.timestamp())
			.until(UNTIL.timestamp())
			.target_branch("develop")
			.exclude_merges(true)
			.exclude_author("blue TV Build".into())
			.build()
			.unwrap();
	}

	fn init_log() {
		let subscriber = tracing_subscriber::fmt()
			.compact()
			.with_file(false)
			.with_line_number(false)
			.with_max_level(tracing::Level::TRACE)
			.with_thread_ids(false)
			.with_thread_names(false)
			.finish();
		tracing::subscriber::set_global_default(subscriber).unwrap();
	}

	fn checkout_repo() -> Repo {
		let repo_dir = std::env::var("TEST_REPO_DIR").expect("Environment variable `TEST_REPO_DIR` is not defined");
		let repo = Repo::from(&repo_dir);
		repo.fetch_all().unwrap();
		repo
	}

	#[test]
	fn test_new_repo() {
		init_log();
		let current_dir = current_dir().unwrap();
		let path = current_dir;
		println!("path: {:?}", path);
		let repo = Repo::try_from(&path).unwrap();
		println!("repo: {}", repo);

		assert_eq!(path.to_str(), repo.to_str());
	}

	#[test]
	fn test_fetch() {
		init_log();
		let mut ticker = Ticker::new();
		checkout_repo();
		println!("fetched repo in {:?}", ticker.tick().0);
	}

	#[test]
	fn test_first_last_commit() {
		init_log();
		let repo = checkout_repo();
		let first_commit = repo.first_commit().unwrap().unwrap();
		println!("first commit: {first_commit:?}");

		let last_commit = repo.last_commit().unwrap().unwrap();
		println!("last commit: {last_commit:?}");
		let first_date = first_commit.get_author_datetime();
		let last_date = last_commit.get_author_datetime();
		println!("first date: {first_date}");
		println!("last date: {last_date}");
	}

	#[test]
	fn test_repo_size() {
		init_log();
		let repo = checkout_repo();
		let size = repo.size().unwrap() * 1000;
		let size_string = humansize::format_size(
			size,
			FormatSizeOptions::default()
				.base_unit(BaseUnit::Byte)
				.decimal_places(1)
				.decimal_zeroes(1),
		);
		println!("repository size: {size_string}");
	}

	#[test]
	fn test_repo_detail() {
		init_log();
		let repo = checkout_repo();
		let details = repo.details().unwrap();
		println!("repository detail: {details}");
	}

	#[test]
	fn test_commits_count() {
		init_log();
		let repo = checkout_repo();
		let commits_count = repo.commits_count().unwrap();
		println!("total commits: {commits_count}");
	}

	#[test]
	fn test_list_commits() {
		init_log();
		let mut ticker = Ticker::new();
		let repo = checkout_repo();
		ticker.tick();

		let args = CommitArgs::default();

		let commits = repo.list_commits(args).unwrap();
		println!("listed commits in {:?}", ticker.tick().0);
		println!("total commits: {}", commits.len());
		assert!(commits.len() > 0);

		ticker.tick();
		let stats = repo.commits_stats(&commits).unwrap();
		println!("listed stats in {:?}", ticker.tick().0);
		println!("total stats: {}", stats.len());
		assert_eq!(commits.len(), stats.len());
	}

	#[test]
	fn test_reduced_stats_per_author() {
		init_log();
		let repo = checkout_repo();
		let commits = repo.list_commits(COMMIT_ARGS.clone()).unwrap();
		println!("total commits: {}", commits.len());
		assert!(commits.len() > 0);

		let stats: Vec<CommitDetail> = repo.commits_stats(&commits).unwrap();
		assert_eq!(commits.len(), stats.len());

		let mut ticker = Ticker::new();
		let commits_per_author = stats.commits_per_author();
		println!("generated commits per author stats in {:?}", ticker.tick().0);
		println!("-----------------------------------------------");

		for (author, entry) in commits_per_author.detailed_stats().iter() {
			println!("Author: {}", author);
			println!("\ttotal commits: {}", entry.len());
			let mut k = 0;
			for stat in entry.iter() {
				println!("\t[{k}] {stat}");
				k += 1;
			}
			println!("-----------------------------------------------");
		}
	}

	#[test]
	fn test_contributors_stats() {
		init_log();
		let mut ticker = Ticker::new();
		let repo = checkout_repo();

		let commits = repo.list_commits(COMMIT_ARGS.clone()).unwrap();
		println!("total commits: {}", commits.len());
		println!("-----------------------------------------------");
		assert!(commits.len() > 0);

		let stats = repo.commits_stats(&commits).unwrap();
		assert_eq!(commits.len(), stats.len());
		let commits_per_author = stats.commits_per_author();

		ticker.tick();
		let mut global_stats = commits_per_author.global_stats(SortStatsBy::LinesAdded);
		global_stats.sort_by(|a, b| b.commits_count.cmp(&a.commits_count));

		println!("generated contributor's stats in {:?}", ticker.tick().0);
		println!("-----------------------------------------------");

		let mut table = Table::new();
		table.set_header([
			"Author", "Commits", "Lines",
		]);

		for global_stat in global_stats.iter() {
			let commits_count = global_stat.commits_count;
			let total_lines = global_stat.stats.lines_added;
			table.add_row([
				(&global_stat.author).name.to_string(),
				commits_count.to_string(),
				total_lines.to_string(),
			]);
		}

		println!("{table}");
	}

	#[test]
	fn test_show() {
		init_log();
		let repo = checkout_repo();
		let commit_hash = CommitHash::try_from("a9ae91ebf675cc57fb93cbcb6e179f89f0199e8e").unwrap();
		let stats = repo.commit_stats(commit_hash).unwrap();
		println!("stats: {}", stats);
	}

	#[test]
	fn test_commits_per_month() {
		init_log();
		let mut ticker = Ticker::new();
		let repo = checkout_repo();

		let commits = repo.list_commits(COMMIT_ARGS.clone()).unwrap();
		println!("total commits: {}", commits.len());
		println!("---------------------------------------------");

		let stats = repo.commits_stats(&commits).unwrap();
		assert_eq!(commits.len(), stats.len());

		ticker.tick();
		let cloned_stats = stats.clone();
		let commits_per_months = cloned_stats.commits_per_month();
		println!("generated commits per month in {:?}", ticker.tick().0);
		println!("---------------------------------------------");

		let mut total_commits = 0;

		for (key, value) in commits_per_months
			.detailed_stats()
			.iter()
			.sorted_by_key(|(key, _)| key.to_string())
		{
			println!("date: {key}");

			for (author, stats) in value.iter() {
				println!("    {author} = {stats}");
				total_commits += stats.commits_count;
			}

			println!("--------------------------------------------");
		}

		assert_eq!(commits.len(), total_commits);

		let global_stats = commits_per_months.global_stats();
		println!("global stats:");
		println!("--------------------------------------------");
		for (key, value) in global_stats.iter().sorted_by_key(|(key, _)| key.to_string()) {
			println!("date: {key}, {}", value);
		}

		let mut points = Vec::new();
		let start = Utc::now().checked_sub_months(Months::new(6)).unwrap();

		for (index, value) in global_stats.iter().sorted_by_key(|(key, _)| key.to_string()).enumerate() {
			points.push((index as f32, value.1.commits_count as f32));
		}

		textplots::Chart::new_with_y_range(100, 50, 0.0, 5.0, 0.0, 50.0)
			.lineplot(&Shape::Bars(&points))
			.x_axis_style(LineStyle::Solid)
			.y_axis_style(LineStyle::Solid)
			.y_tick_display(TickDisplay::Dense)
			.x_label_format(LabelFormat::Custom(Box::new(move |val| {
				let new_start = start.checked_add_months(Months::new(val as u32)).unwrap();
				format!("{}", new_start.format("%Y-%m"))
			})))
			.display();
	}

	#[test]
	fn test_commits_per_weekday() {
		init_log();
		let mut ticker = Ticker::new();
		let repo = checkout_repo();
		println!("checked out repo in {:?}", ticker.tick().0);
		println!("---------------------------------------------");

		let commits = repo.list_commits(COMMIT_ARGS.deref().clone()).unwrap();
		println!("listed commits from repo in {:?}", ticker.tick().0);
		println!("total commits: {}", commits.len());
		println!("---------------------------------------------");

		let stats = repo.commits_stats(&commits).unwrap();
		println!("parsed commits in {:?}", ticker.tick().0);
		println!("total stats: {}", stats.len());
		assert_eq!(commits.len(), stats.len());
		println!("---------------------------------------------");

		let commits_per_weekday = stats.commits_per_weekday();

		println!("commits per weekday created in {:?}", ticker.tick().0);
		println!("---------------------------------------------");

		let mut total_commits = 0;

		for (key, value) in commits_per_weekday.detailed_stats().iter().sorted_by_key(|a| a.0) {
			let weekday = Weekday::from_u8(*key).unwrap();
			println!("WeekDay: {weekday:?}");
			for (author, stats) in value.iter() {
				println!("{author} : {stats}");
				total_commits += stats.commits_count;
			}
			println!("---------------------------------------------");
		}

		println!("total commits: {total_commits}");
		assert_eq!(commits.len(), total_commits);

		let global_stats = commits_per_weekday.global_stats();

		println!("global commits per weekday created in {:?}", ticker.tick().0);
		println!("---------------------------------------------");

		total_commits = 0;

		for (key, value) in global_stats.iter().sorted_by_key(|a| a.0) {
			println!("WeekDay: {:?}, stats: {value}", Weekday::from_u8(*key).unwrap());
			total_commits += value.commits_count;
		}
		println!("total commits: {total_commits}");
		assert_eq!(commits.len(), total_commits);
		println!("---------------------------------------------");
		println!("done. {:?}", ticker.tick().1);
	}

	#[test]
	fn test_commits_per_day_hour() {
		init_log();
		let mut ticker = Ticker::new();
		let repo = checkout_repo();
		println!("checked out repo in {:?}", ticker.tick().0);
		println!("---------------------------------------------");

		let commits = repo.list_commits(COMMIT_ARGS.deref().clone()).unwrap();
		let stats = repo.commits_stats(&commits).unwrap();
		let commits_per_day_hour = stats.commits_per_day_hour();

		println!("commits per day hour created in {:?}", ticker.tick().0);
		println!("---------------------------------------------");

		let mut total_commits = 0;

		for (key, value) in commits_per_day_hour.detailed_stats().iter().sorted_by_key(|(key, _)| *key) {
			println!("Hour: {key}");
			for (author, stats) in value.iter() {
				println!("{author} : {stats}");
				total_commits += stats.commits_count;
			}
			println!("---------------------------------------------");
		}

		println!("total commits: {total_commits}");
		assert_eq!(commits.len(), total_commits);

		let global_stats = commits_per_day_hour.global_stats();

		println!("global commits per day hour created in {:?}", ticker.tick().0);
		println!("---------------------------------------------");

		total_commits = 0;

		for (key, value) in global_stats.iter().sorted_by_key(|(key, _)| *key) {
			println!("Hour: {key}, stats: {value}");
			total_commits += value.commits_count;
		}
		println!("total commits: {total_commits}");
		assert_eq!(commits.len(), total_commits);
		println!("---------------------------------------------");
		println!("done. {:?}", ticker.tick().1);
	}

	#[test]
	fn test_commits_heatmap() {
		init_log();

		let mut ticker = Ticker::new();
		let repo = checkout_repo();
		println!("checked out repo in {:?}", ticker.tick().0);
		println!("---------------------------------------------");

		let commits = repo.list_commits(COMMIT_ARGS.deref().clone()).unwrap();
		let stats = repo.commits_stats(&commits).unwrap();

		ticker.tick();
		let commits_heatmap = stats.commits_heatmap();

		for (_key, value) in commits_heatmap.detailed_stats().iter() {
			assert_eq!(7, value.len());
			for item in value.iter() {
				assert_eq!(24, item.len());
			}
		}

		println!("generated heatmap in {:?}", ticker.tick().0);
		println!("---------------------------------------------");

		let global_stats = commits_heatmap.global_stats();
		println!("generated global heatmap in {:?}", ticker.tick().0);
		println!("---------------------------------------------");

		assert_eq!(7, global_stats.len());
		for stats in global_stats.iter() {
			assert_eq!(24, stats.len());
		}

		let mut table = Table::new();
		table.set_header(vec![
			"Weekday/Hour",
			"0",
			"1",
			"2",
			"3",
			"4",
			"5",
			"6",
			"7",
			"8",
			"9",
			"10",
			"11",
			"12",
			"13",
			"14",
			"15",
			"16",
			"17",
			"18",
			"19",
			"20",
			"21",
			"22",
			"23",
		]);

		let mut rows: Vec<Vec<String>> = Vec::new();
		for weekday in 0..7 {
			let mut row = vec![Weekday::from_u8(weekday).unwrap().to_string()];
			for _hour in 0..24 {
				row.push("0".to_string());
			}
			rows.push(row);
		}

		for (weekday, hours) in global_stats.iter().enumerate() {
			for (hour, stats) in hours.iter().enumerate() {
				let row = rows.get_mut(weekday).unwrap();
				let current_value = row.get((hour + 1) as usize).unwrap().parse::<usize>().unwrap();
				let new_value = current_value + stats.commits_count;
				*row.get_mut((hour + 1) as usize).unwrap() = new_value.to_string();
			}
		}
		table.add_rows(rows);
		println!("{table}");
	}

	#[test]
	fn test_string_to_author() {
		init_log();
		let mut author: Author = "Alessandro Crugnola <alessandro@gmail.com>".try_into().unwrap();
		println!("Author: {}", author);

		author = "Alessandro Crugnola <sephiroth> <alessandro@gmail.com>".try_into().unwrap();
		println!("Author: {}", author);

		author = "Alessandro <alessandro.crugnola_123+1@gmail.com>".try_into().unwrap();
		println!("Author: {}", author);

		author = "Alessandro Crugnola <>".try_into().unwrap();
		println!("Author: {}", author);
	}

	#[derive(Debug)]
	struct Ticker {
		start: Instant,
		current: Instant,
	}

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

		pub fn tick(&mut self) -> (Duration, Duration) {
			let elapsed = self.current.elapsed();
			let total = self.start.elapsed();
			self.current = Instant::now();
			(elapsed, total)
		}
	}
}