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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//! Summaries endpoint.
//!
//! - Ref: <https://wakatime.com/developers#summaries>
//! - Last checked : 2026-05-15
#[cfg(not(feature = "blocking"))]
use crate::ErrorMessage;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use crate::{error::WakapiError, WakapiClient};
/// Request parameters for the Summaries endpoint.
///
/// Ref: <https://wakatime.com/developers#summaries>
#[derive(Serialize, Debug)]
pub struct SummariesParams {
/// required if range not provided - Start date of the time range.
start: Option<String>,
/// required if range not provided - End date of the time range.
end: Option<String>,
/// optional - Only show time logged to this project.
project: Option<String>,
/// optional - Only show coding activity for these branches; comma separated list of branch names.
branches: Option<Vec<String>>,
/// optional - The keystroke timeout preference used when joining heartbeats into durations. Defaults the the user's keystroke timeout value. See the FAQ for more info.
timeout: Option<usize>,
/// optional - The writes_only preference. Defaults to the user's writes_only setting.
writes_only: Option<bool>,
/// optional - The timezone for given start and end dates. Defaults to the user's timezone.
timezone: Option<String>,
/// optional - Alternative way to supply start and end dates. Can be one of “Today”, “Yesterday”, “Last 7 Days”, “Last 7 Days from Yesterday”, “Last 14 Days”, “Last 30 Days”, “This Week”, “Last Week”, “This Month”, or “Last Month”.
range: Option<String>,
/// optional - Timeout in seconds. Defaults to 60 seconds.
fetch_timeout: Option<u64>,
/// optional - Retry count for failed requests. Defaults to 3.
fetch_retries: Option<usize>,
}
impl SummariesParams {
pub fn from_interval(start: &str, end: &str) -> SummariesParams {
SummariesParams {
start: Some(start.to_string()),
end: Some(end.to_string()),
project: None,
branches: None,
timeout: None,
writes_only: None,
timezone: None,
range: None,
fetch_retries: None,
fetch_timeout: None,
}
}
pub fn from_range(range: SummaryRange) -> SummariesParams {
SummariesParams {
range: Some(range.to_string()),
start: None,
end: None,
project: None,
branches: None,
timeout: None,
writes_only: None,
timezone: None,
fetch_retries: None,
fetch_timeout: None,
}
}
pub fn project(mut self, project: &str) -> SummariesParams {
self.project = Some(project.to_string());
self
}
pub fn branches(mut self, branches: Vec<String>) -> SummariesParams {
self.branches = Some(branches);
self
}
pub fn timeout(mut self, timeout: usize) -> SummariesParams {
self.timeout = Some(timeout);
self
}
pub fn writes_only(mut self, writes_only: bool) -> SummariesParams {
self.writes_only = Some(writes_only);
self
}
pub fn timezone(mut self, timezone: &str) -> SummariesParams {
self.timezone = Some(timezone.to_string());
self
}
pub fn range(mut self, range: &str) -> SummariesParams {
self.range = Some(range.to_string());
self.start = None;
self.end = None;
self
}
pub fn start_end(mut self, start: &str, end: &str) -> SummariesParams {
self.start = Some(start.to_string());
self.end = Some(end.to_string());
self.range = None;
self
}
}
impl Default for SummariesParams {
/// Create a new SummariesParams with default values (range = this day)
fn default() -> Self {
SummariesParams::from_range(SummaryRange::Today)
}
}
/// Summary range : <https://wakatime.com/developers#summaries>
/// Alternative way to supply start and end dates. Can be one of “Today”,
/// “Yesterday”, “Last 7 Days”, “Last 7 Days from Yesterday”,
/// “Last 14 Days”, “Last 30 Days”, “This Week”, “Last Week”,
/// “This Month”, or “Last Month”.
#[derive(Debug)]
pub enum SummaryRange {
Today,
Yesterday,
Last7Days,
Last7DaysFromYesterday,
Last14Days,
Last30Days,
ThisWeek,
LastWeek,
ThisMonth,
LastMonth,
}
impl fmt::Display for SummaryRange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
SummaryRange::Today => "Today",
SummaryRange::Yesterday => "Yesterday",
SummaryRange::Last7Days => "Last 7 Days",
SummaryRange::Last7DaysFromYesterday => "Last 7 Days from Yesterday",
SummaryRange::Last14Days => "Last 14 Days",
SummaryRange::Last30Days => "Last 30 Days",
SummaryRange::ThisWeek => "This Week",
SummaryRange::LastWeek => "Last Week",
SummaryRange::ThisMonth => "This Month",
SummaryRange::LastMonth => "Last Month",
};
write!(f, "{}", s)
}
}
/// Summaries endpoint body
///
/// Ref: <https://wakatime.com/developers#summaries>
///
#[derive(Serialize, Deserialize, Debug)]
pub struct Summaries {
/// List of summaries data
pub data: Vec<SummariesData>,
/// Cumulative total coding activity for the date range of summaries
pub cumulative_total: SummariesCumulativeTotal,
/// Daily average coding activity for the date range of summaries
pub daily_average: SummariesDailyAverage,
/// Start of the date range of summaries
pub start: DateTime<Utc>,
/// End of the date range of summaries
pub end: DateTime<Utc>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SummariesData {
/// Total coding activity for this day
pub grand_total: SummariesGrandTotal,
/// Conding stats for each category
pub categories: Vec<CategoryStats>,
/// optional - Only present if the project is not set in the request.
pub projects: Option<Vec<ProjectStats>>,
/// Conding stats for each language
pub languages: Vec<LanguageStats>,
/// Conding stats for each editor
pub editors: Vec<EditorStats>,
/// Conding stats for each operating system
pub operating_systems: Vec<OsStats>,
/// Conding stats for each dependency
pub dependencies: Vec<DependencyStats>,
/// Conding stats for each machine
pub machines: Vec<MachineStats>,
/// optional - only present if project is set in the request
pub branches: Option<Vec<SummariesBranch>>,
/// optional - only present if project is set in the request
pub entities: Option<Vec<SummariesEntity>>,
/// range details
pub range: SummariesRange,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SummariesGrandTotal {
/// total coding activity in digital clock format
pub digital: String,
/// hours portion of coding activity
pub hours: usize,
/// minutes portion of coding activity
pub minutes: usize,
/// total coding activity in human readable format
pub text: String,
/// total coding activity as seconds
pub total_seconds: f64,
/// number of lines added by GenAI for this day
pub ai_additions: Option<i64>,
/// number of lines removed by GenAI for this day
pub ai_deletions: Option<i64>,
/// number of lines added by old-school typing for this day
pub human_additions: Option<i64>,
/// number of lines removed by old-school typing for this day
pub human_deletions: Option<i64>,
/// number of lines added or removed per GenAI agent for this day
pub ai_agent_line_changes: Option<HashMap<String, i64>>,
/// estimated USD cost per GenAI agent for this day
pub ai_agent_costs: Option<HashMap<String, f64>>,
/// number of user input tokens used by GenAI tools
pub ai_input_tokens: Option<i64>,
/// number of output tokens used by GenAI tools
pub ai_output_tokens: Option<i64>,
/// average number of characters typed to AI tools per prompt
pub ai_prompt_length_avg: Option<i64>,
/// sum of characters typed to AI tools
pub ai_prompt_length_sum: Option<i64>,
/// number of prompts included in ai_prompt_length_avg
pub ai_prompt_events: Option<i64>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CategoryStats {
/// name of category, for ex: Coding or Debugging
pub name: String,
/// total coding activity as seconds
pub total_seconds: f64,
/// percent of time spent in this category
pub percent: f64,
/// total coding activity for this category in digital clock format
pub digital: String,
/// total coding activity in human readable format
pub text: String,
/// hours portion of coding activity for this category
pub hours: usize,
/// minutes portion of coding activity for this category
pub minutes: usize,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ProjectStats {
/// name of project
pub name: String,
/// total coding activity as seconds
pub total_seconds: f64,
/// percent of time spent on this project
pub percent: f64,
/// total coding activity for this project in digital clock format
pub digital: String,
/// total coding activity in human readable format
pub text: String,
/// hours portion of coding activity for this project
pub hours: usize,
/// minutes portion of coding activity for this project
pub minutes: usize,
/// number of lines added by GenAI in this project
pub ai_additions: Option<i64>,
/// number of lines removed by GenAI in this project
pub ai_deletions: Option<i64>,
/// number of lines added by old-school typing in this project
pub human_additions: Option<i64>,
/// number of lines removed by old-school typing in this project
pub human_deletions: Option<i64>,
/// number of lines added or removed per GenAI agent in this project
pub ai_agent_line_changes: Option<HashMap<String, i64>>,
/// estimated USD cost per GenAI agent in this project
pub ai_agent_costs: Option<HashMap<String, f64>>,
/// number of user input tokens used by GenAI tools in this project
pub ai_input_tokens: Option<i64>,
/// number of output tokens used by GenAI tools in this project
pub ai_output_tokens: Option<i64>,
/// average number of characters typed to AI tools per user prompt in this project
pub ai_prompt_length_avg: Option<i64>,
/// sum of characters typed to AI tools in this project
pub ai_prompt_length_sum: Option<i64>,
/// number of AI prompts in this project
pub ai_prompt_events: Option<i64>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct LanguageStats {
/// name of language
pub name: String,
/// total coding activity as seconds
pub total_seconds: f64,
/// percent of time spent on this language
pub percent: f64,
/// total coding activity for this language in digital clock format
pub digital: String,
/// total coding activity in human readable format
pub text: String,
/// hours portion of coding activity for this language
pub hours: usize,
/// minutes portion of coding activity for this language
pub minutes: usize,
/// seconds portion of coding activity for this language
pub seconds: Option<usize>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct EditorStats {
/// name of editor
pub name: String,
/// total coding activity as seconds
pub total_seconds: f64,
/// percent of time spent on this editor
pub percent: f64,
/// total coding activity for this editor in digital clock format
pub digital: String,
/// total coding activity in human readable format
pub text: String,
/// hours portion of coding activity for this editor
pub hours: usize,
/// minutes portion of coding activity for this editor
pub minutes: usize,
/// seconds portion of coding activity for this editor
pub seconds: Option<usize>,
/// number of lines added or removed per GenAI agent in this editor
pub ai_agent_line_changes: Option<HashMap<String, i64>>,
/// estimated USD cost per GenAI agent in this editor
pub ai_agent_costs: Option<HashMap<String, f64>>,
/// number of user input tokens used by GenAI tools in this editor
pub ai_input_tokens: Option<i64>,
/// number of output tokens used by GenAI tools in this editor
pub ai_output_tokens: Option<i64>,
/// average number of characters typed to AI tools per user prompt in this editor
pub ai_prompt_length_avg: Option<i64>,
/// sum of characters typed to AI tools in this editor
pub ai_prompt_length_sum: Option<i64>,
/// number of AI prompts in this editor
pub ai_prompt_events: Option<i64>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct OsStats {
/// name of operating system
pub name: String,
/// total coding activity as seconds
pub total_seconds: f64,
/// percent of time spent on this operating system
pub percent: f64,
/// total coding activity for this operating system in digital clock format
pub digital: String,
/// total coding activity in human readable format
pub text: String,
/// hours portion of coding activity for this operating system
pub hours: usize,
/// minutes portion of coding activity for this operating system
pub minutes: usize,
/// seconds portion of coding activity for this operating system
pub seconds: Option<usize>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DependencyStats {
/// name of dependency
pub name: String,
/// total coding activity as seconds
pub total_seconds: f64,
/// percent of time spent on this dependency
pub percent: f64,
/// total coding activity for this dependency in digital clock format
pub digital: String,
/// total coding activity in human readable format
pub text: String,
/// hours portion of coding activity for this dependency
pub hours: usize,
/// minutes portion of coding activity for this dependency
pub minutes: usize,
/// seconds portion of coding activity for this dependency
pub seconds: Option<usize>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct MachineStats {
/// machine hostname and ip address
pub name: String,
/// unique id of this machine
pub machine_name_id: Option<String>,
/// total coding activity as seconds
pub total_seconds: f64,
/// percent of time spent on this machine
pub percent: f64,
/// total coding activity for this machine in digital clock format
pub digital: String,
/// total coding activity in human readable format
pub text: String,
/// hours portion of coding activity for this machine
pub hours: usize,
/// minutes portion of coding activity for this machine
pub minutes: usize,
/// seconds portion of coding activity for this machine
pub seconds: Option<usize>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SummariesBranch {
/// name of branch
pub name: String,
/// total coding activity as seconds
pub total_seconds: f64,
/// percent of time spent on this branch
pub percent: f64,
/// total coding activity for this branch in digital clock format
pub digital: String,
/// total coding activity in human readable format
pub text: String,
/// hours portion of coding activity for this branch
pub hours: usize,
/// minutes portion of coding activity for this branch
pub minutes: usize,
/// seconds portion of coding activity for this branch
pub seconds: usize,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SummariesEntity {
/// name of entity
pub name: String,
/// total coding activity as seconds
pub total_seconds: f64,
/// percent of time spent on this entity
pub percent: f64,
/// total coding activity for this entity in digital clock format
pub digital: String,
/// total coding activity in human readable format
pub text: String,
/// hours portion of coding activity for this entity
pub hours: usize,
/// minutes portion of coding activity for this entity
pub minutes: usize,
/// seconds portion of coding activity for this entity
pub seconds: usize,
/// number of lines added by GenAI in this entity
pub ai_additions: Option<i64>,
/// number of lines removed by GenAI in this entity
pub ai_deletions: Option<i64>,
/// number of lines added by old-school typing in this entity
pub human_additions: Option<i64>,
/// number of lines removed by old-school typing in this entity
pub human_deletions: Option<i64>,
/// number of lines added or removed per GenAI agent in this entity
pub ai_agent_line_changes: Option<HashMap<String, i64>>,
/// estimated USD cost per GenAI agent in this entity
pub ai_agent_costs: Option<HashMap<String, f64>>,
/// number of user input tokens used by GenAI tools in this entity
pub ai_input_tokens: Option<i64>,
/// number of output tokens used by GenAI tools in this entity
pub ai_output_tokens: Option<i64>,
/// average number of characters typed to AI tools per prompt in this entity
pub ai_prompt_length_avg: Option<i64>,
/// sum of characters typed to AI tools in this entity
pub ai_prompt_length_sum: Option<i64>,
/// number of prompts included in ai_prompt_length_avg in this entity
pub ai_prompt_events: Option<i64>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SummariesRange {
/// this day as Date string in YEAR-MONTH-DAY format
pub date: String,
/// start of this day as ISO 8601 UTC datetime
pub start: DateTime<Utc>,
/// end of this day as ISO 8601 UTC datetime
pub end: DateTime<Utc>,
/// this day in human-readable format relative to the current day
pub text: String,
/// timezone used for this request in Olson Country/Region format
pub timezone: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SummariesCumulativeTotal {
/// cumulative number of seconds over the date range of summaries
pub seconds: f64,
/// cumulative total coding activity in human readable format
pub text: String,
/// cumulative total as a decimal
pub decimal: String,
/// cumulative total in digital clock format
pub digital: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SummariesDailyAverage {
/// number of days in this range with no coding time logged
pub holidays: usize,
/// number of days in this range
pub days_including_holidays: usize,
/// number of days in this range excluding days with no activity
pub days_minus_holidays: usize,
/// average coding activity per day as seconds for the given range of time, excluding Other language
pub seconds: f64,
/// daily average, excluding Other language, as human readable string
pub text: String,
/// average coding activity per day as seconds for the given range of time
pub seconds_including_other_language: f64,
/// daily average as human readable string
pub text_including_other_language: String,
}
impl Summaries {
#[cfg(feature = "blocking")]
pub fn fetch(client: &WakapiClient, params: SummariesParams) -> Result<Summaries, WakapiError> {
let url = client.build_url(
"/api/v1/users/:user/summaries",
Some(serde_url_params::to_string(¶ms)?),
);
let mut retries = params.fetch_retries.unwrap_or(3);
loop {
// // Debug : print the url and request text
// println!(
// "url: {}\nbody: {}",
// url,
// reqwest::blocking::Client::new()
// .get(&url)
// .header("Authorization", client.get_auth_header())
// .send()?
// .text()
// .unwrap_or_default()
// );
let mut rqw = reqwest::blocking::ClientBuilder::new();
rqw = rqw.timeout(std::time::Duration::from_secs(
params.fetch_timeout.unwrap_or(60),
));
let response = rqw
.build()?
.get(&url)
.header("Authorization", client.get_auth_header())
.send();
let err: Option<WakapiError>;
match response {
Ok(response) => {
let summaries: Result<Summaries, _> = response.json();
match summaries {
Ok(summaries) => return Ok(summaries),
Err(e) => {
// If JSON parsing fails, return the error immediately
err = Some(WakapiError::RequestError(e));
}
}
}
Err(e) => {
err = Some(WakapiError::RequestError(e));
}
}
retries -= 1;
if retries <= 0 {
if let Some(err) = err {
return Err(err);
} else {
return Err(WakapiError::UnknownError);
}
}
}
}
#[cfg(not(feature = "blocking"))]
pub async fn fetch(
client: &WakapiClient,
params: SummariesParams,
) -> Result<Summaries, WakapiError> {
let url = client.build_url(
"/api/v1/users/:user/summaries",
Some(serde_url_params::to_string(¶ms)?),
);
// Debug : print the url and request text
#[cfg(debug_assertions)]
println!(
"url: {}\nbody: {}",
url,
reqwest::Client::new()
.get(&url)
.header("Authorization", client.get_auth_header())
.send()
.await?
.text()
.await
.unwrap_or_default()
);
let response = reqwest::Client::new()
.get(&url)
.header("Authorization", client.get_auth_header())
.send()
.await?;
if response.status().is_success() {
let summaries: Summaries = response.json().await?;
Ok(summaries)
} else {
let error_message: ErrorMessage = response.json().await?;
Err(WakapiError::ResponseError(error_message))
}
}
}