Skip to content

Commit

Permalink
Update moose tracker to v0.5.1
Browse files Browse the repository at this point in the history
Notable changes include the removal of the `Phase` field in all events, the addition of `TransferDirection` and `protocol_version` fields

Signed-off-by: Martynas Gurskas <[email protected]>
  • Loading branch information
Lipt0nas committed Aug 7, 2023
1 parent 19a97ab commit 507f628
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 138 deletions.
18 changes: 8 additions & 10 deletions drop-analytics/src/file_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fs::File, io::Write, path::Path};
use serde::{Deserialize, Serialize};
use slog::Logger;

use crate::{FileInfo, Phase, TransferInfo, MOOSE_STATUS_SUCCESS, MOOSE_VALUE_NONE};
use crate::{FileInfo, TransferDirection, TransferInfo, MOOSE_STATUS_SUCCESS, MOOSE_VALUE_NONE};

#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
Expand All @@ -20,25 +20,24 @@ enum MooseEventType {

#[derive(Serialize, Deserialize)]
struct InitEvent {
phase: Phase,
result: i32,
app_version: String,
prod: bool,
}

#[derive(Serialize, Deserialize)]
struct BatchEvent {
phase: Phase,
transfer_id: String,
info: TransferInfo,
protocol_version: i32,
}

#[derive(Serialize, Deserialize)]
struct FileEvent {
phase: Phase,
result: i32,
transfer_id: String,
transfer_time: i32,
direction: TransferDirection,
info: FileInfo,
}

Expand Down Expand Up @@ -86,14 +85,13 @@ impl FileImpl {
}

impl super::Moose for FileImpl {
fn service_quality_initialization_init(&self, res: Result<(), i32>, phase: crate::Phase) {
fn service_quality_initialization_init(&self, res: Result<(), i32>) {
let result = match res {
Ok(_) => MOOSE_STATUS_SUCCESS,
Err(e) => e,
};

let event = self.write_event(MooseEventType::Init(InitEvent {
phase,
result,
app_version: self.app_version.clone(),
prod: self.prod,
Expand All @@ -109,14 +107,14 @@ impl super::Moose for FileImpl {
}
fn service_quality_transfer_batch(
&self,
phase: crate::Phase,
transfer_id: String,
info: TransferInfo,
protocol_version: i32,
) {
let event = self.write_event(MooseEventType::Batch(BatchEvent {
phase,
transfer_id,
info,
protocol_version,
}));

if event.is_err() {
Expand All @@ -130,9 +128,9 @@ impl super::Moose for FileImpl {
fn service_quality_transfer_file(
&self,
res: Result<(), i32>,
phase: crate::Phase,
transfer_id: String,
transfer_time: i32,
direction: TransferDirection,
info: Option<FileInfo>,
) {
let result = match res {
Expand All @@ -141,10 +139,10 @@ impl super::Moose for FileImpl {
};

let event = self.write_event(MooseEventType::File(FileEvent {
phase,
result,
transfer_id,
transfer_time,
direction,
info: info.unwrap_or_default(),
}));

Expand Down
21 changes: 13 additions & 8 deletions drop-analytics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const MOOSE_STATUS_SUCCESS: i32 = 0;
const MOOSE_VALUE_NONE: i32 = -1;

#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum Phase {
#[serde(rename = "start")]
Start,
#[serde(rename = "end")]
End,
pub enum TransferDirection {
#[serde(rename = "upload")]
Upload,
#[serde(rename = "download")]
Download,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -43,14 +43,19 @@ pub struct FileInfo {
}

pub trait Moose: Send + Sync {
fn service_quality_initialization_init(&self, res: Result<(), i32>, phase: Phase);
fn service_quality_transfer_batch(&self, phase: Phase, transfer_id: String, info: TransferInfo);
fn service_quality_initialization_init(&self, res: Result<(), i32>);
fn service_quality_transfer_batch(
&self,
transfer_id: String,
info: TransferInfo,
protocol_version: i32,
);
fn service_quality_transfer_file(
&self,
res: Result<(), i32>,
phase: Phase,
transfer_id: String,
transfer_time: i32,
direction: TransferDirection,
info: Option<FileInfo>,
);

Expand Down
8 changes: 4 additions & 4 deletions drop-analytics/src/mock_impl.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use crate::{FileInfo, TransferInfo};
use crate::{FileInfo, TransferDirection, TransferInfo};

pub struct MockImpl;

impl super::Moose for MockImpl {
fn service_quality_initialization_init(&self, _res: Result<(), i32>, _phase: crate::Phase) {}
fn service_quality_initialization_init(&self, _res: Result<(), i32>) {}

fn service_quality_transfer_batch(
&self,
_phase: crate::Phase,
_transfer_id: String,
_info: TransferInfo,
_protocol_version: i32,
) {
}

fn service_quality_transfer_file(
&self,
_res: Result<(), i32>,
_phase: crate::Phase,
_transfer_id: String,
_transfer_time: i32,
_direction: TransferDirection,
_info: Option<FileInfo>,
) {
}
Expand Down
43 changes: 20 additions & 23 deletions drop-analytics/src/moose_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ use mooselibdropapp as moose;
use serde_json::Value;
use slog::{error, info, warn, Logger};

use crate::{FileInfo, TransferInfo, MOOSE_STATUS_SUCCESS, MOOSE_VALUE_NONE};
use crate::{FileInfo, TransferDirection, TransferInfo, MOOSE_STATUS_SUCCESS, MOOSE_VALUE_NONE};

const DROP_MOOSE_APP_NAME: &str = "norddrop";

/// Version of the tracker used, should be updated everytime the tracker library
/// is updated
const DROP_MOOSE_TRACKER_VERSION: &str = "0.4.0";
const DROP_MOOSE_TRACKER_VERSION: &str = "0.5.1";

pub struct MooseImpl {
logger: slog::Logger,
}

struct MooseInitCallback {
logger: slog::Logger,
init_tx: SyncSender<Result<moose::ContextState, moose::MooseError>>,
init_tx: SyncSender<Result<moose::TrackerState, moose::MooseError>>,
}

impl moose::InitCallback for MooseInitCallback {
fn on_init(&self, result_code: &Result<moose::ContextState, moose::MooseError>) {
fn after_init(&self, result_code: &Result<moose::TrackerState, moose::MooseError>) {
info!(self.logger, "[Moose] Init callback: {:?}", result_code);
let _ = self.init_tx.send(*result_code);
}
Expand Down Expand Up @@ -125,34 +125,28 @@ impl Drop for MooseImpl {
}

impl super::Moose for MooseImpl {
fn service_quality_initialization_init(&self, res: Result<(), i32>, phase: super::Phase) {
fn service_quality_initialization_init(&self, res: Result<(), i32>) {
let errno = match res {
Ok(()) => MOOSE_STATUS_SUCCESS,
Err(err) => err,
};

moose!(
self.logger,
send_serviceQuality_initialization_init,
errno,
phase.into()
);
moose!(self.logger, send_serviceQuality_initialization_init, errno);
}

fn service_quality_transfer_batch(
&self,
phase: super::Phase,
transfer_id: String,
info: TransferInfo,
protocol_version: i32,
) {
moose!(
self.logger,
send_serviceQuality_transfer_batch,
phase.into(),
mooselibdropapp::LibdropappEventTrigger::LibdropappEventTriggerNone,
info.extension_list,
info.mime_type_list,
info.file_count,
protocol_version,
info.file_size_list,
transfer_id,
info.transfer_size_kb
Expand All @@ -162,9 +156,9 @@ impl super::Moose for MooseImpl {
fn service_quality_transfer_file(
&self,
res: Result<(), i32>,
phase: crate::Phase,
transfer_id: String,
transfer_time: i32,
direction: TransferDirection,
info: Option<FileInfo>,
) {
let errno = match res {
Expand All @@ -178,10 +172,9 @@ impl super::Moose for MooseImpl {
self.logger,
send_serviceQuality_transfer_file,
errno,
phase.into(),
mooselibdropapp::LibdropappEventTrigger::LibdropappEventTriggerNone,
info.extension,
info.mime_type,
direction.into(),
transfer_id,
info.size_kb,
transfer_time
Expand Down Expand Up @@ -220,11 +213,15 @@ impl super::Moose for MooseImpl {
}
}

impl From<super::Phase> for mooselibdropapp::LibdropappEventPhase {
fn from(value: super::Phase) -> Self {
match value {
crate::Phase::Start => mooselibdropapp::LibdropappEventPhase::LibdropappEventPhaseStart,
crate::Phase::End => mooselibdropapp::LibdropappEventPhase::LibdropappEventPhaseEnd,
impl From<super::TransferDirection> for moose::LibdropappTransferDirection {
fn from(direction: super::TransferDirection) -> Self {
match direction {
super::TransferDirection::Upload => {
moose::LibdropappTransferDirection::LibdropappTransferDirectionUpload
}
super::TransferDirection::Download => {
moose::LibdropappTransferDirection::LibdropappTransferDirectionDownload
}
}
}
}
Expand All @@ -251,9 +248,9 @@ fn populate_context(logger: &Logger) {
if let Ok(foreign_context) = moose::fetch_specific_context(foreign_tracker_name) {
let context = parse_foreign_context(&foreign_context);
set_context_fields!(
set_context_device_brand, context.brand;
set_context_device_type, context.x_type;
set_context_device_model, context.model;
set_context_device_brand, context.brand;
set_context_device_fp, context.fp;
set_context_device_resolution, context.resolution;
set_context_device_os, context.os;
Expand Down
11 changes: 11 additions & 0 deletions drop-transfer/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ pub enum Version {
#[strum(serialize = "v5")]
V5,
}

impl From<Version> for i32 {
fn from(version: Version) -> Self {
match version {
Version::V1 => 1,
Version::V2 => 2,
Version::V4 => 4,
Version::V5 => 5,
}
}
}
32 changes: 10 additions & 22 deletions drop-transfer/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
sync::Arc,
};

use drop_analytics::Moose;
use drop_analytics::{Moose, TransferDirection};
use drop_config::DropConfig;
use drop_storage::Storage;
use slog::{debug, error, warn, Logger};
Expand Down Expand Up @@ -51,9 +51,9 @@ macro_rules! moose_try_file {
Err(e) => {
$moose.service_quality_transfer_file(
Err(u32::from(&e) as i32),
drop_analytics::Phase::Start,
$xfer_id.to_string(),
0,
TransferDirection::Download,
$file_info,
);

Expand Down Expand Up @@ -97,24 +97,14 @@ impl Service {
};

let res = task();
moose.service_quality_initialization_init(res.to_status(), drop_analytics::Phase::Start);
moose.service_quality_initialization_init(res.to_status());

res
}

pub async fn stop(self) -> Result<(), Error> {
let task = async {
self.stop.cancel();
self.join_handle.await.map_err(|_| Error::ServiceStop)
};

let res = task.await;

self.state
.moose
.service_quality_initialization_init(res.to_status(), drop_analytics::Phase::End);

res
self.stop.cancel();
self.join_handle.await.map_err(|_| Error::ServiceStop)
}

pub fn purge_transfers(&self, transfer_ids: Vec<String>) -> Result<(), Error> {
Expand Down Expand Up @@ -169,12 +159,6 @@ impl Service {
}

pub async fn send_request(&mut self, xfer: crate::Transfer) {
self.state.moose.service_quality_transfer_batch(
drop_analytics::Phase::Start,
xfer.id().to_string(),
xfer.info(),
);

if let Err(err) = self.state.storage.insert_transfer(&xfer.storage_info()) {
error!(self.logger, "Failed to insert transfer into storage: {err}",);
}
Expand Down Expand Up @@ -372,12 +356,16 @@ impl Service {

xfer.files().values().for_each(|file| {
let status: u32 = From::from(&Error::Canceled);
let direction = match xfer.is_incoming() {
true => TransferDirection::Download,
false => TransferDirection::Upload,
};

self.state.moose.service_quality_transfer_file(
Err(status as _),
drop_analytics::Phase::End,
xfer.id().to_string(),
0,
direction,
file.info(),
)
});
Expand Down
Loading

0 comments on commit 507f628

Please sign in to comment.