mecomp_storage/db/schemas/
analysis.rs

1#![allow(clippy::module_name_repetitions)]
2#[cfg(not(feature = "db"))]
3use super::{Id, Thing};
4#[cfg(feature = "db")]
5use surrealdb::sql::{Id, Thing};
6
7pub type AnalysisId = Thing;
8
9pub const TABLE_NAME: &str = "analysis";
10
11/// This struct holds the [`Analysis`] of a particular [`Song`].
12///
13/// An [`Analysis`] is the features extracted by the `mecomp-analysis` library and are used for recommendations (nearest neighbor search)
14/// and Collection generation (clustering).
15#[derive(Clone, Debug, PartialEq)]
16#[cfg_attr(feature = "db", derive(surrealqlx::Table))]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18#[cfg_attr(feature = "db", Table("analysis"))]
19pub struct Analysis {
20    /// the unique identifier for this [`Analysis`].
21    #[cfg_attr(feature = "db", field("any"))]
22    pub id: AnalysisId,
23
24    /// The [`Song`]'s audio features.
25    #[cfg_attr(feature = "db", field(dt = "array<float>", index(vector(dim = 20))))]
26    pub features: [f64; 20],
27}
28
29impl Analysis {
30    #[must_use]
31    #[inline]
32    pub fn generate_id() -> AnalysisId {
33        Thing::from((TABLE_NAME, Id::ulid()))
34    }
35}
36
37impl From<&Analysis> for mecomp_analysis::Analysis {
38    #[inline]
39    fn from(analysis: &Analysis) -> Self {
40        Self::new(analysis.features)
41    }
42}
43
44impl From<Analysis> for mecomp_analysis::Analysis {
45    #[inline]
46    fn from(analysis: Analysis) -> Self {
47        Self::new(analysis.features)
48    }
49}