From 88bb73a3f89b58fe338b8f3c82536e19ff0fa630 Mon Sep 17 00:00:00 2001 From: xd Date: Wed, 3 May 2023 17:25:12 +0100 Subject: [PATCH] sort implementation --- src/json/retrieve.cpp | 56 ++++++++++++++++++++++++++++++++++++++++--- src/menu.cpp | 7 +++++- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/json/retrieve.cpp b/src/json/retrieve.cpp index ac96b86..a36a545 100644 --- a/src/json/retrieve.cpp +++ b/src/json/retrieve.cpp @@ -5,7 +5,33 @@ #include #include -/// @brief This is a callback function to process the json data of a node provided by the API into an array of Node structs. +/// @brief Function to compare nodes by the uptime. +/// @param a The first node to compare +/// @param b The node to compare to +/// @return True if the uptime of node b is higher, false if uptime of a is higher. +bool compareNode(const Node &a, const Node &b) { + return a.uptime < b.uptime; +} + +/// @brief Function to compare containers by ID. +/// @param a The first container to compare +/// @param b The container to compare to +/// @return True if the ID of container b is higher, false if uptime of a is higher. +bool compareContainer(const Container &a, const Container &b) { + return a.id < b.id; +} + + +/// @brief Function to compare VMs by ID. +/// @param a The first VM to compare +/// @param b The VM to compare to +/// @return True if the ID of VM b is higher, false if ID of a is higher. +bool compareVM(const VM &a, const VM &b) { + return a.id < b.id; +} + + +/// @brief This is a callback function to process the json data of a node provided by the API into an array of Node structs. Also sorts the nodes based on uptime. /// @param doc A reference to the json document containing the data from the API. /// @param numNodes A reference to an integer containing the number of nodes retrieved from the API. /// @param nodeArray A pointer to the array used to hold the nodes once they have been processed. @@ -27,11 +53,14 @@ void processNodeData(DynamicJsonDocument &doc, int &numNodes, void *&nodeArray) array[i].uptime = nodes[i]["uptime"].as(); } + // Sort the array + std::sort(array, array + nodes.size(), compareNode); + numNodes = nodes.size(); nodeArray = array; } -/// @brief This is a callback function to process the json data of a container provided by the api into an array of Container structs. +/// @brief This is a callback function to process the json data of a container provided by the api into an array of Container structs. Also sorts the containers based on id. /// @param doc A reference to the json document containing the data from the API. /// @param numContainers A reference to an integer containing the number of containers retrieved from the API. /// @param containerArray A pointer to the array used to hold the containers once they have been processed. @@ -50,11 +79,29 @@ void processContainerData(DynamicJsonDocument &doc, int &numContainers, void *&c array[i].uptime = containers[i]["uptime"].as(); } + // Record the starting time + unsigned long startTime = micros(); + + // Run the function you want to measure + std::sort(array, array + containers.size(), compareContainer); + + // Calculate the elapsed time + unsigned long elapsedTime = micros() - startTime; + + // Print the elapsed time in microseconds + Serial.print("Elapsed time: "); + Serial.print(elapsedTime); + Serial.println(" microseconds"); + + + // Sort the array + //std::sort(array, array + containers.size(), compareContainer); + numContainers = containers.size(); containerArray = array; } -/// @brief This is a callback function to process the json data of a VM provided by the api into an array of VM structs. +/// @brief This is a callback function to process the json data of a VM provided by the api into an array of VM structs. Also sorts the VMs based on id. /// @param doc A reference to the json document containing the data from the API. /// @param numVMs A reference to an integer containing the number of VMs retrieved from the API. /// @param vmArray A pointer to the array used to hold the VMs once they have been processed. @@ -75,6 +122,9 @@ void processVMData(DynamicJsonDocument &doc, int &numVMs, void *&vmArray) array[i].netout = vms[i]["netout"].as(); } + // Sort the array + std::sort(array, array + vms.size(), compareVM); + numVMs = vms.size(); vmArray = array; } diff --git a/src/menu.cpp b/src/menu.cpp index 9b6cdc3..9fd50f9 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -65,7 +65,7 @@ int buttonListener(const int &numItems) Serial.println(selectedItem); break; } - if (GO.JOY_X.isAxisPressed() == 1 && selectedPage + 1 < (numItems / ITEMS_PER_PAGE)) + if (GO.JOY_X.isAxisPressed() == 1 && selectedPage + 1 < (float(numItems) / ITEMS_PER_PAGE)) { selectedPage++; selectedItem = selectedPage * ITEMS_PER_PAGE; @@ -342,6 +342,7 @@ void listItems( void listNodes(Node *nodes, const int &numItems) { selectedItem = 0; + selectedPage = 0; void *item; listItems(nodes, &item, "Select Node", numItems, sizeof(Node), false, nodeMenuPrint); @@ -368,6 +369,7 @@ void listNodes(Node *nodes, const int &numItems) int listContainers(Container *containers, const int &numItems) { selectedItem = 0; + selectedPage = 0; void *item; listItems(containers, &item, "Select Container", numItems, sizeof(Container), true, containerMenuPrint); @@ -395,6 +397,7 @@ int listContainers(Container *containers, const int &numItems) int listVMs(VM *vms, const int &numItems) { selectedItem = 0; + selectedPage = 0; void *item; listItems(vms, &item, "Select VM", numItems, sizeof(VM), true, vmMenuPrint); if (item != nullptr) @@ -421,6 +424,7 @@ int listVMs(VM *vms, const int &numItems) String listDisks(Disk *disks, const int &numItems) { selectedItem = 0; + selectedPage = 0; void *item; listItems(disks, &item, "Select Disk", numItems, sizeof(Disk), true, diskMenuPrint); @@ -448,6 +452,7 @@ String listDisks(Disk *disks, const int &numItems) String listPools(Pool *pools, const int &numItems) { selectedItem = 0; + selectedPage = 0; void *item; listItems(pools, &item, "Select Pool", numItems, sizeof(Pool), true, poolMenuPrint); if (item != nullptr)