Twilio Functions allow you to run JavaScript in response to an HTTP request (or request from Studio). You can read about how Functions work in the documentation. To fetch the calls and filter them by their current status, you would use the Call Resource. There are examples in the documentation for fetching calls and filtering and all the documentation has examples in Node.js, as well as PHP so you can compare.
Here's a Function I put together quickly (I haven't tested it, but I think it looks good). It fetches the calls that are ringing and in-progress, and returns an object with individual totals and a total. You can then use this in your widget and refer to the values with variables like {{widgets.FUNCTION_WIDGET_NAME.parsed.ringing}}
.
exports.handler = async (context, event, callback) => {
const client = context.getTwilioClient();
const ringingPromise = client.calls.list({ status: 'ringing' });
const inProgressPromise = client.calls.list({ status: 'in-progress' });
const [ringing, inProgress] = await Promise.all([ringingPromise, inProgressPromise]);
const response = {
ringing: ringing.length,
inProgress: inProgress.length,
total: ringing.length + inProgress.length
};
callback(null, response);
}