Skip to content

Commit 02fe8e5

Browse files
oleksandr-blipandriikachurclaude
authored
feat(cloud-tenants): a tenant serves several customers, so its row names the first and counts the rest CU-17tkuw5z06b (#2796)
Co-authored-by: andriikachur <andriika@flamingo.cx> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9ee0190 commit 02fe8e5

104 files changed

Lines changed: 5529 additions & 346 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎openframe-api-lib/src/main/java/com/openframe/api/dto/device/DeviceFilterCriteria.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.openframe.data.document.device.DeviceStatus;
44
import com.openframe.data.document.device.DeviceType;
5+
import com.openframe.data.document.device.DirectorySource;
6+
import com.openframe.data.document.device.DirectoryUserLink;
57
import com.openframe.data.document.packagesearch.PackageManagerType;
68
import com.openframe.data.document.rmm.script.OsType;
79
import lombok.AllArgsConstructor;
@@ -31,6 +33,8 @@ public class DeviceFilterCriteria {
3133
private String pattern;
3234
private List<String> hostnames;
3335
private List<String> nicknames;
36+
private List<DirectorySource> directorySources;
37+
private List<DirectoryUserLink> userLinks;
3438

3539
public DeviceFilterCriteria(List<DeviceStatus> statuses, List<DeviceType> deviceTypes, List<OsType> osTypes,
3640
List<String> organizationIds, List<String> tagKeys, List<String> tagValues,

‎openframe-api-lib/src/main/java/com/openframe/api/dto/device/DeviceFilterFacet.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,27 @@ public enum DeviceFilterFacet {
2222
OS_TYPES("osTypes"),
2323
ORGANIZATION_IDS("organizationIds"),
2424
TAG_KEYS("tagKeys"),
25+
DIRECTORY_SOURCES("directorySources"),
26+
USER_LINKS("userLinks"),
2527
FILTERED_COUNT("filteredCount");
2628

27-
/** Every facet — the behaviour non-GraphQL callers (the external REST API) still want. */
29+
/**
30+
* Every facet, the directory ones included. For a caller that has no selection set to narrow
31+
* it and whose response can carry all of them. A caller whose response does not expose a
32+
* facet must not use this: each unused facet is a Pinot round trip made for nothing.
33+
*/
2834
public static final Set<DeviceFilterFacet> ALL =
2935
Collections.unmodifiableSet(EnumSet.allOf(DeviceFilterFacet.class));
3036

37+
/**
38+
* What the external REST {@code /filters} response carries: everything except the directory
39+
* facets ({@link #DIRECTORY_SOURCES}, {@link #USER_LINKS}), which its DTO does not expose.
40+
* Listed explicitly so a facet added later stays off the REST path until the DTO exposes it.
41+
*/
42+
public static final Set<DeviceFilterFacet> REST =
43+
Collections.unmodifiableSet(EnumSet.of(
44+
STATUSES, DEVICE_TYPES, OS_TYPES, ORGANIZATION_IDS, TAG_KEYS, FILTERED_COUNT));
45+
3146
private final String graphQlField;
3247

3348
DeviceFilterFacet(String graphQlField) {

‎openframe-api-lib/src/main/java/com/openframe/api/dto/device/DeviceFilters.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ public class DeviceFilters {
1717
private List<DeviceFilterOption> osTypes;
1818
private List<DeviceFilterOption> organizationIds;
1919
private List<TagFilterOption> tagKeys;
20+
private List<DeviceFilterOption> directorySources;
21+
private List<DeviceFilterOption> userLinks;
2022
private Integer filteredCount;
2123
}

‎openframe-api-lib/src/main/java/com/openframe/api/service/device/DeviceFilterService.java‎

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
import com.openframe.data.service.TenantIdProvider;
1010
import lombok.extern.slf4j.Slf4j;
1111
import org.springframework.beans.factory.annotation.Qualifier;
12+
import org.springframework.beans.factory.annotation.Value;
1213
import org.springframework.stereotype.Service;
1314

1415
import java.util.ArrayList;
16+
import java.util.EnumSet;
1517
import java.util.List;
1618
import java.util.Map;
1719
import java.util.Set;
@@ -20,11 +22,13 @@
2022
import java.util.function.Supplier;
2123

2224
import static com.openframe.api.dto.device.DeviceFilterFacet.DEVICE_TYPES;
25+
import static com.openframe.api.dto.device.DeviceFilterFacet.DIRECTORY_SOURCES;
2326
import static com.openframe.api.dto.device.DeviceFilterFacet.FILTERED_COUNT;
2427
import static com.openframe.api.dto.device.DeviceFilterFacet.ORGANIZATION_IDS;
2528
import static com.openframe.api.dto.device.DeviceFilterFacet.OS_TYPES;
2629
import static com.openframe.api.dto.device.DeviceFilterFacet.STATUSES;
2730
import static com.openframe.api.dto.device.DeviceFilterFacet.TAG_KEYS;
31+
import static com.openframe.api.dto.device.DeviceFilterFacet.USER_LINKS;
2832
import static java.util.Collections.emptyList;
2933
import static java.util.concurrent.CompletableFuture.completedFuture;
3034
import static java.util.concurrent.CompletableFuture.supplyAsync;
@@ -33,25 +37,36 @@
3337
@Slf4j
3438
public class DeviceFilterService {
3539

40+
private static final Set<DeviceFilterFacet> DIRECTORY_FACETS = EnumSet.of(DIRECTORY_SOURCES, USER_LINKS);
41+
3642
private final PinotDeviceRepository pinotDeviceRepository;
3743
private final DeviceFilterOptionMapper optionMapper;
3844
private final TenantIdProvider tenantIdProvider;
3945
private final Executor facetExecutor;
46+
private final boolean directoryFacetsEnabled;
4047

48+
// The directory facets count values only an external directory integration writes. Without one (OSS) every
49+
// machine reads as no source and NONE, so the two Pinot queries are skipped and the facets stay empty. The
50+
// directory filters still apply either way.
4151
public DeviceFilterService(PinotDeviceRepository pinotDeviceRepository,
4252
DeviceFilterOptionMapper optionMapper,
4353
TenantIdProvider tenantIdProvider,
4454
@Qualifier(DeviceFilterExecutorConfig.DEVICE_FILTER_FACET_EXECUTOR)
45-
Executor facetExecutor) {
55+
Executor facetExecutor,
56+
@Value("${openframe.device.directory-facets.enabled:false}")
57+
boolean directoryFacetsEnabled) {
4658
this.pinotDeviceRepository = pinotDeviceRepository;
4759
this.optionMapper = optionMapper;
4860
this.tenantIdProvider = tenantIdProvider;
4961
this.facetExecutor = facetExecutor;
62+
this.directoryFacetsEnabled = directoryFacetsEnabled;
5063
}
5164

5265
/**
53-
* Every facet. For callers with no selection set of their own — the external REST API, whose
54-
* response DTO always carries all of them.
66+
* Every facet ({@link DeviceFilterFacet#ALL}); the directory ones only while
67+
* {@code openframe.device.directory-facets.enabled} is true, otherwise they come back empty. A
68+
* caller whose response does not expose all of them should pass its own set instead, as the
69+
* external REST API does with {@link DeviceFilterFacet#REST}.
5570
*/
5671
public CompletableFuture<DeviceFilters> getDeviceFilters(DeviceFilterCriteria filters) {
5772
return getDeviceFilters(filters, DeviceFilterFacet.ALL);
@@ -70,7 +85,7 @@ public CompletableFuture<DeviceFilters> getDeviceFilters(DeviceFilterCriteria fi
7085
*/
7186
public CompletableFuture<DeviceFilters> getDeviceFilters(DeviceFilterCriteria filters,
7287
Set<DeviceFilterFacet> requestedFacets) {
73-
Set<DeviceFilterFacet> facets = requestedFacets != null ? requestedFacets : DeviceFilterFacet.ALL;
88+
Set<DeviceFilterFacet> facets = queriedFacets(requestedFacets != null ? requestedFacets : DeviceFilterFacet.ALL);
7489
List<String> statuses = filters != null && filters.getStatuses() != null ?
7590
filters.getStatuses().stream().map(Enum::name).toList() : emptyList();
7691
List<String> deviceTypes = filters != null && filters.getDeviceTypes() != null ?
@@ -80,36 +95,55 @@ public CompletableFuture<DeviceFilters> getDeviceFilters(DeviceFilterCriteria fi
8095
List<String> organizationIds = filters != null ? filters.getOrganizationIds() : emptyList();
8196
List<String> tagKeys = filters != null ? filters.getTagKeys() : emptyList();
8297
List<String> tagKeyValues = buildTagKeyValuesFilter(filters);
98+
List<String> directorySources = filters != null && filters.getDirectorySources() != null ?
99+
filters.getDirectorySources().stream().map(Enum::name).toList() : emptyList();
100+
List<String> userLinks = filters != null && filters.getUserLinks() != null ?
101+
filters.getUserLinks().stream().map(Enum::name).toList() : emptyList();
83102

84103
String tenantId = tenantIdProvider.getTenantId();
85104

86105
CompletableFuture<Map<String, Integer>> statusesFuture = facetQuery(facets, STATUSES, () ->
87-
pinotDeviceRepository.getStatusFilterOptions(tenantId, statuses, deviceTypes, osTypes, organizationIds, tagKeys, tagKeyValues));
106+
pinotDeviceRepository.getStatusFilterOptions(tenantId, statuses, deviceTypes, osTypes, organizationIds, tagKeys, tagKeyValues, directorySources, userLinks));
88107
CompletableFuture<Map<String, Integer>> deviceTypesFuture = facetQuery(facets, DEVICE_TYPES, () ->
89-
pinotDeviceRepository.getDeviceTypeFilterOptions(tenantId, statuses, deviceTypes, osTypes, organizationIds, tagKeys, tagKeyValues));
108+
pinotDeviceRepository.getDeviceTypeFilterOptions(tenantId, statuses, deviceTypes, osTypes, organizationIds, tagKeys, tagKeyValues, directorySources, userLinks));
90109
CompletableFuture<Map<String, Integer>> osTypesFuture = facetQuery(facets, OS_TYPES, () ->
91-
pinotDeviceRepository.getOsTypeFilterOptions(tenantId, statuses, deviceTypes, osTypes, organizationIds, tagKeys, tagKeyValues));
110+
pinotDeviceRepository.getOsTypeFilterOptions(tenantId, statuses, deviceTypes, osTypes, organizationIds, tagKeys, tagKeyValues, directorySources, userLinks));
92111
CompletableFuture<Map<String, Integer>> organizationsFuture = facetQuery(facets, ORGANIZATION_IDS, () ->
93-
pinotDeviceRepository.getOrganizationFilterOptions(tenantId, statuses, deviceTypes, osTypes, organizationIds, tagKeys, tagKeyValues));
112+
pinotDeviceRepository.getOrganizationFilterOptions(tenantId, statuses, deviceTypes, osTypes, organizationIds, tagKeys, tagKeyValues, directorySources, userLinks));
94113
CompletableFuture<Map<String, Integer>> tagKeysFuture = facetQuery(facets, TAG_KEYS, () ->
95-
pinotDeviceRepository.getTagKeyFilterOptions(tenantId, statuses, deviceTypes, osTypes, organizationIds, tagKeys, tagKeyValues));
114+
pinotDeviceRepository.getTagKeyFilterOptions(tenantId, statuses, deviceTypes, osTypes, organizationIds, tagKeys, tagKeyValues, directorySources, userLinks));
115+
CompletableFuture<Map<String, Integer>> directorySourcesFuture = facetQuery(facets, DIRECTORY_SOURCES, () ->
116+
pinotDeviceRepository.getDirectorySourceFilterOptions(tenantId, statuses, deviceTypes, osTypes, organizationIds, tagKeys, tagKeyValues, directorySources, userLinks));
117+
CompletableFuture<Map<String, Integer>> userLinksFuture = facetQuery(facets, USER_LINKS, () ->
118+
pinotDeviceRepository.getUserLinkFilterOptions(tenantId, statuses, deviceTypes, osTypes, organizationIds, tagKeys, tagKeyValues, directorySources, userLinks));
96119
CompletableFuture<Integer> filteredCountFuture = facetQuery(facets, FILTERED_COUNT, () ->
97-
pinotDeviceRepository.getFilteredDeviceCount(tenantId, statuses, deviceTypes, osTypes, organizationIds, tagKeys, tagKeyValues));
120+
pinotDeviceRepository.getFilteredDeviceCount(tenantId, statuses, deviceTypes, osTypes, organizationIds, tagKeys, tagKeyValues, directorySources, userLinks));
98121

99122
return CompletableFuture.allOf(
100-
statusesFuture, deviceTypesFuture, osTypesFuture,
101-
organizationsFuture, tagKeysFuture, filteredCountFuture)
123+
statusesFuture, deviceTypesFuture, osTypesFuture, organizationsFuture, tagKeysFuture,
124+
directorySourcesFuture, userLinksFuture, filteredCountFuture)
102125
.thenApply(v -> DeviceFilters.builder()
103126
.statuses(optionMapper.selfLabeled(statusesFuture.join()))
104127
.deviceTypes(optionMapper.selfLabeled(deviceTypesFuture.join()))
105128
.osTypes(optionMapper.selfLabeled(osTypesFuture.join()))
106129
.organizationIds(optionMapper.organizationLabeled(organizationsFuture.join()))
107130
.tagKeys(optionMapper.tagLabeled(tagKeysFuture.join()))
131+
.directorySources(optionMapper.selfLabeled(directorySourcesFuture.join()))
132+
.userLinks(optionMapper.selfLabeled(userLinksFuture.join()))
108133
.filteredCount(filteredCountFuture.join())
109134
.build()
110135
);
111136
}
112137

138+
private Set<DeviceFilterFacet> queriedFacets(Set<DeviceFilterFacet> requested) {
139+
if (directoryFacetsEnabled || requested.isEmpty()) {
140+
return requested;
141+
}
142+
EnumSet<DeviceFilterFacet> queried = EnumSet.copyOf(requested);
143+
queried.removeAll(DIRECTORY_FACETS);
144+
return queried;
145+
}
146+
113147
/**
114148
* Submits one facet query, or short-circuits to {@code null} when the caller didn't ask for it.
115149
*

‎openframe-api-lib/src/main/java/com/openframe/api/service/device/DeviceService.java‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.springframework.stereotype.Service;
3939
import org.springframework.validation.annotation.Validated;
4040

41-
import java.time.Instant;
4241
import java.util.ArrayList;
4342
import java.util.Arrays;
4443
import java.util.Collection;
@@ -49,6 +48,7 @@
4948
import java.util.stream.Collectors;
5049

5150
import static com.openframe.data.service.machine.MachineFields.NICKNAME;
51+
import static com.openframe.data.service.machine.MachineFields.STATUS;
5252
import static com.openframe.data.service.machine.MachineUpdate.machineUpdate;
5353

5454
@Service
@@ -183,6 +183,9 @@ private DeviceFilters deviceFilters(MachineQueryFilter filter, String search) {
183183
.osTypes(deviceFilterOptionMapper.selfLabeled(machineRepository.facet(tenantId, filter, search, DeviceFacetDimension.OS_TYPE)))
184184
.organizationIds(deviceFilterOptionMapper.organizationLabeled(machineRepository.facet(tenantId, filter, search, DeviceFacetDimension.ORGANIZATION_ID)))
185185
.tagKeys(List.of())
186+
// The pickers' Mongo facets have no directory dimension: the two filters apply, their counts are []
187+
.directorySources(List.of())
188+
.userLinks(List.of())
186189
.filteredCount((int) machineRepository.countMachines(tenantId, filter, search))
187190
.build();
188191
}
@@ -275,7 +278,9 @@ private MachineQueryFilter machineFilter(DeviceFilterCriteria filter,
275278
.hostnames(filter.getHostnames())
276279
.nicknames(filter.getNicknames())
277280
.tagKeys(filter.getTagKeys())
278-
.tagValues(filter.getTagValues());
281+
.tagValues(filter.getTagValues())
282+
.directorySources(filter.getDirectorySources())
283+
.directoryUserLinks(filter.getUserLinks());
279284
filterOsTypes = filter.getOsTypes();
280285
}
281286
return builder.osTypes(resolveOsTypes(filterOsTypes, osTypeScope))
@@ -359,13 +364,14 @@ public void updateStatusByMachineId(@NotBlank String machineId, @NotNull DeviceS
359364
deviceSoftwareConnectivitySync.onConnectivityChanged(machine.getTenantId(), machineId, connected);
360365
}
361366

362-
machine.setStatus(status);
363-
machine.setUpdatedAt(Instant.now());
364-
machineRepository.save(machine);
367+
// Field-level: a whole-document save would overwrite what other writers set on the machine since the read
368+
Machine updated = machineWriter.update(machineId, machineUpdate().set(STATUS, status))
369+
.map(MachineWriteResult::after)
370+
.orElseThrow(() -> new DeviceNotFoundException("Device not found: " + machineId));
365371
log.info("Device {} status updated to {}", machineId, status);
366372

367373
try {
368-
deviceStatusProcessor.postProcessStatusUpdated(machine);
374+
deviceStatusProcessor.postProcessStatusUpdated(updated);
369375
} catch (Exception e) {
370376
log.error("Post-processor failed for machineId={}: {}", machineId, e.getMessage(), e);
371377
}

0 commit comments

Comments
 (0)