examples_suggest_cli/
main.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::{collections::HashMap, sync::Arc};

use anyhow::Result;
use clap::{Parser, Subcommand, ValueEnum};

use remote_settings::RemoteSettingsServer;
use suggest::{
    AmpMatchingStrategy, SuggestIngestionConstraints, SuggestStore, SuggestStoreBuilder,
    SuggestionProvider, SuggestionProviderConstraints, SuggestionQuery,
};

static DB_FILENAME: &str = "suggest.db";

const DEFAULT_LOG_FILTER: &str = "suggest::store=info";
const DEFAULT_LOG_FILTER_VERBOSE: &str = "suggest::store=trace";

#[derive(Debug, Parser)]
#[command(about, long_about = None)]
struct Cli {
    #[arg(short = 's')]
    remote_settings_server: Option<RemoteSettingsServerArg>,
    #[arg(short = 'b')]
    remote_settings_bucket: Option<String>,
    #[arg(long, short, action)]
    verbose: bool,
    // Custom { url: String },
    #[command(subcommand)]
    command: Commands,
}

#[derive(Clone, Debug, ValueEnum)]
enum RemoteSettingsServerArg {
    Prod,
    Stage,
    Dev,
}

#[derive(Debug, Subcommand)]
enum Commands {
    /// Ingest data
    Ingest {
        #[clap(long, short, action)]
        reingest: bool,
        #[clap(long, short)]
        providers: Vec<SuggestionProviderArg>,
    },
    /// Query against ingested data
    Query {
        #[arg(long, action)]
        fts_match_info: bool,
        #[clap(long, short)]
        provider: Option<SuggestionProviderArg>,
        /// Input to search
        input: String,
        #[clap(long, short)]
        amp_matching_strategy: Option<AmpMatchingStrategyArg>,
    },
}

#[derive(Clone, Debug, ValueEnum)]
enum AmpMatchingStrategyArg {
    /// Use keyword matching, without keyword expansion
    NoKeyword,
    /// Use FTS matching
    Fts,
    /// Use FTS matching against the title
    FtsTitle,
}

impl From<AmpMatchingStrategyArg> for AmpMatchingStrategy {
    fn from(val: AmpMatchingStrategyArg) -> Self {
        match val {
            AmpMatchingStrategyArg::NoKeyword => AmpMatchingStrategy::NoKeywordExpansion,
            AmpMatchingStrategyArg::Fts => AmpMatchingStrategy::FtsAgainstFullKeywords,
            AmpMatchingStrategyArg::FtsTitle => AmpMatchingStrategy::FtsAgainstTitle,
        }
    }
}

#[derive(Clone, Debug, ValueEnum)]
enum SuggestionProviderArg {
    Amp,
    Wikipedia,
    Amo,
    Pocket,
    Yelp,
    Mdn,
    Weather,
    Fakespot,
}

impl From<SuggestionProviderArg> for SuggestionProvider {
    fn from(value: SuggestionProviderArg) -> Self {
        match value {
            SuggestionProviderArg::Amp => Self::Amp,
            SuggestionProviderArg::Wikipedia => Self::Wikipedia,
            SuggestionProviderArg::Amo => Self::Amo,
            SuggestionProviderArg::Pocket => Self::Pocket,
            SuggestionProviderArg::Yelp => Self::Yelp,
            SuggestionProviderArg::Mdn => Self::Mdn,
            SuggestionProviderArg::Weather => Self::Weather,
            SuggestionProviderArg::Fakespot => Self::Fakespot,
        }
    }
}

fn main() -> Result<()> {
    let cli = Cli::parse();
    env_logger::init_from_env(env_logger::Env::default().filter_or(
        "RUST_LOG",
        if cli.verbose {
            DEFAULT_LOG_FILTER_VERBOSE
        } else {
            DEFAULT_LOG_FILTER
        },
    ));
    viaduct_reqwest::use_reqwest_backend();
    let store = build_store(&cli);
    match cli.command {
        Commands::Ingest {
            reingest,
            providers,
        } => ingest(&store, reingest, providers, cli.verbose),
        Commands::Query {
            provider,
            input,
            fts_match_info,
            amp_matching_strategy,
        } => query(
            &store,
            provider,
            input,
            fts_match_info,
            amp_matching_strategy,
            cli.verbose,
        ),
    };
    Ok(())
}

fn build_store(cli: &Cli) -> Arc<SuggestStore> {
    Arc::new(SuggestStoreBuilder::default())
        .data_path(cli_support::cli_data_path(DB_FILENAME))
        .remote_settings_server(match cli.remote_settings_server {
            None => RemoteSettingsServer::Prod,
            Some(RemoteSettingsServerArg::Dev) => RemoteSettingsServer::Dev,
            Some(RemoteSettingsServerArg::Stage) => RemoteSettingsServer::Stage,
            Some(RemoteSettingsServerArg::Prod) => RemoteSettingsServer::Prod,
        })
        .remote_settings_bucket_name(
            cli.remote_settings_bucket
                .clone()
                .unwrap_or_else(|| "main".to_owned()),
        )
        .build()
        .unwrap_or_else(|e| panic!("Error building store: {e}"))
}

fn ingest(
    store: &SuggestStore,
    reingest: bool,
    providers: Vec<SuggestionProviderArg>,
    verbose: bool,
) {
    if reingest {
        print_header("Reingesting data");
        store.force_reingest();
    } else {
        print_header("Ingesting data");
    }
    let constraints = if providers.is_empty() {
        SuggestIngestionConstraints::all_providers()
    } else {
        SuggestIngestionConstraints {
            providers: Some(providers.into_iter().map(Into::into).collect()),
            ..SuggestIngestionConstraints::default()
        }
    };

    let metrics = store
        .ingest(constraints)
        .unwrap_or_else(|e| panic!("Error in ingest: {e}"));
    if verbose && !metrics.ingestion_times.is_empty() {
        print_header("Ingestion times");
        let mut ingestion_times = metrics.ingestion_times;
        let download_times: HashMap<String, u64> = metrics
            .download_times
            .into_iter()
            .map(|s| (s.label, s.value))
            .collect();

        ingestion_times.sort_by_key(|s| s.value);
        ingestion_times.reverse();
        for sample in ingestion_times {
            let label = &sample.label;
            let ingestion_time = sample.value / 1000;
            let download_time = download_times.get(label).unwrap_or(&0) / 1000;

            println!(
                "{label:30} Download: {download_time:>5}ms    Ingestion: {ingestion_time:>5}ms"
            );
        }
    }
    print_header("Done");
}

fn query(
    store: &SuggestStore,
    provider: Option<SuggestionProviderArg>,
    input: String,
    fts_match_info: bool,
    amp_matching_strategy: Option<AmpMatchingStrategyArg>,
    verbose: bool,
) {
    let query = SuggestionQuery {
        providers: match provider {
            Some(provider) => vec![provider.into()],
            None => SuggestionProvider::all().to_vec(),
        },
        keyword: input,
        provider_constraints: Some(SuggestionProviderConstraints {
            amp_alternative_matching: amp_matching_strategy.map(Into::into),
            ..SuggestionProviderConstraints::default()
        }),
        ..SuggestionQuery::default()
    };
    let mut results = store
        .query_with_metrics(query)
        .unwrap_or_else(|e| panic!("Error querying store: {e}"));
    if results.suggestions.is_empty() {
        print_header("No Results");
    } else {
        print_header("Results");
        let count = results.suggestions.len();
        for suggestion in results.suggestions {
            let title = suggestion.title();
            let url = suggestion.url().unwrap_or("[no-url]");
            let icon = if suggestion.icon_data().is_some() {
                "with icon"
            } else {
                "no icon"
            };
            println!("* {title} ({url}) ({icon})");
            if fts_match_info {
                if let Some(match_info) = suggestion.fts_match_info() {
                    println!("   {match_info:?}")
                } else {
                    println!("   <no match info>");
                }
                println!("+ {} other sugestions", count - 1);
                break;
            }
        }
    }
    if verbose {
        print_header("Query times");
        results.query_times.sort_by_key(|s| s.value);
        results.query_times.reverse();
        for s in results.query_times {
            println!("{:33} Time: {:>5}us", s.label, s.value);
        }
    }
}

fn print_header(msg: impl Into<String>) {
    let mut msg = msg.into();
    if msg.len() % 2 == 1 {
        msg.push(' ');
    }
    let width = (70 - msg.len() - 2) / 2;
    println!();
    println!("{} {msg} {}", "=".repeat(width), "=".repeat(width));
}