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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*<LICENSE>
    This file is part of Memthol.

    Copyright (C) 2020 OCamlPro.

    Memthol is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Memthol is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Memthol.  If not, see <https://www.gnu.org/licenses/>.
*/

//! Server-side chart handling.
//!
//! Note that most types in this crate implement (de)serialization to/from json. These types
//! implement the [`Json`] trait which provides straightforward conversion functions. This is used
//! heavily for server/client exchanges.
//!
//! # Basic Workflow
//!
//! All allocation-related data is stored in a global state in the [`data`] module. It features a
//! [`Watcher`] type which, after [`start`]ing it, will monitor a directory for init and diff files.
//!
//! [`Json`]: yew::format::Json (The Json trait)
//! [`Watcher`]: data::Watcher (The Watcher struct in module data)
//! [`start`]: data::start (The start function in module data)

#![deny(missing_docs)]

pub extern crate alloc_data;
pub extern crate palette;
pub extern crate plotters;

#[macro_use]
pub mod prelude;

pub mod chart;
pub mod color;
#[cfg(any(test, feature = "server"))]
pub mod data;
pub mod filter;
pub mod msg;
pub mod point;

#[cfg(any(test, feature = "server"))]
pub use chart::Chart;

#[cfg(any(test, feature = "server"))]
prelude! {}

/// Trait implemented by all charts.
#[cfg(any(test, feature = "server"))]
pub trait ChartExt {
    /// Generates the new points of the chart.
    fn new_points(&mut self, filters: &mut Filters, init: bool) -> Res<Points>;

    /// Resets the chart.
    fn reset(&mut self, filters: &Filters);
}

/// Aggregates some charts.
#[cfg(any(test, feature = "server"))]
pub struct Charts {
    /// List of active charts.
    charts: Vec<Chart>,
    /// List of filters.
    filters: Filters,
    /// Start time of the run.
    ///
    /// This is used to check whether we need to detect that the init file of the run has changed
    /// and that we need to reset the charts.
    start_time: Option<time::Date>,
    /// List of messages for the client, populated/drained when receiving messages.
    to_client_msgs: msg::to_client::Msgs,
    /// Settings.
    settings: settings::Charts,
}

#[cfg(any(test, feature = "server"))]
impl Charts {
    /// Constructor.
    pub fn new() -> Self {
        Self {
            charts: vec![],
            filters: Filters::new(),
            start_time: None,
            to_client_msgs: msg::to_client::Msgs::with_capacity(7),
            settings: settings::Charts::new(),
        }
    }

    /// All the charts.
    pub fn charts(&self) -> &Vec<Chart> {
        &self.charts
    }
    /// All the filters.
    pub fn filters(&self) -> &Filters {
        &self.filters
    }
    /// Start time.
    pub fn start_time(&self) -> Option<&time::Date> {
        self.start_time.as_ref()
    }

    /// Runs filter generation.
    ///
    /// Returns the number of filter generated.
    #[cfg(any(test, feature = "server"))]
    pub fn auto_gen() -> Res<Self> {
        let (filters, charts) = Filters::auto_gen(&*data::get()?, filter::gen::get())?;
        Ok(Self {
            charts,
            filters,
            start_time: None,
            to_client_msgs: msg::to_client::Msgs::with_capacity(7),
            settings: settings::Charts::new(),
        })
    }

    /// Pushes a new chart.
    pub fn push(&mut self, chart: Chart) {
        self.charts.push(chart)
    }

    /// Chart mutable accessor.
    pub fn get_mut(&mut self, uid: uid::Chart) -> Res<&mut Chart> {
        for chart in self.charts.iter_mut() {
            if chart.uid() == uid {
                return Ok(chart);
            }
        }
        bail!("cannot access chart with unknown UID #{}", uid)
    }
}

#[cfg(any(test, feature = "server"))]
impl Charts {
    /// Restarts the charts and the filters if needed.
    fn restart_if_needed(&mut self) -> Res<bool> {
        let data = data::get();
        let start_time = data
            .and_then(|data| data.start_time())
            .chain_err(|| "while checking if the charts should be restarted")?;
        if self.start_time != Some(start_time) {
            self.start_time = Some(start_time);
            for chart in &mut self.charts {
                chart.reset(&self.filters)
            }
            self.filters.reset();
            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Extracts the new points for the different charts.
    ///
    /// The boolean indicates whether the points should overwrite existing points. It is typically
    /// true when the init file of the run has changed (the run was restarted).
    pub fn new_points(&mut self, init: bool) -> Res<(point::ChartPoints, bool)> {
        let restarted = self.restart_if_needed()?;
        let mut points = point::ChartPoints::new();
        for chart in &mut self.charts {
            if let Some(chart_points) = chart.new_points(
                restarted || init,
                &mut self.filters,
                self.settings.time_windopt(),
            )? {
                let prev = points.insert(chart.uid(), chart_points);
                debug_assert!(prev.is_none())
            }
        }
        Ok((points, restarted || init))
    }

    /// Handles a charts message from the client.
    pub fn handle_chart_msg(&mut self, msg: msg::to_server::ChartsMsg) -> Res<bool> {
        debug_assert!(self.to_client_msgs.is_empty());

        let reloaded = match msg {
            msg::to_server::ChartsMsg::New(x_axis, y_axis) => {
                let all_active = self.filters.fold(BTMap::new(), |mut map, uid| {
                    let prev = map.insert(uid, true);
                    debug_assert_eq!(prev, None);
                    map
                });
                let nu_chart = chart::Chart::new(&mut self.filters, x_axis, y_axis, all_active)
                    .chain_err(|| "while creating new chart")?;

                // Chart creation message.
                self.to_client_msgs
                    .push(msg::to_client::ChartsMsg::new_chart(
                        nu_chart.spec().clone(),
                        nu_chart.settings().clone(),
                    ));
                // // Initial points message.
                // let points = nu_chart.new_points(&mut self.filters, true).chain_err(|| {
                //     format!(
                //         "while generating the initial points for new chart #{}",
                //         nu_chart.uid()
                //     )
                // })?;
                // to_client_msgs.push(msg::to_client::ChartMsg::new_points(nu_chart.uid(), points));

                self.charts.push(nu_chart);
                true
            }

            msg::to_server::ChartsMsg::Reload => {
                let msg = self.reload_points(None, false)?;
                self.to_client_msgs.push(msg);
                true
            }

            msg::to_server::ChartsMsg::ChartUpdate { uid, msg } => {
                let reload = self.get_mut(uid)?.update(msg);
                if reload {
                    let msg = self.reload_points(Some(uid), false)?;
                    self.to_client_msgs.push(msg);
                    reload
                } else {
                    false
                }
            }

            msg::to_server::ChartsMsg::Settings(settings) => {
                let send_new_points = self.settings.overwrite(settings);
                if send_new_points {
                    let msg = self.reload_points(None, false)?;
                    self.to_client_msgs.push(msg);
                }
                false
            }
        };

        Ok(reloaded)
    }

    /// Recomputes all the points, and returns them as a message for the client.
    pub fn reload_points(
        &mut self,
        uid: Option<uid::Chart>,
        refresh_filters: bool,
    ) -> Res<msg::to_client::Msg> {
        let mut new_points = point::ChartPoints::new();
        for chart in &mut self.charts {
            if let Some(uid) = uid {
                if chart.uid() != uid {
                    continue;
                }
            }
            chart.reset(&self.filters);
            self.filters.reset();
            let points_opt = chart
                .new_points(true, &mut self.filters, self.settings.time_windopt())
                .chain_err(|| format!("while generating points for chart #{}", chart.uid()))?;
            if let Some(points) = points_opt {
                let prev = new_points.insert(chart.uid(), points);
                if prev.is_some() {
                    bail!("chart UID collision on #{}", chart.uid())
                }
            }
        }
        Ok(msg::to_client::ChartsMsg::new_points(
            new_points,
            refresh_filters,
        ))
    }

    /// Handles a message from the client.
    pub fn handle_msg<'me>(
        &'me mut self,
        msg: msg::to_server::Msg,
    ) -> Res<(impl Iterator<Item = msg::to_client::Msg> + 'me, bool)> {
        use msg::to_server::Msg::*;

        let reload = match msg {
            Charts(msg) => self.handle_chart_msg(msg)?,
            Filters(msg) => {
                let (mut msgs, should_reload) = self.filters.update(msg)?;
                if should_reload {
                    msgs.push(self.reload_points(None, true)?)
                }
                self.to_client_msgs.extend(msgs);
                should_reload
            }
        };

        Ok((self.to_client_msgs.drain(0..), reload))
    }
}