Пример #1
0
 internal void AddEvent(ActivityWatch.API.V1.Event ev)
 {
     if (_doShutdown) //(_cancellationTokenSource?.IsCancellationRequested ?? false)
     {
         return;
     }
     _events.Enqueue(ev);
     _continueLoopManualResetEvent.Set();
     startThread();
 }
Пример #2
0
        private bool mergeEventsAndOutFinishedEvent(out ActivityWatch.API.V1.Event logEvent)
        {
            logEvent = null;
            if (_events.TryDequeue(out ActivityWatch.API.V1.Event newEvent))
            {
                _package.LogService.Log($"new Event for {getBucketId(newEvent)}: " + newEvent.ToJson(), LogService.EErrorLevel.Debug);

                ActivityWatch.API.V1.Event oldEvent = null;
                // do we have an old event?
                if (bucketIdPartLastEvent.ContainsKey(newEvent.Data.BucketIDCustomPart))
                {
                    // compare old to new event
                    oldEvent = bucketIdPartLastEvent[newEvent.Data.BucketIDCustomPart];
                    var duration = (newEvent.Timestamp - oldEvent.Timestamp).TotalSeconds;
                    oldEvent.Duration = duration;

                    if (oldEvent.Equals(newEvent) && duration < MIN_HEARTBEAT_SECONDS)
                    {
                        // same event - keep old event, throw away new event
                    }
                    else if (!oldEvent.Equals(newEvent))
                    {
                        // different event, log and keep the new one
                        bucketIdPartLastEvent.Remove(oldEvent.Data.BucketIDCustomPart);
                        bucketIdPartLastEvent[newEvent.Data.BucketIDCustomPart] = newEvent;
                        logEvent = oldEvent;
                        return(true);
                    }
                    else
                    {
                        // same event, but older than minimum hearbeat interval different event, push
                        // old event with new duration, and save old
                        bucketIdPartLastEvent[newEvent.Data.BucketIDCustomPart] = newEvent;
                    }
                }
                else
                {
                    // no old event, keep the record
                    bucketIdPartLastEvent[newEvent.Data.BucketIDCustomPart] = newEvent;
                }
            }
            else
            {
                if (_doShutdown) //(_cancellationTokenSource.IsCancellationRequested)
                {
                    // we are shutting down, create stop events
                    var stopEvent = bucketIdPartLastEvent.Values.FirstOrDefault();
                    if (stopEvent != null)
                    {
                        // write event, we are about to stop
                        bucketIdPartLastEvent.Remove(stopEvent.Data.BucketIDCustomPart);
                        stopEvent.Duration = (DateTimeOffset.UtcNow - stopEvent.Timestamp).TotalSeconds;
                        logEvent           = stopEvent;
                        return(true);
                    }
                }
                else
                {
                    // nothing to dequeue, create stop events (AFK)
                    var stopEvent = bucketIdPartLastEvent.Values.FirstOrDefault(e => e.Timestamp.AddSeconds((int)e.Duration) < DateTimeOffset.UtcNow.AddSeconds(-AFK_SECONDS));
                    if (stopEvent != null)
                    {
                        // write event, user is afk
                        bucketIdPartLastEvent.Remove(stopEvent.Data.BucketIDCustomPart);
                        stopEvent.Duration = AFK_SECONDS;
                        logEvent           = stopEvent;
                        return(true);
                    }
                }
            }
            return(false);
        }