use native_db::{native_db, ToKey};
use native_model::{native_model, Model};
use serde::{Deserialize, Serialize};
pub use super::v1::{
Action, AncestorLocal, Device, DevicePrettyLocation, DeviceType, Heartbeat, Herd, Layer,
MediaType, Plan, PlanInsert, PlanType, ResponseScout, ResponseScoutStatus, Session,
SessionLocal, Syncable, TagObservationType, Zone,
};
pub use super::v2::{Event, EventLocal, Operator, OperatorLocal};
pub use super::v6::{Connectivity, ConnectivityLocal};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[native_model(id = 19, version = 4)]
#[native_db]
pub struct ArtifactLocal {
pub id: Option<i64>,
#[primary_key]
pub id_local: Option<String>,
#[secondary_key]
pub ancestor_id_local: Option<String>,
pub created_at: Option<String>,
pub file_path: String,
#[secondary_key]
pub session_id: Option<i64>,
pub timestamp_observation: Option<String>,
pub modality: Option<String>,
pub device_id: i64,
pub updated_at: Option<String>,
pub timestamp_observation_end: String,
pub has_uploaded_file_to_storage: bool,
pub upload_url: Option<String>,
pub upload_url_generated_at: Option<String>,
pub embedding_qwen_vl_2b: Option<Vec<f32>>,
pub embedding_vertex_mm_01: Option<Vec<f32>>,
pub file_size_bytes: Option<i64>,
pub tagged_at: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Artifact {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created_at: Option<String>,
pub file_path: String,
pub session_id: Option<i64>,
pub timestamp_observation: Option<String>,
pub modality: Option<String>,
pub device_id: i64,
pub updated_at: Option<String>,
pub timestamp_observation_end: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(
default,
deserialize_with = "super::serde_helpers::deserialize_embedding"
)]
pub embedding_qwen_vl_2b: Option<Vec<f32>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(
default,
deserialize_with = "super::serde_helpers::deserialize_embedding"
)]
pub embedding_vertex_mm_01: Option<Vec<f32>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub file_size_bytes: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tagged_at: Option<String>,
}
impl Default for ArtifactLocal {
fn default() -> Self {
use chrono::Utc;
Self {
id: None,
id_local: None,
ancestor_id_local: None,
created_at: None,
file_path: String::new(),
session_id: None,
timestamp_observation: None,
modality: None,
device_id: 0,
updated_at: None,
timestamp_observation_end: Utc::now().to_rfc3339(),
has_uploaded_file_to_storage: false,
upload_url: None,
upload_url_generated_at: None,
embedding_qwen_vl_2b: None,
embedding_vertex_mm_01: None,
file_size_bytes: None,
tagged_at: None,
}
}
}
impl super::v1::Syncable for ArtifactLocal {
fn id(&self) -> Option<i64> {
self.id
}
fn set_id(&mut self, id: i64) {
self.id = Some(id);
}
fn id_local(&self) -> Option<String> {
self.id_local.clone()
}
fn set_id_local(&mut self, id_local: String) {
self.id_local = Some(id_local);
}
}
impl super::v1::AncestorLocal for ArtifactLocal {
fn ancestor_id_local(&self) -> Option<String> {
self.ancestor_id_local.clone()
}
fn set_ancestor_id_local(&mut self, ancestor_id_local: String) {
self.ancestor_id_local = Some(ancestor_id_local);
}
}
impl From<ArtifactLocal> for Artifact {
fn from(local: ArtifactLocal) -> Self {
Self {
id: local.id,
created_at: local.created_at,
file_path: local.file_path,
session_id: local.session_id,
timestamp_observation: local.timestamp_observation,
modality: local.modality,
device_id: local.device_id,
updated_at: local.updated_at,
timestamp_observation_end: local.timestamp_observation_end,
embedding_qwen_vl_2b: local.embedding_qwen_vl_2b,
embedding_vertex_mm_01: local.embedding_vertex_mm_01,
file_size_bytes: local.file_size_bytes,
tagged_at: local.tagged_at,
}
}
}
impl From<Artifact> for ArtifactLocal {
fn from(a: Artifact) -> Self {
Self {
id: a.id,
id_local: None,
ancestor_id_local: None,
created_at: a.created_at,
file_path: a.file_path,
session_id: a.session_id,
timestamp_observation: a.timestamp_observation,
modality: a.modality,
device_id: a.device_id,
updated_at: a.updated_at,
timestamp_observation_end: a.timestamp_observation_end,
has_uploaded_file_to_storage: false,
upload_url: None,
upload_url_generated_at: None,
embedding_qwen_vl_2b: a.embedding_qwen_vl_2b,
embedding_vertex_mm_01: a.embedding_vertex_mm_01,
file_size_bytes: a.file_size_bytes,
tagged_at: a.tagged_at,
}
}
}
impl Artifact {
pub fn new(
file_path: String,
session_id: Option<i64>,
device_id: i64,
modality: Option<String>,
timestamp_observation: Option<String>,
) -> Self {
use chrono::Utc;
Self {
id: None,
created_at: None,
file_path,
session_id,
timestamp_observation,
modality,
device_id,
updated_at: None,
timestamp_observation_end: Utc::now().to_rfc3339(),
embedding_qwen_vl_2b: None,
embedding_vertex_mm_01: None,
file_size_bytes: None,
tagged_at: None,
}
}
}
impl ArtifactLocal {
pub fn new(
file_path: String,
session_id: Option<i64>,
device_id: i64,
modality: Option<String>,
timestamp_observation: Option<String>,
) -> Self {
use chrono::Utc;
Self {
id: None,
id_local: None,
ancestor_id_local: None,
created_at: None,
file_path,
session_id,
timestamp_observation,
modality,
device_id,
updated_at: None,
timestamp_observation_end: Utc::now().to_rfc3339(),
has_uploaded_file_to_storage: false,
upload_url: None,
upload_url_generated_at: None,
embedding_qwen_vl_2b: None,
embedding_vertex_mm_01: None,
file_size_bytes: None,
tagged_at: None,
}
}
pub fn mark_file_uploaded(&mut self) {
self.has_uploaded_file_to_storage = true;
}
pub fn mark_file_not_uploaded(&mut self) {
self.has_uploaded_file_to_storage = false;
}
pub fn is_file_uploaded(&self) -> bool {
self.has_uploaded_file_to_storage
}
pub fn needs_file_upload(&self) -> bool {
!self.has_uploaded_file_to_storage
}
}
impl From<super::v2::ArtifactLocal> for ArtifactLocal {
fn from(v2: super::v2::ArtifactLocal) -> Self {
Self {
id: v2.id,
id_local: v2.id_local,
ancestor_id_local: v2.ancestor_id_local,
created_at: v2.created_at,
file_path: v2.file_path,
session_id: v2.session_id,
timestamp_observation: v2.timestamp_observation,
modality: v2.modality,
device_id: v2.device_id,
updated_at: v2.updated_at,
timestamp_observation_end: v2.timestamp_observation_end,
has_uploaded_file_to_storage: v2.has_uploaded_file_to_storage,
upload_url: v2.upload_url,
upload_url_generated_at: v2.upload_url_generated_at,
embedding_qwen_vl_2b: v2.embedding_qwen_vl_2b,
embedding_vertex_mm_01: v2.embedding_vertex_mm_01,
file_size_bytes: None,
tagged_at: None,
}
}
}
impl From<super::v2::Artifact> for Artifact {
fn from(v2: super::v2::Artifact) -> Self {
Self {
id: v2.id,
created_at: v2.created_at,
file_path: v2.file_path,
session_id: v2.session_id,
timestamp_observation: v2.timestamp_observation,
modality: v2.modality,
device_id: v2.device_id,
updated_at: v2.updated_at,
timestamp_observation_end: v2.timestamp_observation_end,
embedding_qwen_vl_2b: v2.embedding_qwen_vl_2b,
embedding_vertex_mm_01: v2.embedding_vertex_mm_01,
file_size_bytes: None,
tagged_at: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[native_model(id = 17, version = 2)]
#[native_db]
pub struct TagLocal {
pub id: Option<i64>,
#[primary_key]
pub id_local: Option<String>,
pub inserted_at: Option<String>,
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
pub conf: f64,
pub observation_type: super::v1::TagObservationType,
#[serde(rename = "class")]
pub class: String,
#[secondary_key]
pub event_id: Option<i64>,
#[secondary_key]
pub ancestor_id_local: Option<String>,
pub location: Option<String>,
pub origin_location: Option<String>,
pub origin_pitch: Option<f64>,
pub origin_heading: Option<f64>,
pub origin_roll: Option<f64>,
pub sensor_pitch: Option<f64>,
pub sensor_yaw: Option<f64>,
pub sensor_roll: Option<f64>,
pub origin_height: Option<f64>,
pub subject_location: Option<String>,
pub subject_height: Option<f64>,
pub artifact_id: Option<i64>,
#[serde(default)]
pub detector: String,
pub timestamp_observation: Option<String>,
pub frame_index: Option<i64>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Tag {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub inserted_at: Option<String>,
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
pub conf: f64,
pub observation_type: super::v1::TagObservationType,
#[serde(rename = "class")]
pub class: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub event_id: Option<i64>,
pub location: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub origin_location: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub origin_pitch: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub origin_heading: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub origin_roll: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sensor_pitch: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sensor_yaw: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sensor_roll: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub origin_height: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub subject_location: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub subject_height: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub artifact_id: Option<i64>,
#[serde(default)]
pub detector: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub timestamp_observation: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub frame_index: Option<i64>,
}
impl Default for TagLocal {
fn default() -> Self {
Self {
id: None,
id_local: None,
inserted_at: None,
x: 0.0,
y: 0.0,
width: 0.0,
height: 0.0,
conf: 0.0,
observation_type: super::v1::TagObservationType::Auto,
class: String::new(),
event_id: None,
ancestor_id_local: None,
location: None,
origin_location: None,
origin_pitch: None,
origin_heading: None,
origin_roll: None,
sensor_pitch: None,
sensor_yaw: None,
sensor_roll: None,
origin_height: None,
subject_location: None,
subject_height: None,
artifact_id: None,
detector: String::new(),
timestamp_observation: None,
frame_index: None,
}
}
}
impl Default for Tag {
fn default() -> Self {
Self {
id: None,
inserted_at: None,
x: 0.0,
y: 0.0,
width: 0.0,
height: 0.0,
conf: 0.0,
observation_type: super::v1::TagObservationType::Manual,
class: String::new(),
event_id: None,
location: None,
origin_location: None,
origin_pitch: None,
origin_heading: None,
origin_roll: None,
sensor_pitch: None,
sensor_yaw: None,
sensor_roll: None,
origin_height: None,
subject_location: None,
subject_height: None,
artifact_id: None,
detector: String::new(),
timestamp_observation: None,
frame_index: None,
}
}
}
impl super::v1::Syncable for TagLocal {
fn id(&self) -> Option<i64> {
self.id
}
fn set_id(&mut self, id: i64) {
self.id = Some(id);
}
fn id_local(&self) -> Option<String> {
self.id_local.clone()
}
fn set_id_local(&mut self, id_local: String) {
self.id_local = Some(id_local);
}
}
impl super::v1::Syncable for Tag {
fn id(&self) -> Option<i64> {
self.id
}
fn set_id(&mut self, id: i64) {
self.id = Some(id);
}
fn id_local(&self) -> Option<String> {
None
}
fn set_id_local(&mut self, _: String) {}
}
impl super::v1::AncestorLocal for TagLocal {
fn ancestor_id_local(&self) -> Option<String> {
self.ancestor_id_local.clone()
}
fn set_ancestor_id_local(&mut self, ancestor_id_local: String) {
self.ancestor_id_local = Some(ancestor_id_local);
}
}
impl From<TagLocal> for Tag {
fn from(local: TagLocal) -> Self {
Self {
id: local.id,
inserted_at: local.inserted_at,
x: local.x,
y: local.y,
width: local.width,
height: local.height,
conf: local.conf,
observation_type: local.observation_type,
class: local.class,
event_id: local.event_id,
location: local.location,
origin_location: local.origin_location,
origin_pitch: local.origin_pitch,
origin_heading: local.origin_heading,
origin_roll: local.origin_roll,
sensor_pitch: local.sensor_pitch,
sensor_yaw: local.sensor_yaw,
sensor_roll: local.sensor_roll,
origin_height: local.origin_height,
subject_location: local.subject_location,
subject_height: local.subject_height,
artifact_id: local.artifact_id,
detector: local.detector,
timestamp_observation: local.timestamp_observation,
frame_index: local.frame_index,
}
}
}
impl From<Tag> for TagLocal {
fn from(tag: Tag) -> Self {
Self {
id: tag.id,
id_local: None,
inserted_at: tag.inserted_at,
x: tag.x,
y: tag.y,
width: tag.width,
height: tag.height,
conf: tag.conf,
observation_type: tag.observation_type,
class: tag.class,
event_id: tag.event_id,
ancestor_id_local: None,
location: tag.location,
origin_location: tag.origin_location,
origin_pitch: tag.origin_pitch,
origin_heading: tag.origin_heading,
origin_roll: tag.origin_roll,
sensor_pitch: tag.sensor_pitch,
sensor_yaw: tag.sensor_yaw,
sensor_roll: tag.sensor_roll,
origin_height: tag.origin_height,
subject_location: tag.subject_location,
subject_height: tag.subject_height,
artifact_id: tag.artifact_id,
detector: tag.detector,
timestamp_observation: tag.timestamp_observation,
frame_index: tag.frame_index,
}
}
}
impl Tag {
pub fn new(
_class_id: i64,
x: f64,
y: f64,
width: f64,
height: f64,
conf: f64,
observation_type: super::v1::TagObservationType,
class: String,
) -> Self {
Self {
id: None,
inserted_at: None,
x,
y,
width,
height,
conf,
observation_type,
class,
event_id: None,
location: None,
origin_location: None,
origin_pitch: None,
origin_heading: None,
origin_roll: None,
sensor_pitch: None,
sensor_yaw: None,
sensor_roll: None,
origin_height: None,
subject_location: None,
subject_height: None,
artifact_id: None,
detector: String::new(),
timestamp_observation: None,
frame_index: None,
}
}
pub fn new_with_location(
_class_id: i64,
x: f64,
y: f64,
width: f64,
height: f64,
conf: f64,
observation_type: super::v1::TagObservationType,
class: String,
latitude: f64,
longitude: f64,
) -> Self {
let mut tag = Self::new(
_class_id,
x,
y,
width,
height,
conf,
observation_type,
class,
);
tag.set_location(latitude, longitude);
tag
}
pub fn update_event_id(&mut self, event_id: i64) {
self.event_id = Some(event_id);
}
pub fn set_location(&mut self, latitude: f64, longitude: f64) {
self.location = Some(Self::format_location(latitude, longitude));
}
pub fn clear_location(&mut self) {
self.location = None;
}
pub fn format_location(latitude: f64, longitude: f64) -> String {
format!("POINT({} {})", longitude, latitude)
}
pub fn parse_location(location: &str) -> Option<(f64, f64)> {
if let Some(coords) = location
.strip_prefix("POINT(")
.and_then(|s| s.strip_suffix(")"))
{
let parts: Vec<&str> = coords.split_whitespace().collect();
if parts.len() == 2 {
if let (Ok(lon), Ok(lat)) = (parts[0].parse::<f64>(), parts[1].parse::<f64>()) {
return Some((lat, lon));
}
}
}
None
}
pub fn get_coordinates(&self) -> Option<(f64, f64)> {
self.location
.as_ref()
.and_then(|loc| Self::parse_location(loc))
}
}
impl TagLocal {
pub fn new(
_class_id: i64,
x: f64,
y: f64,
width: f64,
height: f64,
conf: f64,
observation_type: super::v1::TagObservationType,
class: String,
) -> Self {
Self {
id: None,
id_local: None,
inserted_at: None,
x,
y,
width,
height,
conf,
observation_type,
class,
event_id: None,
ancestor_id_local: None,
location: None,
origin_location: None,
origin_pitch: None,
origin_heading: None,
origin_roll: None,
sensor_pitch: None,
sensor_yaw: None,
sensor_roll: None,
origin_height: None,
subject_location: None,
subject_height: None,
artifact_id: None,
detector: String::new(),
timestamp_observation: None,
frame_index: None,
}
}
pub fn new_with_location(
_class_id: i64,
x: f64,
y: f64,
width: f64,
height: f64,
conf: f64,
observation_type: super::v1::TagObservationType,
class: String,
latitude: f64,
longitude: f64,
) -> Self {
let mut tag = Self::new(
_class_id,
x,
y,
width,
height,
conf,
observation_type,
class,
);
tag.set_location(latitude, longitude);
tag
}
pub fn update_event_id(&mut self, event_id: i64) {
self.event_id = Some(event_id);
}
pub fn update_ancestor_id_local(&mut self, ancestor_id_local: String) {
self.ancestor_id_local = Some(ancestor_id_local);
}
pub fn set_location(&mut self, latitude: f64, longitude: f64) {
self.location = Some(Self::format_location(latitude, longitude));
}
pub fn clear_location(&mut self) {
self.location = None;
}
pub fn format_location(latitude: f64, longitude: f64) -> String {
format!("POINT({} {})", longitude, latitude)
}
pub fn parse_location(location: &str) -> Option<(f64, f64)> {
if let Some(coords) = location
.strip_prefix("POINT(")
.and_then(|s| s.strip_suffix(")"))
{
let parts: Vec<&str> = coords.split_whitespace().collect();
if parts.len() == 2 {
if let (Ok(lon), Ok(lat)) = (parts[0].parse::<f64>(), parts[1].parse::<f64>()) {
return Some((lat, lon));
}
}
}
None
}
pub fn get_coordinates(&self) -> Option<(f64, f64)> {
self.location
.as_ref()
.and_then(|loc| Self::parse_location(loc))
}
}
impl From<super::v1::TagLocal> for TagLocal {
fn from(v1: super::v1::TagLocal) -> Self {
Self {
id: v1.id,
id_local: v1.id_local,
inserted_at: v1.inserted_at,
x: v1.x,
y: v1.y,
width: v1.width,
height: v1.height,
conf: v1.conf,
observation_type: v1.observation_type,
class: v1.class_name,
event_id: Some(v1.event_id),
ancestor_id_local: v1.ancestor_id_local,
location: v1.location,
origin_location: None,
origin_pitch: None,
origin_heading: None,
origin_roll: None,
sensor_pitch: None,
sensor_yaw: None,
sensor_roll: None,
origin_height: None,
subject_location: None,
subject_height: None,
artifact_id: None,
detector: String::new(),
timestamp_observation: None,
frame_index: None,
}
}
}
impl From<super::v1::Tag> for Tag {
fn from(v1: super::v1::Tag) -> Self {
Self {
id: v1.id,
inserted_at: v1.inserted_at,
x: v1.x,
y: v1.y,
width: v1.width,
height: v1.height,
conf: v1.conf,
observation_type: v1.observation_type,
class: v1.class_name,
event_id: Some(v1.event_id),
location: v1.location,
origin_location: None,
origin_pitch: None,
origin_heading: None,
origin_roll: None,
sensor_pitch: None,
sensor_yaw: None,
sensor_roll: None,
origin_height: None,
subject_location: None,
subject_height: None,
artifact_id: None,
detector: String::new(),
timestamp_observation: None,
frame_index: None,
}
}
}