/// <summary> /// Occurs when user counter data has be retrieved. /// </summary> /// <param name="counter">The user counter data that was retrieved.</param> protected virtual void OnUserCounterRetrieved(UserCounter counter) { if (UserCounterRetrieved != null) { UserCounterRetrieved(this, counter); } }
/// <summary> /// Occurs when user counter has been reset. /// </summary> /// <param name="counter">The user counter data that was reset.</param> protected virtual void OnUserCounterReset(UserCounter counter) { if (UserCounterReset != null) { UserCounterReset(this, counter); } }
/// <summary> /// Checks user retry after value. /// </summary> /// <param name="userHash">User hash value.</param> /// <returns>Retry-After value for User.</returns> public int CheckUserRetryAfterSecounds(string userHash) { using var wl = this.userLock.EnterWriteLock(); if (this.userLimit <= 0) { return(60); } UserCounter counter; if (!this.userCounters.TryGetValue(userHash, out counter)) { counter = new UserCounter(); this.userCounters.TryAdd(userHash, counter); return(0); } int result = 0; double thresholdSecs = 60.0f / this.userLimit; double secs = counter.ElapsedTimer.Elapsed.TotalSeconds; if (secs > thresholdSecs) { counter.ElapsedTimer.Restart(); if (Log.IsDebugEnabled) { Log.Debug("User Counter restarted."); } } else { result = (int)(thresholdSecs - secs); if (result < 1) { result = 1; } if (Log.IsDebugEnabled) { Log.Debug("User Counter: Retry-After={RetryAfter}", result); } } return(result); }
/// <summary> /// Clears all of the counters for the specified user to 0. /// </summary> /// <param name="counter">The counter to reset the values for.</param> /// <param name="deviceId">The internal id of the device to clear.</param> /// <returns>True if the counters were reset, false if otherwise.</returns> public bool Clear(UserCounter counter, uint deviceId = 0) { if (counter == null) { return(false); } return(Connect(() => { IEnumerable <field> fields = counter.Fields.GetUpdatableFields(IsUserCounterUpdatable).Clear(); if ((fields == null) || (!fields.Any())) { return (false); } return (Update(deviceId, Convert.ToString(counter.Index), counter.Id, fields.ToArray(), USAGE_COUNTER_USER, true)); })); }
/// <summary> /// Clears all of the counters for the system to zero. /// </summary> /// <param name="deviceId">The internal id of the device to clear.</param> /// <remarks> /// For performance, if you previously called <see cref="GetUserCounters"/>, instead of /// calling this function, you should instead call <see cref="Clear(System.Collections.Generic.IEnumerable{Ricoh.Models.UserCounter})"/> passing /// in the results from the previous call. In order to avoid some of the overhead of /// retrieving each object, only the ids are sent and the default object capability is /// used to clear the counters for each user. /// </remarks> public void Clear(uint deviceId = 0) { Connect(() => { Trace.TraceInformation("Retrieving all objects for {0}", USAGE_COUNTER_USER); try { string[] objectIds = Client.getObjects(SessionId, deviceId, USAGE_COUNTER_USER); if ((objectIds != null) && (objectIds.Length > 0)) { // Get the "default" object capabilities // ------------------------------------------------------- // Instead of querying each object, we're just going to use the fields returned // for the first item for each of the objectIds that were retrieved. To do this // "correctly" (i.e. slowly), we should query each object capabilities, then do // the update. Once we have the fields, we just get back those that are updateable objectCapability capability = GetDefaultObjectCapability(deviceId, objectIds); field[] fields = capability.fieldList.GetUpdatableFields(IsUserCounterCapabilityUpdatable); objectIds.AsParallel().ForAll((i) => { uint objectId; if (UInt32.TryParse(i, out objectId)) { if (Update(deviceId, Convert.ToString(UserCounter.ExtractIndexFromObjectId(objectId)), objectId, fields, USAGE_COUNTER_USER, true)) { Trace.TraceInformation("Counters reset for object {0}. Counters: {1}", objectId, String.Join(",", fields.Select(f => f.name))); } else { Trace.TraceError("Reset counter for object {0} failed.", objectId, String.Join(",", fields.Select(f => f.name))); } } }); } } catch (Exception e) { Trace.TraceError(e.Message); Trace.TraceError(e.StackTrace); throw (new RicohOperationFailedException(this, "Clear", e)); } return(true); // Required to satisfy Func<T> }); }
/// <summary> /// Retrieves the current user counters from the system. /// </summary> /// <param name="deviceId">The internal id of the device.</param> /// <returns>The collection of user counters for the device.</returns> public IEnumerable <UserCounter> GetUserCounters(uint deviceId = 0) { return(Connect(() => { IList <UserCounter> counters = new List <UserCounter>(); Trace.TraceInformation("Retrieving all objects for {0}", USAGE_COUNTER_USER); try { string[] objectIds = Client.getObjects(SessionId, deviceId, USAGE_COUNTER_USER); if ((objectIds != null) && (objectIds.Length > 0)) { Trace.TraceInformation("{0} objects: {1}", USAGE_COUNTER_USER, String.Join(",", objectIds)); // Get the "default" object capabilities objectCapability capability = GetDefaultObjectCapability(deviceId, objectIds); // In parallel, retrieve the user counters. objectIds.AsParallel().ForAll((i) => { uint objectId; if (UInt32.TryParse(i, out objectId)) { UserCounter counter = GetUserCounter(deviceId, objectId, ref capability); if (counter != null) { OnUserCounterRetrieved(counter); counters.Add(counter); } else { Trace.TraceWarning("Failed to retrieve information for object {0}.", objectId); } } }); } } catch (Exception e) { Trace.TraceError(e.Message); Trace.TraceError(e.StackTrace); throw (new RicohOperationFailedException(this, "GetUserCounters", e)); } return (counters); })); }
// when user gets the data, tokketuser class and Usercounter should have the same equivalent class to be fetched public static TokketUsers SetCounts(this TokketUsers user, UserCounter counter) { if (counter == null) { user.Toks = 0; user.Points = 0; user.Coins = 0; user.Sets = 0; user.StrikesTokBlitz = 0; user.Reactions = 0; user.Likes = 0; user.Dislikes = 0; user.Accurates = 0; user.Inaccurates = 0; user.Comments = 0; user.Reports = 0; user.Followers = 0; user.Following = 0; } else { user.Toks = (counter?.Toks != null) ? (long)(counter?.Toks) : 0; user.Points = (counter?.Points != null) ? (long)(counter?.Points) : 0; user.Coins = (counter?.Coins != null) ? (long)(counter?.Coins) : 0; user.Sets = (counter?.Sets != null) ? (long)(counter?.Sets) : 0; user.StrikesTokBlitz = (counter?.StrikesTokBlitz != null) ? (long)(counter?.StrikesTokBlitz) : 0; user.Reactions = (counter?.Reactions != null) ? (long)(counter?.Reactions) : 0; user.Likes = (counter?.Likes != null) ? (long)(counter?.Likes) : 0; user.Dislikes = (counter?.Dislikes != null) ? (long)(counter?.Dislikes) : 0; user.Accurates = (counter?.Accurates != null) ? (long)(counter?.Accurates) : 0; user.Inaccurates = (counter?.Inaccurates != null) ? (long)(counter?.Inaccurates) : 0; user.Comments = (counter?.Comments != null) ? (long)(counter?.Comments) : 0; user.Reports = (counter?.Reports != null) ? (long)(counter?.Reports) : 0; user.Followers = (counter?.Followers != null) ? (long)(counter?.Followers) : 0; user.Following = (counter?.Following != null) ? (long)(counter?.Following) : 0; user.SavedTokBlitz = (counter?.TokblitzSaved != null) ? (long)(counter?.TokblitzSaved) : 0; user.SavedTokBlast = (counter?.TokblastSaved != null) ? (long)(counter?.TokblastSaved) : 0; user.tokblitzNumTeam = (counter?.tokblitzNumTeam != null) ? (int)(counter?.tokblitzNumTeam) : 0; user.IsRoomPurchasedTokBlitz = (counter?.IsRoomPurchasedTokBlitz != null) ? (bool)(counter?.IsRoomPurchasedTokBlitz) : false; } return(user); }
public static async Task <IActionResult> createusercounter([HttpTrigger(AuthorizationLevel.Function, "post", Route = Constants.Version + "/" + "createusercounter/" + "{id}")] HttpRequest req, ILogger log, ExecutionContext context, string id) { try { // if there is a new item to add in knowledeg (update the usercounter class or tokketuser) UserCounter item = new UserCounter(); item.Id = id + "-counter"; item.PartitionKey = id + "-counter"; item.Reactions = 0; item.Sets = 0; item.DeletedCoins = 0; item.TokblastSaved = 1; item.TokblitzSaved = 1; item.StrikesTokBlitz = 1; item.UserId = id; item.Reports = 0; item.Followers = 0; item.DeletedComments = 0; item.DeletedPoints = 0; item.Label = "usercounter"; item.Likes = 0; item.Points = 0; item.Toks = 0; item.Dislikes = 0; item.tokblitzNumTeam = 1; item.IsRoomPurchasedTokBlitz = false; await Api <UserCounter> .CreateItemAsyncKnowledge(item, Constants.PkRequest(item.PartitionKey)); return(new OkResult()); } catch (Exception e) { return(new BadRequestObjectResult(e.Message)); } }
private UserCounter UserCounter; /* consider to read this from file */ public ChatHub(PrimitiveHolder pH, UserCounter userCounter) { this.pH = pH; this.UserCounter = userCounter; }