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
use crate::symbology::{market::ExchangeMarketKind, Cpty, MarketKind, MarketRef};
use anyhow::{anyhow, bail, Result};
use api::{marketdata::NetidxFeedPaths, symbology::CptyId, ComponentId};
use fxhash::{FxHashMap, FxHashSet};
use netidx::path::Path;

/// Paths to services in netidx, "config-as-service-discovery"
#[derive(Debug, Clone)]
pub struct Paths {
    pub hosted_base: Path,
    pub local_base: Path,
    pub core_base: Path,
    pub local_components: FxHashSet<ComponentId>,
    pub remote_components: FxHashMap<ComponentId, Path>,
    pub component_kind: FxHashMap<ComponentId, String>,
    pub use_local_symbology: bool,
    pub use_local_licensedb: bool,
    pub use_local_marketdata: FxHashSet<CptyId>,
    pub use_legacy_marketdata: FxHashSet<CptyId>,
    pub use_legacy_hist_marketdata: FxHashSet<CptyId>,
}

impl Paths {
    /// Symbology server
    pub fn sym(&self) -> Path {
        let base =
            if self.use_local_symbology { &self.local_base } else { &self.hosted_base };
        base.append("sym")
    }

    /// Marketdata feeds
    pub fn marketdata(&self, cpty: Cpty, hist: bool) -> Path {
        let use_local = self.use_local_marketdata.contains(&cpty.id());
        let use_legacy = self.use_legacy_marketdata.contains(&cpty.id())
            || (hist && self.use_legacy_hist_marketdata.contains(&cpty.id()));
        let base = if use_local { &self.local_base } else { &self.hosted_base };
        if use_legacy {
            base.append("qf")
        } else {
            base.append("marketdata")
        }
    }

    /// Realtime marketdata feed
    pub fn marketdata_rt(&self, cpty: Cpty) -> Path {
        self.marketdata(cpty, false)
            .append("rt")
            .append(&cpty.venue.name)
            .append(&cpty.route.name)
    }

    /// Realtime marketdata feed for a specific market, referenced by-id
    pub fn marketdata_rt_by_id(&self, market: MarketRef) -> Path {
        let cpty = market.cpty();
        if self.use_legacy_marketdata.contains(&cpty.id()) {
            self.marketdata(cpty, false)
                .append("rt")
                .append("by-id")
                .append(&market.id.to_string())
        } else {
            market.path_by_id(&self.marketdata_rt(cpty))
        }
    }

    /// Realtime marketdata feed for a specific market, aliased by-name
    pub fn marketdata_rt_by_name(&self, market: MarketRef) -> Path {
        let cpty = market.cpty();
        if self.use_legacy_marketdata.contains(&cpty.id()) {
            match market.kind {
                MarketKind::Exchange(ExchangeMarketKind { base, quote }) => self
                    .marketdata(cpty, false)
                    .append("rt")
                    .append("by-name")
                    .append(&market.venue.name)
                    .append(&market.route.name)
                    .append(base.name.as_str())
                    .append(quote.name.as_str()),
                _ => {
                    panic!("legacy_marketdata_paths only supported for Exchange markets");
                }
            }
        } else {
            market.path_by_name(&self.marketdata_rt(cpty))
        }
    }

    /// Realtime marketdata candles base
    pub fn marketdata_ohlc(&self, cpty: Cpty) -> Path {
        self.marketdata(cpty, false)
            .append("ohlc")
            .append(&cpty.venue.name)
            .append(&cpty.route.name)
    }

    /// Realtime marketdata candles, aliased by-name
    /// If hist is true, pull the name as if we were querying historical data
    pub fn marketdata_ohlc_by_name(&self, market: MarketRef, hist: bool) -> Path {
        let cpty = market.cpty();
        if self.use_legacy_marketdata.contains(&cpty.id())
            || (hist && self.use_legacy_hist_marketdata.contains(&cpty.id()))
        {
            match market.kind {
                MarketKind::Exchange(ExchangeMarketKind { base, quote }) => self
                    .marketdata(cpty, hist)
                    .append("ohlc")
                    .append("by-name")
                    .append(&market.venue.name)
                    .append(&market.route.name)
                    .append(base.name.as_str())
                    .append(quote.name.as_str()),
                _ => {
                    panic!("legacy_marketdata_paths only supported for Exchange markets");
                }
            }
        } else {
            let base = self.marketdata_ohlc(cpty);
            market.path_by_name(&base)
        }
    }

    /// Historical marketdata candles recorder
    pub fn marketdata_hist_ohlc(&self, cpty: Cpty) -> Path {
        if self.use_legacy_marketdata.contains(&cpty.id())
            || self.use_legacy_hist_marketdata.contains(&cpty.id())
        {
            self.marketdata(cpty, true)
                .append("hist")
                .append("ohlc")
                .append("by-name")
                .append(&cpty.venue.name)
                .append(&cpty.route.name)
        } else {
            self.marketdata(cpty, true)
                .append("hist")
                .append("ohlc")
                .append(&cpty.venue.name)
                .append(&cpty.route.name)
        }
    }

    /// Marketdata APIs (RPCs)
    pub fn marketdata_api(&self, cpty: Cpty) -> Path {
        self.marketdata(cpty, false)
            .append("api")
            .append(&cpty.venue.name)
            .append(&cpty.route.name)
    }

    pub fn historical_marketdata_api(&self) -> Path {
        let use_local = true;
        let base = if use_local { &self.local_base } else { &self.hosted_base };
        base.append("marketdata").append("historical-api")
    }

    /// RFQ feeds
    pub fn marketdata_rfq(&self, cpty: Cpty) -> Path {
        self.marketdata(cpty, false)
            .append("rfq")
            .append(&cpty.venue.name)
            .append(&cpty.route.name)
    }

    /// Marketdata snaphots and USD marks service
    pub fn marketdata_snapshots(&self, local: bool) -> Path {
        let base = if local { &self.local_base } else { &self.hosted_base };
        base.append("marketdata").append("snapshots")
    }

    pub fn marketdata_marks(&self) -> Path {
        self.local_base.append("qf").append("marks")
    }

    /// Core RPCs base path
    pub fn core(&self) -> Path {
        self.core_base.clone()
    }

    /// Channel path; if com = None, then this core; if com is some component,
    /// then find the component's core and return its channel path
    pub fn channel(&self, com: Option<ComponentId>) -> Result<Path> {
        match com {
            None => Ok(self.core_base.append("channel")),
            Some(com) => {
                if self.local_components.contains(&com) {
                    Ok(self.core_base.append("channel"))
                } else if let Some(base) = self.remote_components.get(&com) {
                    Ok(base.append("channel"))
                } else {
                    bail!("no path to component {}", com);
                }
            }
        }
    }

    /// One-way write-only channel for core-to-core communication
    pub fn in_channel(&self) -> Path {
        self.core_base.append("in-channel")
    }

    /// Component RPC mount path and alias
    pub fn component(&self, com: ComponentId) -> Result<(Path, Path)> {
        let kind = self
            .component_kind
            .get(&com)
            .ok_or_else(|| anyhow!("BUG: unknown component kind for {}", com))?;
        let base = if self.local_components.contains(&com) {
            self.local_base.clone()
        } else if let Some(_base) = self.remote_components.get(&com) {
            // CR alee: not really correct...need to rethink config as a whole
            // in context with rsync and core_id
            self.local_base.clone()
            // base.dirname().ok_or_else("BUG: remote component path has no parent")?
        } else {
            bail!("BUG: unknown component {}", com);
        };
        Ok((
            base.append("components").append(&com.to_string()),
            base.append("components").append(kind).append(&com.to_string()),
        ))
    }

    /// License server
    pub fn licensedb(&self) -> Path {
        let base =
            if self.use_local_licensedb { &self.local_base } else { &self.hosted_base };
        base.append("licensedb")
    }
}