コード例 #1
0
ファイル: XGenParser.cs プロジェクト: Trayek/xGenerator
        static XGenParser()
        {
            Factories = new Dictionary <string, VariableFactory>
            {
                { "PageViews", VariableFactory.Lambda((segment, token, parser) => segment.VisitVariables.AddOrReplace(Variables.Random("PageViews", new PoissonGenerator(token.Value <double>()).Truncate(1, 20)))) },
                { "VisitCount", VariableFactory.Lambda((segment, token, parser) => segment.VisitorVariables.AddOrReplace(Variables.Random("VisitCount", new PoissonGenerator(token.Value <double>()).Truncate(1, 20)))) },
                { "BounceRate", VariableFactory.Lambda((segment, token, parser) => segment.VisitVariables.AddOrReplace(Variables.Boolean("Bounce", token.Value <double>()))) },
                { "Duration", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var mean = token.Value <double>();
                        segment.RequestVariables.AddOrReplace(Variables.Duration(new SkewNormalGenerator(mean, mean, 3), 1));
                    }) },
                { "StartDate", VariableFactory.Lambda((segment, token, parser) => segment.DateGenerator.Start = token.Value <DateTime>()) },
                { "EndDate", VariableFactory.Lambda((segment, token, parser) => segment.DateGenerator.End = token.Value <DateTime>()) },
                { "DayOfWeek", VariableFactory.Lambda((segment, token, parser) => segment.DateGenerator.DayOfWeek(t => t.Clear().Weighted(builder =>
                    {
                        foreach (var kv in (JObject)token)
                        {
                            DayOfWeek day;
                            builder.Add(Enum.TryParse(kv.Key, out day) ? (int)day : int.Parse(kv.Key), kv.Value.Value <double>());
                        }
                    }))) },
                { "YearlyTrend", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        if (token.Value <double>() != 1)
                        {
                            segment.DateGenerator.Year(t => t.Clear().MoveAbsolutePercentage(0).LineAbsolutePercentage(1, token.Value <double>()));
                        }
                        //segment.DateGenerator.YearWeight = 1;
                    }) },
                { "Month", new MonthFactory() },
                { "Identified", VariableFactory.Lambda((segment, token, parser) => segment.VisitorVariables.AddOrReplace(new ContactDataVariable(token.Value <double>()))) },
                { "Campaign", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var campaignPct = token.Value <double?>("Percentage") ?? 1;
                        var campaigns   = parser.ParseWeightedSet <string>(token["Weights"]);
                        segment.VisitVariables.AddOrReplace(Variables.Random("Campaign", () => Randomness.Random.NextDouble() < campaignPct ? campaigns() : null, true));
                    }) },
                { "Channel", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var channelPct = token.Value <double?>("Percentage") ?? 1;
                        var channels   = parser.ParseWeightedSet <string>(token["Weights"]);
                        segment.VisitVariables.AddOrReplace(Variables.Random("Channel", () => Randomness.Random.NextDouble() < channelPct ? channels() : null, true));
                    }) },
                { "Referrer", VariableFactory.Lambda((segment, token, parser) => segment.VisitVariables.AddOrReplace(Variables.Random("Referrer", parser.ParseWeightedSet <string>(token), true))) },
                { "Geo", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var regionId = parser.ParseWeightedSet <string>(token["Region"]);
                        segment.VisitorVariables.AddOrReplace(new GeoVariables(() => new GetRandomCityService().GetRandomCity(regionId())));
                    }) },
                { "Devices", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        Func <string> userAgent;
                        if (!token.HasValues)
                        {
                            userAgent = new DeviceRepository().GetAll().Select(d => d.UserAgent).Exponential(.8, 8);
                        }
                        else
                        {
                            var id    = parser.ParseWeightedSet <string>(token);
                            userAgent = () => new DeviceRepository().GetAll().ToDictionary(ga => ga.Id, ga => ga)[id()].UserAgent;
                        }
                        segment.VisitorVariables.AddOrReplace(Variables.Random("UserAgent", userAgent));
                    }) },
                { "Outcomes", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var value = new NormalGenerator(10, 5).Truncate(1);
                        segment.VisitVariables.AddOrReplace(new OutcomeVariable(parser.ParseSet <string>(token), value.Next));
                    }) },

                { "Goal", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var goalPct = token.Value <double?>("Percentage") ?? 0.2;
                        var goals   = parser.ParseWeightedSet <string>(token["Weights"]);
                        segment.VisitVariables.AddOrReplace(Variables.Random("Goal", () => Randomness.Random.NextDouble() < goalPct ? goals() : null, true));
                    }) },

                { "InternalSearch", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var searchPct = token.Value <double?>("Percentage") ?? 0.2;
                        var keywords  = parser.ParseWeightedSet <string>(token["Keywords"]);
                        segment.VisitVariables.AddOrReplace(Variables.Random("InternalSearch", () => Randomness.Random.NextDouble() < searchPct ? keywords() : null, true));
                    }) },
                { "ExternalSearch", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var searchPct = token.Value <double?>("Percentage") ?? 0.2;
                        var keywords  = parser.ParseWeightedSet <string>(token["Keywords"]);

                        var engineId = parser.ParseWeightedSet <string>(token["Engine"]);

                        segment.VisitVariables.AddOrReplace(new ExternalSearchVariable(() => Randomness.Random.NextDouble() >= searchPct ? null : SearchEngine.SearchEngines.ToDictionary(s => s.Id)[engineId()], () => new[] { keywords() }));
                    }) },
                { "Language", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var languages = parser.ParseWeightedSet <string>(token);
                        segment.VisitVariables.AddOrReplace(Variables.Random("Language", languages));
                    }) },
                { "LandingPage", new LandingPageFactory() }
            };
        }