@@ -108,38 +108,8 @@ type CIMProcess struct {
108108
109109type CIMProcessList []CIMProcess
110110
111- func GetProcessIds (tokens config.ProcessTokens , excludes config.ProcessTokens ) (pids map [int ]string , err error ) {
112- output , err := executils .CommandCombinedOutput (executils .PSGetProcessIds )
113- if err != nil {
114- return
115- }
116-
117- cimProcessList := CIMProcessList {}
118- err = json .Unmarshal (output , & cimProcessList )
119- if err != nil {
120- return
121- }
122-
123- pids = map [int ]string {}
124-
125- logger .Debug ().Msgf ("m3_windows GetProcessIds tokens: %v" , tokens )
126- logger .Debug ().Msgf ("m3_windows GetProcessIds excludes: %v" , excludes )
127- logger .Debug ().Msgf ("m3_windows GetProcessIds cimProcessList: %v" , cimProcessList )
128-
129- // 1. Preprocess excludes - identify excluded processes
130- excludedProcesses := make (map [int ]bool )
131- // exclude self Pid in case some of the cmdline args matches
132- excludedProcesses [os .Getpid ()] = true
133- for _ , cimProcess := range cimProcessList {
134- for _ , exclude := range excludes {
135- if strings .Contains (cimProcess .CommandLine , string (exclude )) {
136- excludedProcesses [cimProcess .ProcessId ] = true
137- break
138- }
139- }
140- }
141-
142- // 2. Preprocess tokens - parse once, before entering loop for performance consideration
111+ // parseTokens parses process tokens and extracts app names
112+ func parseTokens (tokens config.ProcessTokens ) []ParsedToken {
143113 parsedTokens := make ([]ParsedToken , 0 , len (tokens ))
144114 for _ , t := range tokens {
145115 tokenStr := string (t )
@@ -164,8 +134,51 @@ func GetProcessIds(tokens config.ProcessTokens, excludes config.ProcessTokens) (
164134 appName : appName ,
165135 })
166136 }
137+ return parsedTokens
138+ }
167139
168- // 3. Process matching
140+ // ProcessWithAppName represents a CIM process with its associated app name
141+ type ProcessWithAppName struct {
142+ CIMProcess
143+ AppName string
144+ }
145+
146+ // getFilteredCIMProcesses contains the common logic for filtering processes based on tokens and excludes
147+ // Returns both the filtered processes and their associated app names
148+ func getFilteredCIMProcesses (tokens config.ProcessTokens , excludes config.ProcessTokens ) ([]ProcessWithAppName , error ) {
149+ output , err := executils .CommandCombinedOutput (executils .PSGetProcessIds )
150+ if err != nil {
151+ return nil , err
152+ }
153+
154+ cimProcessList := CIMProcessList {}
155+ err = json .Unmarshal (output , & cimProcessList )
156+ if err != nil {
157+ return nil , err
158+ }
159+
160+ logger .Debug ().Msgf ("m3_windows getFilteredCIMProcesses tokens: %v" , tokens )
161+ logger .Debug ().Msgf ("m3_windows getFilteredCIMProcesses excludes: %v" , excludes )
162+ logger .Debug ().Msgf ("m3_windows getFilteredCIMProcesses cimProcessList: %v" , cimProcessList )
163+
164+ // 1. Preprocess excludes - identify excluded processes
165+ excludedProcesses := make (map [int ]bool )
166+ // exclude self Pid in case some of the cmdline args matches
167+ excludedProcesses [os .Getpid ()] = true
168+ for _ , cimProcess := range cimProcessList {
169+ for _ , exclude := range excludes {
170+ if strings .Contains (cimProcess .CommandLine , string (exclude )) {
171+ excludedProcesses [cimProcess .ProcessId ] = true
172+ break
173+ }
174+ }
175+ }
176+
177+ // 2. Parse tokens once for performance
178+ parsedTokens := parseTokens (tokens )
179+
180+ // 3. Process matching - collect matched processes with their app names
181+ matchedProcesses := make (map [int ]string ) // ProcessId -> AppName
169182 for _ , token := range parsedTokens {
170183 for _ , cimProcess := range cimProcessList {
171184 // Skip excluded processes
@@ -184,13 +197,57 @@ func GetProcessIds(tokens config.ProcessTokens, excludes config.ProcessTokens) (
184197 }
185198
186199 if matched {
187- if _ , exists := pids [cimProcess .ProcessId ]; ! exists {
188- pids [cimProcess .ProcessId ] = token .appName
200+ if _ , exists := matchedProcesses [cimProcess .ProcessId ]; ! exists {
201+ matchedProcesses [cimProcess .ProcessId ] = token .appName
189202 }
190203 }
191204 }
192205 }
193206
207+ // 4. Build result list with app names
208+ var result []ProcessWithAppName
209+ for _ , cimProcess := range cimProcessList {
210+ if appName , matched := matchedProcesses [cimProcess .ProcessId ]; matched {
211+ result = append (result , ProcessWithAppName {
212+ CIMProcess : cimProcess ,
213+ AppName : appName ,
214+ })
215+ }
216+ }
217+
218+ logger .Debug ().Msgf ("m3_windows getFilteredCIMProcesses result: %v" , result )
219+ return result , nil
220+ }
221+
222+ // GetCIMProcesses returns filtered CIM processes based on tokens and excludes
223+ func GetCIMProcesses (tokens config.ProcessTokens , excludes config.ProcessTokens ) ([]CIMProcess , error ) {
224+ processesWithAppNames , err := getFilteredCIMProcesses (tokens , excludes )
225+ if err != nil {
226+ return nil , err
227+ }
228+
229+ // Extract just the CIMProcess part
230+ result := make ([]CIMProcess , len (processesWithAppNames ))
231+ for i , processWithAppName := range processesWithAppNames {
232+ result [i ] = processWithAppName .CIMProcess
233+ }
234+
235+ return result , nil
236+ }
237+
238+ func GetProcessIds (tokens config.ProcessTokens , excludes config.ProcessTokens ) (pids map [int ]string , err error ) {
239+ processesWithAppNames , err := getFilteredCIMProcesses (tokens , excludes )
240+ if err != nil {
241+ return nil , err
242+ }
243+
244+ pids = make (map [int ]string )
245+
246+ // Directly use the app names from the filtered results - no need to re-match!
247+ for _ , processWithAppName := range processesWithAppNames {
248+ pids [processWithAppName .ProcessId ] = processWithAppName .AppName
249+ }
250+
194251 logger .Debug ().Msgf ("m3_windows GetProcessIds pids: %v" , pids )
195- return
252+ return pids , nil
196253}
0 commit comments