Quicksort Request

This subflow communicates with the URL provided in the UI (can be changed in the FUNCTIONS_URL attribute), which by default is a Docker container running on your PC, and executes the Quicksort algorithm. Users also have the option to get the reverse result by setting msg.payload.value.reverse to true. It uses the Docker image available on Docker Hub: kazakos13/common-functions. If the Docker endpoint is not available, the subflow falls back to running the local build of the Quicksort algorithm.

Input Structure

The subflow takes as input a list with the following structure, which should be placed in the msg.payload.value.data property:

{
  "attr_name1": [
    {"value": x, "date": y},
    {"value": z, "date": e}
  ],
  "attr_name2": [
    {"value": q, "date": w},
    ...
  ]
}

Each attribute (attr_name1, attr_name2, etc.) is associated with a list of dictionaries representing value entries.

Each value entry dictionary must contain two keys:

  • "value": Specifies the value of the attribute for a specific date.
  • "date": Specifies the date of the value entry in UNIX timestamp format.

Output Structure

The subflow returns the same structure as the input list, but with the lists of each attribute key sorted by their date, either in normal order or in reverse order, based on the chosen option (to set it in reverse, set msg.payload.value.reverse to true). The output can be found in the msg.payload.value.data property.

Usage

  1. Prepare the input data structure and place it in the msg.payload.value.data property.
  2. Execute the subflow to perform the Quicksort operation.
  3. The subflow will return the sorted lists based on the chosen option.
  4. Check the output in the msg.payload.value.data property.
[{"id":"6fd479bb119a914b","type":"subflow","name":"QuickSort","info":"<div>\n  <p>\n    This is the implementation of the Quick Sort algorithm, which can also perform a reverse sort if desired.\n    To choose reverse sort, put <code>msg.payload.value.reverse</code> to True, else put it False.\n  </p>\n  <p>\n    It takes as input a list with the following structure (put this in the <code>msg.payload.value.data</code>):\n    <code>\n      {\n        \"attr_name1\": [\n          {\"value\": x, \"date\": y},\n          {\"value\": z, \"date\": e}\n        ],\n        \"attr_name2\": [\n          {\"value\": q, \"date\": w},\n          ...\n        ]\n      }\n    </code>\n  </p>\n  <p>\n    In the input list, each attribute (<em>attr_name1</em>, <em>attr_name2</em>, etc.) is associated with a list of dictionaries.\n    Each dictionary represents a value entry and contains two keys that should remain as is:\n  </p>\n  <ul>\n    <li><strong>\"value\"</strong>: Specifies the value of the attribute for a specific date.</li>\n    <li><strong>\"date\"</strong>: Specifies the date of the value entry. The date should be in UNIX timestamp format.</li>\n  </ul>\n  <p>\n    The subflow returns the same structure as the input list, but with the lists of each attribute key sorted either normally or in reverse order, based on the chosen option. The output can be found in <code>msg.payload.value.data</code>.\n  </p>\n</div>\n","category":"","in":[{"x":140,"y":120,"wires":[{"id":"8a975444807b5c68"}]}],"out":[{"x":580,"y":120,"wires":[{"id":"6fef86d08511923a","port":0}]}],"env":[],"meta":{},"color":"#DDAA99"},{"id":"8a975444807b5c68","type":"function","z":"6fd479bb119a914b","name":"Quick Sort","func":"function quickSort(arr, left, right) {\n    if (left < right) {\n        const pivotIndex = partition(arr, left, right);\n        quickSort(arr, left, pivotIndex - 1);\n        quickSort(arr, pivotIndex + 1, right);\n    }\n    return arr;\n}\n\nfunction partition(arr, left, right) {\n    // const pivot = new Date(arr[right].date);\n    const pivot = arr[right].date;\n    let i = left - 1;\n\n    for (let j = left; j < right; j++) {\n        // const currentDate = new Date(arr[j].date);\n        const currentDate = arr[j].date;\n        if (currentDate < pivot) {\n            i++;\n            swap(arr, i, j);\n        }\n    }\n\n    swap(arr, i + 1, right);\n    return i + 1;\n}\n\nfunction swap(arr, i, j) {\n    const temp = arr[i];\n    arr[i] = arr[j];\n    arr[j] = temp;\n}\n\nvar list = msg.payload.value.data;\nfor (let key in list) {\n    list[key] = quickSort(list[key], 0, list[key].length - 1);\n}\nmsg.payload.value.data = list;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":120,"wires":[["6fef86d08511923a"]]},{"id":"6fef86d08511923a","type":"function","z":"6fd479bb119a914b","name":"Reverse Sort","func":"// Input: list - array of dictionaries [{ temperature: number, date: string }]\nif (!msg.payload.value.reverse) {\n    return msg;\n}\n\nvar list = msg.payload.value.data;\n// console.log(list);\nfor (let key in list) {\n    list[key] = list[key].reverse();\n}\nmsg.payload.value.data = list;\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":120,"wires":[[]]},{"id":"668a9e3b55094c55","type":"subflow","name":"QuickSort Req Docker","info":"<div>\n  <p>\n    This subflow communicates with the url provided in the ui (you can change it in FUNCTIONS_URL attribute), the default is a Docker container running on your PC  and runs the QuickSort Algorithm (user can also get the reverse result - put <code>msg.payload.value.reverse = True</code>). It uses the Docker image available on Docker Hub:\n    <a href=\"https://hub.docker.com/r/kazakos13/common-functions\">kazakos13/common-functions</a>.\n    If the Docker endpoint is not available, it falls back to running the local build of the Quicksort subflow.\n  </p>\n  <h3>Input Structure</h3>\n  <p>\n    The subflow takes as input a list with the following structure, which should be placed in the <code>msg.payload.value.data</code> property:\n  </p>\n  <pre>\n    <code>\n{\n  \"attr_name1\": [\n    {\"value\": x, \"date\": y},\n    {\"value\": z, \"date\": e}\n  ],\n  \"attr_name2\": [\n    {\"value\": q, \"date\": w},\n    ...\n  ]\n}\n    </code>\n  </pre>\n  <p>\n    In the input list, each attribute (<em>attr_name1</em>, <em>attr_name2</em>, etc.) is associated with a list of dictionaries.\n    Each dictionary represents a value entry and contains two keys that should remain as is:\n  </p>\n  <ul>\n    <li><strong>\"value\"</strong>: Specifies the value of the attribute for a specific date.</li>\n    <li><strong>\"date\"</strong>: Specifies the date of the value entry. The date should be in UNIX timestamp format.</li>\n  </ul>\n  <h3>Output Structure</h3>\n  <p>\n    The subflow returns the same structure as the input list, but with the lists of each attribute key sorted either in normal order or in reverse order, based on the chosen option. The output can be found in the <code>msg.payload.value.data</code> property.\n  </p>\n</div>\n","category":"","in":[{"x":20,"y":80,"wires":[{"id":"296d26534dff0291"}]}],"out":[{"x":880,"y":80,"wires":[{"id":"12b6c9f8293ef70e","port":0},{"id":"b67072cd46aba415","port":0}]}],"env":[{"name":"FUNCTIONS_URL","type":"str","value":"http://127.0.0.1:8080/run"}],"meta":{},"color":"#E2D96E"},{"id":"296d26534dff0291","type":"change","z":"668a9e3b55094c55","name":"","rules":[{"t":"set","p":"payload.value.function","pt":"msg","to":"quicksort","tot":"str"},{"t":"set","p":"copy_input","pt":"msg","to":"payload","tot":"msg","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":180,"y":80,"wires":[["3eef448fa0ef8d0e"]]},{"id":"3eef448fa0ef8d0e","type":"http request","z":"668a9e3b55094c55","name":"Quicksort Docker","method":"POST","ret":"obj","paytoqs":"ignore","url":"${FUNCTIONS_URL}","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":410,"y":80,"wires":[["b67072cd46aba415"]]},{"id":"b67072cd46aba415","type":"function","z":"668a9e3b55094c55","name":"Error Handler","func":"if (msg.statusCode != 200) {\n    msg.payload = msg.copy_input;\n    return [null,msg];\n}\nreturn[msg,null];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":160,"wires":[[],["12b6c9f8293ef70e","303eebea317b6d0d"]]},{"id":"303eebea317b6d0d","type":"debug","z":"668a9e3b55094c55","name":"Error Docker Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"\"Could not find url quicksort algorithm. Executing local quicksort subflow.\"","targetType":"jsonata","statusVal":"","statusType":"auto","x":760,"y":220,"wires":[]},{"id":"12b6c9f8293ef70e","type":"subflow:6fd479bb119a914b","z":"668a9e3b55094c55","name":"","x":760,"y":160,"wires":[[]]}]

Flow Info

Created 2 years, 3 months ago
Updated 2 years, 2 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • change (x1)
  • debug (x1)
  • function (x3)
  • http request (x1)
Other
  • subflow (x2)
  • subflow:6fd479bb119a914b (x1)

Tags

  • quicksort
  • request
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option