use std::time::Duration;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::chart::{DataPoint, SymbolInfo};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StudyResult {
pub symbol_info: SymbolInfo,
pub data: Vec<DataPoint>,
pub study_id: String,
pub total_points_received: usize,
pub elapsed: Duration,
}
impl StudyResult {
#[inline]
pub fn len(&self) -> usize {
self.data.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
#[inline]
pub fn points(&self) -> &[DataPoint] {
&self.data
}
#[inline]
pub fn points_mut(&mut self) -> &mut [DataPoint] {
&mut self.data
}
#[inline]
pub fn into_points(self) -> Vec<DataPoint> {
self.data
}
#[inline]
pub fn first_timestamp(&self) -> Option<i64> {
self.data
.first()
.and_then(|dp| dp.value.first().copied().map(|v| v as i64))
}
#[inline]
pub fn last_timestamp(&self) -> Option<i64> {
self.data
.last()
.and_then(|dp| dp.value.first().copied().map(|v| v as i64))
}
#[inline]
pub fn first_datetime(&self) -> Option<DateTime<Utc>> {
self.first_timestamp()
.and_then(|ts| DateTime::<Utc>::from_timestamp(ts, 0))
}
#[inline]
pub fn last_datetime(&self) -> Option<DateTime<Utc>> {
self.last_timestamp()
.and_then(|ts| DateTime::<Utc>::from_timestamp(ts, 0))
}
}