Debian Codegraph @

src/widgets/process_table/query/attribute.rs

raw symbols SCIP
1 //! Code related to attributes, which should be "searchable" leaf nodes.
2
3 use regex::Regex;
4
5 use crate::{
6 collection::processes::ProcessHarvest,
7 widgets::query::{
8 NumericalQuery, PrefixType, QueryOptions, TimeQuery,
9 error::{QueryError, QueryResult},
10 new_regex,
11 },
12 };
13
14 /// An attribute (leaf node) for a process.
15 #[derive(Debug)]
16 pub(super) enum ProcessAttribute {
17 /// This is a bit of a hack to allow for "empty" attributes. We can fix it
18 /// properly, but it would potentially require handling "empty" queries
19 /// better. Currently, we just treat it as a leaf node that always
20 /// succeeds on matches.
21 Empty,
22 Pid(Regex),
23 CpuPercentage(NumericalQuery),
24 MemBytes(NumericalQuery),
25 MemPercentage(NumericalQuery),
26 ReadPerSecond(NumericalQuery),
27 WritePerSecond(NumericalQuery),
28 TotalRead(NumericalQuery),
29 TotalWrite(NumericalQuery),
30 /// Note this is an "untagged" attribute (e.g. "btm", "firefox").
31 Name(Regex),
32 State(Regex),
33 User(Regex),
34 Time(TimeQuery),
35 #[cfg(unix)]
36 Nice(NumericalQuery),
37 Priority(NumericalQuery),
38 #[cfg(feature = "gpu")]
39 GpuPercentage(NumericalQuery),
40 #[cfg(feature = "gpu")]
41 GpuMemoryPercentage(NumericalQuery),
42 #[cfg(feature = "gpu")]
43 GpuMemoryBytes(NumericalQuery),
44 }
45
46 impl ProcessAttribute {
47 pub(super) fn check(&self, process: &ProcessHarvest, is_using_command: bool) -> bool {
48 match self {
49 ProcessAttribute::Empty => true,
50 ProcessAttribute::Pid(re) => re.is_match(process.pid.to_string().as_str()),
51 ProcessAttribute::CpuPercentage(cmp) => cmp.check(process.cpu_usage_percent),
52 ProcessAttribute::MemBytes(cmp) => cmp.check(process.mem_usage as f64),
53 ProcessAttribute::MemPercentage(cmp) => cmp.check(process.mem_usage_percent),
54 ProcessAttribute::ReadPerSecond(cmp) => cmp.check(process.read_per_sec as f64),
55 ProcessAttribute::WritePerSecond(cmp) => cmp.check(process.write_per_sec as f64),
56 ProcessAttribute::TotalRead(cmp) => cmp.check(process.total_read as f64),
57 ProcessAttribute::TotalWrite(cmp) => cmp.check(process.total_write as f64),
58 ProcessAttribute::Name(re) => re.is_match(if is_using_command {
59 process.command.as_str()
60 } else {
61 process.name.as_str()
62 }),
63 ProcessAttribute::State(re) => re.is_match(process.process_state.0),
64 ProcessAttribute::User(re) => match process.user.as_ref() {
65 Some(user) => re.is_match(user),
66 None => re.is_match("N/A"),
67 },
68 ProcessAttribute::Time(time) => time.check(process.time),
69 // TODO: It's a bit silly for some of these, like nice/priority, where it's casted to an
70 // f64.
71 #[cfg(unix)]
72 ProcessAttribute::Nice(cmp) => cmp.check(process.nice as f64),
73 ProcessAttribute::Priority(cmp) => cmp.check(process.priority as f64),
74 #[cfg(feature = "gpu")]
75 ProcessAttribute::GpuPercentage(cmp) => cmp.check(process.gpu_util as f64),
76 #[cfg(feature = "gpu")]
77 ProcessAttribute::GpuMemoryPercentage(cmp) => cmp.check(process.gpu_mem_percent as f64),
78 #[cfg(feature = "gpu")]
79 ProcessAttribute::GpuMemoryBytes(cmp) => cmp.check(process.gpu_mem as f64),
80 }
81 }
82 }
83
84 /// Given a string prefix type, obtain the appropriate [`ProcessAttribute`].
85 pub(super) fn new_string_attribute(
86 prefix_type: PrefixType, base: &str, regex_options: &QueryOptions,
87 ) -> QueryResult<ProcessAttribute> {
88 match prefix_type {
89 PrefixType::Pid | PrefixType::Name | PrefixType::State | PrefixType::User => {
90 let re = new_regex(base, regex_options)?;
91
92 match prefix_type {
93 PrefixType::Pid => Ok(ProcessAttribute::Pid(re)),
94 PrefixType::Name => Ok(ProcessAttribute::Name(re)),
95 PrefixType::State => Ok(ProcessAttribute::State(re)),
96 PrefixType::User => Ok(ProcessAttribute::User(re)),
97 _ => unreachable!(),
98 }
99 }
100 _ => Err(QueryError::new(format!(
101 "process attribute type {prefix_type:?} is not a supported string attribute"
102 ))),
103 }
104 }
105
106 /// Given a time prefix type, obtain the appropriate [`ProcessAttribute`].
107 pub(super) fn new_time_attribute(
108 prefix_type: PrefixType, query: TimeQuery,
109 ) -> QueryResult<ProcessAttribute> {
110 match prefix_type {
111 PrefixType::Time => Ok(ProcessAttribute::Time(query)),
112 _ => Err(QueryError::new(format!(
113 "process attribute type {prefix_type:?} is not a supported time attribute"
114 ))),
115 }
116 }
117
118 /// Given a numerical prefix type, obtain the appropriate [`ProcessAttribute`].
119 pub(super) fn new_numerical_attribute(
120 prefix_type: PrefixType, query: NumericalQuery,
121 ) -> QueryResult<ProcessAttribute> {
122 match prefix_type {
123 PrefixType::CpuPercentage => Ok(ProcessAttribute::CpuPercentage(query)),
124 PrefixType::MemBytes => Ok(ProcessAttribute::MemBytes(query)),
125 PrefixType::MemPercentage => Ok(ProcessAttribute::MemPercentage(query)),
126 PrefixType::ReadPerSecond => Ok(ProcessAttribute::ReadPerSecond(query)),
127 PrefixType::WritePerSecond => Ok(ProcessAttribute::WritePerSecond(query)),
128 PrefixType::TotalRead => Ok(ProcessAttribute::TotalRead(query)),
129 PrefixType::TotalWrite => Ok(ProcessAttribute::TotalWrite(query)),
130 #[cfg(unix)]
131 PrefixType::Nice => Ok(ProcessAttribute::Nice(query)),
132 PrefixType::Priority => Ok(ProcessAttribute::Priority(query)),
133 #[cfg(feature = "gpu")]
134 PrefixType::GpuPercentage => Ok(ProcessAttribute::GpuPercentage(query)),
135 #[cfg(feature = "gpu")]
136 PrefixType::GpuMemoryBytes => Ok(ProcessAttribute::GpuMemoryBytes(query)),
137 #[cfg(feature = "gpu")]
138 PrefixType::GpuMemoryPercentage => Ok(ProcessAttribute::GpuMemoryPercentage(query)),
139 _ => Err(QueryError::new(format!(
140 "process attribute type {prefix_type:?} is not a supported numerical attribute"
141 ))),
142 }
143 }