Skip to content

Commit

Permalink
Services are now sorted by the 'weight' field.
Browse files Browse the repository at this point in the history
* Default for discovered services is 0
* Default weight for configured services is their index within their
  group scaled by 100, i.e. (index + 1) * 100
* Should be backwards compatible with current loose ordering
  • Loading branch information
jameswynn committed Jan 24, 2023
1 parent 5ecb946 commit 8d01662
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/utils/config/api-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ import {
} from "utils/config/service-helpers";
import { cleanWidgetGroups, widgetsFromConfig } from "utils/config/widget-helpers";

/**
* Compares services by weight then by name.
*/
function compareServices(service1, service2) {
const comp = service1.weight - service2.weight;
if (comp !== 0) {
return comp;
}
return service1.name.localeCompare(service2.name);
}

export async function bookmarksResponse() {
checkAndCopyConfig("bookmarks.yaml");

Expand Down Expand Up @@ -112,7 +123,8 @@ export async function servicesResponse() {
...discoveredDockerGroup.services,
...discoveredKubernetesGroup.services,
...configuredGroup.services
].filter((service) => service),
].filter((service) => service)
.sort(compareServices),
};

if (definedLayouts) {
Expand Down
21 changes: 21 additions & 0 deletions src/utils/config/service-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ export async function servicesFromConfig() {
})),
}));

// add default weight to services based on their position in the configuration
servicesArray.forEach((group, groupIndex) => {
group.services.forEach((service, serviceIndex) => {
if(!service.weight) {
servicesArray[groupIndex].services[serviceIndex].weight = (serviceIndex + 1) * 100;
}
});
});

return servicesArray;
}

Expand Down Expand Up @@ -152,6 +161,7 @@ export async function servicesFromKubernetes() {
href: ingress.metadata.annotations[`${ANNOTATION_BASE}/href`] || getUrlFromIngress(ingress),
name: ingress.metadata.annotations[`${ANNOTATION_BASE}/name`] || ingress.metadata.name,
group: ingress.metadata.annotations[`${ANNOTATION_BASE}/group`] || "Kubernetes",
weight: ingress.metadata.annotations[`${ANNOTATION_BASE}/weight`] || '0',
icon: ingress.metadata.annotations[`${ANNOTATION_BASE}/icon`] || '',
description: ingress.metadata.annotations[`${ANNOTATION_BASE}/description`] || '',
};
Expand Down Expand Up @@ -201,6 +211,17 @@ export function cleanServiceGroups(groups) {
name: serviceGroup.name,
services: serviceGroup.services.map((service) => {
const cleanedService = { ...service };
if (typeof service.weight === 'string') {
const weight = parseInt(service.weight, 10);
if (Number.isNaN(weight)) {
cleanedService.weight = 0;
} else {
cleanedService.weight = weight;
}
}
if (typeof cleanedService.weight !== "number") {
cleanedService.weight = 0;
}

if (cleanedService.widget) {
// whitelisted set of keys to pass to the frontend
Expand Down

0 comments on commit 8d01662

Please sign in to comment.