<commit_before>package com.devicehive.service;

import com.devicehive.configuration.Constants;
import com.devicehive.dao.DeviceDAO;
import com.devicehive.model.Device;
import com.devicehive.model.DeviceClass;
import com.devicehive.utils.LogExecutionTime;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.ejb.*;
import javax.websocket.Session;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@Singleton
@Startup
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
@EJB(beanInterface = DeviceActivityService.class, name = "DeviceActivityService")
@LogExecutionTime
public class DeviceActivityService {

    private static final Logger logger = LoggerFactory.getLogger(DeviceActivityService.class);



    @EJB
    private HazelcastService hazelcastService;

    @EJB
    private DeviceDAO deviceDAO;


    private HazelcastInstance hazelcast;
    private IMap<Long,Long> deviceTimestampMap;


    @PostConstruct
    public void postConstruct() {
        hazelcast = hazelcastService.getHazelcast();
        deviceTimestampMap = hazelcast.getMap(Constants.DEVICE_ACTIVITY_MAP);
    }


    public void update(long deviceId) {
        deviceTimestampMap.putAsync(deviceId, hazelcast.getCluster().getClusterTime());
    }




    @Schedule(hour = "*", minute = "*/1")
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public void processOfflineDevices() {
        long now = hazelcast.getCluster().getClusterTime();
        for (Long deviceId : deviceTimestampMap.localKeySet()) {
            Device device = deviceDAO.findById(deviceId);
            logger.debug("Checking device {} ", device.getGuid());
            DeviceClass deviceClass = device.getDeviceClass();
            if (deviceClass.getOfflineTimeout() != null) {
                if (now - deviceTimestampMap.get(deviceId) > deviceClass.getOfflineTimeout() * 1000) {
                    deviceDAO.setOffline(deviceId);
                    logger.warn("Device {} is now offline", device.getGuid());
                }
            }
        }
    }

}<commit_msg>Offline devices are now removed from map<commit_after>package com.devicehive.service;

import com.devicehive.configuration.Constants;
import com.devicehive.dao.DeviceDAO;
import com.devicehive.model.Device;
import com.devicehive.model.DeviceClass;
import com.devicehive.utils.LogExecutionTime;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.ejb.*;
import javax.websocket.Session;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Singleton
@Startup
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
@EJB(beanInterface = DeviceActivityService.class, name = "DeviceActivityService")
@LogExecutionTime
public class DeviceActivityService {

    private static final Logger logger = LoggerFactory.getLogger(DeviceActivityService.class);



    @EJB
    private HazelcastService hazelcastService;

    @EJB
    private DeviceDAO deviceDAO;


    private HazelcastInstance hazelcast;
    private IMap<Long,Long> deviceTimestampMap;


    @PostConstruct
    public void postConstruct() {
        hazelcast = hazelcastService.getHazelcast();
        deviceTimestampMap = hazelcast.getMap(Constants.DEVICE_ACTIVITY_MAP);
    }


    public void update(long deviceId) {
        deviceTimestampMap.putAsync(deviceId, hazelcast.getCluster().getClusterTime());
    }




    @Schedule(hour = "*", minute = "*/1")
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public void processOfflineDevices() {
        long now = hazelcast.getCluster().getClusterTime();
        for (Iterator<Long> iter = deviceTimestampMap.localKeySet().iterator(); iter.hasNext();) {
            Long deviceId = iter.next();
            Device device = deviceDAO.findById(deviceId);
            logger.debug("Checking device {} ", device.getGuid());
            DeviceClass deviceClass = device.getDeviceClass();
            if (deviceClass.getOfflineTimeout() != null) {
                if (now - deviceTimestampMap.get(deviceId) > deviceClass.getOfflineTimeout() * 1000) {
                    deviceDAO.setOffline(deviceId);
                    iter.remove();
                    logger.warn("Device {} is now offline", device.getGuid());
                }
            }
        }
    }

}
{'fraction_non_alphanumeric': 0.10117056856187291, 'fraction_numerical': 0.002926421404682274, 'mean_word_length': 3.6501457725947524, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4554655', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1356, 'n_words': 253}