@@ -0,0 +1,2986 @@
+package top.quantic.sentry.job;%0A%0Aimport com.codahale.metrics.Gauge;%0Aimport com.codahale.metrics.MetricRegistry;%0Aimport com.ibasco.agql.protocols.valve.steam.webapi.pojos.SteamPlayerProfile;%0Aimport org.quartz.Job;%0Aimport org.quartz.JobDataMap;%0Aimport org.quartz.JobExecutionContext;%0Aimport org.quartz.JobExecutionException;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport top.quantic.sentry.service.GameQueryService;%0A%0Aimport java.util.LinkedHashSet;%0Aimport java.util.Map;%0Aimport java.util.Set;%0Aimport java.util.concurrent.ConcurrentHashMap;%0A%0Apublic class SteamProfilePoller implements Job %7B%0A%0A    private static final Logger log = LoggerFactory.getLogger(SteamProfilePoller.class);%0A%0A    @Autowired%0A    private GameQueryService gameQueryService;%0A%0A    @Autowired%0A    private MetricRegistry metricRegistry;%0A%0A    private final Map%3CLong, SteamPlayerProfile%3E profileMap = new ConcurrentHashMap%3C%3E();%0A%0A    @Override%0A    public void execute(JobExecutionContext context) throws JobExecutionException %7B%0A        JobDataMap dataMap = context.getMergedJobDataMap();%0A        String keys = dataMap.getString(%22keys%22);%0A        if (keys == null) %7B%0A            throw new JobExecutionException(%22Data map key 'keys' is missing%22);%0A        %7D%0A%0A        Set%3CLong%3E checked = new LinkedHashSet%3C%3E();%0A%0A        for (String key : keys.split(%22;%22)) %7B%0A            Long id = gameQueryService.getSteamId64(key).exceptionally(t -%3E %7B%0A                log.warn(%22Could not get profile ID from key %7B%7D%22, key, t);%0A                return null;%0A            %7D).join();%0A            if (id != null) %7B%0A                profileMap.remove(id);%0A                registerGauges(id);%0A                checked.add(id);%0A            %7D%0A        %7D%0A%0A        // clean up removed ids%0A        metricRegistry.removeMatching((name, metric) -%3E name.startsWith(%22steam.profile%22) && !checked.contains(extractId(name)));%0A    %7D%0A%0A    private Long extractId(String name) %7B%0A        return Long.valueOf(name.replaceAll(%22%5Esteam%5C%5C.profile%5C%5C..+%5C%5C%5BsteamId:(%5B0-9%5D+)%5D$%22, %22$1%22));%0A    %7D%0A%0A    private void registerGauges(Long id) %7B%0A        if (metricRegistry.getGauges((name, metric) -%3E name.contains(%22steamId:%22 + id)).isEmpty()) %7B%0A            metricRegistry.register(%22steam.profile.status%5BsteamId:%22 + id + %22%5D%22, (Gauge%3CInteger%3E) () -%3E profileMap.computeIfAbsent(id, this::getPlayerProfile).getPersonaState());%0A            metricRegistry.register(%22steam.profile.lastLogOff%5BsteamId:%22 + id + %22%5D%22, (Gauge%3CLong%3E) () -%3E profileMap.computeIfAbsent(id, this::getPlayerProfile).getLastLogOff());%0A        %7D%0A    %7D%0A%0A    private SteamPlayerProfile getPlayerProfile(Long id) %7B%0A        return gameQueryService.getPlayerProfile(id).exceptionally(t -%3E %7B%0A            log.warn(%22Could not retrieve profile info for %7B%7D%22, id, t);%0A            SteamPlayerProfile dummy = new SteamPlayerProfile();%0A            dummy.setPersonaState(0);%0A            dummy.setLastLogOff(0);%0A            return dummy;%0A        %7D).join();%0A    %7D%0A%7D%0A

Add a job to report status of Steam profiles