UniSet @VERSION@
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#include "USingleProcess.h"
30// --------------------------------------------------------------------------
31namespace uniset
32{
33 // -----------------------------------------------------------------------------
149 //------------------------------------------------------------------------------------------
152 private USingleProcess,
153 public UObject_SK
154 {
155 public:
156 BackendClickHouse( uniset::ObjectId objId, xmlNode* cnode, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
157 const std::string& prefix = "clickhouse" );
158 virtual ~BackendClickHouse();
159
161 static std::shared_ptr<BackendClickHouse> init_clickhouse( int argc, const char* const* argv,
162 uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
163 const std::string& prefix = "clickhouse" );
164
166 static void help_print( int argc, const char* const* argv );
167
168 inline std::shared_ptr<LogAgregator> getLogAggregator()
169 {
170 return loga;
171 }
172 inline std::shared_ptr<DebugStream> log()
173 {
174 return mylog;
175 }
176
177 enum Timers
178 {
179 tmFlushBuffer,
180 tmReconnect,
181 tmLastNumberOfTimer
182 };
183
184 protected:
186
187 // переопределяем callback, чтобы оптимизировать
188 // обработку большого количества сообщений
189 // и убрать не нужную в данном процессе обработку (включая sleep_msec)
190 virtual void callback() noexcept override;
191
192 virtual void askSensors( UniversalIO::UIOCommand cmd ) override;
193 virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
194 virtual void timerInfo( const uniset::TimerMessage* tm ) override;
195 virtual void sysCommand( const uniset::SystemMessage* sm ) override;
196 virtual std::string getMonitInfo() const override;
197
198 void init( xmlNode* cnode );
199 bool flushBuffer();
200 bool reconnect();
201
202 std::shared_ptr<SMInterface> shm;
203
204 using Tag = std::pair<std::string, std::string>;
205 using TagList = std::vector<Tag>;
206
208 {
209 const std::string name;
210 TagList tags;
211
212 ParamInfo( const std::string& _name, const TagList& _tags ):
213 name(_name), tags(_tags) {}
214 };
215
216 std::unordered_map<uniset::ObjectId, ParamInfo> clickhouseParams;
217 TagList globalTags;
218
219 timeout_t bufSyncTime = { 5000 };
220 size_t bufSize = { 5000 };
221 size_t bufMaxSize = { 100000 }; // drop messages
222 bool timerIsOn = { false };
223 timeout_t reconnectTime = { 5000 };
224 std::string lastError;
225
226 // работа с ClickHouse
227 std::shared_ptr<clickhouse::ColumnDateTime64> colTimeStamp;
228 std::shared_ptr<clickhouse::ColumnFloat64> colValue;
229 std::shared_ptr<clickhouse::ColumnString> colName;
230 std::shared_ptr<clickhouse::ColumnString> colNodeName;
231 std::shared_ptr<clickhouse::ColumnString> colProducer;
232 std::shared_ptr<clickhouse::ColumnArray> arrTagKeys;
233 std::shared_ptr<clickhouse::ColumnArray> arrTagValues;
234
235 void createColumns();
236 void clearData();
237 static TagList parseTags( const std::string& tags );
238
239 std::unique_ptr<uniset::ClickHouseTagsConfig> dyntags;
240
241 std::string fullTableName;
242 std::unique_ptr<ClickHouseInterface> db;
243
244 std::string dbhost;
245 int dbport;
246 std::string dbuser;
247 std::string dbpass;
248 std::string dbname;
249
250 private:
251 std::string prefix;
252 bool connect_ok = { false };
253 };
254 // --------------------------------------------------------------------------
255} // end of namespace uniset
256// -----------------------------------------------------------------------------
257#endif // _BackendClickHouse_H_
258// -----------------------------------------------------------------------------
Definition UObject_SK.h:30
Definition BackendClickHouse.h:154
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:241
virtual std::string getMonitInfo() const override
Definition BackendClickHouse.cc:486
virtual void callback() noexcept override
Definition BackendClickHouse.cc:276
static void help_print(int argc, const char *const *argv)
Definition BackendClickHouse.cc:203
Definition MessageType.h:127
Definition MessageType.h:171
Definition MessageType.h:214
Definition USingleProcess.h:28
Definition AccessConfig.h:30
long ObjectId
Definition UniSetTypes_i.idl:30
Definition BackendClickHouse.h:208