use std::sync::Arc;
use adk_core::{Clock, SystemClock};
use adk_harness::web::SearchProvider;
use adk_kernel::charter::KpiSource;
use adk_server::awp::BeaconStore;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::{Tally, check_site, total};
const HITS: usize = 10;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AeoObservation {
pub provider: String,
pub question: String,
pub cited: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rank: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AeoReport {
pub at: DateTime<Utc>,
pub observations: Vec<AeoObservation>,
pub failures: Vec<String>,
}
pub struct AeoTracker {
site: Uuid,
domain: String,
questions: Vec<String>,
providers: Vec<Arc<dyn SearchProvider>>,
counts: Arc<dyn BeaconStore>,
clock: Arc<dyn Clock>,
}
impl std::fmt::Debug for AeoTracker {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AeoTracker")
.field("site", &self.site)
.field("domain", &self.domain)
.field("questions", &self.questions.len())
.field("providers", &self.providers.len())
.finish_non_exhaustive()
}
}
impl AeoTracker {
pub fn new(site: Uuid, domain: impl Into<String>, counts: Arc<dyn BeaconStore>) -> Self {
Self {
site,
domain: domain.into().trim_start_matches("www.").to_ascii_lowercase(),
questions: Vec::new(),
providers: Vec::new(),
counts,
clock: Arc::new(SystemClock),
}
}
#[must_use]
pub fn question(mut self, question: impl Into<String>) -> Self {
self.questions.push(question.into());
self
}
#[must_use]
pub fn provider(mut self, provider: Arc<dyn SearchProvider>) -> Self {
self.providers.push(provider);
self
}
#[must_use]
pub fn with_clock(mut self, clock: Arc<dyn Clock>) -> Self {
self.clock = clock;
self
}
fn cites(&self, url: &str) -> bool {
url::Url::parse(url)
.ok()
.and_then(|url| url.host_str().map(str::to_ascii_lowercase))
.is_some_and(|host| host == self.domain || host.ends_with(&format!(".{}", self.domain)))
}
pub async fn run(&self) -> adk_kernel::Result<AeoReport> {
let at = self.clock.now();
let day = at.date_naive();
let mut tally = Tally::default();
let mut report = AeoReport { at, observations: Vec::new(), failures: Vec::new() };
for provider in &self.providers {
for question in &self.questions {
match provider.search(question, HITS, None).await {
Ok(hits) => {
let found = hits
.iter()
.take(HITS)
.enumerate()
.find(|(_, hit)| self.cites(&hit.url));
tally.add(day, "aeo.queries", 1);
if let Some((index, _)) = found {
tally.add(day, "aeo.citations", 1);
tally.add(day, format!("aeo.citations:{}", provider.name()), 1);
if index < 3 {
tally.add(day, "aeo.top3", 1);
}
}
report.observations.push(AeoObservation {
provider: provider.name().to_string(),
question: question.clone(),
cited: found.is_some(),
rank: found.map(|(index, _)| index + 1),
url: found.map(|(_, hit)| hit.url.clone()),
});
}
Err(error) => {
tracing::warn!(provider = provider.name(), error = %error, "an AEO query failed");
report.failures.push(format!("{}: {error}", provider.name()));
}
}
}
}
tally.flush("aeo", self.counts.as_ref()).await?;
Ok(report)
}
}
#[derive(Clone)]
pub struct AeoKpi {
site: Uuid,
counts: Arc<dyn BeaconStore>,
}
impl std::fmt::Debug for AeoKpi {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AeoKpi").field("site", &self.site).finish_non_exhaustive()
}
}
impl AeoKpi {
pub fn new(site: Uuid, counts: Arc<dyn BeaconStore>) -> Self {
Self { site, counts }
}
}
#[async_trait]
impl KpiSource for AeoKpi {
fn name(&self) -> &str {
"aeo"
}
async fn read(
&self,
site: Uuid,
metric: &str,
from: DateTime<Utc>,
to: DateTime<Utc>,
) -> adk_kernel::Result<Option<f64>> {
check_site("aeo", self.site, site)?;
match metric {
"pages.answer_citations" => total("aeo", &self.counts, "aeo.citations", from, to).await,
"aeo.citation_rate" => {
let queries = total("aeo", &self.counts, "aeo.queries", from, to).await?;
let citations = total("aeo", &self.counts, "aeo.citations", from, to).await?;
Ok(queries
.zip(citations)
.filter(|(queries, _)| *queries > 0.0)
.map(|(queries, citations)| citations / queries))
}
_ => total("aeo", &self.counts, metric, from, to).await,
}
}
}