Skip to content

Commit e45a361

Browse files
committed
feat: list deployments in order
1 parent 2f2d74c commit e45a361

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

server/src/main/java/com/aws/greengrass/cli/CLIEventStreamAgent.java

+31-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package com.aws.greengrass.cli;
77

88
import com.aws.greengrass.componentmanager.ComponentStore;
9+
import com.aws.greengrass.config.Node;
910
import com.aws.greengrass.config.Topics;
1011
import com.aws.greengrass.deployment.DeploymentQueue;
1112
import com.aws.greengrass.deployment.model.ConfigurationUpdateOperation;
@@ -75,7 +76,9 @@
7576
import java.util.ArrayList;
7677
import java.util.Base64;
7778
import java.util.Collection;
79+
import java.util.Comparator;
7880
import java.util.HashMap;
81+
import java.util.Iterator;
7982
import java.util.List;
8083
import java.util.Map;
8184
import java.util.UUID;
@@ -543,17 +546,28 @@ protected void onStreamClosed() {
543546

544547
}
545548

549+
public List<Node> sortDeploymentsByTime(Iterator<Node> deploymentsIterator, Comparator<Node> comparator) {
550+
List<Node> list = new ArrayList<>();
551+
while (deploymentsIterator.hasNext()) {
552+
list.add(deploymentsIterator.next());
553+
}
554+
list.sort(comparator);
555+
return list;
556+
}
557+
546558
@Override
547559
public ListLocalDeploymentsResponse handleRequest(ListLocalDeploymentsRequest request) {
548560
return translateExceptions(() -> {
549561
authorizeRequest(componentName);
550562
List<LocalDeployment> persistedDeployments = new ArrayList<>();
551563
Topics localDeployments = cliServiceConfig.findTopics(PERSISTENT_LOCAL_DEPLOYMENTS);
552-
if (localDeployments != null) {
553-
localDeployments.forEach(topic -> {
554-
Topics topics = (Topics) topic;
564+
List<Topics> deploymentsByTime = sortDeploymentsByTime(localDeployments.iterator(),
565+
new DeploymentTimeComparator());
566+
if (deploymentsByTime != null) {
567+
deploymentsByTime.forEach(topics -> {
555568
LocalDeployment localDeployment = new LocalDeployment();
556569
localDeployment.setDeploymentId(topics.getName());
570+
557571
localDeployment.setStatus(
558572
deploymentStatusFromString(Coerce.toString(topics.find(DEPLOYMENT_STATUS_KEY_NAME))));
559573
persistedDeployments.add(localDeployment);
@@ -565,6 +579,20 @@ public ListLocalDeploymentsResponse handleRequest(ListLocalDeploymentsRequest re
565579
});
566580
}
567581

582+
private class DeploymentTimeComparator implements Comparator {
583+
@Override
584+
public int compare(Object o1, Object o2) {
585+
Topics t1 = (Topics) o1;
586+
Topics t2 = (Topics) o2;
587+
if (t1.getModtime() < t2.getModtime()) {
588+
return 1;
589+
} else if (t1.getModtime() > t2.getModtime()) {
590+
return -1;
591+
}
592+
return 0;
593+
}
594+
}
595+
568596
@Override
569597
public void handleStreamEvent(EventStreamJsonMessage streamRequestEvent) {
570598

0 commit comments

Comments
 (0)