Turi Create  4.0
server.hpp
1 /* Copyright © 2018 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 
7 #include <visualization/server/plot.hpp>
8 
9 #include <memory>
10 #include <unordered_map>
11 
12 namespace turi {
13 
14 class sframe_reader;
15 class unity_sframe;
16 
17 namespace visualization {
18 
19  class WebServer {
20  public:
21  struct table {
22  table(const std::shared_ptr<unity_sframe>& sf, std::unique_ptr<sframe_reader> reader, const std::string& title);
23  std::shared_ptr<unity_sframe> sf;
24  std::unique_ptr<sframe_reader> reader;
25  std::string title;
26  };
27 
28  typedef std::unordered_map< std::string, Plot > plot_map;
29  typedef std::vector< table > table_vector;
30 
31  static WebServer& get_instance();
32  ~WebServer();
33 
34  // Returns the ID of the added element
35  std::string add_plot(const Plot& plot);
36  std::string add_table(const std::shared_ptr<unity_sframe>& table, const std::string& title);
37 
38  // Generates and returns the URL (optionally to a given visualization object).
39  // Spins up the web server lazily, if needed.
40  static std::string get_base_url();
41  static std::string get_url_for_plot(const Plot& plot);
42  static std::string get_url_for_table(const std::shared_ptr<unity_sframe>& table, const std::string& title);
43 
44  private:
45  WebServer();
46 
47  class Impl;
48  plot_map m_plots;
49  table_vector m_tables;
50  std::unique_ptr<Impl> m_impl;
51  };
52 
53  extern std::string VISUALIZATION_WEB_SERVER_ROOT_DIRECTORY;
54 
55 }}