Skip to content
Open
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
60 changes: 60 additions & 0 deletions api/src/org/labkey/api/exp/api/SampleChangeNotify.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2026 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.api.exp.api;

import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.labkey.api.admin.notification.NotificationService;
import org.labkey.api.data.Container;
import org.labkey.api.security.SecurityManager;
import org.labkey.api.security.User;
import org.labkey.api.security.permissions.Permission;
import org.labkey.api.security.permissions.ReadPermission;
import org.labkey.api.util.logging.LogHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public enum SampleChangeNotify
{
SampleDataChanged;

private static final Logger LOG = LogHelper.getLogger(SampleChangeNotify.class, "Sample data change WebSocket notifications");

public static void fireSampleDataChanged(@Nullable Container container)
{
if (container == null)
return;

try
{
Set<Class<? extends Permission>> read = Collections.singleton(ReadPermission.class);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Consider streaming to an unmodifiable list.

List<Integer> userIds = SecurityManager.getUsersWithPermissions(container, Set.of(ReadPermission.class))
        .stream()
        .map(UserPrincipal::getUserId)
        .toList();

List<User> readers = SecurityManager.getUsersWithPermissions(container, read);
List<Integer> userIds = new ArrayList<>(readers.size());
for (User user : readers)
userIds.add(user.getUserId());

if (!userIds.isEmpty())
NotificationService.get().sendServerEvent(userIds, SampleChangeNotify.SampleDataChanged);
}
catch (Exception e)
{
LOG.warn("Failed to send sample data changed notification for container " + container.getPath(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
import org.labkey.api.exp.api.ObjectReferencer;
import org.labkey.api.exp.api.ProtocolImplementation;
import org.labkey.api.exp.api.ProvenanceService;
import org.labkey.api.exp.api.SampleChangeNotify;
import org.labkey.api.exp.api.SampleTypeService;
import org.labkey.api.exp.api.SimpleRunRecord;
import org.labkey.api.exp.list.ListService;
Expand Down Expand Up @@ -5078,6 +5079,10 @@ public int deleteMaterialBySqlFilter(
() -> SearchService.get().deleteResources(docids),
POSTCOMMIT);

// Notify connected clients that sample data changed so cached "insights" counts can be flagged stale.
// Deletes don't go through onSamplesChanged, so the notification is fired here.
transaction.addCommitTask(() -> SampleChangeNotify.fireSampleDataChanged(container), POSTCOMMIT);

transaction.commit();
if (timing != null)
LOG.info("SampleType delete timings\n{}", timing.dump());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import org.labkey.api.exp.api.ExpSampleType;
import org.labkey.api.exp.api.ExperimentService;
import org.labkey.api.exp.api.NameExpressionOptionService;
import org.labkey.api.exp.api.SampleChangeNotify;
import org.labkey.api.exp.api.SampleTypeService;
import org.labkey.api.exp.property.Domain;
import org.labkey.api.exp.property.DomainProperty;
Expand Down Expand Up @@ -1223,6 +1224,10 @@ private void fireSamplesChanged(SampleTypeServiceImpl.SampleChangeType reason, @
{
if (_sampleType != null)
_sampleType.onSamplesChanged(getUser(), null, reason, changedSince);

// Notify connected clients so cached "insights" counts can be flagged stale (insert/update/merge).
// Deletes bypass this path -- see ExperimentServiceImpl.deleteMaterialByRowIds for that notification.
SampleChangeNotify.fireSampleDataChanged(getContainer());
}

static @Nullable Timestamp captureChangedSince()
Expand Down