/src/duckdb/src/main/capi/profiling_info-c.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "duckdb/main/capi/capi_internal.hpp" |
2 | | |
3 | | using duckdb::Connection; |
4 | | using duckdb::DuckDB; |
5 | | using duckdb::EnumUtil; |
6 | | using duckdb::MetricsType; |
7 | | using duckdb::optional_ptr; |
8 | | using duckdb::ProfilingNode; |
9 | | |
10 | 0 | duckdb_profiling_info duckdb_get_profiling_info(duckdb_connection connection) { |
11 | 0 | if (!connection) { |
12 | 0 | return nullptr; |
13 | 0 | } |
14 | 0 | Connection *conn = reinterpret_cast<Connection *>(connection); |
15 | 0 | optional_ptr<ProfilingNode> profiling_node; |
16 | 0 | try { |
17 | 0 | profiling_node = conn->GetProfilingTree(); |
18 | 0 | } catch (std::exception &ex) { |
19 | 0 | return nullptr; |
20 | 0 | } |
21 | | |
22 | 0 | if (!profiling_node) { |
23 | 0 | return nullptr; |
24 | 0 | } |
25 | 0 | return reinterpret_cast<duckdb_profiling_info>(profiling_node.get()); |
26 | 0 | } |
27 | | |
28 | 0 | duckdb_value duckdb_profiling_info_get_value(duckdb_profiling_info info, const char *key) { |
29 | 0 | if (!info) { |
30 | 0 | return nullptr; |
31 | 0 | } |
32 | 0 | auto &node = *reinterpret_cast<duckdb::ProfilingNode *>(info); |
33 | 0 | auto &profiling_info = node.GetProfilingInfo(); |
34 | 0 | auto key_enum = EnumUtil::FromString<MetricsType>(duckdb::StringUtil::Upper(key)); |
35 | 0 | if (!profiling_info.Enabled(profiling_info.settings, key_enum)) { |
36 | 0 | return nullptr; |
37 | 0 | } |
38 | | |
39 | 0 | auto str = profiling_info.GetMetricAsString(key_enum); |
40 | 0 | return duckdb_create_varchar_length(str.c_str(), strlen(str.c_str())); |
41 | 0 | } |
42 | | |
43 | 0 | duckdb_value duckdb_profiling_info_get_metrics(duckdb_profiling_info info) { |
44 | 0 | if (!info) { |
45 | 0 | return nullptr; |
46 | 0 | } |
47 | | |
48 | 0 | auto &node = *reinterpret_cast<duckdb::ProfilingNode *>(info); |
49 | 0 | auto &profiling_info = node.GetProfilingInfo(); |
50 | |
|
51 | 0 | duckdb::InsertionOrderPreservingMap<duckdb::string> metrics_map; |
52 | 0 | for (const auto &metric : profiling_info.metrics) { |
53 | 0 | auto key = EnumUtil::ToString(metric.first); |
54 | 0 | if (!profiling_info.Enabled(profiling_info.settings, metric.first)) { |
55 | 0 | continue; |
56 | 0 | } |
57 | | |
58 | 0 | if (key == EnumUtil::ToString(MetricsType::OPERATOR_TYPE)) { |
59 | 0 | auto type = duckdb::PhysicalOperatorType(metric.second.GetValue<uint8_t>()); |
60 | 0 | metrics_map[key] = EnumUtil::ToString(type); |
61 | 0 | } else { |
62 | 0 | metrics_map[key] = metric.second.ToString(); |
63 | 0 | } |
64 | 0 | } |
65 | |
|
66 | 0 | auto map = duckdb::Value::MAP(metrics_map); |
67 | 0 | return reinterpret_cast<duckdb_value>(new duckdb::Value(map)); |
68 | 0 | } |
69 | | |
70 | 0 | idx_t duckdb_profiling_info_get_child_count(duckdb_profiling_info info) { |
71 | 0 | if (!info) { |
72 | 0 | return 0; |
73 | 0 | } |
74 | 0 | auto &node = *reinterpret_cast<duckdb::ProfilingNode *>(info); |
75 | 0 | return node.GetChildCount(); |
76 | 0 | } |
77 | | |
78 | 0 | duckdb_profiling_info duckdb_profiling_info_get_child(duckdb_profiling_info info, idx_t index) { |
79 | 0 | if (!info) { |
80 | 0 | return nullptr; |
81 | 0 | } |
82 | 0 | auto &node = *reinterpret_cast<duckdb::ProfilingNode *>(info); |
83 | 0 | if (index >= node.GetChildCount()) { |
84 | 0 | return nullptr; |
85 | 0 | } |
86 | | |
87 | 0 | ProfilingNode *profiling_info_ptr = node.GetChild(index).get(); |
88 | 0 | return reinterpret_cast<duckdb_profiling_info>(profiling_info_ptr); |
89 | 0 | } |