diff --git a/src/data/entities/metrics.rs b/src/data/entities/metrics.rs index 5315cbf..4e4a7e7 100644 --- a/src/data/entities/metrics.rs +++ b/src/data/entities/metrics.rs @@ -52,12 +52,8 @@ impl Related for Entity { impl ActiveModelBehavior for ActiveModel {} impl Model { - pub fn extract(&self, value: &serde_json::Value) -> Result { - let x = Utc::now().timestamp() as f64; - let y = jql::walker(value, self.query.as_str())? - .as_f64() - .ok_or(FetchError::JQLError("query result is null".to_string()))?; - Ok(PlotPoint { x, y }) + pub fn extract(&self, value: &serde_json::Value) -> Result, FetchError> { + Ok(jql::walker(value, self.query.as_str())?.as_f64()) } } diff --git a/src/worker/surveyor.rs b/src/worker/surveyor.rs index 5f9ddac..390eb4a 100644 --- a/src/worker/surveyor.rs +++ b/src/worker/surveyor.rs @@ -74,14 +74,19 @@ pub async fn surveyor_loop( error!(target: "surveyor", "[{}] Failed setting last_update ({:?}) for source {:?} but successfully fetched '{}', aborting", index, e, source_clone, res); return; } + let now = Utc::now().timestamp() as f64; for metric in metrics_snapshot.iter().filter(|x| source_clone.id == x.source_id) { match metric.extract(&res) { - Ok(v) => { - if let Err(e) = entities::points::Entity::insert( - entities::points::ActiveModel { - id: NotSet, metric_id: Set(metric.id), x: Set(v.x), y: Set(v.y), - }).exec(&db_clone).await { - error!(target: "surveyor", "[{}] Could not insert record {:?} : {:?}", index, v, e); + // note that Err and None mean different things: Err for broken queries, None for + // missing values. Only first one is reported + Ok(value) => { + if let Some(v) = value { + if let Err(e) = entities::points::Entity::insert( + entities::points::ActiveModel { + id: NotSet, metric_id: Set(metric.id), x: Set(now), y: Set(v), + }).exec(&db_clone).await { + error!(target: "surveyor", "[{}] Could not insert record ({},{}) : {:?}", index, now, v, e); + } } }, Err(e) => error!(target: "surveyor", "[{}] Failed extracting '{}' from {}: {:?}", index, metric.name, source_clone.name, e),