# Add the data from a table in a watcher

**URL:** <https://discuss.elastic.co/t/add-the-data-from-a-table-in-a-watcher/317689>\
**Category:** Kibana\
**Tags:** elastic-stack-alerting\
**Created:** [October 28, 2022, 1:58pm UTC](https://discuss.elastic.co/t/add-the-data-from-a-table-in-a-watcher/317689 "2022-10-28T13:58:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nalin](https://avatars.discourse-cdn.com/v4/letter/n/ecd19e/32.png) [@Nalin](https://discuss.elastic.co/u/Nalin)\
**Post date:** [October 28, 2022, 1:58pm UTC](https://discuss.elastic.co/t/add-the-data-from-a-table-in-a-watcher/317689/1 "2022-10-28T13:58:03Z")

</div>

Hi All,

We are able to generate data in a tabular format through a watcher

| **IndexName** | **StoreSize(in MB)** | **DocumentCount** |
| --- | --- | --- |
| index1 | 23 | 220 |
| index-2 | 20 | 129 |
| index-3 | 26 | 178 |

Is there any way in which we could add the total Store size and total document count separately and print that in the Watcher mail?

Regards,  
Nalin

---

<div class="post-metadata">

**Author:** ![Nalin](https://avatars.discourse-cdn.com/v4/letter/n/ecd19e/32.png) [@Nalin](https://discuss.elastic.co/u/Nalin)\
**Post date:** [October 31, 2022, 7:20am UTC](https://discuss.elastic.co/t/add-the-data-from-a-table-in-a-watcher/317689/2 "2022-10-31T07:20:23Z")

</div>

Hi @richcollier ,

Could you please help on this?

Regards,  
Nalin

---

<div class="post-metadata">

**Author:** ![richcollier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/richcollier/32/115035_2.png) [@richcollier](https://discuss.elastic.co/u/richcollier)\
**Post date:** [November 2, 2022, 5:06pm UTC](https://discuss.elastic.co/t/add-the-data-from-a-table-in-a-watcher/317689/3 "2022-11-02T17:06:31Z")

</div>

Can you provide the content of your current watch?

---

<div class="post-metadata">

**Author:** ![Nalin](https://avatars.discourse-cdn.com/v4/letter/n/ecd19e/32.png) [@Nalin](https://discuss.elastic.co/u/Nalin)\
**Post date:** [November 2, 2022, 5:23pm UTC](https://discuss.elastic.co/t/add-the-data-from-a-table-in-a-watcher/317689/4 "2022-11-02T17:23:46Z")

</div>

Hi Rich,

Please find the current watcher below:

Watcher:  
{  
"trigger": {  
"schedule": {  
"weekly": [  
{  
"on": [  
"FRI"  
],  
"at": [  
"06:10"  
]  
}  
]  
}  
},  
"input": {  
"http": {  
"request": {  
"scheme": "https",  
"host": "#######",  
"port": 9243,  
"method": "get",  
"path": "\_cat/indices/\*",  
"params": {  
"format": "yaml",  
"human": "true",  
"bytes": "mb"  
},  
"headers": {},  
"auth": {  
"basic": {  
"username": "elastic",  
"password": "#######"  
}  
}  
}  
}  
},  
"condition": {  
"script": {  
"source": "return true;",  
"lang": "painless"  
}  
},  
"actions": {  
"displayResults": {  
"logging": {  
"level": "info",  
"text": "the output is: {{ctx.payload.data}}"  
}  
},  
"email\_administrator": {  
"email": {  
"profile": "standard",  
"attachments": {  
"index.yml": {  
"data": {  
"format": "yaml"  
}  
}  
},  
"to": [  
"[abc@xyz.com](mailto:abc@xyz.com)"  
],  
"subject": "Watcher Alert",  
"body": {  
"html": """

Index Data SizePlease find the required data below:IndexNameStoreSize(in MB)DocumentCount{{#ctx.payload.data}}{{index}}  
{{/ctx.payload.data}}{{#ctx.payload.data}}{{store.size}}  
{{/ctx.payload.data}}{{#ctx.payload.data}}{{docs.count}}  
{{/ctx.payload.data}}""" } } } } }

My requirement is to add the total StoreSize(in MB) and print it along with the table, in the watcher mail.

It would be of great assistance if you help me with this.

Thanks & Regards,  
Nalin

---

<div class="post-metadata">

**Author:** ![richcollier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/richcollier/32/115035_2.png) [@richcollier](https://discuss.elastic.co/u/richcollier)\
**Post date:** [November 2, 2022, 6:21pm UTC](https://discuss.elastic.co/t/add-the-data-from-a-table-in-a-watcher/317689/5 "2022-11-02T18:21:28Z")

</div>

You're going to have to build a more complicated data structure and also increment a few variables to keep track of the totals...something like:

```auto
      "displayResults": {
        "transform": {
          "script": """
           def indices = new HashMap();
           def totals= new HashMap(); 

           def total_docs=0L;
           def total_store=0L;
           for (def data : ctx.payload.data) {
               String key = data.index;
               def info = indices.get(key);
               if (info == null) {
                 info = new HashMap();
                 indices.put(key, info);
               }
               info.put("doc_count",data['docs.count']);
               info.put("store_size",data['store.size']);
               total_docs += Integer.parseInt(data['docs.count']);
               total_store += Integer.parseInt(data['store.size']);
            }
            totals.put("total_docs",total_docs);
            totals.put("total_store",total_store);
            indices.put("totals",totals);
            
            return indices;
          """
        },
        "logging": {
          "text": "{{ctx.payload}}"
        }
      }

```

So that your payload looks like this:

```auto
            "payload": {
              "totals": {
                "total_store": 40468078,
                "total_docs": 31808
              },
              "kibana_sample_data_ecommerce": {
                "doc_count": "4675",
                "store_size": "8888714"
              },
              "kibana_sample_data_logs": {
                "doc_count": "14074",
                "store_size": "18893956"
              },
              "kibana_sample_data_flights": {
                "doc_count": "13059",
                "store_size": "12685408"
              }
            }

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 30, 2022, 12:30am UTC](https://discuss.elastic.co/t/add-the-data-from-a-table-in-a-watcher/317689/6 "2022-11-30T00:30:02Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 27, 2022, 9:28pm UTC](https://discuss.elastic.co/t/add-the-data-from-a-table-in-a-watcher/317689/7 "2022-12-27T21:28:44Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 24, 2023, 9:29pm UTC](https://discuss.elastic.co/t/add-the-data-from-a-table-in-a-watcher/317689/8 "2023-01-24T21:29:14Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
