Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(adm): Make suggestion score configurable #235

Merged
merged 2 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions merino-adm/src/remote_settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod client;

use crate::remote_settings::client::RemoteSettingsClient;
use anyhow::Context;
use async_trait::async_trait;
use cadence::{Histogrammed, StatsdClient};
use deduped_dashmap::DedupedMap;
Expand All @@ -16,7 +17,7 @@ use merino_suggest::{
};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use std::{collections::HashMap, sync::Arc, time::Instant};
use std::{collections::HashMap, convert::TryInto, sync::Arc, time::Instant};

lazy_static! {
static ref NON_SPONSORED_IAB_CATEGORIES: Vec<&'static str> = vec!["5 - Education"];
Expand Down Expand Up @@ -56,8 +57,14 @@ impl RemoteSettingsSuggester {
.clone(),
)?;
let suggestions = Arc::new(DedupedMap::new());
let suggestion_score = config
.suggestion_score
.try_into()
.context("converting score to proportion")
.map_err(SetupError::InvalidConfiguration)?;

Self::sync(&mut remote_settings_client, &*suggestions, suggestion_score).await?;

Self::sync(&mut remote_settings_client, &*suggestions).await?;
{
let task_suggestions = Arc::clone(&suggestions);
let task_interval = config.resync_interval;
Expand All @@ -74,7 +81,9 @@ impl RemoteSettingsSuggester {
timer.tick().await;
let loop_suggestions = &*(Arc::clone(&task_suggestions));
if let Some(loop_client) = Arc::get_mut(&mut task_client) {
if let Err(error) = Self::sync(loop_client, loop_suggestions).await {
if let Err(error) =
Self::sync(loop_client, loop_suggestions, suggestion_score).await
{
tracing::error!(
?error,
"Error while syncing remote settings suggestions"
Expand Down Expand Up @@ -108,6 +117,7 @@ impl RemoteSettingsSuggester {
pub async fn sync(
remote_settings_client: &mut RemoteSettingsClient,
suggestions: &DedupedMap<String, (), Suggestion>,
suggestion_score: Proportion,
) -> Result<(), SetupError> {
tracing::info!(
r#type = "adm.remote-settings.sync-start",
Expand Down Expand Up @@ -179,7 +189,7 @@ impl RemoteSettingsSuggester {
is_sponsored: !NON_SPONSORED_IAB_CATEGORIES
.contains(&adm_suggestion.iab_category.as_str()),
icon: icon_url,
score: Proportion::from(0.2),
score: suggestion_score,
};

for keyword in &adm_suggestion.keywords {
Expand Down
28 changes: 28 additions & 0 deletions merino-integration-tests/src/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::{merino_test_macro, utils::test_tools::TestReqwestClient, TestingTools};
use anyhow::Result;
use lazy_static::lazy_static;
use merino_settings::providers::{
FixedConfig, KeywordFilterConfig, MultiplexerConfig, RemoteSettingsConfig,
SuggestionProviderConfig,
Expand Down Expand Up @@ -167,6 +168,33 @@ async fn suggest_adm_rs_works_content(
Ok(())
}

lazy_static! {
static ref TEST_SCORE: f64 = std::f64::consts::TAU.fract();
}

#[merino_test_macro(|settings| {
settings.suggestion_providers.insert(
"adm".to_string(),
SuggestionProviderConfig::RemoteSettings(RemoteSettingsConfig {
suggestion_score: *TEST_SCORE as f32,
..RemoteSettingsConfig::default()
})
);
settings.remote_settings.test_changes = Some(vec!["apple".to_string()]);
})]
async fn suggest_adm_rs_score_is_configurable(
TestingTools { test_client, .. }: TestingTools,
) -> Result<()> {
let response = test_client.get("/api/v1/suggest?q=apple").send().await?;

assert_eq!(response.status(), StatusCode::OK);
let body: serde_json::Value = response.json().await?;
// Check that the score is approximately correct.
assert!((body["suggestions"][0]["score"].as_f64().unwrap() - *TEST_SCORE).abs() < 0.001);

Ok(())
}

#[merino_test_macro(|settings| {
// Wiki fruit is only enabled when debug is true.
settings.debug = true;
Expand Down
4 changes: 4 additions & 0 deletions merino-settings/src/providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ pub struct RemoteSettingsConfig {
#[serde_as(as = "DurationSeconds")]
#[serde(rename = "resync_interval_sec")]
pub resync_interval: Duration,

/// The score value to assign to suggestions. A float between 0.0 and 1.0 inclusive.
pub suggestion_score: f32,
}

impl Default for RemoteSettingsConfig {
Expand All @@ -147,6 +150,7 @@ impl Default for RemoteSettingsConfig {
bucket: None,
collection: None,
resync_interval: Duration::from_secs(60 * 60 * 3), // 3 hours
suggestion_score: 0.3,
}
}
}
Expand Down