Skip to content

Commit 9b3159b

Browse files
committed
Add a "new!" label
I leave the philosophical discussion if no data equals the cow isn't new to you Dear Reader. I'm writing the commit message already so it would be way too much work to change that now even though I changed my mind on this in the space of the last 10 seconds.
1 parent 1c9c06d commit 9b3159b

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

moooodotfarm-backend/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

moooodotfarm-backend/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ tonic = "0.12.3"
3434
prost = "0.13.5"
3535
async-trait = "0.1"
3636
rand = "0.8"
37+
lazy_static = "1.5"
3738

3839
[build-dependencies]
3940
tonic-build = "0.12.3"

moooodotfarm-backend/src/app/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ impl TryFrom<domain::CensoredHerd> for Herd {
134134
pub struct Cow {
135135
name: domain::Name,
136136
character: Character,
137+
first_seen: Option<DateTime>,
137138
last_seen: Option<DateTime>,
138139
status: CowStatus,
139140
}
@@ -147,6 +148,10 @@ impl Cow {
147148
&self.character
148149
}
149150

151+
pub fn first_seen(&self) -> Option<&DateTime> {
152+
self.first_seen.as_ref()
153+
}
154+
150155
pub fn last_seen(&self) -> Option<&DateTime> {
151156
self.last_seen.as_ref()
152157
}
@@ -163,6 +168,7 @@ impl TryFrom<&domain::CensoredCow> for Cow {
163168
Ok(Self {
164169
name: value.name().clone(),
165170
character: value.character().clone(),
171+
first_seen: value.first_seen().cloned(),
166172
last_seen: value.last_seen().cloned(),
167173
status: CowStatus::new(value),
168174
})

moooodotfarm-backend/src/ports/http/mod.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::app::GetHerdHandler;
22
use crate::config::Environment;
3+
use crate::domain::time::DateTime;
34
use crate::errors::{Error, Result};
45
use crate::{app, config};
56
use askama::Template;
@@ -245,29 +246,41 @@ struct TemplateCow {
245246
name_with_kind: TemplateCowName,
246247
last_seen: String,
247248
status: CowStatus,
249+
is_new: bool,
250+
}
251+
252+
lazy_static::lazy_static! {
253+
static ref RECENTLY_SEEN_THRESHOLD: crate::domain::time::Duration =
254+
crate::domain::time::Duration::new_from_hours(2);
255+
256+
static ref NEW_COW_THRESHOLD: crate::domain::time::Duration =
257+
crate::domain::time::Duration::new_from_days(14);
248258
}
249259

250260
impl From<&app::Cow> for TemplateCow {
251261
fn from(value: &app::Cow) -> Self {
252-
use crate::domain::time::{DateTime, Duration};
253-
254262
let last_seen_str = value
255263
.last_seen()
256264
.map(|v| {
257265
let now = DateTime::now();
258266
let duration = &now - v;
259-
if duration < Duration::new_from_hours(2) {
267+
if duration < *RECENTLY_SEEN_THRESHOLD {
260268
"very recently".to_string()
261269
} else {
262270
v.ago()
263271
}
264272
})
265273
.unwrap_or_else(|| "never".to_string());
274+
let is_new = value
275+
.first_seen()
276+
.map(|v| DateTime::now() - v < *NEW_COW_THRESHOLD)
277+
.unwrap_or(false);
266278

267279
Self {
268280
name_with_kind: value.name().into(),
269281
last_seen: last_seen_str,
270282
status: value.status().into(),
283+
is_new,
271284
}
272285
}
273286
}

moooodotfarm-backend/src/ports/http/templates/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@
235235
{% endmatch %}
236236
<div class="cow-meta">last seen: {{ cow.last_seen }}</div>
237237
</div>
238+
{% if cow.is_new %}
239+
<div class="new-indicator">new!</div>
240+
{% endif %}
238241
<div class="status-indicator {{ cow.status }}"></div>
239242
</li>
240243
{% else %}

0 commit comments

Comments
 (0)