public UserHistorySnapshot Query(string username, bool forceReevaluation = false) { var entry = dbClient.GetKeyValueEntry(KVSTORE_USER_HISTORY_ENTRY_TYPE, username); UserHistorySnapshot existingRecord = null; if (entry.ExistedInDatabase && TryParseUserHistorySnapshot(entry.Value, out existingRecord) && !forceReevaluation && (DateTime.Now - entry.UpdatedAt) < TimeSpan.FromDays(1)) { log.WriteLine("Using cached user history: " + username); return(existingRecord); } log.WriteLine("Fetching latest user history: " + username); var snapshot = existingRecord ?? new UserHistorySnapshot(); var commentStats = UpdateUserRecordCommentsInternal(username, snapshot); var postStats = UpdateUserRecordPostsInternal(username, snapshot); dbClient.PutKeyValueEntry(KVSTORE_USER_HISTORY_ENTRY_TYPE, username, JsonUtils.ToJson(snapshot)); log.WriteLine($"Done. {snapshot.Comments.Count}c/{snapshot.Posts.Count}p, {commentStats.added}c/{postStats.added}p Added, {commentStats.updated}c/{postStats.updated}p Updated."); return(snapshot); }
private (int added, int updated) UpdateUserRecordPostsInternal(string username, UserHistorySnapshot record) { var(added, updated, nextPosts) = UpdateUserRecordContributionsInternal <PostSnapshot, Post>( username, record.Posts, p => { var(content, postType) = p is LinkPost lp ? (lp.URL, PostType.Link) : p is SelfPost sp ? (sp.SelfText, PostType.Self) : throw new NotSupportedException("Unknown post type: " + p.GetType().FullName); return(new PostSnapshot { FullName = p.Fullname, Subreddit = p.Subreddit, Score = p.Score, Title = p.Title, Content = content, PostType = postType, CreationTime = p.Created, Removed = p.Removed, }); },
private (int added, int updated) UpdateUserRecordCommentsInternal(string username, UserHistorySnapshot record) { var(added, updated, nextComments) = UpdateUserRecordContributionsInternal <CommentSnapshot, Comment>( username, record.Comments, c => new CommentSnapshot { FullName = c.Fullname, Subreddit = c.Subreddit, Score = c.Score, Text = c.Body, CreationTime = c.Created, Removed = c.Removed, }, () => redditClient.EnumerateUserCommentsBatchedTimeDescendingLimit1000ish(username), c => c.Created, c => c.Fullname, c => c.Subreddit, "comment"); record.Comments = nextComments; return(added, updated); }