private static void ScanChat() { var lastMessage = _view.ReadAsJsonOrNull <Sample1LastReadMessage>("sample1.dat"); var nextOffset = lastMessage == null ? new EventStoreOffset(0) : new EventStoreOffset(lastMessage.LastOffset); while (true) { var last = EventStoreOffset.Zero; var existMessages = false; foreach (var message in _client.ReadAllEvents(nextOffset)) { last = message.Next; existMessages = true; var text = Encoding.UTF8.GetString(message.EventData); var userName = text.Split('|')[0]; var msg = text.Split(new[] { '|' }, 2)[1]; if (userName == _userName) { continue; } ClearCurrentConsoleLine(); if (userName != "") { WriteColorText(userName + ">", ConsoleColor.Red); Console.WriteLine(msg); } else { WriteColorText(msg + Environment.NewLine, ConsoleColor.DarkCyan); } WriteColorText(_userName + ">", ConsoleColor.Green); Console.Write(_userMessage); } if (existMessages) { _view.WriteAsJson(new Sample1LastReadMessage(last.OffsetInBytes), "sample1.dat"); nextOffset = last; } Thread.Sleep(1000); } }
static void ProcessNextIncrementOfEventsOrSleep(Sample2Data data, IRawEventStoreClient reader, ViewClient views) { var nextOffset = data.NextOffset; // try to read next 10000 events from the platform, // starting from the recorded offset. // This is more efficient, than reading one event by one, since it // reduces cost of reading/writing data by batching const int maxRecordCount = 10000; var nextEvents = reader.ReadAllEvents(new EventStoreOffset(nextOffset), maxRecordCount); var emptyData = true; // process foreach (var dataRecord in nextEvents) { // update next offset data.NextOffset = dataRecord.Next.OffsetInBytes; // update distribution if (data.Distribution.ContainsKey(dataRecord.EventData.Length)) { data.Distribution[dataRecord.EventData.Length]++; } else { data.Distribution[dataRecord.EventData.Length] = 1; } emptyData = false; } if (emptyData) { // we didn't have any new data, so sleep const int seconds = 1; Thread.Sleep(seconds * 1000); } else { // we had some events incoming, so save projection // at least to update offset record PrintDataToConsole(data, false); views.WriteAsJson(data, ViewName); } }
private static void CommentProjection(IRawEventStoreClient store, ViewClient views) { var data = views.ReadAsJsonOrGetNew<CommentDistributionView>(CommentDistributionView.FileName); var processingInfo = views.ReadAsJsonOrGetNew<ProcessingInfoView>(CommentDistributionView.FileName + ".info"); Console.WriteLine("Next comment offset: {0}", processingInfo.NextOffsetInBytes); while (true) { var nextOffset = processingInfo.NextOffsetInBytes; processingInfo.LastOffsetInBytes = processingInfo.NextOffsetInBytes; processingInfo.DateProcessingUtc = DateTime.UtcNow; var records = store.ReadAllEvents(new EventStoreOffset(nextOffset), 10000); var emptyData = true; foreach (var dataRecord in records) { processingInfo.NextOffsetInBytes = dataRecord.Next.OffsetInBytes; processingInfo.EventsProcessed += 1; var user = User.TryGetFromBinary(dataRecord.EventData); if (user != null) { data.Users[user.Id] = user; emptyData = false; continue; } var comment = Comment.TryGetFromBinary(dataRecord.EventData); if (comment != null) { if (data.Distribution.ContainsKey(comment.UserId)) data.Distribution[comment.UserId] += 1; else data.Distribution[comment.UserId] = 1; emptyData = false; } } if (emptyData) { views.WriteAsJson(processingInfo, CommentDistributionView.FileName + ".info"); Thread.Sleep(1000); } else { try { views.WriteAsJson(data, CommentDistributionView.FileName); views.WriteAsJson(processingInfo, CommentDistributionView.FileName + ".info"); } catch (Exception ex) { Console.WriteLine("Exception on writing view - {0}\r\n{1}", CommentDistributionView.FileName, ex.Message); } Console.WriteLine("Next comment offset: {0}", processingInfo.NextOffsetInBytes); } } }
private static void UserCommentsPerDayDistributionProjection(IRawEventStoreClient store, ViewClient views) { var data = views.ReadAsJsonOrGetNew<UserCommentsDistributionView>(UserCommentsDistributionView.FileName); var processingInfo = views.ReadAsJsonOrGetNew<ProcessingInfoView>(UserCommentsDistributionView.FileName + ".info"); Console.WriteLine("Next user offset: {0}", processingInfo.NextOffsetInBytes); while (true) { var nextOffcet = processingInfo.NextOffsetInBytes; var records = store.ReadAllEvents(new EventStoreOffset(nextOffcet), 10000); var emptyData = true; foreach (var dataRecord in records) { processingInfo.NextOffsetInBytes = dataRecord.Next.OffsetInBytes; var user = User.TryGetFromBinary(dataRecord.EventData); if (user != null) { data.Users[user.Id] = user; emptyData = false; continue; } var comment = Comment.TryGetFromBinary(dataRecord.EventData); if (comment != null) { if (!data.Distribution.ContainsKey(comment.UserId)) { data.Distribution.Add(comment.UserId, new long[7]); } var dayOfWeek = (int) comment.CreationDate.Date.DayOfWeek; data.Distribution[comment.UserId][dayOfWeek]++; processingInfo.EventsProcessed += 1; emptyData = false; } } if (emptyData) { views.WriteAsJson(processingInfo, UserCommentsDistributionView.FileName + ".info"); Thread.Sleep(1000); } else { try { views.WriteAsJson(data, UserCommentsDistributionView.FileName); views.WriteAsJson(processingInfo, UserCommentsDistributionView.FileName + ".info"); } catch (Exception ex) { Console.WriteLine("Exception on writing view - {0}\r\n{1}", UserCommentsDistributionView.FileName, ex.Message); } Console.WriteLine("Next user offset: {0}", processingInfo.NextOffsetInBytes); } } }
private static void TagProjection(IRawEventStoreClient store, ViewClient views) { var data = views.ReadAsJsonOrGetNew<TagsDistributionView>(TagsDistributionView.FileName); var processingInfo = views.ReadAsJsonOrGetNew<ProcessingInfoView>(TagsDistributionView.FileName + ".info"); Console.WriteLine("Next post offset: {0}", processingInfo.NextOffsetInBytes); while (true) { var nextOffcet = processingInfo.NextOffsetInBytes; processingInfo.LastOffsetInBytes = processingInfo.NextOffsetInBytes; processingInfo.DateProcessingUtc = DateTime.UtcNow; var records = store.ReadAllEvents(new EventStoreOffset(nextOffcet), 10000); var emptyData = true; foreach (var dataRecord in records) { processingInfo.NextOffsetInBytes = dataRecord.Next.OffsetInBytes; var post = Post.TryGetFromBinary(dataRecord.EventData); if (post == null) continue; foreach (var tag in post.Tags) { if (data.Distribution.ContainsKey(tag)) data.Distribution[tag]++; else data.Distribution[tag] = 1; } processingInfo.EventsProcessed += 1; emptyData = false; } if (emptyData) { views.WriteAsJson(processingInfo, TagsDistributionView.FileName + ".info"); Thread.Sleep(1000); } else { try { views.WriteAsJson(data, TagsDistributionView.FileName); views.WriteAsJson(processingInfo, TagsDistributionView.FileName + ".info"); } catch (Exception ex) { Console.WriteLine("Exception on writing view - {0}\r\n{1}", TagsDistributionView.FileName, ex.Message); } Console.WriteLine("Next post offset: {0}", processingInfo.NextOffsetInBytes); } } }
private static void CommentProjection(IRawEventStoreClient store, ViewClient views) { var data = views.ReadAsJsonOrGetNew <CommentDistributionView>(CommentDistributionView.FileName); var processingInfo = views.ReadAsJsonOrGetNew <ProcessingInfoView>(CommentDistributionView.FileName + ".info"); Console.WriteLine("Next comment offset: {0}", processingInfo.NextOffsetInBytes); while (true) { var nextOffset = processingInfo.NextOffsetInBytes; processingInfo.LastOffsetInBytes = processingInfo.NextOffsetInBytes; processingInfo.DateProcessingUtc = DateTime.UtcNow; var records = store.ReadAllEvents(new EventStoreOffset(nextOffset), 10000); var emptyData = true; foreach (var dataRecord in records) { processingInfo.NextOffsetInBytes = dataRecord.Next.OffsetInBytes; processingInfo.EventsProcessed += 1; var user = User.TryGetFromBinary(dataRecord.EventData); if (user != null) { data.Users[user.Id] = user; emptyData = false; continue; } var comment = Comment.TryGetFromBinary(dataRecord.EventData); if (comment != null) { if (data.Distribution.ContainsKey(comment.UserId)) { data.Distribution[comment.UserId] += 1; } else { data.Distribution[comment.UserId] = 1; } emptyData = false; } } if (emptyData) { views.WriteAsJson(processingInfo, CommentDistributionView.FileName + ".info"); Thread.Sleep(1000); } else { try { views.WriteAsJson(data, CommentDistributionView.FileName); views.WriteAsJson(processingInfo, CommentDistributionView.FileName + ".info"); } catch (Exception ex) { Console.WriteLine("Exception on writing view - {0}\r\n{1}", CommentDistributionView.FileName, ex.Message); } Console.WriteLine("Next comment offset: {0}", processingInfo.NextOffsetInBytes); } } }
private static void TagProjection(IRawEventStoreClient store, ViewClient views) { var data = views.ReadAsJsonOrGetNew <TagsDistributionView>(TagsDistributionView.FileName); var processingInfo = views.ReadAsJsonOrGetNew <ProcessingInfoView>(TagsDistributionView.FileName + ".info"); Console.WriteLine("Next post offset: {0}", processingInfo.NextOffsetInBytes); while (true) { var nextOffcet = processingInfo.NextOffsetInBytes; processingInfo.LastOffsetInBytes = processingInfo.NextOffsetInBytes; processingInfo.DateProcessingUtc = DateTime.UtcNow; var records = store.ReadAllEvents(new EventStoreOffset(nextOffcet), 10000); var emptyData = true; foreach (var dataRecord in records) { processingInfo.NextOffsetInBytes = dataRecord.Next.OffsetInBytes; var post = Post.TryGetFromBinary(dataRecord.EventData); if (post == null) { continue; } foreach (var tag in post.Tags) { if (data.Distribution.ContainsKey(tag)) { data.Distribution[tag]++; } else { data.Distribution[tag] = 1; } } processingInfo.EventsProcessed += 1; emptyData = false; } if (emptyData) { views.WriteAsJson(processingInfo, TagsDistributionView.FileName + ".info"); Thread.Sleep(1000); } else { try { views.WriteAsJson(data, TagsDistributionView.FileName); views.WriteAsJson(processingInfo, TagsDistributionView.FileName + ".info"); } catch (Exception ex) { Console.WriteLine("Exception on writing view - {0}\r\n{1}", TagsDistributionView.FileName, ex.Message); } Console.WriteLine("Next post offset: {0}", processingInfo.NextOffsetInBytes); } } }
private static void UserCommentsPerDayDistributionProjection(IRawEventStoreClient store, ViewClient views) { var data = views.ReadAsJsonOrGetNew <UserCommentsDistributionView>(UserCommentsDistributionView.FileName); var processingInfo = views.ReadAsJsonOrGetNew <ProcessingInfoView>(UserCommentsDistributionView.FileName + ".info"); Console.WriteLine("Next user offset: {0}", processingInfo.NextOffsetInBytes); while (true) { var nextOffcet = processingInfo.NextOffsetInBytes; var records = store.ReadAllEvents(new EventStoreOffset(nextOffcet), 10000); var emptyData = true; foreach (var dataRecord in records) { processingInfo.NextOffsetInBytes = dataRecord.Next.OffsetInBytes; var user = User.TryGetFromBinary(dataRecord.EventData); if (user != null) { data.Users[user.Id] = user; emptyData = false; continue; } var comment = Comment.TryGetFromBinary(dataRecord.EventData); if (comment != null) { if (!data.Distribution.ContainsKey(comment.UserId)) { data.Distribution.Add(comment.UserId, new long[7]); } var dayOfWeek = (int)comment.CreationDate.Date.DayOfWeek; data.Distribution[comment.UserId][dayOfWeek]++; processingInfo.EventsProcessed += 1; emptyData = false; } } if (emptyData) { views.WriteAsJson(processingInfo, UserCommentsDistributionView.FileName + ".info"); Thread.Sleep(1000); } else { try { views.WriteAsJson(data, UserCommentsDistributionView.FileName); views.WriteAsJson(processingInfo, UserCommentsDistributionView.FileName + ".info"); } catch (Exception ex) { Console.WriteLine("Exception on writing view - {0}\r\n{1}", UserCommentsDistributionView.FileName, ex.Message); } Console.WriteLine("Next user offset: {0}", processingInfo.NextOffsetInBytes); } } }