UniSet 2.32.1
BackendClickHouse.h
1/*
2 * Copyright (c) 2020 Pavel Vainerman.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation, version 2.1.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Lesser Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16// -----------------------------------------------------------------------------
17#ifndef _BackendClickHouse_H_
18#define _BackendClickHouse_H_
19// -----------------------------------------------------------------------------
20#include <memory>
21#include <unordered_map>
22#include <chrono>
23#include "UObject_SK.h"
24#include "SMInterface.h"
25#include "SharedMemory.h"
26#include "extensions/Extensions.h"
27#include "ClickHouseInterface.h"
28#include "ClickHouseTagsConfig.h"
29// --------------------------------------------------------------------------
30namespace uniset
31{
32 // -----------------------------------------------------------------------------
148 //------------------------------------------------------------------------------------------
151 public UObject_SK
152 {
153 public:
154 BackendClickHouse( uniset::ObjectId objId, xmlNode* cnode, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
155 const std::string& prefix = "clickhouse" );
156 virtual ~BackendClickHouse();
157
159 static std::shared_ptr<BackendClickHouse> init_clickhouse( int argc, const char* const* argv,
160 uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
161 const std::string& prefix = "clickhouse" );
162
164 static void help_print( int argc, const char* const* argv );
165
166 inline std::shared_ptr<LogAgregator> getLogAggregator()
167 {
168 return loga;
169 }
170 inline std::shared_ptr<DebugStream> log()
171 {
172 return mylog;
173 }
174
175 enum Timers
176 {
177 tmFlushBuffer,
178 tmReconnect,
179 tmLastNumberOfTimer
180 };
181
182 protected:
184
185 // переопределяем callback, чтобы оптимизировать
186 // обработку большого количества сообщений
187 // и убрать не нужную в данном процессе обработку (включая sleep_msec)
188 virtual void callback() noexcept override;
189
190 virtual void askSensors( UniversalIO::UIOCommand cmd ) override;
191 virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
192 virtual void timerInfo( const uniset::TimerMessage* tm ) override;
193 virtual void sysCommand( const uniset::SystemMessage* sm ) override;
194 virtual std::string getMonitInfo() const override;
195
196 void init( xmlNode* cnode );
197 bool flushBuffer();
198 bool reconnect();
199
200 std::shared_ptr<SMInterface> shm;
201
202 using Tag = std::pair<std::string, std::string>;
203 using TagList = std::vector<Tag>;
204
206 {
207 const std::string name;
208 TagList tags;
209
210 ParamInfo( const std::string& _name, const TagList& _tags ):
211 name(_name), tags(_tags) {}
212 };
213
214 std::unordered_map<uniset::ObjectId, ParamInfo> clickhouseParams;
215 TagList globalTags;
216
217 timeout_t bufSyncTime = { 5000 };
218 size_t bufSize = { 5000 };
219 size_t bufMaxSize = { 100000 }; // drop messages
220 bool timerIsOn = { false };
221 timeout_t reconnectTime = { 5000 };
222 std::string lastError;
223
224 // работа с ClickHouse
225 std::shared_ptr<clickhouse::ColumnDateTime64> colTimeStamp;
226 std::shared_ptr<clickhouse::ColumnFloat64> colValue;
227 std::shared_ptr<clickhouse::ColumnString> colName;
228 std::shared_ptr<clickhouse::ColumnString> colNodeName;
229 std::shared_ptr<clickhouse::ColumnString> colProducer;
230 std::shared_ptr<clickhouse::ColumnArray> arrTagKeys;
231 std::shared_ptr<clickhouse::ColumnArray> arrTagValues;
232
233 void createColumns();
234 void clearData();
235 static TagList parseTags( const std::string& tags );
236
237 std::unique_ptr<uniset::ClickHouseTagsConfig> dyntags;
238
239 std::string fullTableName;
240 std::unique_ptr<ClickHouseInterface> db;
241
242 std::string dbhost;
243 int dbport;
244 std::string dbuser;
245 std::string dbpass;
246 std::string dbname;
247
248 private:
249 std::string prefix;
250 bool connect_ok = { false };
251 };
252 // --------------------------------------------------------------------------
253} // end of namespace uniset
254// -----------------------------------------------------------------------------
255#endif // _BackendClickHouse_H_
256// -----------------------------------------------------------------------------
Definition UObject_SK.h:30
Definition BackendClickHouse.h:152
static std::shared_ptr< BackendClickHouse > init_clickhouse(int argc, const char *const *argv, uniset::ObjectId shmID, const std::shared_ptr< SharedMemory > &ic=nullptr, const std::string &prefix="clickhouse")
Definition BackendClickHouse.cc:239
virtual std::string getMonitInfo() const override
Definition BackendClickHouse.cc:484
virtual void callback() noexcept override
Definition BackendClickHouse.cc:274
static void help_print(int argc, const char *const *argv)
Definition BackendClickHouse.cc:202
Definition MessageType.h:127
Definition MessageType.h:171
Definition MessageType.h:214
Definition Calibration.h:27
long ObjectId
Definition UniSetTypes_i.idl:30
Definition BackendClickHouse.h:206