/// <summary>Calculates the top N users over a time interval.</summary> /// <param name="time">the current time</param> /// <param name="metricName">Name of metric</param> /// <returns/> private RollingWindowManager.TopN GetTopUsersForMetric(long time, string metricName , RollingWindowManager.RollingWindowMap rollingWindows) { RollingWindowManager.TopN topN = new RollingWindowManager.TopN(topUsersCnt); IEnumerator <KeyValuePair <string, RollingWindow> > iterator = rollingWindows.GetEnumerator (); while (iterator.HasNext()) { KeyValuePair <string, RollingWindow> entry = iterator.Next(); string userName = entry.Key; RollingWindow aWindow = entry.Value; long windowSum = aWindow.GetSum(time); // do the gc here if (windowSum == 0) { Log.Debug("gc window of metric: {} userName: {}", metricName, userName); iterator.Remove(); continue; } Log.Debug("offer window of metric: {} userName: {} sum: {}", metricName, userName , windowSum); topN.Offer(new RollingWindowManager.NameValuePair(userName, windowSum)); } Log.Info("topN size for command {} is: {}", metricName, topN.Count); return(topN); }
/// <summary>Get the rolling window specified by metric and user.</summary> /// <param name="metric">the updated metric</param> /// <param name="user">the user that updated the metric</param> /// <returns>the rolling window</returns> private RollingWindow GetRollingWindow(string metric, string user) { RollingWindowManager.RollingWindowMap rwMap = metricMap[metric]; if (rwMap == null) { rwMap = new RollingWindowManager.RollingWindowMap(); RollingWindowManager.RollingWindowMap prevRwMap = metricMap.PutIfAbsent(metric, rwMap ); if (prevRwMap != null) { rwMap = prevRwMap; } } RollingWindow window = rwMap[user]; if (window != null) { return(window); } window = new RollingWindow(windowLenMs, bucketsPerWindow); RollingWindow prevWindow = rwMap.PutIfAbsent(user, window); if (prevWindow != null) { window = prevWindow; } return(window); }
/// <summary>Take a snapshot of current top users in the past period.</summary> /// <param name="time">the current time</param> /// <returns> /// a TopWindow describing the top users for each metric in the /// window. /// </returns> public virtual RollingWindowManager.TopWindow Snapshot(long time) { RollingWindowManager.TopWindow window = new RollingWindowManager.TopWindow(windowLenMs ); if (Log.IsDebugEnabled()) { ICollection <string> metricNames = metricMap.Keys; Log.Debug("iterating in reported metrics, size={} values={}", metricNames.Count, metricNames); } foreach (KeyValuePair <string, RollingWindowManager.RollingWindowMap> entry in metricMap) { string metricName = entry.Key; RollingWindowManager.RollingWindowMap rollingWindows = entry.Value; RollingWindowManager.TopN topN = GetTopUsersForMetric(time, metricName, rollingWindows ); int size = topN.Count; if (size == 0) { continue; } RollingWindowManager.OP op = new RollingWindowManager.OP(metricName, topN.GetTotal ()); window.AddOp(op); // Reverse the users from the TopUsers using a stack, // since we'd like them sorted in descending rather than ascending order Stack <RollingWindowManager.NameValuePair> reverse = new Stack <RollingWindowManager.NameValuePair >(); for (int i = 0; i < size; i++) { reverse.Push(topN.Poll()); } for (int i_1 = 0; i_1 < size; i_1++) { RollingWindowManager.NameValuePair userEntry = reverse.Pop(); RollingWindowManager.User user = new RollingWindowManager.User(userEntry.name, Sharpen.Extensions.ValueOf (userEntry.value)); op.AddUser(user); } } return(window); }