use std::collections::HashMap;
use serde::ser::{SerializeMap, Serializer};
use serde::{Deserialize, Serialize};
use qql_plan::{
PlanFacetValue, PlanGroupId, PlanPointId, PlanShardKey, PlanVectorStruct, QuotaConfig,
};
use crate::backend::CollectionInfo;
use super::telemetry::{PhaseTimings, ServerTelemetry, ServerUsage};
#[derive(Debug, Clone, Serialize)]
pub struct ExecResponse {
pub ok: bool,
pub operation: String,
pub message: String,
pub data: Option<ExecData>,
#[serde(skip_serializing_if = "Option::is_none")]
pub telemetry: Option<ServerTelemetry>,
}
#[derive(Debug, Clone, Serialize)]
pub struct ExecutionReport {
pub ok: bool,
pub results: Vec<ExecResponse>,
pub succeeded: usize,
pub failed: usize,
#[serde(skip_serializing_if = "Option::is_none")]
pub telemetry: Option<ServerTelemetry>,
}
impl ExecutionReport {
pub fn from_results(results: Vec<ExecResponse>) -> Self {
let succeeded = results.iter().filter(|r| r.ok).count();
let failed = results.len() - succeeded;
let telemetry =
ServerTelemetry::aggregate(results.iter().filter_map(|r| r.telemetry.as_ref()));
Self {
ok: failed == 0,
results,
succeeded,
failed,
telemetry,
}
}
pub fn single(resp: ExecResponse) -> Self {
let ok = resp.ok;
let telemetry = resp.telemetry.clone();
Self {
ok,
results: vec![resp],
succeeded: if ok { 1 } else { 0 },
failed: if ok { 0 } else { 1 },
telemetry,
}
}
pub fn first(&self) -> Option<&ExecResponse> {
self.results.first()
}
pub fn hits(&self, stmt: usize) -> Option<Vec<SearchHit>> {
self.results.get(stmt).and_then(|r| r.hits())
}
pub fn ids(&self, stmt: usize) -> Vec<u64> {
self.results.get(stmt).map(|r| r.ids()).unwrap_or_default()
}
pub fn count(&self, stmt: usize) -> Option<u64> {
self.results.get(stmt).and_then(|r| r.count())
}
pub fn facet(&self, stmt: usize) -> Option<Vec<(PlanFacetValue, u64)>> {
self.results.get(stmt).and_then(|r| r.facet())
}
pub fn first_count(&self) -> Option<u64> {
self.count(0)
}
pub fn first_hits(&self) -> Option<Vec<SearchHit>> {
self.hits(0)
}
pub fn first_ids(&self) -> Vec<u64> {
self.ids(0)
}
pub fn first_facet(&self) -> Option<Vec<(PlanFacetValue, u64)>> {
self.facet(0)
}
}
#[derive(Debug, Clone, Serialize)]
pub struct AnalyzeReport {
pub ok: bool,
pub plan: String,
pub phases: PhaseTimings,
#[serde(skip_serializing_if = "Option::is_none")]
pub server_time_s: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub usage: Option<ServerUsage>,
pub results: Vec<ExecResponse>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum OnError {
#[default]
Stop,
Continue,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct SearchHit {
pub id: PlanPointId,
#[serde(serialize_with = "serialize_score_f32")]
pub score: f32,
pub payload: Option<HashMap<String, serde_json::Value>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub collection: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub vector: Option<PlanVectorStruct>,
}
fn serialize_score_f32<S>(score: &f32, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match score.to_string().parse::<f64>() {
Ok(value) => serializer.serialize_f64(value),
Err(_) => serializer.serialize_f32(*score),
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct GroupedSearchResult {
#[serde(rename = "id")]
pub group_id: PlanGroupId,
pub hits: Vec<SearchHit>,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct FacetHit {
pub value: PlanFacetValue,
pub count: u64,
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq)]
pub enum ExecData {
Hits(Vec<SearchHit>),
Groups(Vec<GroupedSearchResult>),
Count(u64),
Facet(Vec<FacetHit>),
Mutation {
affected: Option<u64>,
},
Collections(Vec<String>),
Collection(CollectionInfo),
ShardKeys(Vec<PlanShardKey>),
Quotas(QuotaConfig),
}
impl Serialize for ExecData {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ExecData::Hits(hits) => hits.serialize(serializer),
ExecData::Groups(groups) => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("groups", groups)?;
map.end()
}
ExecData::Count(count) => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("count", count)?;
map.end()
}
ExecData::Facet(hits) => hits.serialize(serializer),
ExecData::Mutation {
affected: Some(count),
} => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("count", count)?;
map.end()
}
ExecData::Mutation { affected: None } => serializer.serialize_none(),
ExecData::Collections(collections) => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("collections", collections)?;
map.end()
}
ExecData::Collection(info) => info.serialize(serializer),
ExecData::ShardKeys(keys) => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("shard_keys", keys)?;
map.end()
}
ExecData::Quotas(config) => config.serialize(serializer),
}
}
}
impl ExecData {
pub fn hits(&self) -> Option<&[SearchHit]> {
match self {
ExecData::Hits(hits) => Some(hits),
_ => None,
}
}
pub fn groups(&self) -> Option<&[GroupedSearchResult]> {
match self {
ExecData::Groups(groups) => Some(groups),
_ => None,
}
}
pub fn count(&self) -> Option<u64> {
match self {
ExecData::Count(count) => Some(*count),
ExecData::Mutation { affected } => *affected,
_ => None,
}
}
pub fn mutation_affected(&self) -> Option<Option<u64>> {
match self {
ExecData::Mutation { affected } => Some(*affected),
_ => None,
}
}
pub fn facet(&self) -> Option<&[FacetHit]> {
match self {
ExecData::Facet(hits) => Some(hits),
_ => None,
}
}
pub fn collections(&self) -> Option<&[String]> {
match self {
ExecData::Collections(collections) => Some(collections),
_ => None,
}
}
pub fn collection(&self) -> Option<&CollectionInfo> {
match self {
ExecData::Collection(info) => Some(info),
_ => None,
}
}
pub fn shard_keys(&self) -> Option<&[PlanShardKey]> {
match self {
ExecData::ShardKeys(keys) => Some(keys),
_ => None,
}
}
pub fn quotas(&self) -> Option<&QuotaConfig> {
match self {
ExecData::Quotas(config) => Some(config),
_ => None,
}
}
pub fn is_empty(&self) -> bool {
match self {
ExecData::Hits(hits) => hits.is_empty(),
ExecData::Groups(groups) => groups.is_empty(),
ExecData::Count(_)
| ExecData::Mutation { .. }
| ExecData::Collection(_)
| ExecData::Quotas(_) => false,
ExecData::Facet(hits) => hits.is_empty(),
ExecData::Collections(collections) => collections.is_empty(),
ExecData::ShardKeys(keys) => keys.is_empty(),
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct BackendResponse {
pub data: ExecData,
#[serde(skip_serializing_if = "Option::is_none")]
pub telemetry: Option<ServerTelemetry>,
}
impl ExecResponse {
pub fn hits_ref(&self) -> Option<&[SearchHit]> {
self.data.as_ref().and_then(ExecData::hits)
}
pub fn hits(&self) -> Option<Vec<SearchHit>> {
self.hits_ref().map(<[SearchHit]>::to_vec)
}
pub fn ids(&self) -> Vec<u64> {
let Some(hits) = self.hits_ref() else {
return Vec::new();
};
hits.iter()
.filter_map(|hit| match &hit.id {
PlanPointId::Number(id) => Some(*id),
PlanPointId::String(id) => id.parse().ok(),
})
.collect()
}
pub fn count(&self) -> Option<u64> {
self.data.as_ref().and_then(ExecData::count)
}
pub fn facet(&self) -> Option<Vec<(PlanFacetValue, u64)>> {
let entries = self.data.as_ref()?.facet()?;
Some(
entries
.iter()
.map(|entry| (entry.value.clone(), entry.count))
.collect(),
)
}
}