Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public class SoftwareActionDeviceFilterInput {

private List<SoftwareActionStatus> statuses;
private List<String> organizationIds;
private List<String> deviceTagIds;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.openframe.data.document.packagesearch.PackageManagerType;
import com.openframe.data.document.rmm.software.SoftwareAction;
import com.openframe.data.document.rmm.software.SoftwareActionKind;
import com.openframe.data.document.rmm.software.SoftwareActionStatus;
import lombok.Data;

Expand All @@ -12,5 +13,6 @@ public class SoftwareActionFilterInput {

private List<SoftwareActionStatus> statuses;
private List<SoftwareAction> actions;
private List<SoftwareActionKind> kinds;
private List<PackageManagerType> engines;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SoftwareActionFilters {

private List<ScriptFilterOption> statuses;
private List<ScriptFilterOption> actions;
private List<ScriptFilterOption> kinds;
private List<ScriptFilterOption> engines;
private Integer filteredCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.openframe.data.document.packagesearch.PackageManagerType;
import com.openframe.data.document.rmm.software.SoftwareAction;
import com.openframe.data.document.rmm.software.SoftwareActionKind;
import com.openframe.data.document.rmm.software.SoftwareActionStatus;
import lombok.Builder;
import lombok.Data;
Expand All @@ -18,6 +19,7 @@ public class SoftwareActionResponse {
private String software;
private String version;
private SoftwareAction action;
private SoftwareActionKind kind;
private PackageManagerType engine;
private SoftwareActionStatus status;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,31 @@
import com.openframe.data.document.rmm.script.ScriptExecution;
import com.openframe.data.document.rmm.software.PackageUpdateDeviceState;
import com.openframe.data.document.rmm.software.SoftwareActionStatus;
import com.openframe.data.document.tag.TagAssignment;
import com.openframe.data.document.tag.TagEntityType;
import com.openframe.data.repository.device.MachineRepository;
import com.openframe.data.repository.organization.OrganizationRepository;
import com.openframe.data.repository.rmm.PackageUpdateDeviceStateRepository;
import com.openframe.data.repository.rmm.ScriptExecutionRepository;
import com.openframe.data.repository.rmm.SoftwareBundleOnlineDispatchRepository;
import com.openframe.data.repository.rmm.SoftwareScheduleMachineAssignedRepository;
import com.openframe.data.repository.rmm.SoftwareScheduleOnlineDispatchRepository;
import com.openframe.data.repository.tag.TagAssignmentRepository;
import com.openframe.data.service.TenantIdProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.springframework.util.StringUtils.hasText;

Expand All @@ -45,6 +50,7 @@ public class SoftwareActionDetailService {
private final PackageUpdateDeviceStateRepository packageUpdateDeviceStateRepository;
private final MachineRepository machineRepository;
private final OrganizationRepository organizationRepository;
private final TagAssignmentRepository tagAssignmentRepository;
private final TenantIdProvider tenantIdProvider;

public List<SoftwareActionDeviceResponse> devices(String executionId, String bundleId, String scheduleId,
Expand Down Expand Up @@ -73,14 +79,30 @@ public List<SoftwareActionDeviceResponse> devices(String executionId, String bun
}

enrichWithCustomer(tenantId, byMachine);
Set<String> tagged = taggedMachines(byMachine.keySet(), filter);

return byMachine.values().stream()
.filter(row -> matchesStatus(row, filter))
.filter(row -> matchesCustomer(row, filter))
.filter(row -> tagged == null || tagged.contains(row.getMachineId()))
.filter(row -> matchesSearch(row, search))
.toList();
}

// null = no tag filter; a device matches when it carries any of the tags
private Set<String> taggedMachines(Set<String> machineIds, SoftwareActionDeviceFilterInput filter) {
if (filter == null || filter.getDeviceTagIds() == null || filter.getDeviceTagIds().isEmpty()) {
return null;
}
if (machineIds.isEmpty()) {
return Set.of();
}
return tagAssignmentRepository.findByEntityIdInAndTagIdInAndEntityType(
new ArrayList<>(machineIds), filter.getDeviceTagIds(), TagEntityType.DEVICE).stream()
.map(TagAssignment::getEntityId)
.collect(Collectors.toSet());
}

private void addPending(Map<String, SoftwareActionDeviceResponse> byMachine, String tenantId,
String bundleId, String scheduleId) {
if (hasText(bundleId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class SoftwareActionService {

private static final String FIELD_STATUS = "status";
private static final String FIELD_ACTION = "action";
private static final String FIELD_KIND = "kind";
private static final String FIELD_ENGINE = "packageManager";

private final SoftwareActionAggregationRepository aggregationRepository;
Expand Down Expand Up @@ -97,6 +98,7 @@ public SoftwareActionFilters filters(SoftwareActionFilterInput filter, String se

Map<String, Integer> statuses = new LinkedHashMap<>(aggregationRepository.facet(tenantId, queryFilter, search, FIELD_STATUS));
Map<String, Integer> actions = new LinkedHashMap<>(aggregationRepository.facet(tenantId, queryFilter, search, FIELD_ACTION));
Map<String, Integer> kinds = new LinkedHashMap<>(aggregationRepository.facet(tenantId, queryFilter, search, FIELD_KIND));
Map<String, Integer> engines = new LinkedHashMap<>(aggregationRepository.facet(tenantId, queryFilter, search, FIELD_ENGINE));

List<SoftwareActionResponse> scheduled = includeScheduled(filter)
Expand All @@ -105,6 +107,7 @@ public SoftwareActionFilters filters(SoftwareActionFilterInput filter, String se
for (SoftwareActionResponse row : scheduled) {
bump(statuses, row.getStatus() != null ? row.getStatus().name() : null);
bump(actions, row.getAction() != null ? row.getAction().name() : null);
bump(kinds, row.getKind() != null ? row.getKind().name() : null);
bump(engines, row.getEngine() != null ? row.getEngine().name() : null);
}

Expand All @@ -113,6 +116,7 @@ public SoftwareActionFilters filters(SoftwareActionFilterInput filter, String se
return SoftwareActionFilters.builder()
.statuses(optionMapper.selfLabeled(statuses))
.actions(optionMapper.selfLabeled(actions))
.kinds(optionMapper.selfLabeled(kinds))
.engines(optionMapper.selfLabeled(engines))
.filteredCount((int) filteredCount)
.build();
Expand Down Expand Up @@ -152,6 +156,7 @@ private List<SoftwareActionResponse> scheduledRows(String tenantId, SoftwareActi
.executionId(executionId)
.software(pkg.getPackageName())
.action(schedule.getAction())
.kind(schedule.getAction().kind())
.engine(pkg.getPackageManager())
.status(SoftwareActionStatus.SCHEDULED)
.totalMachineCount(deviceCount)
Expand All @@ -172,6 +177,10 @@ private static boolean scheduledMatchesFilter(SoftwareActionFilterInput filter,
&& !filter.getActions().contains(schedule.getAction())) {
return false;
}
if (filter.getKinds() != null && !filter.getKinds().isEmpty()
&& !filter.getKinds().contains(schedule.getAction().kind())) {
return false;
}
if (filter.getEngines() != null && !filter.getEngines().isEmpty()
&& !filter.getEngines().contains(pkg.getPackageManager())) {
return false;
Expand Down Expand Up @@ -209,6 +218,7 @@ private static SoftwareActionQueryFilter toQueryFilter(SoftwareActionFilterInput
return SoftwareActionQueryFilter.builder()
.statuses(input.getStatuses())
.actions(input.getActions())
.kinds(input.getKinds())
.engines(input.getEngines())
.build();
}
Expand All @@ -224,6 +234,7 @@ private static SoftwareActionResponse toResponse(SoftwareActionSummary s) {
.software(s.getPackageName())
.version(s.getVersion())
.action(s.getAction())
.kind(s.getKind())
.engine(s.getPackageManager())
.status(s.getStatus())
.totalMachineCount(s.getTotalMachineCount())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ private void writeActionResults(SoftwareBundle bundle, List<SoftwareBundlePackag
.tenantId(bundle.getTenantId())
.executionId(executionId)
.action(bundle.getAction())
.kind(bundle.getAction().kind())
.packageManager(pkg.getPackageManager())
.packageName(pkg.getPackageName())
.version(pkg.getVersion())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
import com.openframe.data.document.rmm.software.PackageUpdateDeviceState;
import com.openframe.data.document.rmm.software.SoftwareActionStatus;
import com.openframe.data.document.rmm.software.SoftwareBundleOnlineDispatch;
import com.openframe.data.document.tag.TagAssignment;
import com.openframe.data.document.tag.TagEntityType;
import com.openframe.data.repository.device.MachineRepository;
import com.openframe.data.repository.organization.OrganizationRepository;
import com.openframe.data.repository.rmm.PackageUpdateDeviceStateRepository;
import com.openframe.data.repository.rmm.ScriptExecutionRepository;
import com.openframe.data.repository.rmm.SoftwareBundleOnlineDispatchRepository;
import com.openframe.data.repository.rmm.SoftwareScheduleMachineAssignedRepository;
import com.openframe.data.repository.rmm.SoftwareScheduleOnlineDispatchRepository;
import com.openframe.data.repository.tag.TagAssignmentRepository;
import com.openframe.data.service.TenantIdProvider;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -34,6 +37,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.verifyNoInteractions;
Expand All @@ -52,6 +56,7 @@ class SoftwareActionDetailServiceTest {
@Mock private PackageUpdateDeviceStateRepository packageUpdateDeviceStateRepository;
@Mock private MachineRepository machineRepository;
@Mock private OrganizationRepository organizationRepository;
@Mock private TagAssignmentRepository tagAssignmentRepository;
@Mock private TenantIdProvider tenantIdProvider;

private SoftwareActionDetailService service;
Expand All @@ -60,7 +65,7 @@ class SoftwareActionDetailServiceTest {
void setUp() {
service = new SoftwareActionDetailService(scriptExecutionRepository, bundleOnlineDispatchRepository,
scheduleOnlineDispatchRepository, scheduleMachineAssignedRepository, packageUpdateDeviceStateRepository,
machineRepository, organizationRepository, tenantIdProvider);
machineRepository, organizationRepository, tagAssignmentRepository, tenantIdProvider);
when(tenantIdProvider.getTenantId()).thenReturn(TENANT);
// Enrichment always runs; default to no machine metadata unless a test provides it.
lenient().when(machineRepository.findByTenantIdAndMachineIdIn(eq(TENANT), any())).thenReturn(List.of());
Expand Down Expand Up @@ -136,6 +141,44 @@ void enrichAndFilter() {
assertThat(search).extracting(SoftwareActionDeviceResponse::getMachineId).containsExactly("beta");
}

@Test
@DisplayName("device tags: only the action's devices that carry any of the tags")
void deviceTagFilter() {
when(scriptExecutionRepository.findByTenantIdAndExecutionId(TENANT, EXEC)).thenReturn(List.of(
leaf("alpha", ExecutionStatus.SUCCESS),
leaf("beta", ExecutionStatus.SUCCESS),
leaf("gamma", ExecutionStatus.FAILED)));
when(tagAssignmentRepository.findByEntityIdInAndTagIdInAndEntityType(
argThat(ids -> ids.containsAll(List.of("alpha", "beta", "gamma"))),
eq(List.of("tag-vip", "tag-lab")), eq(TagEntityType.DEVICE)))
.thenReturn(List.of(tagged("alpha", "tag-vip"), tagged("gamma", "tag-lab"), tagged("gamma", "tag-vip")));
SoftwareActionDeviceFilterInput filter = new SoftwareActionDeviceFilterInput();
filter.setDeviceTagIds(List.of("tag-vip", "tag-lab"));

List<SoftwareActionDeviceResponse> rows = service.devices(EXEC, null, null, filter, null);

assertThat(rows).extracting(SoftwareActionDeviceResponse::getMachineId).containsExactlyInAnyOrder("alpha", "gamma");
}

@Test
@DisplayName("device tags: no tag filter, no tag lookup")
void noDeviceTagFilter_noLookup() {
when(scriptExecutionRepository.findByTenantIdAndExecutionId(TENANT, EXEC)).thenReturn(List.of(
leaf("alpha", ExecutionStatus.SUCCESS)));

assertThat(service.devices(EXEC, null, null, new SoftwareActionDeviceFilterInput(), null)).hasSize(1);

verifyNoInteractions(tagAssignmentRepository);
}

private static TagAssignment tagged(String machineId, String tagId) {
TagAssignment assignment = new TagAssignment();
assignment.setEntityId(machineId);
assignment.setTagId(tagId);
assignment.setEntityType(TagEntityType.DEVICE);
return assignment;
}

@Test
@DisplayName("package update: a running leaf reads WAITING from its device state; finished leaves keep their outcome")
void packageUpdateWaiting() {
Expand Down
Loading
Loading