Math calculation in kibana to visualize

I need to calculate below math calculations and visualize it in dashboard. When i push new data it should calculate the same and change. Can anyone please help on this.

  1. precision = tp/ (tp + fp)
  2. recall = tp/tp + fn
  3. False alarm rate = fp/ (tp + fp)

Methods i have tried :

  1. Using KQL filters i was not able to do any math operations.
  2. I have tried Painless scripting but have so many exceptions and not able to resolve them. Here are few.
{"log":"607045d54125a1004057194c","algorithm_name":"Seatbelt","algorithm_version":0.1,"annotation_stream":"60811f2cec0687004291502c","timestamp":"2021-04-08 11:40:25","LabelID_d":0,"Label_d":"Seat belt on","LabelID_a":1,"Label_a":"Seat belt on","Seatclass":"Driver","confidence_d":100,"status":"TP","UUID_a":"3352970a-e3e4-4045-8d3b-5ecd2aad4f97","id":1}
{"log":"607045d54125a1004057194a","algorithm_name":"Seatbelt","algorithm_version":0.2,"annotation_stream":"60811f2cec0687004291502c","timestamp":"2021-04-09 11:40:25","LabelID_d":0,"Label_d":"Seat belt off","LabelID_a":1,"Label_a":"Seat belt off","Seatclass":"Driver","confidence_d":1,"status":"TN","UUID_a":"3352970a-e3e3-4045-8d3b-5ecd2aad4f97","id":2}

In kibana painless Lab:

int precision_cal() {
    int tpcount = 0;
    int fpcount = 0;
    if ((doc["status"].value) == "TP") {
        tpcount+=tpcount;
    } 
    if ((doc["status"].value) == "FP") {
        fpcount+=fpcount;
    }
    precision=tpcount/(tpcount + fpcount);
    return precision;
}

Error 1:

{
  "root_cause": [
    {
      "type": "x_content_parse_exception",
      "reason": "[14:2] Unexpected character ('{' (code 123)): was expecting comma to separate Object entries\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 14, column: 2]"
    }
  ],
  "type": "x_content_parse_exception",
  "reason": "[13:370] [painless_execute_request] failed to parse field [context_setup]",
  "caused_by": {
    "type": "x_content_parse_exception",
    "reason": "[14:2] Unexpected character ('{' (code 123)): was expecting comma to separate Object entries\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 14, column: 2]",
    "caused_by": {
      "type": "json_parse_exception",
      "reason": "Unexpected character ('{' (code 123)): was expecting comma to separate Object entries\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 14, column: 2]"
    }
  }
}

Error 2:

Unhandled Exception illegal_argument_exception

cannot resolve symbol [doc]

Stack:
[
  "... t fpcount = 0;\r\n    if ((doc[\"status\"].value) == \" ...",
  "                             ^---- HERE"
]

i'm stuck on the logic for this math calculation from a week now. It would be of great help if anyone can help on this. Also let me know if any other info is required for debugging.

Thanks.

Hi @Nanditha

welcome to the Kibana community.

From my understanding your math formula should be something like:

count(kql='status:"TP"') / (count(kql='status:"TP"') + count(kql='status:"FP"'))

Does it look right? If so, you can try Lens formula and use the one above here.

Hi @Marco_Liberati ,

Thank you so much for your response.
yes, i want to calculate same as what you have mentioned. Can i make use of Metric as i want a number (Int or float, double) for my visualization.

Thanks.

Hi @Marco_Liberati ,

I used it in Metric aggregation and it works perfectly fine.
Thank you so much for solving the doubt.

Thanks.