Skip to content

Commit

Permalink
convert ReflectionSummary to PaceReflection to wrap tera::Context
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <[email protected]>
  • Loading branch information
simonsan committed Mar 23, 2024
1 parent 7a5a53c commit f3d7597
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
6 changes: 2 additions & 4 deletions crates/core/src/commands/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use pace_time::{
};
use serde_derive::Serialize;
use std::path::PathBuf;
use tera::Context;
use tracing::debug;
use typed_builder::TypedBuilder;

Expand All @@ -18,7 +17,7 @@ use crate::{
error::{PaceResult, TemplatingErrorKind, UserMessage},
service::{activity_store::ActivityStore, activity_tracker::ActivityTracker},
storage::get_storage_from_config,
template::TEMPLATES,
template::{PaceReflectionTemplate, TEMPLATES},
};

/// `reflect` subcommand options
Expand Down Expand Up @@ -182,8 +181,7 @@ impl ReflectCommandOptions {
}

Some(ReflectionsFormatKind::Html) => {
let context = Context::from_serialize(reflection)
.map_err(TemplatingErrorKind::FailedToGenerateContextFromSerialize)?;
let context = PaceReflectionTemplate::from(reflection).into_context();

let html = if template_file.is_none() {
TEMPLATES
Expand Down
41 changes: 41 additions & 0 deletions crates/core/src/template.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use lazy_static::lazy_static;
use tera::Tera;

use crate::prelude::{ReflectionSummary, SummaryActivityGroup};

lazy_static! {
pub static ref TEMPLATES: Tera = {
let mut tera = match Tera::new("templates/reflections/**") {
Expand All @@ -15,3 +17,42 @@ lazy_static! {
tera
};
}

#[derive(Debug)]
pub struct PaceReflectionTemplate {
context: tera::Context,
}

impl PaceReflectionTemplate {
pub fn into_context(self) -> tera::Context {
self.context

Check warning on line 28 in crates/core/src/template.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/template.rs#L27-L28

Added lines #L27 - L28 were not covered by tests
}
}

impl From<ReflectionSummary> for PaceReflectionTemplate {
fn from(value: ReflectionSummary) -> Self {
let mut context = tera::Context::new();
context.insert("time_range_start", &value.time_range().start());
context.insert("time_range_end", &value.time_range().end());

Check warning on line 36 in crates/core/src/template.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/template.rs#L33-L36

Added lines #L33 - L36 were not covered by tests

context.insert("total_time_spent", &value.total_time_spent());
context.insert("total_break_duration", &value.total_break_duration());

Check warning on line 39 in crates/core/src/template.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/template.rs#L38-L39

Added lines #L38 - L39 were not covered by tests

// key must be a string, because of the way tera works with nested objects
// we need to convert the key to a string

// merge key tuples into a single string
let summary_groups_by_category = value

Check warning on line 45 in crates/core/src/template.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/template.rs#L45

Added line #L45 was not covered by tests
.summary_groups_by_category()
.iter()
.map(|((category, subcategory), summary_group)| {
let key = format!("{category}::{subcategory}");
(key, summary_group)

Check warning on line 50 in crates/core/src/template.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/template.rs#L48-L50

Added lines #L48 - L50 were not covered by tests
})
.collect::<std::collections::HashMap<String, &SummaryActivityGroup>>();

context.insert("summary_groups_by_category", &summary_groups_by_category);

Check warning on line 54 in crates/core/src/template.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/template.rs#L54

Added line #L54 was not covered by tests

Self { context }
}
}
35 changes: 7 additions & 28 deletions templates/reflections/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
<h1>Activity Report</h1>

<h2>Time Range</h2>
<p>Start: <span id="start"></span></p>
<p>End: <span id="end"></span></p>
<p>Start: <span id="start">{{ time_range_start }}</span></p>
<p>End: <span id="end">{{ time_range_end }}</span></p>

<h2>Total Time Spent</h2>
<p><span id="total_time"></span> seconds</p>
<p><span id="total_time">{{ total_time_spent }}</span> seconds</p>

<h2>Total Break Duration</h2>
<p><span id="total_time_break">{{ total_break_duration }}</span> seconds</p>

<h2>Summary Groups by Category</h2>

Expand All @@ -44,32 +47,8 @@ <h2>Summary Groups by Category</h2>
<th>Description</th>
<th>Adjusted Duration</th>
</tr>
{{ summary_groups_by_category }}
</table>

<script>
import * as data from './data.json';

// Populate the time range
document.getElementById("start").innerText = data.time_range.start;
document.getElementById("end").innerText = data.time_range.end;

// Populate the total time spent
document.getElementById("total_time").innerText = data.total_time_spent;

// Populate the summary table
const summaryTable = document.getElementById("summary_table");
for (const category in data.summary_groups_by_category) {
const categoryData = data.summary_groups_by_category[category];
categoryData.activity_groups.forEach(activityGroup => {
const row = summaryTable.insertRow();
row.insertCell().innerText = category;
row.insertCell().innerText = categoryData.total_duration;
row.insertCell().innerText = activityGroup.description;
row.insertCell().innerText = activityGroup.adjusted_duration;
});
}
</script>

</body>

</html>

0 comments on commit f3d7597

Please sign in to comment.