1 /**
2  * Copyright (c) 2007-2012, Timothy Stack
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of Timothy Stack nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef db_sub_source_hh
31 #define db_sub_source_hh
32 
33 #include <string>
34 #include <vector>
35 #include <iterator>
36 
37 #include <sqlite3.h>
38 
39 #include "textview_curses.hh"
40 #include "hist_source.hh"
41 
42 class db_label_source : public text_sub_source, public text_time_translator {
43 public:
~db_label_source()44     ~db_label_source() {
45         this->clear();
46     }
47 
has_log_time_column() const48     bool has_log_time_column() const {
49         return !this->dls_time_column.empty();
50     };
51 
text_line_count()52     size_t text_line_count() {
53         return this->dls_rows.size();
54     };
55 
text_size_for_line(textview_curses & tc,int line,line_flags_t flags)56     size_t text_size_for_line(textview_curses &tc, int line, line_flags_t flags) {
57         return this->text_line_width(tc);
58     };
59 
text_line_width(textview_curses & curses)60     size_t text_line_width(textview_curses &curses) {
61         size_t retval = 0;
62 
63         for (auto &dls_header : this->dls_headers) {
64             retval += dls_header.hm_column_size + 1;
65         }
66         return retval;
67     };
68 
69     void text_value_for_line(textview_curses &tc,
70                              int row,
71                              std::string &label_out,
72                              line_flags_t flags);
73 
74     void text_attrs_for_line(textview_curses &tc, int row, string_attrs_t &sa);
75 
76     void push_header(const std::string &colstr, int type, bool graphable);
77 
78     void push_column(const char *colstr);
79 
80     void clear();
81 
82     long column_name_to_index(const std::string &name) const;
83 
84     nonstd::optional<vis_line_t> row_for_time(struct timeval time_bucket);
85 
time_for_row(vis_line_t row)86     nonstd::optional<struct timeval> time_for_row(vis_line_t row) {
87         if ((row < 0_vl) || (((size_t) row) >= this->dls_time_column.size())) {
88             return nonstd::nullopt;
89         }
90 
91         return this->dls_time_column[row];
92     };
93 
94     struct header_meta {
header_metadb_label_source::header_meta95         explicit header_meta(std::string name)
96             : hm_name(std::move(name)),
97               hm_column_type(SQLITE3_TEXT),
98               hm_graphable(false),
99               hm_log_time(false),
100               hm_column_size(0) {
101 
102         };
103 
operator ==db_label_source::header_meta104         bool operator==(const std::string &name) const {
105             return this->hm_name == name;
106         };
107 
108         std::string hm_name;
109         int hm_column_type;
110         unsigned int hm_sub_type{0};
111         bool hm_graphable;
112         bool hm_log_time;
113         size_t hm_column_size;
114     };
115 
116     stacked_bar_chart<std::string> dls_chart;
117     std::vector<header_meta> dls_headers;
118     std::vector<std::vector<const char *>> dls_rows;
119     std::vector<struct timeval> dls_time_column;
120     std::vector<size_t> dls_cell_width;
121     int dls_time_column_index{-1};
122 
123     static const char *NULL_STR;
124 };
125 
126 class db_overlay_source : public list_overlay_source {
127 public:
128     size_t list_overlay_count(const listview_curses &lv);
129 
130     bool list_value_for_overlay(const listview_curses &lv,
131                                 int y, int bottom,
132                                 vis_line_t row,
133                                 attr_line_t &value_out) override;
134 
135     bool dos_active{false};
136     db_label_source *dos_labels{nullptr};
137     std::vector<attr_line_t> dos_lines;
138 };
139 #endif
140