示例#1
0
        public async Task <PipelineMessage> ProcessAsync(PipelineMessage message, TraceWriter traceWriter)
        {
            traceWriter.Warning("Information about fruit crates");

            var fruits = JsonConvert.DeserializeObject <List <Fruit> >(message.Body);

            traceWriter.Warning($"- Total number of crates: {fruits.Count}");

            traceWriter.Warning("- Number of crates per fruit:");
            var cratesPerFruit = fruits.GroupBy(x => x.Name)
                                 .Select(g => (Name: g.Key, Count: g.Count()))
                                 .OrderByDescending(x => x.Count);

            foreach (var(Name, Count) in cratesPerFruit)
            {
                traceWriter.Warning($"-- {Name}: {Count}");
            }

            traceWriter.Warning("- Number of crates per country of origin:");
            var cratesPerCountry = fruits.GroupBy(x => x.CountryOfOrigin)
                                   .Select(g => (Country: g.Key, Count: g.Count()))
                                   .OrderByDescending(x => x.Country);

            foreach (var(Country, Count) in cratesPerCountry)
            {
                traceWriter.Warning($"-- {Country}: {Count}");
            }

            var isFairTradeCount = fruits.Count(x => x.IsFairTrade);

            traceWriter.Warning($"- Percentage of fair trade fruit crates: {Math.Round((double)isFairTradeCount / fruits.Count * 100, 2)}%");

            return(message);
        }
示例#2
0
        public async Task TestCodecToStreamingCodecConfiguration()
        {
            var config = CodecToStreamingCodecConfiguration <int> .Conf
                         .Set(CodecToStreamingCodecConfiguration <int> .Codec, GenericType <IntCodec> .Class)
                         .Build();

            IStreamingCodec <PipelineMessage <int> > streamingCodec =
                TangFactory.GetTang().NewInjector(config).GetInstance <IStreamingCodec <PipelineMessage <int> > >();

            CancellationToken token = new CancellationToken();

            int obj = 5;
            PipelineMessage <int> message = new PipelineMessage <int>(obj, true);
            var         stream            = new MemoryStream();
            IDataWriter writer            = new StreamDataWriter(stream);

            streamingCodec.Write(message, writer);
            PipelineMessage <int> message1 = new PipelineMessage <int>(obj + 1, false);
            await streamingCodec.WriteAsync(message1, writer, token);

            stream.Position = 0;
            IDataReader reader = new StreamDataReader(stream);
            var         res1   = streamingCodec.Read(reader);
            var         res2   = await streamingCodec.ReadAsync(reader, token);

            Assert.Equal(obj, res1.Data);
            Assert.Equal(obj + 1, res2.Data);
            Assert.Equal(true, res1.IsLast);
            Assert.Equal(false, res2.IsLast);
        }
示例#3
0
        public void GetMessages_Returns_Information_Messages_When_All_Types_Of_Messages_Are_Added()
        {
            var information     = new PipelineMessage(nameof(Bag), MessageType.Information);
            var pipelineContext = new Bag()
                                  .Warning(nameof(Bag))
                                  .Message(information)
                                  .Error(nameof(Bag));

            pipelineContext.MessageObjects(MessageFilter.Info).Should()
            .HaveCount(1, "only one information message was added")
            .And
            .AllBeEquivalentTo(information, "this message was added to collection");
        }
        private void PersistMessage(PipelineMessage pMessage)
        {
            Guard.AgainstNullAndEmpty(nameof(pMessage.Id), pMessage.Id);
            Guard.AgainstNullAndEmpty(nameof(pMessage.Content), pMessage.Content);
            Guard.AgainstNull(nameof(pMessage.Targets), pMessage.Targets);

            string sessionId = pMessage.Id;
            string appId     = pMessage.Targets[WidgetConst.PMSGIDX_APPID];
            string jobId     = pMessage.Targets[WidgetConst.PMSGIDX_JOBID];

            Repository.AddSession(sessionId, appId, null);
            Repository.AddJob(sessionId, jobId, pMessage.Content, null);
        }
 private void OnMessageReceived(object sender, PipelineMessageEventArgs e)
 {
     try {
         PipelineMessage message = e.Message;
         ExtractMessageTargets(ref message);
         PluginObject.PersistingMessage(message);
         PersistMessage(message);
         PluginObject.PersistedMessage(message);
     }
     catch (Exception ex) {
         Logger.LogError(ex, ex.ToString());
     }
 }
示例#6
0
 public void PersistedMessage(PipelineMessage pMessage)
 {
     if (OnMessagePersisted == null)
     {
         return;
     }
     Parallel.ForEach(OnMessagePersisted.GetInvocationList(), t => {
         ((EventHandler <PipelineMessageEventArgs>)t).BeginInvoke(
             this, new PipelineMessageEventArgs()
         {
             Message = pMessage
         }, null, null);
     });
 }
        private void TryEnqueueJobs(PipelineMessage pMessage)
        {
            string sessionId = pMessage.Id;
            string appId     = pMessage.Targets[WidgetConst.PMSGIDX_APPID];

            string[] scopes = pMessage.Targets[WidgetConst.PMSGIDX_SCOPES].Split(';');

            void EnqueueJob(dynamic profile)
            {
                string profileJson    = ((object)(profile.User)).ToJson();
                string profileName    = profile.System.ProfileName;
                string procedure      = profile.System.Procedure;
                string cronExpression = profile.System.CronExpression;
                int    maxAge         = profile.System.MaxAge;

                BackgroundJob.Enqueue <TrackerBuilder>(t => t.AddSessionState(
                                                           sessionId, nameof(SessionStates.Pending)));

                string syncJob = $"{appId}.{profileName}";

                if (!SchedulerJobs.Contains(syncJob))
                {
                    var scheduler = new Scheduler {
                        CronExpression = cronExpression
                    };
                    scheduler.OnTime += (s, e) => {
                        BackgroundJob.Enqueue <TrackerBuilder>(t => t.SyncSessionStates(procedure, profileJson));
                    };
                    SchedulerJobs.Add(syncJob);
                    Task.Run(() => { scheduler.Start(); scheduler.Dispose(); });
                    Task.Run(() => { SpinWait.SpinUntil(() => false, maxAge); scheduler.Abort(); SchedulerJobs.Remove(syncJob); });
                }
            }

            try {
                var profiles = Profile.LoadProfile(appId, Const.PLUGIN_NAME, scopes);
                Parallel.ForEach(profiles, profile => {
                    EnqueueJob(profile);
                });
            }
            catch (AggregateException e) {
                foreach (var ie in e.InnerExceptions)
                {
                    Logger.LogError(ie, ie.ToString());
                }
            }
            catch (Exception e) {
                Logger.LogError(e, e.ToString());
            }
        }
        public async Task <PipelineMessage> ProcessAsync(PipelineMessage message, TraceWriter traceWriter)
        {
            traceWriter.Warning("Calculating average stats by class");

            var typeDefinition = new[]
            {
                new {
                    Name         = "",
                    Class        = "",
                    Race         = "",
                    Age          = 0,
                    Level        = 0,
                    Experience   = 0,
                    Intelligence = 0,
                    Charisma     = 0,
                    Wisdom       = 0,
                    Dexterity    = 0,
                    Strength     = 0
                }
            };

            var characters = JsonConvert.DeserializeAnonymousType(message.Body, typeDefinition);

            var averages = characters
                           .GroupBy(x => x.Class)
                           .Select(x =>
                                   (
                                       Class: x.Key,
                                       Count: x.Count(),
                                       AverageIntelligence: x.Average(y => y.Intelligence),
                                       AverageWisdom: x.Average(y => y.Wisdom),
                                       AverageDexterity: x.Average(y => y.Dexterity),
                                       AverageCharisma: x.Average(y => y.Charisma),
                                       Averagestrength: x.Average(y => y.Strength)
                                   ));

            foreach (var(Class, Count, AverageIntelligence, AverageWisdom, AverageDexterity, AverageCharisma, Averagestrength) in averages)
            {
                traceWriter.Warning($"- Class '{Class}' ({Count})");
                traceWriter.Warning($"-- Int: {Math.Round(AverageIntelligence, 2)}");
                traceWriter.Warning($"-- Dex: {Math.Round(AverageDexterity, 2)}");
                traceWriter.Warning($"-- Cha: {Math.Round(AverageCharisma, 2)}");
                traceWriter.Warning($"-- Wis: {Math.Round(AverageWisdom, 2)}");
                traceWriter.Warning($"-- Str: {Math.Round(Averagestrength, 2)}");
            }

            return(message);
        }
示例#9
0
        private void TryEnqueueJobs(PipelineMessage pMessage)
        {
            string sessionId = pMessage.Id;
            string jobId     = pMessage.Targets[WidgetConst.PMSGIDX_JOBID];
            string appId     = pMessage.Targets[WidgetConst.PMSGIDX_APPID];

            string[] scopes = pMessage.Targets[WidgetConst.PMSGIDX_SCOPES].Split(';');

            void EnqueueJob(dynamic profile)
            {
                string profileId   = Generator.RandomLongId;
                string profileName = profile.System.ProfileName;
                string profileJson = ((object)profile).ToJson();

                string paramJob = BackgroundJob.Enqueue <TransBuilder>(t => t.AddJobParam(
                                                                           jobId, profileId, "Profile", profileJson, profileName));

                string taskId  = Generator.RandomLongId;
                var    taskJob = BackgroundJob.ContinueJobWith <TransBuilder>(paramJob, t => t.AddJobTask(
                                                                                  jobId, taskId, profileName));

                var stateJob = BackgroundJob.ContinueJobWith <TransBuilder>(taskJob, t => t.AddTaskState(
                                                                                taskId, nameof(TaskStates.Pending)));

                string requestId  = Generator.RandomLongId;
                var    requestJob = BackgroundJob.ContinueJobWith <TransBuilder>(taskJob, t => t.AddTaskRequest(
                                                                                     taskId, requestId, jobId, profileId));

                BackgroundJob.ContinueJobWith <TransBuilder>(requestJob, t => t.AddTaskResponse(
                                                                 taskId, requestId, sessionId));
            }

            try {
                var profiles = Profile.LoadProfile(appId, Const.PLUGIN_NAME, scopes);
                Parallel.ForEach(profiles, profile => {
                    EnqueueJob(profile);
                });
            }
            catch (AggregateException e) {
                foreach (var ie in e.InnerExceptions)
                {
                    Logger.LogError(ie, ie.ToString());
                }
            }
            catch (Exception e) {
                Logger.LogError(e, e.ToString());
            }
        }
示例#10
0
        public async Task <PipelineMessage> ProcessAsync(PipelineMessage message, TraceWriter traceWriter)
        {
            traceWriter.Warning("Appending strings to themselves");

            var lines = message.Body.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            var sb = new StringBuilder();

            foreach (var line in lines)
            {
                sb.AppendLine(line + "|" + line);
            }

            message.Body = sb.ToString();

            return(message);
        }
        public async Task <PipelineMessage> ProcessAsync(PipelineMessage message, TraceWriter traceWriter)
        {
            traceWriter.Warning("Finding fruit with maximum number of crates");

            var fruits = JsonConvert.DeserializeObject <List <Fruit> >(message.Body);

            var(Name, Count, MaxFruits) = fruits
                                          .GroupBy(x => x.Name)
                                          .Select(g => (Name: g.Key, Count: g.Count(), Fruits: g.ToList()))
                                          .OrderByDescending(x => x.Count)
                                          .FirstOrDefault();

            traceWriter.Warning($"{Name} has most crates ({Count})");

            message.Body = JsonConvert.SerializeObject(MaxFruits);

            return(message);
        }
示例#12
0
        public async Task <PipelineMessage> ProcessAsync(PipelineMessage message, TraceWriter traceWriter)
        {
            traceWriter.Warning("Converting DnD characters XML to JSON");

            var xdoc = XDocument.Parse(message.Body);
            var json = JsonConvert.SerializeXNode(xdoc);

            var typeDefinition = new
            {
                ArrayOfCharacter = new
                {
                    Character = new[] {
                        new {
                            Name         = "",
                            Class        = "",
                            Race         = "",
                            Age          = 0,
                            Level        = 0,
                            Experience   = 0,
                            Intelligence = 0,
                            Charisma     = 0,
                            Wisdom       = 0,
                            Dexterity    = 0,
                            Strength     = 0
                        }
                    }
                }
            };

            var anonymousObj = JsonConvert.DeserializeAnonymousType(json, typeDefinition);

            var characters = anonymousObj.ArrayOfCharacter.Character.ToList();

            traceWriter.Warning("Conversion finished.");

            message.Body = JsonConvert.SerializeObject(characters);

            return(message);
        }
示例#13
0
        private static async Task InitMessengers(Task <IPipelineMessenger[]> messengers, PluginObject pluginObject)
        {
            Guard.AgainstNull(nameof(messengers), messengers);
            Guard.AgainstNull(nameof(pluginObject), pluginObject);

            void OnMessageRecived(byte[] body, Dictionary <string, string> context)
            {
                var message = new PipelineMessage {
                    Id      = context[WidgetConst.PMSG_ID],
                    Source  = context[WidgetConst.PMSG_SOURCE],
                    Content = Encoding.UTF8.GetString(body),
                    Targets = new List <string> {
                        context[WidgetConst.PMSG_JOBID]
                    },
                };

                pluginObject.ReceiveMessage(message);
            }

            await Task.Run(() => {
                try {
                    var options = new ParallelOptions {
                        MaxDegreeOfParallelism = 1
                    };
                    Parallel.ForEach(messengers.Result, options, messenger => {
                        messenger.OnMessageReceived += OnMessageRecived;
                        messenger.Initialize(Logger);
                    });
                }
                catch (AggregateException e) {
                    foreach (var ie in e.InnerExceptions)
                    {
                        Console.WriteLine(ie.ToString());
                    }
                }
            });
        }
        private void ExtractMessageTargets(ref PipelineMessage pMessage)
        {
            string content = pMessage.Content;

            Guard.AgainstNullAndEmpty(nameof(content), content);
            dynamic entity = content.FromJson <dynamic>();

            Guard.AgainstNull(nameof(entity), entity);
            dynamic auth = entity.auth;

            Guard.AgainstNull(nameof(auth), auth);
            string appId = auth.app_id;

            Guard.AgainstNullAndEmpty(nameof(appId), appId);
            dynamic meta = entity.meta;

            Guard.AgainstNull(nameof(meta), meta);
            string scopes = meta.scopes;

            Guard.AgainstNullAndEmpty(nameof(scopes), scopes);

            pMessage.Targets.Add(appId);
            pMessage.Targets.Add(scopes);
        }
 public virtual PipelineContextTestObject WithMessage(PipelineMessage message)
 {
     this.AddMessageObject(message);
     return(this);
 }