Aggregation with script code with previous result

Hello everyone

I would like to know if it is possible in 1 single request,

in the aggregations, to make a script which calculates an aggs based on the previous results:

I need to dynamically calculate the interval
the goal being to find an interval with this formula:

My custom code

$coef_test = $price_max / $price_min;

if(  $coef_test == 0 ){ 
    $step = 2;
}elseif($coef_test > 0 && $coef_test <= 15){    
	$step = 10;
}else{
    $step = 20;
}

$interval =  ($price_max - $price_min) / $step;

and here is the ES query

{

    "size": 0,
    "aggs": {
      "price_min": {
            "nested": {
                "path": "onsale_price.EUR"
            },
            "aggs": {
                "facet_name": {
                    "min": {
                        "field": "onsale_price.EUR.value"
                    }
                }
            }
        },
        "price_max": {
            "nested": {
                "path": "onsale_price.EUR"
            },
            "aggs": {
                "facet_name": {
                    "max": {
                        "field": "onsale_price.EUR.value"
                    }
                }
            }
        },
        
        "price_histogram": {
            "nested": {
                "path": "onsale_price.EUR"
            },
            "aggs": {
                "facet_name": {
                    "histogram": {
                        "field": "onsale_price.EUR.value",
                        "interval":  $interval <--------------------------- here
                    }
                }
            }
        }
    }
}

Thank you for your light