Sign inSign up

kazakos13/common-functions

By kazakos13

•Updated over 3 years ago

node-red flow that implements kmeans clustering, quicksort, weighted average

Image
0

466

kazakos13/common-functions repository overview

⁠Common Functions - 2nd Hackathon 2023

This Docker repository is a collection of functions implementing three algorithms: quicksort, k-means clustering, and weighted average for 2nd Hackathon 2023. These algorithms provide essential functionality for sorting data, performing clustering analysis, and calculating weighted averages.

To run the Docker container associated with this repository, you can use the following command:

docker run -p 8080:8080 -it kazakos13/common-functions

This command will start the container and map port 8080 of the host machine to port 8080 of the container, allowing you to access the functions and their results through the specified port.

By running this Docker container, you gain access to the powerful capabilities of quicksort, k-means clustering, and weighted average calculations, providing efficient solutions for various data processing and analysis tasks.

⁠Example Input for Weighted Average:

curl --location 'http://localhost:8080/run' --header 'Content-Type: application/json' --data '{
  "value": {
  "data":{
    "temperature": [
      { "value": 20, "date": 1664515200000 },
      { "value": 23, "date": 1664774400000 },
      { "value": 18, "date": 1664428800000 },
      { "value": 25, "date": 1664601600000 },
      { "value": 22, "date": 1664688000000 }
    ],
    "humidity": [
      { "value": 20, "date": 1664515200000 },
      { "value": 23, "date": 1664774400000 },
      { "value": 18, "date": 1664428800000 },
      { "value": 25, "date": 1664601600000 },
      { "value": 22, "date": 1664688000000 }
    ]
  },
  "reverse": false,
  "function": "wavg"
}}'

⁠Example Output for Weighted Average:

{"value":{"data":{"temperature":22.4,"humidity":22.4},"reverse":false,"function":"wavg"}}

You can also set the "reverse" to true if you want to give more weight (in the average) to the earlier dates.

⁠Example Input for QuickSort:

curl --location 'http://localhost:8080/run' --header 'Content-Type: application/json' --data '{
  "value": {
  "data":{
    "temperature": [
      { "value": 20, "date": 1664515200000 },
      { "value": 23, "date": 1664774400000 },
      { "value": 18, "date": 1664428800000 },
      { "value": 25, "date": 1664601600000 },
      { "value": 22, "date": 1664688000000 }
    ],
    "humidity": [
      { "value": 20, "date": 1664515200000 },
      { "value": 23, "date": 1664774400000 },
      { "value": 18, "date": 1664428800000 },
      { "value": 25, "date": 1664601600000 },
      { "value": 22, "date": 1664688000000 }
    ]
  },
  "reverse": false,
  "function": "quicksort"
}}'

⁠Example Output for QuickSort:

{"value":{"data":{"temperature":[{"value":18,"date":1664428800000},{"value":20,"date":1664515200000},{"value":25,"date":1664601600000},{"value":22,"date":1664688000000},{"value":23,"date":1664774400000}],"humidity":[{"value":18,"date":1664428800000},{"value":20,"date":1664515200000},{"value":25,"date":1664601600000},{"value":22,"date":1664688000000},{"value":23,"date":1664774400000}]},"reverse":false,"function":"quicksort"}}

You can also set the "reverse" to true if you want the results to appear in reverse order.

⁠Example Input for K-Means Clustering:

curl --location 'http://localhost:8080/run' \
--header 'Content-Type: application/json' \
--data '{"value":{
    "data": [
        {
            "name": "cpu",
            "value": [
                0.015876524497427744,
                0.1902030897354209,
                0.005733681579652651,
                0.030480619519163435,
                0.010371581070262016,
                0.012062255039754171,
                0.015249307746178593,
                7.264773527105429e-7,
                0.0050459685298976376,
                0.004886347069983335

            ]
        },
        {
            "name": "memory",
            "value": [
                27979776,
                38883328,
                23392256,
                22237184,
                26570752,
                24121344,
                22372352,
                22405120,
                23105536,
                22122496
            ]
        }
    ], 
    "mode": "multiple",
    "function":"kmeans"
}}'

⁠Example Output for K-Means Clustering:

{"centroids":[{"cpu":[0.00520766094542967,0.018417176700630984,0.1902030897354209]},{"memory":[22822326.85714286,27275264,38883328]}]}

⁠Openwhisk Run:

If you do not have openwhisk already installed, check the slides of this presentation⁠, and follow the instructions.

First of all you have to be connected into the Harokopio University Server with VPN.

Secondly configure the whisk properly using this commands :

wsk property set --apihost https://10.100.59.208
wsk property set --auth 23bc46b1-71f6-4ed5-8c54-816aa4f8c502:123zO3xZCLrMN6v2BKK1dXYFpXlPkccOFqm12CdAsMgRU4VrNZ9lyGVCGuMDGIwP

Once you have pulled the docker image, do the following to create the openwhisk action:

wsk action create -i <action_name> --docker kazakos13/common-functions

In the following examples, we set <action_name> to “functionstest”.

Here is a test run of the weighted average in openwhisk:

wsk -i action invoke functionstest --param data '{
  "temperature": [
    { "value": 20, "date": 1664515200000 },
    { "value": 23, "date": 1664774400000 },
    { "value": 18, "date": 1664428800000 },
    { "value": 25, "date": 1664601600000 },
    { "value": 22, "date": 1664688000000 }
  ],
  "humidity": [
    { "value": 20, "date": 1664515200000 },
    { "value": 23, "date": 1664774400000 },
    { "value": 18, "date": 1664428800000 },
    { "value": 25, "date": 1664601600000 },
    { "value": 22, "date": 1664688000000 }
  ]
}' --param reverse false --param function wavg

Then you take the id and run the following:

wsk activation get -i <activation_id>

You should take a result like so (it is in “value”[“data”]):

ok: got activation 1715ad361b08410495ad361b080104cf
{
    "namespace": "guest",
    "name": "functionstest",
    "version": "0.0.1",
    "subject": "guest",
    "activationId": "1715ad361b08410495ad361b080104cf",
    "start": 1685626783313,
    "end": 1685626783349,
    "duration": 36,
    "statusCode": 0,
    "response": {
        "status": "success",
        "statusCode": 0,
        "success": true,
        "result": {
            "action_name": "/guest/functionstest",
            "action_version": "0.0.1",
            "activation_id": "1715ad361b08410495ad361b080104cf",
            "deadline": "1685626843312",
            "namespace": "guest",
            "transaction_id": "yMRST8JTpAuVWehNNLe4Y2hP9wtIneHY",
            "value": {
                "data": {
                    "humidity": 22.4,
                    "temperature": 22.4
                },
                "function": "wavg",
                "reverse": false
            }
        }
    },
    "logs": [],
    "annotations": [
        {
            "key": "path",
            "value": "guest/functionstest"
        },
        {
            "key": "waitTime",
            "value": 435
        },
        {
            "key": "kind",
            "value": "blackbox"
        },
        {
            "key": "timeout",
            "value": false
        },
        {
            "key": "limits",
            "value": {
                "concurrency": 1,
                "logs": 10,
                "memory": 256,
                "timeout": 60000
            }
        }
    ],
    "publish": false
}

Here is also a input for running quicksort in openwhisk:

wsk -i action invoke functionstest --param data '{
  "temperature": [
    { "value": 20, "date": 1664515200000 },
    { "value": 23, "date": 1664774400000 },
    { "value": 18, "date": 1664428800000 },
    { "value": 25, "date": 1664601600000 },
    { "value": 22, "date": 1664688000000 }
  ],
  "humidity": [
    { "value": 20, "date": 1664515200000 },
    { "value": 23, "date": 1664774400000 },
    { "value": 18, "date": 1664428800000 },
    { "value": 25, "date": 1664601600000 },
    { "value": 22, "date": 1664688000000 }
  ]
}' --param reverse true --param function quicksort

Similarly, you run the following to get the result:

wsk activation get -i <activation_id>

To change between functions, put the desired function in the “function” param. Your options are: wavg quicksort kmeans

the input should be similar to the one specified in the docker image.

** make sure to have put the credentials key in the .node-red/settings.js before running wsk activation get -i:

module.exports = {
  // ...
  credentialSecret: 'your-credential-secret',
  // ...
};

Tag summary

Content type

Image

Digest

sha256:741f6f6f5…

Size

173.8 MB

Last updated

over 3 years ago

docker pull kazakos13/common-functions