Skip to content

Commit

Permalink
Completely separate user/gear logic from parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
danielalvsaaker committed Aug 16, 2022
1 parent e8c9c79 commit 9c4fb0f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 1 addition & 3 deletions crates/tf-models/src/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ use serde::{Deserialize, Serialize};

use crate::{
types::{AngularVelocity, DateTime, Duration, Energy, LengthF64, LengthU32, Power, Velocity},
ActivityId, GearId, UserId,
ActivityId,
};

#[derive(Clone, Serialize, Deserialize)]
pub struct Activity {
pub owner: UserId,
pub id: ActivityId,
pub gear_id: Option<GearId>,
pub session: Session,
pub record: Record,
pub lap: Vec<Lap>,
Expand Down
7 changes: 2 additions & 5 deletions crates/tf-parse/src/fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use chrono::{offset::Local, DateTime};
use tf_models::{
activity::{Lap, Record, Session},
types::{AngularVelocity, Energy, LengthF64, LengthU32, Power, Velocity},
Activity, ActivityId, Sport, UserId,
Activity, ActivityId, Sport,
};

use uom::si::{
Expand Down Expand Up @@ -48,7 +48,7 @@ fn between(lhs: &Option<DateTime<Local>>, rhs: Option<DateTime<Local>>) -> Optio

const MULTIPLIER: f64 = 180_f64 / (2_u32 << 30) as f64;

pub fn parse(user: UserId, fit_data: &[u8]) -> Result<Activity> {
pub fn parse(fit_data: &[u8]) -> Result<Activity> {
let mut session: Session = Session::default();
let mut record: Record = Record::default();
let mut lap_vec: Vec<Lap> = Vec::new();
Expand Down Expand Up @@ -123,9 +123,6 @@ pub fn parse(user: UserId, fit_data: &[u8]) -> Result<Activity> {
.as_bytes(),
)
.unwrap(),
// TODO: Correct username
owner: user,
gear_id: None,
session,
record,
lap: lap_vec,
Expand Down
10 changes: 9 additions & 1 deletion src/routes/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ use tf_auth::scopes::{Activity, Grant, Read, Write};
use tf_database::{
primitives::Key,
query::{ActivityQuery, UserQuery},
resource::index::DefaultGear,
Database,
};
use tf_models::{
activity::{Lap, Record, Session},
gear::Gear,
user::User,
};

Expand Down Expand Up @@ -72,7 +74,7 @@ async fn post_activity_index(
let (send, recv) = tokio::sync::oneshot::channel();

rayon::spawn(move || {
let _ = send.send(tf_parse::parse(query.user_id, &file));
let _ = send.send(tf_parse::parse(&file));
});

recv.await
Expand Down Expand Up @@ -107,6 +109,12 @@ async fn post_activity_index(
root.traverse::<Vec<Lap>>()?
.insert(&activity_query, &parsed.lap, &query)?;

if let Some(default_gear) = root.traverse::<DefaultGear>()?.key(&query)? {
root.traverse::<Session>()?
.traverse::<Gear>(&activity_query)?
.link(&activity_query, &default_gear)?;
}

Ok::<_, tf_database::error::Error>(())
})
.await??;
Expand Down

0 comments on commit 9c4fb0f

Please sign in to comment.