From 627860f0334185c63eba8917eeb8bf14348626ba Mon Sep 17 00:00:00 2001 From: Devin Jean Date: Thu, 8 Aug 2024 12:20:37 -0500 Subject: [PATCH 1/3] saturate rgba inputs --- src/procedures/matlab/matlab.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/procedures/matlab/matlab.js b/src/procedures/matlab/matlab.js index 6590b40c..907a5cac 100644 --- a/src/procedures/matlab/matlab.js +++ b/src/procedures/matlab/matlab.js @@ -40,7 +40,7 @@ async function requestWithRetry(url, body, numRetries = 0) { * * @param {Image} img Image to convert into a matrix * @param {Boolean=} alpha ``true`` to include the alpha/transparency values (default ``false``) - * @returns {Array} The resulting pixel matrix + * @returns {Array>>} The resulting pixel matrix */ MATLAB.imageToMatrix = async function (img, alpha = false) { let matches = img.match( @@ -75,7 +75,7 @@ MATLAB.imageToMatrix = async function (img, alpha = false) { * Converts a HxWx3 matrix of RGB (``[red, green, blue]``) pixel values into an image. * For each pixel, an optional additional alpha/transparency value can be included (default ``255``). * - * @param {Array, 3, 4>>>} matrix The input matrix of pixel data + * @param {Array>>} matrix The input matrix of pixel data * @returns {Image} The constructed image/costume */ MATLAB.imageFromMatrix = async function (matrix) { @@ -89,11 +89,12 @@ MATLAB.imageFromMatrix = async function (matrix) { } logger.log(`reconstructing a ${width}x${height} image`); + const clamp = (x) => x <= 0 ? 0 : x >= 255 ? 255 : Math.round(x); const res = new jimp(width, height); for (y = 0; y < height; ++y) { for (x = 0; x < width; ++x) { const [r, g, b, a = 255] = matrix[y][x]; - res.setPixelColor(jimp.rgbaToInt(r, g, b, a), x, y); + res.setPixelColor(jimp.rgbaToInt(clamp(r), clamp(g), clamp(b), clamp(a)), x, y); } } return utils.sendImageBuffer( From 7e5311ab206cb461041a82816cba08287083b489 Mon Sep 17 00:00:00 2001 From: Devin Jean Date: Thu, 8 Aug 2024 12:32:00 -0500 Subject: [PATCH 2/3] deprecate ThisXDoesNotExist.getHomeInterior --- src/procedures/this-x-does-not-exist/this-x-does-not-exist.js | 1 + test/procedures/this-x-does-not-exist.spec.js | 1 + 2 files changed, 2 insertions(+) diff --git a/src/procedures/this-x-does-not-exist/this-x-does-not-exist.js b/src/procedures/this-x-does-not-exist/this-x-does-not-exist.js index f3106c4a..90139e65 100644 --- a/src/procedures/this-x-does-not-exist/this-x-does-not-exist.js +++ b/src/procedures/this-x-does-not-exist/this-x-does-not-exist.js @@ -116,6 +116,7 @@ TXDNE.getPony = function () { /** * Gets an image of a home interior that does not exist * + * @deprecated * @returns {Image} a random image of the given type */ TXDNE.getHomeInterior = function () { diff --git a/test/procedures/this-x-does-not-exist.spec.js b/test/procedures/this-x-does-not-exist.spec.js index 94fe76b8..1ddc39ef 100644 --- a/test/procedures/this-x-does-not-exist.spec.js +++ b/test/procedures/this-x-does-not-exist.spec.js @@ -19,6 +19,7 @@ describe(utils.suiteName(__filename), function () { "getPerson", "getCat", "getHorse", + "getHomeInterior", ]; utils.verifyRPCInterfaces("ThisXDoesNotExist", rpcs); From cd1db0ab3643f47c8252c262080deb7d845135e3 Mon Sep 17 00:00:00 2001 From: Devin Jean Date: Fri, 9 Aug 2024 13:49:01 -0500 Subject: [PATCH 3/3] reduced logging + show retries (#253) * reduced logging + show retries * Fix code formatting --------- Co-authored-by: Format Bot --- src/procedures/matlab/matlab.js | 37 ++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/procedures/matlab/matlab.js b/src/procedures/matlab/matlab.js index 907a5cac..dd8cfe9e 100644 --- a/src/procedures/matlab/matlab.js +++ b/src/procedures/matlab/matlab.js @@ -23,16 +23,19 @@ const MATLAB = {}; MATLAB.serviceName = "MATLAB"; async function requestWithRetry(url, body, numRetries = 0) { - try { - return await request.post(url, body, { - timeout: 10000, - }); - } catch (err) { - if (err.code === "ECONNABORTED" && numRetries > 0) { - return requestWithRetry(url, body, numRetries - 1); + let err = undefined; + for (let attempts = 1; attempts <= 1 + numRetries; ++attempts) { + try { + const res = await request.post(url, body, { timeout: 10000 }); + return [res, attempts]; + } catch (e) { + err = e; + if (e.code !== "ECONNABORTED") { + break; + } } - throw err; } + throw err || Error("no attempts were made"); } /** @@ -94,7 +97,11 @@ MATLAB.imageFromMatrix = async function (matrix) { for (y = 0; y < height; ++y) { for (x = 0; x < width; ++x) { const [r, g, b, a = 255] = matrix[y][x]; - res.setPixelColor(jimp.rgbaToInt(clamp(r), clamp(g), clamp(b), clamp(a)), x, y); + res.setPixelColor( + jimp.rgbaToInt(clamp(r), clamp(g), clamp(b), clamp(a)), + x, + y, + ); } } return utils.sendImageBuffer( @@ -126,14 +133,16 @@ MATLAB.function = async function (fn, args = [], numReturnValues = 1) { // - start // - batch requests while starting // - send requests on start - // - keepWarm + const startTime = Date.now(); - const resp = await requestWithRetry(`${MATLAB_URL}/feval-fast`, body, 5); + const [resp, attempts] = await requestWithRetry( + `${MATLAB_URL}/feval-fast`, + body, + 5, + ); const duration = Date.now() - startTime; logger.info( - `${duration} body: ${JSON.stringify(body)} response: ${ - JSON.stringify(resp.data) - }`, + `matlab response -- duration: ${duration / 1000}s, attempts: ${attempts}`, ); const results = resp.data.FEvalResponse;