<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to derive percentage of expected and actual count from two searches with different count strategies in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-derive-percentage-of-expected-and-actual-count-from-two/m-p/678535#M232028</link>
    <description>&lt;P&gt;Hi,&lt;BR /&gt;I have two separate searches that are working independently (expected count, actual count).&amp;nbsp; I want to combine the searches to get a percentage for actual count to expected count; however append, appendcols, and other ways to add the searches together have so far not worked for me.&amp;nbsp; Curious if there's a better way to use stats, eval, transaction commands to achieve the combination of these searches. The end goal is to provide a visualization to understand if there's an issue when the actual count does not match the expected count - so open to suggestions on better ways to achieve that goal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Search 1 (counting all records that are sent through producer class not part of refresh process):&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;index=index | search ("ProducerClass" AND "*Sending message:*") NOT "*REFRESH*" | stats count as actual_count&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Search 2 (sum of record counts on files processed through opportunity class):&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;index=index | search "OpportunityClass" AND "Processing file: file_name" | rex field=_raw "Processing file: file_name with (?&amp;lt;record_count&amp;gt;[^\s]+) records" | stats sum(record_count) as expected_count
&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;I have tried append like this and it has not worked:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;index=index | search ("ProducerClass" AND "*Sending message:*" ) NOT "*REFRESH*"
| stats count as actual_count
| append [
search index=index "OpportunityClass" AND "Processing file: "
| rex field=_raw "Processing file: file_name with (?&amp;lt;record_count&amp;gt;[^\s]+) records"
| stats sum(record_count) as expected_count]
| eval percent =expected_count/actual_count * 100&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;appendcols similarly did not work ("Aborting Long Running Search").&amp;nbsp; Assuming I am incorrectly understanding how I am combining these searches and it is causing issues when using append type commands.&amp;nbsp; Using an OR on the searches works, but unsure how to use other commands to group the results properly after:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;index=index | search (("ProducerClass" AND "*Sending message:*" ) NOT "*REFRESH*") OR ("OpportunityClass" AND "Processing file: ")
| ...&lt;/LI-CODE&gt;</description>
    <pubDate>Fri, 23 Feb 2024 21:06:09 GMT</pubDate>
    <dc:creator>alexa</dc:creator>
    <dc:date>2024-02-23T21:06:09Z</dc:date>
    <item>
      <title>How to derive percentage of expected and actual count from two searches with different count strategies</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-derive-percentage-of-expected-and-actual-count-from-two/m-p/678535#M232028</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;I have two separate searches that are working independently (expected count, actual count).&amp;nbsp; I want to combine the searches to get a percentage for actual count to expected count; however append, appendcols, and other ways to add the searches together have so far not worked for me.&amp;nbsp; Curious if there's a better way to use stats, eval, transaction commands to achieve the combination of these searches. The end goal is to provide a visualization to understand if there's an issue when the actual count does not match the expected count - so open to suggestions on better ways to achieve that goal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Search 1 (counting all records that are sent through producer class not part of refresh process):&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;index=index | search ("ProducerClass" AND "*Sending message:*") NOT "*REFRESH*" | stats count as actual_count&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Search 2 (sum of record counts on files processed through opportunity class):&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;index=index | search "OpportunityClass" AND "Processing file: file_name" | rex field=_raw "Processing file: file_name with (?&amp;lt;record_count&amp;gt;[^\s]+) records" | stats sum(record_count) as expected_count
&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;I have tried append like this and it has not worked:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;index=index | search ("ProducerClass" AND "*Sending message:*" ) NOT "*REFRESH*"
| stats count as actual_count
| append [
search index=index "OpportunityClass" AND "Processing file: "
| rex field=_raw "Processing file: file_name with (?&amp;lt;record_count&amp;gt;[^\s]+) records"
| stats sum(record_count) as expected_count]
| eval percent =expected_count/actual_count * 100&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;appendcols similarly did not work ("Aborting Long Running Search").&amp;nbsp; Assuming I am incorrectly understanding how I am combining these searches and it is causing issues when using append type commands.&amp;nbsp; Using an OR on the searches works, but unsure how to use other commands to group the results properly after:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;index=index | search (("ProducerClass" AND "*Sending message:*" ) NOT "*REFRESH*") OR ("OpportunityClass" AND "Processing file: ")
| ...&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 23 Feb 2024 21:06:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-derive-percentage-of-expected-and-actual-count-from-two/m-p/678535#M232028</guid>
      <dc:creator>alexa</dc:creator>
      <dc:date>2024-02-23T21:06:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to derive percentage of expected and actual count from two searches with different count strategies</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-derive-percentage-of-expected-and-actual-count-from-two/m-p/678543#M232031</link>
      <description>&lt;P&gt;There's a few different techniques for combining things like this.&amp;nbsp; The one I think you might find most useful could be...&lt;/P&gt;&lt;P&gt;OK, example off some silly data I have.&amp;nbsp; Once I work through that and explain, I'll make an attempt at doing your searches too.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway - Amazon Glacier uploads for my little server, every night it tries to push up new files.&amp;nbsp; I think it's similar enough to your data that the example may work, though forgive me for it being stupidly contrived in so many ways.&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The idea is there are two messages.&amp;nbsp; One is contains "uploaded part" and the other contains "created an upload_id".&amp;nbsp; I don't have a real good "number" to rex out, but I have a PID I can steal the first two digits of to pretend I have numbers.&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index="glacier" ( "uploaded part" OR "created an upload_id")
| eval is_actual = if(searchmatch("*created an upload_id*"), 1, 0)
| rex "PID\s+(?&amp;lt;dumb_counter&amp;gt;\d\d)"
| eval is_expected = if(searchmatch("*uploaded part*"), dumb_counter, 0)
| stats sum(dumb_counter) as is_expected, sum(is_actual) as is_actual&lt;/LI-CODE&gt;&lt;P&gt;So looking at that, the first line gets all the data, both types.&lt;/P&gt;&lt;P&gt;The second line is using an eval to create "is_actual". And when the event matches 'created an upload_id', that is_actual will be set to 1.&amp;nbsp; Otherwise 0.&lt;/P&gt;&lt;P&gt;The third line is a rex, just like yours only more dumb.&amp;nbsp; It creates a field "dumb_counter" which will either be a two digit number, or will be null if it didn't match.&amp;nbsp; (Unfortunately, ALL my lines have a PID, so ... this is broken, but I fix it in the next line using logic just like in line 2.&lt;/P&gt;&lt;P&gt;Line 4 then is the fix, where I eval 'is_expected' to either be the dumb_counter I wrote IF the line matches what I need it to match, or 0 if it doesn't.&amp;nbsp; (I don't think you'll need this extra logic, but I do and it was easy enough to explain!)&lt;/P&gt;&lt;P&gt;The the last line just adds up the two independently.&amp;nbsp; Afterwords you can easily do a new eval for percent or whatever.&amp;nbsp; We'll do this when we try YOUR search.&lt;/P&gt;&lt;P&gt;And it's time for that now. We'll use the same technique, only it'll be messier because you have more conditions to work with.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=index ( ("ProducerClass" AND "*Sending message:*") NOT "*REFRESH*") OR ("OpportunityClass" AND "Processing file: file_name")
| eval is_actual = if(searchmatch("*ProducerClass*") AND searchmatch("*Sending message:*"), 1, 0)
| rex field=_raw "Processing file: file_name with (?&amp;lt;record_count&amp;gt;[^\s]+) records"
| eval is_expected = if(searchmatch("*OpportunityClass*") AND searchmatch("*Processing file: *"), record_count, 0)
| stats sum(is_expected) as is_expected, sum(is_actual) as is_actual
| eval percent = (is_expected / is_actual) *100&lt;/LI-CODE&gt;&lt;P&gt;Again, line 1 pulls all the data in.&amp;nbsp; (Special note, you use NOT ... which means those records won't be there and we can ignore them in the eval, you'll see!&lt;/P&gt;&lt;P&gt;Line 2 creates our is_actual.&amp;nbsp; This line could be left here or moved to after the rex - it won't really matter.&lt;/P&gt;&lt;P&gt;Line 3 is our rec to get our record count...&lt;/P&gt;&lt;P&gt;Which in line 4 we convert into a new field 'is_expected' ONLY if the event is the right event - this is very, very likely to not be needed, you could extract the field in line 3 with the name 'is_expected', remove this line, and it probably should all work the same.&amp;nbsp; But we're being careful here.&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The we just sum those in line 5, and do some math in line 6.&lt;/P&gt;&lt;P&gt;So of special note!&lt;/P&gt;&lt;P&gt;If "file_name" actually stands in for the filename which changes, we'll have to work around that with a wildcard or something.&amp;nbsp; OR if you can drop in a line from each event type (appropriately obfuscated, of course) then we can just work it using one of the other methods.&lt;/P&gt;&lt;P&gt;For instance, we may be able to ignore "filename" in the base search, then just edit the rex a wee bit to work around it later, too.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyhow, give those a shot, and if it works for you (or is easily "fixed" because I'm sure there's some typos in it), then great!&amp;nbsp; Otherwise, let us know what's happening and we can help more.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Happy Splunking,&lt;/P&gt;&lt;P&gt;Rich&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2024 21:37:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-derive-percentage-of-expected-and-actual-count-from-two/m-p/678543#M232031</guid>
      <dc:creator>Richfez</dc:creator>
      <dc:date>2024-02-23T21:37:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to derive percentage of expected and actual count from two searches with different count strategies</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-derive-percentage-of-expected-and-actual-count-from-two/m-p/678550#M232034</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;I have tried append like this and it has not worked:&lt;/BLOCKQUOTE&gt;&lt;P&gt;When you have a requirement like this, the method&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/205010"&gt;@Richfez&lt;/a&gt;&amp;nbsp;presented should be your first choice and not thinking in terms of append or appendcols. &amp;nbsp;Meanwhile, there shouldn't be any reason why append would not work if you combine the two rows with another stats&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=index ("ProducerClass" AND "*Sending message:*" ) NOT "*REFRESH*"
``` ^^^ do not separate filter from index search ```
| stats count as actual_count
| append
  [search index=index "OpportunityClass" AND "Processing file: "
  | rex field=_raw "Processing file: file_name with (?&amp;lt;record_count&amp;gt;[^\s]+) records"
  | stats sum(record_count) as expected_count]
| stats values(*) as *
| eval percent =expected_count/actual_count * 100&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note in the first line, do not use a pipe sign to add a search filter if the filter applies directly to index search. &amp;nbsp;Adding all applicable filters in index search will greatly improve performance.&lt;/P&gt;&lt;P&gt;Similarly, there is no reason why appendcols will not work to your expectation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=index ("ProducerClass" AND "*Sending message:*" ) NOT "*REFRESH*"
``` ^^^ do not separate filter from index search ```
| stats count as actual_count
| appendcols
  [search index=index "OpportunityClass" AND "Processing file: "
  | rex field=_raw "Processing file: file_name with (?&amp;lt;record_count&amp;gt;[^\s]+) records"
  | stats sum(record_count) as expected_count]
| eval percent =expected_count/actual_count * 100&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(If you think appendcol didn't work, you should post your search, and give sample (anonymized) results, then explain why the results are wrong.)&lt;/P&gt;&lt;P&gt;Here are three simulations to illustrate how they give the same results&lt;/P&gt;&lt;P&gt;1. append&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=_internal
| stats count as actual_count
| append
    [search index=_audit
    | stats count as expected_count]
| stats values(*) as *
| eval percent = round(expected_count/actual_count * 100, 2)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;actual_count&lt;/TD&gt;&lt;TD&gt;expected_count&lt;/TD&gt;&lt;TD&gt;percent&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;237531&lt;/TD&gt;&lt;TD&gt;4686&lt;/TD&gt;&lt;TD&gt;1.97&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;2. appendcols&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=_internal
| stats count as actual_count
| appendcols
    [search index=_audit
    | stats count as expected_count]
| eval percent = round(expected_count/actual_count * 100, 2)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;actual_count&lt;/TD&gt;&lt;TD&gt;expected_count&lt;/TD&gt;&lt;TD&gt;percent&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;236458&lt;/TD&gt;&lt;TD&gt;4660&lt;/TD&gt;&lt;TD&gt;1.97&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;3. Rich's method&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index = _internal OR index = _audit
| eval is_actual = if(searchmatch("index = _internal"), "true", null())
| eval is_expected = if(searchmatch("index = _audit"), "true", null())
| stats count(is_actual) as actual_count count(is_expected) as expected_count
| eval percent = round(expected_count/actual_count * 100, 2)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;actual_count&lt;/TD&gt;&lt;TD&gt;expected_count&lt;/TD&gt;&lt;TD&gt;percent&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;245978&lt;/TD&gt;&lt;TD&gt;4861&lt;/TD&gt;&lt;TD&gt;1.98&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;(Any difference in illustrated numbers is due to data change between searches.)&lt;/P&gt;</description>
      <pubDate>Sat, 24 Feb 2024 00:13:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-derive-percentage-of-expected-and-actual-count-from-two/m-p/678550#M232034</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2024-02-24T00:13:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to derive percentage of expected and actual count from two searches with different count strategies</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-derive-percentage-of-expected-and-actual-count-from-two/m-p/678719#M232062</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/205010"&gt;@Richfez&lt;/a&gt;&amp;nbsp;- once I substituted my values into the search it works great!&amp;nbsp; And the explanation/example makes it very clear what is happening in the search.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 20:38:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-derive-percentage-of-expected-and-actual-count-from-two/m-p/678719#M232062</guid>
      <dc:creator>alexa</dc:creator>
      <dc:date>2024-02-26T20:38:55Z</dc:date>
    </item>
  </channel>
</rss>

