Elasticsearch Painless aggregation script into Kibana Painless Lab

Let's say I have a Painless script written in the console that has aggregations, and I want to write it to Painless Lab to add a new scripted field. what is the best way (FAST, LOWEST COST) to convert the code? like how do I categorize all documents according to 'plateNo.keyword'?

GET vehicles/_search
{
  "size": 0,
  "aggs": {
    "vehicles": {
      "terms": {
        "field": "plateNo.keyword",
        "size": 100
      },
      "aggs": {
        "distance": {
          "scripted_metric": {
            "init_script": "state.dt_point_map=new TreeMap(); state.distance=0; ",
            "map_script": "state.dt_point_map.put(doc.time.value,doc.latitude.value);",
            "combine_script": "int i=0;int prev=0; for(int p : state.dt_point_map.values()){if(i==0){prev=p;i++;}else{state.distance+=Math.abs(p-prev);prev=p;i++;}} return state.distance;",
            "reduce_script": "double overallDistance = 0; for (distance in states) { overallDistance += distance } return overallDistance;"
          }
        }
      }
    }
  }
}