| 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 |
use std::fmt::Write as _; |
| 46 |
|
| 47 |
use makeover_layout as layout; |
| 48 |
use quasi_router::screen::{Cell, Cells, Column, Figure, Row, Tag}; |
| 49 |
use quasi_router::{Action, Node, RegionKind, Request, Response, RouteError, Slot}; |
| 50 |
use quasi_webview::Webview; |
| 51 |
|
| 52 |
use super::Viewer; |
| 53 |
use crate::db; |
| 54 |
|
| 55 |
|
| 56 |
pub const SCREEN: &str = "user_analytics"; |
| 57 |
|
| 58 |
|
| 59 |
pub const PATH: &str = "/dashboard/tabs/analytics"; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
pub const REGION: &str = "user-analytics"; |
| 70 |
|
| 71 |
|
| 72 |
const CHART_SLOT: &str = "analytics-chart"; |
| 73 |
|
| 74 |
|
| 75 |
const RANGES: [(&str, &str); 4] = [ |
| 76 |
("7d", "Last 7 days"), |
| 77 |
("30d", "Last 30 days"), |
| 78 |
("90d", "Last 90 days"), |
| 79 |
("all", "All time"), |
| 80 |
]; |
| 81 |
|
| 82 |
|
| 83 |
pub struct StatView { |
| 84 |
label: String, |
| 85 |
value: String, |
| 86 |
change: Option<String>, |
| 87 |
positive: bool, |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
pub struct BarView { |
| 92 |
label: String, |
| 93 |
value: String, |
| 94 |
count: i64, |
| 95 |
height_pct: f64, |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
pub struct ProjectView { |
| 100 |
title: String, |
| 101 |
revenue: String, |
| 102 |
sales: String, |
| 103 |
views: String, |
| 104 |
conversion: String, |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
pub struct TotalView { |
| 109 |
title: String, |
| 110 |
revenue: String, |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
pub struct Analytics { |
| 116 |
range: String, |
| 117 |
stats: Vec<StatView>, |
| 118 |
bars: Vec<BarView>, |
| 119 |
projects: Vec<ProjectView>, |
| 120 |
totals: Vec<TotalView>, |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
pub fn screen(viewer: &Viewer, request: Request) -> Result<Response, RouteError> { |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
let carried = request.carried; |
| 132 |
let range = carried |
| 133 |
.get("range") |
| 134 |
.and_then(|r| r.parse::<db::analytics::TimeRange>().ok()) |
| 135 |
.unwrap_or(db::analytics::TimeRange::Days30); |
| 136 |
|
| 137 |
let analytics = read(viewer, &range)?; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
viewer.fill(CHART_SLOT, chart_markup(&analytics.bars)); |
| 142 |
|
| 143 |
Ok(Response::fragment(REGION, pane(&analytics))) |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
fn read(viewer: &Viewer, range: &db::analytics::TimeRange) -> Result<Analytics, RouteError> { |
| 148 |
let db = &viewer.app.db; |
| 149 |
let user_id = viewer.user.id; |
| 150 |
let currency = viewer.user.settlement_currency; |
| 151 |
let failed = |_| RouteError::internal("your analytics could not be read"); |
| 152 |
|
| 153 |
let buckets = viewer |
| 154 |
.block_on(db::analytics::get_revenue_timeseries( |
| 155 |
db, user_id, None, None, range, |
| 156 |
)) |
| 157 |
.map_err(failed)?; |
| 158 |
let comparison = viewer |
| 159 |
.block_on(db::analytics::get_period_comparison( |
| 160 |
db, user_id, None, None, range, |
| 161 |
)) |
| 162 |
.map_err(failed)?; |
| 163 |
let (current_views, prev_views) = viewer |
| 164 |
.block_on(db::page_views::get_view_period_comparison( |
| 165 |
db, user_id, None, range, |
| 166 |
)) |
| 167 |
.map_err(failed)?; |
| 168 |
|
| 169 |
let bars = crate::routes::pages::dashboard::build_chart_bars(&buckets, currency) |
| 170 |
.into_iter() |
| 171 |
.map(|bar| BarView { |
| 172 |
label: bar.label, |
| 173 |
value: bar.value, |
| 174 |
count: bar.count, |
| 175 |
height_pct: bar.height_pct, |
| 176 |
}) |
| 177 |
.collect(); |
| 178 |
|
| 179 |
let view_change = db::analytics::pct_change(current_views, prev_views); |
| 180 |
let mut stats = vec![ |
| 181 |
StatView { |
| 182 |
label: "Views".into(), |
| 183 |
value: current_views.to_string(), |
| 184 |
change: view_change.as_ref().map(|(text, _)| text.clone()), |
| 185 |
positive: view_change.is_none_or(|(_, up)| up), |
| 186 |
}, |
| 187 |
StatView { |
| 188 |
label: "Revenue".into(), |
| 189 |
value: crate::formatting::format_revenue( |
| 190 |
comparison.current_revenue_cents.as_i64(), |
| 191 |
currency, |
| 192 |
), |
| 193 |
change: comparison.revenue_change().map(|(text, _)| text), |
| 194 |
positive: comparison.revenue_change().is_none_or(|(_, up)| up), |
| 195 |
}, |
| 196 |
StatView { |
| 197 |
label: "Sales".into(), |
| 198 |
value: comparison.current_sales.to_string(), |
| 199 |
change: comparison.sales_change().map(|(text, _)| text), |
| 200 |
positive: comparison.sales_change().is_none_or(|(_, up)| up), |
| 201 |
}, |
| 202 |
StatView { |
| 203 |
label: "Followers".into(), |
| 204 |
value: comparison.current_followers.to_string(), |
| 205 |
change: comparison.followers_change().map(|(text, _)| text), |
| 206 |
positive: comparison.followers_change().is_none_or(|(_, up)| up), |
| 207 |
}, |
| 208 |
]; |
| 209 |
|
| 210 |
|
| 211 |
if current_views > 0 { |
| 212 |
stats.push(StatView { |
| 213 |
label: "Conversion".into(), |
| 214 |
value: format!( |
| 215 |
"{:.1}%", |
| 216 |
comparison.current_sales as f64 / current_views as f64 * 100.0 |
| 217 |
), |
| 218 |
change: None, |
| 219 |
positive: true, |
| 220 |
}); |
| 221 |
} |
| 222 |
|
| 223 |
let project_data = viewer |
| 224 |
.block_on(db::transactions::get_revenue_by_user_projects_in_range( |
| 225 |
db, user_id, range, |
| 226 |
)) |
| 227 |
.map_err(failed)?; |
| 228 |
let project_views = viewer |
| 229 |
.block_on(db::page_views::get_views_by_seller_projects( |
| 230 |
db, user_id, range, |
| 231 |
)) |
| 232 |
.map_err(failed)?; |
| 233 |
let projects = project_data |
| 234 |
.iter() |
| 235 |
.map(|(id, title, revenue, sales)| { |
| 236 |
let views = project_views |
| 237 |
.iter() |
| 238 |
.find(|(other, _)| other == id) |
| 239 |
.map_or(0, |(_, seen)| *seen); |
| 240 |
ProjectView { |
| 241 |
title: title.clone(), |
| 242 |
revenue: revenue.display(currency), |
| 243 |
sales: sales.to_string(), |
| 244 |
views: views.to_string(), |
| 245 |
conversion: if views > 0 { |
| 246 |
format!("{:.1}%", *sales as f64 / views as f64 * 100.0) |
| 247 |
} else { |
| 248 |
"-".to_owned() |
| 249 |
}, |
| 250 |
} |
| 251 |
}) |
| 252 |
.collect(); |
| 253 |
|
| 254 |
let totals = viewer |
| 255 |
.block_on(db::transactions::get_revenue_by_user_projects(db, user_id)) |
| 256 |
.map_err(failed)? |
| 257 |
.into_iter() |
| 258 |
.map(|(_, title, revenue)| TotalView { |
| 259 |
title, |
| 260 |
revenue: revenue.display(currency), |
| 261 |
}) |
| 262 |
.collect(); |
| 263 |
|
| 264 |
Ok(Analytics { |
| 265 |
range: range.to_string(), |
| 266 |
stats, |
| 267 |
bars, |
| 268 |
projects, |
| 269 |
totals, |
| 270 |
}) |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
fn pane(analytics: &Analytics) -> Node { |
| 275 |
let mut slot = |
| 276 |
Slot::new(REGION, RegionKind::Pane).with(Node::section(range_heading(&analytics.range))); |
| 277 |
|
| 278 |
for chip in range_chips(&analytics.range) { |
| 279 |
slot = slot.with(chip); |
| 280 |
} |
| 281 |
|
| 282 |
slot = slot.with(stats(&analytics.stats)); |
| 283 |
|
| 284 |
slot = slot.with(Node::section("Revenue Over Time")); |
| 285 |
slot = if analytics.bars.is_empty() { |
| 286 |
slot.with(Node::empty( |
| 287 |
"Once you publish items and make sales, revenue data will appear here.", |
| 288 |
)) |
| 289 |
} else { |
| 290 |
|
| 291 |
|
| 292 |
slot.with(Node::Region(Slot::bespoke(CHART_SLOT, "revenue-chart"))) |
| 293 |
}; |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
if analytics.projects.len() > 1 { |
| 298 |
slot = slot |
| 299 |
.with(Node::section("Project Comparison")) |
| 300 |
.with(comparison(&analytics.projects)); |
| 301 |
} |
| 302 |
|
| 303 |
slot = slot.with(Node::section("Top Projects by Revenue")); |
| 304 |
slot = if analytics.totals.is_empty() { |
| 305 |
slot.with(Node::empty( |
| 306 |
"No revenue data yet. Sales across your projects will appear here.", |
| 307 |
)) |
| 308 |
} else { |
| 309 |
slot.with(totals(&analytics.totals)) |
| 310 |
}; |
| 311 |
|
| 312 |
Node::Region(slot) |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
fn range_heading(range: &str) -> &'static str { |
| 317 |
RANGES |
| 318 |
.iter() |
| 319 |
.find(|(value, _)| *value == range) |
| 320 |
.map_or("All time", |(_, name)| *name) |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
fn range_chips(range: &str) -> Vec<Node> { |
| 325 |
RANGES |
| 326 |
.iter() |
| 327 |
.map(|(value, _)| { |
| 328 |
Node::Token( |
| 329 |
Tag::chip(*value, Action::get(PATH).carrying("range", *value)) |
| 330 |
.latched(*value == range), |
| 331 |
) |
| 332 |
}) |
| 333 |
.collect() |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
fn stats(stats: &[StatView]) -> Node { |
| 338 |
Node::Stats { |
| 339 |
figures: stats |
| 340 |
.iter() |
| 341 |
.map(|stat| { |
| 342 |
let mut figure = Figure::new(stat.value.clone(), stat.label.clone()); |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
if let Some(change) = &stat.change { |
| 349 |
figure = figure.change(change.clone()).tone(if stat.positive { |
| 350 |
layout::Tone::Success |
| 351 |
} else { |
| 352 |
layout::Tone::Danger |
| 353 |
}); |
| 354 |
} |
| 355 |
(figure, None) |
| 356 |
}) |
| 357 |
.collect(), |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
fn comparison(projects: &[ProjectView]) -> Node { |
| 363 |
Node::Table { |
| 364 |
columns: vec![ |
| 365 |
Column::new("Project") |
| 366 |
.width(layout::Width::Fill) |
| 367 |
.priority(layout::Priority::Essential), |
| 368 |
Column::new("Revenue") |
| 369 |
.width(layout::Width::Content) |
| 370 |
.priority(layout::Priority::Essential), |
| 371 |
Column::new("Sales").width(layout::Width::Content), |
| 372 |
Column::new("Views").width(layout::Width::Content), |
| 373 |
Column::new("Conversion") |
| 374 |
.width(layout::Width::Content) |
| 375 |
.priority(layout::Priority::Optional), |
| 376 |
], |
| 377 |
rows: projects |
| 378 |
.iter() |
| 379 |
.map(|project| { |
| 380 |
Cells::new([ |
| 381 |
Cell::new(project.title.clone()), |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
Cell::new(project.revenue.clone()), |
| 387 |
Cell::new(project.sales.clone()), |
| 388 |
Cell::new(project.views.clone()), |
| 389 |
Cell::new(project.conversion.clone()), |
| 390 |
]) |
| 391 |
}) |
| 392 |
.collect(), |
| 393 |
|
| 394 |
|
| 395 |
more: None, |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
fn totals(totals: &[TotalView]) -> Node { |
| 401 |
Node::List { |
| 402 |
rows: totals |
| 403 |
.iter() |
| 404 |
.map(|total| Row::new(total.title.clone()).meta(total.revenue.clone())) |
| 405 |
.collect(), |
| 406 |
more: None, |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
fn chart_markup(bars: &[BarView]) -> String { |
| 421 |
let mut html = String::from("<div class=\"chart-bars\">"); |
| 422 |
for bar in bars { |
| 423 |
let plural = if bar.count == 1 { "" } else { "s" }; |
| 424 |
let _ = write!( |
| 425 |
html, |
| 426 |
"<div class=\"chart-bar-col\" data-tooltip=\"{} / {} sale{plural}\">\ |
| 427 |
<div class=\"chart-bar\" style=\"--fill: {}%;\"></div>\ |
| 428 |
<div class=\"chart-bar-label\">{}</div></div>", |
| 429 |
escape(&bar.value), |
| 430 |
bar.count, |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
format_args!("{:.4}", bar.height_pct), |
| 435 |
escape(&bar.label), |
| 436 |
); |
| 437 |
} |
| 438 |
html.push_str("</div>"); |
| 439 |
html |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
fn escape(text: &str) -> String { |
| 444 |
text.replace('&', "&") |
| 445 |
.replace('<', "<") |
| 446 |
.replace('>', ">") |
| 447 |
.replace('"', """) |
| 448 |
.replace('\'', "'") |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
pub fn renderer(viewer: &Viewer) -> Webview { |
| 456 |
let mut webview = Webview::new().with_shell(viewer.shell()); |
| 457 |
for (slot, markup) in viewer.drawn() { |
| 458 |
webview = webview.with_fill(slot, markup); |
| 459 |
} |
| 460 |
webview |
| 461 |
} |
| 462 |
|
| 463 |
#[cfg(test)] |
| 464 |
mod tests { |
| 465 |
use super::*; |
| 466 |
use quasi_axum::Serves; |
| 467 |
|
| 468 |
fn analytics() -> Analytics { |
| 469 |
Analytics { |
| 470 |
range: "30d".into(), |
| 471 |
stats: vec![ |
| 472 |
StatView { |
| 473 |
label: "Views".into(), |
| 474 |
value: "1,204".into(), |
| 475 |
change: Some("+12.5%".into()), |
| 476 |
positive: true, |
| 477 |
}, |
| 478 |
StatView { |
| 479 |
label: "Conversion".into(), |
| 480 |
value: "3.1%".into(), |
| 481 |
change: None, |
| 482 |
positive: true, |
| 483 |
}, |
| 484 |
], |
| 485 |
bars: vec![BarView { |
| 486 |
label: "Aug 1".into(), |
| 487 |
value: "$42.00".into(), |
| 488 |
count: 3, |
| 489 |
height_pct: 62.5, |
| 490 |
}], |
| 491 |
projects: vec![ |
| 492 |
ProjectView { |
| 493 |
title: "Atlas".into(), |
| 494 |
revenue: "$120.00".into(), |
| 495 |
sales: "4".into(), |
| 496 |
views: "300".into(), |
| 497 |
conversion: "1.3%".into(), |
| 498 |
}, |
| 499 |
ProjectView { |
| 500 |
title: "Beacon".into(), |
| 501 |
revenue: "$60.00".into(), |
| 502 |
sales: "2".into(), |
| 503 |
views: "150".into(), |
| 504 |
conversion: "1.3%".into(), |
| 505 |
}, |
| 506 |
], |
| 507 |
totals: vec![TotalView { |
| 508 |
title: "Atlas".into(), |
| 509 |
revenue: "$980.00".into(), |
| 510 |
}], |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
fn render(node: &Node) -> String { |
| 515 |
Webview::new().fragment(node) |
| 516 |
} |
| 517 |
|
| 518 |
#[test] |
| 519 |
fn the_region_matches_what_the_tab_nav_targets() { |
| 520 |
let nav = include_str!("../../templates/partials/tabs/user_analytics.html"); |
| 521 |
assert!(nav.contains(&format!("hx-target=\"#{REGION}\""))); |
| 522 |
assert!(nav.contains(&format!("hx-get=\"{PATH}?range=7d\""))); |
| 523 |
} |
| 524 |
|
| 525 |
#[test] |
| 526 |
fn exactly_one_range_is_held_down_and_each_carries_its_own() { |
| 527 |
let html = render(&Node::Region( |
| 528 |
range_chips("90d") |
| 529 |
.into_iter() |
| 530 |
.fold(Slot::new(REGION, RegionKind::Pane), Slot::with), |
| 531 |
)); |
| 532 |
|
| 533 |
assert_eq!(html.matches("latched").count(), 1, "{html}"); |
| 534 |
for range in ["7d", "30d", "90d", "all"] { |
| 535 |
assert!( |
| 536 |
html.contains(&format!("range={range}")), |
| 537 |
"{range} is offered: {html}" |
| 538 |
); |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
assert_eq!(range_heading("nonsense"), "All time"); |
| 543 |
assert_eq!(range_heading("7d"), "Last 7 days"); |
| 544 |
} |
| 545 |
|
| 546 |
#[test] |
| 547 |
fn a_delta_is_toned_and_a_card_without_one_is_not() { |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
let html = render(&stats(&analytics().stats)); |
| 552 |
|
| 553 |
assert!(html.contains("+12.5%"), "{html}"); |
| 554 |
assert_eq!( |
| 555 |
html.matches("data-tone").count(), |
| 556 |
1, |
| 557 |
"one card is toned: {html}" |
| 558 |
); |
| 559 |
assert!(html.contains("data-tone=\"success\""), "{html}"); |
| 560 |
assert!( |
| 561 |
html.contains("3.1%"), |
| 562 |
"the untoned card is still there: {html}" |
| 563 |
); |
| 564 |
} |
| 565 |
|
| 566 |
#[test] |
| 567 |
fn the_chart_is_the_markup_the_template_already_emits() { |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
let html = chart_markup(&analytics().bars); |
| 572 |
|
| 573 |
assert!(html.contains("class=\"chart-bars\""), "{html}"); |
| 574 |
assert!(html.contains("class=\"chart-bar-col\""), "{html}"); |
| 575 |
assert!(html.contains("--fill: 62.5000%"), "{html}"); |
| 576 |
assert!(html.contains("3 sales"), "{html}"); |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
let one = chart_markup(&[BarView { |
| 581 |
label: "Aug 2".into(), |
| 582 |
count: 1, |
| 583 |
..analytics().bars.pop().expect("one bar") |
| 584 |
}]); |
| 585 |
assert!(one.contains("1 sale ") || one.contains("1 sale\""), "{one}"); |
| 586 |
} |
| 587 |
|
| 588 |
#[test] |
| 589 |
fn a_bespoke_fill_is_the_apps_markup_and_still_escapes_its_data() { |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
let html = chart_markup(&[BarView { |
| 595 |
label: "<script>x()</script>".into(), |
| 596 |
value: "\" onload=\"x()".into(), |
| 597 |
count: 1, |
| 598 |
height_pct: 10.0, |
| 599 |
}]); |
| 600 |
|
| 601 |
assert!(!html.contains("<script>x()"), "{html}"); |
| 602 |
assert!(!html.contains("\" onload="), "{html}"); |
| 603 |
} |
| 604 |
|
| 605 |
#[test] |
| 606 |
fn the_comparison_only_draws_when_there_is_something_to_compare() { |
| 607 |
let mut one = analytics(); |
| 608 |
one.projects.truncate(1); |
| 609 |
let html = render(&pane(&one)); |
| 610 |
assert!(!html.contains("Project Comparison"), "{html}"); |
| 611 |
|
| 612 |
let both = render(&pane(&analytics())); |
| 613 |
assert!(both.contains("Project Comparison"), "{both}"); |
| 614 |
assert!(both.contains("Atlas") && both.contains("Beacon"), "{both}"); |
| 615 |
} |
| 616 |
|
| 617 |
#[test] |
| 618 |
fn an_account_with_no_history_says_so_in_both_places() { |
| 619 |
let empty = Analytics { |
| 620 |
range: "30d".into(), |
| 621 |
stats: vec![], |
| 622 |
bars: vec![], |
| 623 |
projects: vec![], |
| 624 |
totals: vec![], |
| 625 |
}; |
| 626 |
let html = render(&pane(&empty)); |
| 627 |
|
| 628 |
assert!(html.contains("revenue data will appear here."), "{html}"); |
| 629 |
assert!(html.contains("Sales across your projects"), "{html}"); |
| 630 |
|
| 631 |
|
| 632 |
assert!(!html.contains(CHART_SLOT), "{html}"); |
| 633 |
} |
| 634 |
} |
| 635 |
|