-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
141 lines (121 loc) · 4.19 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class View(object):
meta_columns = (
'dt', 'ts', 'dbname', 'dbhost', 'dbport'
)
table_name = ''
sql_template = """
CREATE VIEW pg_telemetry.{} AS
{}
ORDER BY ts DESC
"""
sql_select = ''
def __init__(self, client):
self.client = client
def _get_exists_tables(self):
sql = "SHOW TABLES FROM pg_telemetry"
ret = self.client.execute(sql)
return [x[0] for x in ret]
def create(self):
if self.table_name not in self._get_exists_tables():
sql_select = self.sql_select.format(mc=','.join(self.meta_columns))
sql = self.sql_template.format(self.table_name, sql_select)
return self.client.execute(sql)
def drop(self):
if self.table_name in self._get_exists_tables():
sql = "DROP TABLE pg_telemetry.{}".format(self.table_name)
return self.client.execute(sql)
class ResponseTimeView(View):
table_name = 'response_time'
sql_select = """
SELECT {mc},
queryid,
query,
divide(total_time, calls) AS response_time
FROM pg_telemetry.pg_stat_statements
"""
class RollbacksView(View):
table_name = 'rollbacks'
sql_select = """
SELECT {mc},
runningDifference(xact_rollback)/runningDifference(ts) AS rps
FROM pg_telemetry.pg_stat_database
"""
class PerformanceView(View):
table_name = 'performance'
sql_select = """
SELECT {mc},
tps,
qps
FROM (
SELECT {mc},
runningDifference(sum(calls)) / runningDifference(ts) AS qps
FROM pg_telemetry.pg_stat_statements
GROUP BY {mc}
)
ANY FULL JOIN (
SELECT {mc},
runningDifference(xact_commit + xact_rollback) / runningDifference(ts) AS tps
FROM pg_telemetry.pg_stat_database
)
USING {mc}
"""
class QueryPerfomanceView(View):
table_name = 'query_perfomance'
sql_select = """
SELECT {mc},
substringUTF8(
replaceRegexpAll(query, '[\n\t ]+', ' '), 1, 100
) AS query_sample,
queryid,
anyHeavy(userid),
runningDifference(sum(calls)) / runningDifference(ts) AS qps
FROM pg_telemetry.pg_stat_statements
GROUP BY {mc}, query, queryid
""" # FIXME: Работает не правильно, runningDifference ходит по разным query
class CacheHitRatioView(View):
table_name = 'cache_hit_ratio'
sql_select = """
SELECT {mc},
-- show cache hit ratio, values closer to 100 are better
round(100 * sum(blks_hit) / sum(blks_hit + blks_read), 3) as cache_hit_ratio
FROM pg_telemetry.pg_stat_database
GROUP BY {mc}
"""
class FetchedRowsRatioView(View):
table_name = 'fetched_rows_ratio'
sql_select = """
SELECT {mc},
-- show fetched rows ratio, values closer to 100 are better
round(100 * sum(tup_fetched) / sum(tup_fetched + tup_returned), 3) as fetched_ratio
FROM pg_telemetry.pg_stat_database
GROUP BY {mc}
"""
class TempFilesPgssView(View):
table_name = 'temp_files_pgss'
sql_select = """
SELECT {mc},
queryid,
calls,
(temp_blks_read + temp_blks_written) * 8192 as temp_io,
(temp_blks_written * 8192) / calls as temp_size_avg,
query
FROM pg_telemetry.pg_stat_statements
WHERE temp_blks_read + temp_blks_written > 0
ORDER BY (temp_blks_written / calls) DESC
"""
class RunningPgStatStatements(View):
"""
NOTE: https://stackoverflow.com/questions/51856397/clickhouse-running-diff-with-grouping/51873915#51873915
"""
table_name = 'running_pg_stat_statements'
sql_select = """
SELECT {mc},
queryid,
runningDifference(calls) as delta_calls,
runningDifference(rows) as delta_rows,
runningDifference(total_time) as delta_total_time
from (
select * from pg_telemetry.pg_stat_statements
order by queryid, ts
)
"""