1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "stat_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 namespace {
21 enum Index { EVENT_NAME = 0, STAT_EVENT_TYPE = 1, COUNT = 2, SEVERITY = 3, SOURCE = 4 };
22 }
StatTable(const TraceDataCache * dataCache)23 StatTable::StatTable(const TraceDataCache* dataCache) : TableBase(dataCache)
24 {
25 tableColumn_.push_back(TableBase::ColumnInfo("event_name", "TEXT"));
26 tableColumn_.push_back(TableBase::ColumnInfo("stat_type", "TEXT"));
27 tableColumn_.push_back(TableBase::ColumnInfo("count", "INTEGER"));
28 tableColumn_.push_back(TableBase::ColumnInfo("serverity", "TEXT"));
29 tableColumn_.push_back(TableBase::ColumnInfo("source", "TEXT"));
30 tablePriKey_.push_back("event_name");
31 tablePriKey_.push_back("stat_type");
32 }
33
~StatTable()34 StatTable::~StatTable() {}
35
CreateCursor()36 std::unique_ptr<TableBase::Cursor> StatTable::CreateCursor()
37 {
38 return std::make_unique<Cursor>(dataCache_, this);
39 }
40
Cursor(const TraceDataCache * dataCache,TableBase * table)41 StatTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
42 : TableBase::Cursor(dataCache, table, STAT_EVENT_MAX * TRACE_EVENT_MAX)
43 {
44 }
45
~Cursor()46 StatTable::Cursor::~Cursor() {}
47
Column(int column) const48 int StatTable::Cursor::Column(int column) const
49 {
50 const StatAndInfo stat = dataCache_->GetConstStatAndInfo();
51 SupportedTraceEventType eventType = static_cast<SupportedTraceEventType>(CurrentRow() / STAT_EVENT_MAX);
52 StatType statType = static_cast<StatType>(CurrentRow() % STAT_EVENT_MAX);
53 switch (column) {
54 case EVENT_NAME:
55 sqlite3_result_text(context_, dataCache_->GetConstStatAndInfo().GetEvent(eventType).c_str(),
56 STR_DEFAULT_LEN, nullptr);
57 break;
58 case STAT_EVENT_TYPE:
59 sqlite3_result_text(context_, dataCache_->GetConstStatAndInfo().GetStat(statType).c_str(), STR_DEFAULT_LEN,
60 nullptr);
61 break;
62 case COUNT:
63 sqlite3_result_int64(context_,
64 static_cast<int64_t>(dataCache_->GetConstStatAndInfo().GetValue(eventType, statType)));
65 break;
66 case SEVERITY:
67 sqlite3_result_text(context_,
68 dataCache_->GetConstStatAndInfo().GetSeverityDesc(eventType, statType).c_str(),
69 STR_DEFAULT_LEN, nullptr);
70 break;
71 case SOURCE:
72 sqlite3_result_text(context_, "trace", STR_DEFAULT_LEN, nullptr);
73 break;
74 default:
75 TS_LOGF("Unregistered column : %d", column);
76 break;
77 }
78 return SQLITE_OK;
79 }
80 } // namespace TraceStreamer
81 } // namespace SysTuning
82