Coverage for procpath/procret.py: 100%

65 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2025-04-05 18:56 +0000

1import json 

2from contextlib import closing 

3from datetime import datetime 

4from typing import List, Mapping, NamedTuple, Optional 

5 

6 

7try: 

8 import apsw as sqlite 

9 from apsw import SQLError as SqlError 

10except ImportError: # nocov 

11 import sqlite3 as sqlite 

12 from sqlite3 import OperationalError as SqlError 

13 

14 

15__all__ = 'create_query', 'query', 'registry', 'Query', 'QueryError', 'QueryExecutionError' 

16 

17registry = {} 

18 

19 

20class QueryError(Exception): 

21 """General query error.""" 

22 

23 

24class QueryExecutionError(Exception): 

25 """SQL query execution error.""" 

26 

27 

28class Query(NamedTuple): 

29 query: str 

30 """The SQL query itself.""" 

31 

32 title: str 

33 """Query title displayed on a plot.""" 

34 

35 name: Optional[str] = None 

36 """Short code used by the command-line interface.""" 

37 

38 min_version: tuple = (1,) 

39 """Minimal SQLite version compatible with the query.""" 

40 

41 procfile_required: frozenset = frozenset() 

42 """Procfiles required by the query. ``stat`` is assumed.""" 

43 

44 def get_short_query(self, *, ts_as_milliseconds=False) -> str: 

45 result = self.query.split('-- filter cut line --', 1)[0] (empty)procpath.test.cmd.TestExploreCommand.test_exploreprocpath.test.cmd.TestExploreCommand.test_explore_preload_databaseprocpath.test.cmd.TestExploreCommand.test_explore_preload_database_missingprocpath.test.unit.TestProcret.test_query_get_short_queryprocpath.test.unit.TestSqlitevizQuery.test_total_cpu_usageprocpath.test.unit.TestSqlitevizQuery.test_total_memory_consumption

46 if ts_as_milliseconds: (empty)procpath.test.cmd.TestExploreCommand.test_exploreprocpath.test.cmd.TestExploreCommand.test_explore_preload_databaseprocpath.test.cmd.TestExploreCommand.test_explore_preload_database_missingprocpath.test.unit.TestProcret.test_query_get_short_queryprocpath.test.unit.TestSqlitevizQuery.test_total_cpu_usageprocpath.test.unit.TestSqlitevizQuery.test_total_memory_consumption

47 result = result.replace('ts, -- unix timestamp', 'ts * 1000 ts,') (empty)procpath.test.cmd.TestExploreCommand.test_exploreprocpath.test.cmd.TestExploreCommand.test_explore_preload_databaseprocpath.test.cmd.TestExploreCommand.test_explore_preload_database_missingprocpath.test.unit.TestProcret.test_query_get_short_queryprocpath.test.unit.TestSqlitevizQuery.test_total_cpu_usageprocpath.test.unit.TestSqlitevizQuery.test_total_memory_consumption

48 

49 return result (empty)procpath.test.cmd.TestExploreCommand.test_exploreprocpath.test.cmd.TestExploreCommand.test_explore_preload_databaseprocpath.test.cmd.TestExploreCommand.test_explore_preload_database_missingprocpath.test.unit.TestProcret.test_query_get_short_queryprocpath.test.unit.TestSqlitevizQuery.test_total_cpu_usageprocpath.test.unit.TestSqlitevizQuery.test_total_memory_consumption

50 

51 

52def create_query( 

53 value_expr: str, title: str, *, cte='', table='record', procfile_required=None, **kwargs 

54) -> Query: 

55 return Query( (empty)procpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_get_short_query

56 f''' 

57 {cte.rstrip()} 

58 SELECT 

59 ts, -- unix timestamp 

60 stat_pid pid, 

61 {value_expr} value 

62 FROM {table} 

63 -- filter cut line -- 

64 WHERE 

65 (:after IS NULL OR :after <= ts) 

66 AND (:before IS NULL OR ts <= :before) 

67 AND (:pid_list IS NULL OR instr(:pid_list, ',' || stat_pid || ',')) 

68 ORDER BY stat_pid, record_id 

69 ''', 

70 title, 

71 procfile_required=frozenset(procfile_required or []), 

72 **kwargs, 

73 ) 

74 

75 

76def register_query(obj: Query): 

77 registry[obj.name] = obj 

78 

79 

80def query( 

81 database: str, 

82 query: Query, 

83 after: Optional[datetime] = None, 

84 before: Optional[datetime] = None, 

85 pid_list: Optional[List[int]] = None, 

86) -> List[Mapping]: 

87 with closing(sqlite.Connection(database)) as conn: # type: ignore[module-attr] procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_query_error_missing_requirementprocpath.test.cmd.TestPlotCommand.test_plot_query_error_old_sqliteprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_query_procfile_required_missingprocpath.test.unit.TestProcret.test_query_procfile_required_missing_old_dbprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

88 cursor = conn.cursor() procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_query_error_missing_requirementprocpath.test.cmd.TestPlotCommand.test_plot_query_error_old_sqliteprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_query_procfile_required_missingprocpath.test.unit.TestProcret.test_query_procfile_required_missing_old_dbprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

89 

90 sqlite_version = cursor.execute('SELECT sqlite_version()').fetchone()[0] procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_query_error_missing_requirementprocpath.test.cmd.TestPlotCommand.test_plot_query_error_old_sqliteprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_query_procfile_required_missingprocpath.test.unit.TestProcret.test_query_procfile_required_missing_old_dbprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

91 sqlite_version = tuple(map(int, sqlite_version.split('.'))) procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_query_error_missing_requirementprocpath.test.cmd.TestPlotCommand.test_plot_query_error_old_sqliteprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_query_procfile_required_missingprocpath.test.unit.TestProcret.test_query_procfile_required_missing_old_dbprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

92 if sqlite_version < query.min_version: procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_query_error_missing_requirementprocpath.test.cmd.TestPlotCommand.test_plot_query_error_old_sqliteprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_query_procfile_required_missingprocpath.test.unit.TestProcret.test_query_procfile_required_missing_old_dbprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

93 raise QueryError( procpath.test.cmd.TestPlotCommand.test_plot_query_error_old_sqlite

94 f'{query.title!r} requires SQLite version >= {query.min_version}, ' 

95 f'installed {sqlite_version}. Install apsw and try again.' 

96 ) 

97 

98 if query.procfile_required: procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_query_error_missing_requirementprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_query_procfile_required_missingprocpath.test.unit.TestProcret.test_query_procfile_required_missing_old_dbprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

99 sql = "SELECT value FROM meta WHERE key = 'procfile_list'" procpath.test.cmd.TestPlotCommand.test_plot_query_error_missing_requirementprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_query_procfile_required_missingprocpath.test.unit.TestProcret.test_query_procfile_required_missing_old_db

100 procfile_list = cursor.execute(sql).fetchone() procpath.test.cmd.TestPlotCommand.test_plot_query_error_missing_requirementprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_query_procfile_required_missingprocpath.test.unit.TestProcret.test_query_procfile_required_missing_old_db

101 procfile_provided = set(json.loads(procfile_list[0])) if procfile_list else set() procpath.test.cmd.TestPlotCommand.test_plot_query_error_missing_requirementprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_query_procfile_required_missingprocpath.test.unit.TestProcret.test_query_procfile_required_missing_old_db

102 missing = ', '.join(sorted(query.procfile_required - procfile_provided)) procpath.test.cmd.TestPlotCommand.test_plot_query_error_missing_requirementprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_query_procfile_required_missingprocpath.test.unit.TestProcret.test_query_procfile_required_missing_old_db

103 if missing: procpath.test.cmd.TestPlotCommand.test_plot_query_error_missing_requirementprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_query_procfile_required_missingprocpath.test.unit.TestProcret.test_query_procfile_required_missing_old_db

104 raise QueryError( procpath.test.cmd.TestPlotCommand.test_plot_query_error_missing_requirementprocpath.test.unit.TestProcret.test_query_procfile_required_missingprocpath.test.unit.TestProcret.test_query_procfile_required_missing_old_db

105 f'{query.title!r} requires the following procfiles missing ' 

106 f'in the database: {missing}' 

107 ) 

108 

109 row_factory = lambda cur, row: dict(zip([t[0] for t in cur.description], row)) procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

110 try: procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

111 conn.row_factory = row_factory procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

112 except AttributeError: procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

113 conn.setrowtrace(row_factory) # type: ignore[attribute-error] procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

114 

115 cursor = conn.cursor() procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

116 try: procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

117 cursor.execute(query.query, { procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

118 'after': after.timestamp() if after else None, 

119 'before': before.timestamp() if before else None, 

120 'pid_list': ',{},'.format(','.join(map(str, pid_list))) if pid_list else None, 

121 }) 

122 except SqlError as ex: # type: ignore[mro-error] procpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_error

123 raise QueryExecutionError(str(ex)) from ex procpath.test.cmd.TestPlotCommand.test_plot_sql_errorprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_error

124 else: 

125 return cursor.fetchall() procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestPlayCommand.test_play_record_plotprocpath.test.cmd.TestPlotCommand.test_plotprocpath.test.cmd.TestPlotCommand.test_plot_custom_query_fileprocpath.test.cmd.TestPlotCommand.test_plot_custom_value_exprprocpath.test.cmd.TestPlotCommand.test_plot_logarithmic_two_axesprocpath.test.cmd.TestPlotCommand.test_plot_moving_average_windowprocpath.test.cmd.TestPlotCommand.test_plot_rdp_epsilonprocpath.test.cmd.TestPlotCommand.test_plot_share_y_axisprocpath.test.cmd.TestPlotCommand.test_plot_title_overrideprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sqlprocpath.test.unit.TestProcret.test_cpuprocpath.test.unit.TestProcret.test_cpu_filter_pidprocpath.test.unit.TestProcret.test_cpu_filter_tsprocpath.test.unit.TestProcret.test_create_queryprocpath.test.unit.TestProcret.test_query_procfile_requiredprocpath.test.unit.TestProcret.test_rssprocpath.test.unit.TestProcret.test_rss_filter_pidprocpath.test.unit.TestProcret.test_rss_filter_pid_short_and_longprocpath.test.unit.TestProcret.test_rss_filter_ts

126 

127 

128register_query(create_query( 

129 "100.0 * tick_diff / (SELECT value FROM meta WHERE key = 'clock_ticks') / ts_diff", 

130 'CPU Usage, %', 

131 cte=( 

132 ''' 

133 WITH diff_all AS ( 

134 SELECT 

135 record_id, 

136 ts, 

137 stat_pid, 

138 stat_utime + stat_stime - LAG(stat_utime + stat_stime) OVER ( 

139 PARTITION BY stat_pid 

140 ORDER BY record_id 

141 ) tick_diff, 

142 ts - LAG(ts) OVER ( 

143 PARTITION BY stat_pid 

144 ORDER BY record_id 

145 ) ts_diff 

146 FROM record 

147 ), diff AS ( 

148 SELECT * FROM diff_all WHERE tick_diff IS NOT NULL 

149 ) 

150 ''' 

151 ), 

152 table='diff', 

153 name='cpu', 

154 min_version=(3, 25), 

155)) 

156 

157register_query(create_query( 

158 "stat_rss / 1024.0 / 1024 * (SELECT value FROM meta WHERE key = 'page_size')", 

159 'Resident Set Size, MiB', 

160 name='rss', 

161)) 

162 

163register_query(create_query( 

164 "smaps_rollup_pss / 1024.0", 

165 'Proportional Set Size, MiB', 

166 name='pss', 

167 procfile_required=['smaps_rollup'], 

168)) 

169register_query(create_query( 

170 "(smaps_rollup_private_clean + smaps_rollup_private_dirty) / 1024.0", 

171 'Unique Set Size, MiB', 

172 name='uss', 

173 procfile_required=['smaps_rollup'], 

174)) 

175register_query(create_query( 

176 "smaps_rollup_swap / 1024.0", 

177 'Swap, MiB', 

178 name='swap', 

179 procfile_required=['smaps_rollup'], 

180)) 

181 

182register_query(create_query( 

183 'fd_anon + fd_dir + fd_chr + fd_blk + fd_reg + fd_fifo + fd_lnk + fd_sock', 

184 'Open File Descriptors', 

185 name='fd', 

186 procfile_required=['fd'], 

187)) 

188 

189register_query(create_query( 

190 'byte_diff / ts_diff', 

191 'Disk Read, B/s', 

192 cte=( 

193 ''' 

194 WITH diff_all AS ( 

195 SELECT 

196 record_id, 

197 ts, 

198 stat_pid, 

199 io_read_bytes - LAG(io_read_bytes) OVER ( 

200 PARTITION BY stat_pid 

201 ORDER BY record_id 

202 ) byte_diff, 

203 ts - LAG(ts) OVER ( 

204 PARTITION BY stat_pid 

205 ORDER BY record_id 

206 ) ts_diff 

207 FROM record 

208 ), diff AS ( 

209 SELECT * FROM diff_all WHERE byte_diff IS NOT NULL 

210 ) 

211 ''' 

212 ), 

213 table='diff', 

214 name='rbs', 

215 min_version=(3, 25), 

216 procfile_required=['io'], 

217)) 

218register_query(create_query( 

219 'byte_diff / ts_diff', 

220 'Disk Write, B/s', 

221 cte=( 

222 ''' 

223 WITH diff_all AS ( 

224 SELECT 

225 record_id, 

226 ts, 

227 stat_pid, 

228 io_write_bytes - LAG(io_write_bytes) OVER ( 

229 PARTITION BY stat_pid 

230 ORDER BY record_id 

231 ) byte_diff, 

232 ts - LAG(ts) OVER ( 

233 PARTITION BY stat_pid 

234 ORDER BY record_id 

235 ) ts_diff 

236 FROM record 

237 ), diff AS ( 

238 SELECT * FROM diff_all WHERE byte_diff IS NOT NULL 

239 ) 

240 ''' 

241 ), 

242 table='diff', 

243 name='wbs', 

244 min_version=(3, 25), 

245 procfile_required=['io'], 

246)) 

247 

248register_query(create_query( 

249 "100.0 * tick_diff / (SELECT value FROM meta WHERE key = 'clock_ticks') / ts_diff", 

250 'I/O wait, %', 

251 cte=( 

252 ''' 

253 WITH diff_all AS ( 

254 SELECT 

255 record_id, 

256 ts, 

257 stat_pid, 

258 stat_delayacct_blkio_ticks - LAG(stat_delayacct_blkio_ticks) OVER ( 

259 PARTITION BY stat_pid 

260 ORDER BY record_id 

261 ) tick_diff, 

262 ts - LAG(ts) OVER ( 

263 PARTITION BY stat_pid 

264 ORDER BY record_id 

265 ) ts_diff 

266 FROM record 

267 ), diff AS ( 

268 SELECT * FROM diff_all WHERE tick_diff IS NOT NULL 

269 ) 

270 ''' 

271 ), 

272 table='diff', 

273 name='wait', 

274 min_version=(3, 25), 

275))