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", "STRING"));
26 tableColumn_.push_back(TableBase::ColumnInfo("stat_type", "STRING"));
27 tableColumn_.push_back(TableBase::ColumnInfo("count", "INT"));
28 tableColumn_.push_back(TableBase::ColumnInfo("serverity", "STRING"));
29 tableColumn_.push_back(TableBase::ColumnInfo("source", "STRING"));
30 tablePriKey_.push_back("event_name");
31 tablePriKey_.push_back("stat_type");
32 }
33
~StatTable()34 StatTable::~StatTable() {}
35
CreateCursor()36 void StatTable::CreateCursor()
37 {
38 cursor_ = std::make_unique<Cursor>(dataCache_);
39 }
40
Cursor(const TraceDataCache * dataCache)41 StatTable::Cursor::Cursor(const TraceDataCache* dataCache)
42 : TableBase::Cursor(dataCache, 0, 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(),
60 STR_DEFAULT_LEN, nullptr);
61 break;
62 case COUNT:
63 sqlite3_result_int64(context_, static_cast<int64_t>(dataCache_->GetConstStatAndInfo().GetValue(eventType,
64 statType)));
65 break;
66 case SEVERITY:
67 sqlite3_result_text(context_, dataCache_->GetConstStatAndInfo().GetSeverityDesc(eventType,
68 statType).c_str(), STR_DEFAULT_LEN, nullptr);
69 break;
70 case SOURCE:
71 sqlite3_result_text(context_, "trace", STR_DEFAULT_LEN, nullptr);
72 break;
73 default:
74 TS_LOGF("Unregistered column : %d", column);
75 break;
76 }
77 return SQLITE_OK;
78 }
79 } // namespace TraceStreamer
80 } // namespace SysTuning
81