Esempio n. 1
0
        private static async Task MainAsync()
        {
            MongoDBConfig.RegisterClassMaps();

            var builder = new ContainerBuilder();

            builder.RegisterModule(new Common.AutofacModule());
            builder.RegisterModule(new AutofacModule());

            var container = builder.Build();

            while (true)
            {
                var sw = Stopwatch.StartNew();

                var updatePricesJob = container.Resolve <UpdatePricesJob>();
                await updatePricesJob.UpdatePrices();

                sw.Stop();
                Console.WriteLine("Took " + TimeSpan.FromMilliseconds(sw.ElapsedMilliseconds).ToString(@"dd\.hh\:mm\:ss\.fff") + " to process prices.");
                Console.WriteLine();
                sw.Restart();

                using (var alertUsersJob = container.Resolve <AlertUsersJob>())
                {
                    await alertUsersJob.SendAlerts();
                }

                sw.Stop();
                Console.WriteLine("Took " + TimeSpan.FromMilliseconds(sw.ElapsedMilliseconds).ToString(@"dd\.hh\:mm\:ss\.fff") + " to process alerts.");
                Console.WriteLine();

                Thread.Sleep(TimeSpan.FromHours(16));
            }
        }
Esempio n. 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services
            .AddOptions()
            .AddCors(options => {
                var policy = new Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicy();
                policy.Origins.Add("*");
                policy.Headers.Add("*");
                policy.Methods.Add("*");
                policy.SupportsCredentials = true;

                options.AddPolicy("CorsPolicy", policy);
            })
            .AddMvc()
            .AddJsonOptions(options =>
            {
                var settings = options.SerializerSettings;

                settings.ContractResolver  = new CamelCasePropertyNamesContractResolver();
                settings.NullValueHandling = NullValueHandling.Ignore;
                settings.Formatting        = Formatting.Indented;
            });

            services.AddAuthorization(options =>
            {
                options.AddPolicy("LocalOnly", policy => policy.Requirements.Add(new LocalAuthorizationOnlyRequirement()));
                options.AddPolicy("Admin", policy => policy.Requirements.Add(new GroupAuthorizationRequirement("Admin")));
            });

            services.Configure <MongoDBOptions>(Configuration.GetSection("mongodb"));
            MongoDBConfig.RegisterClassMaps();

            return(IocConfig.RegisterComponents(services));
        }
Esempio n. 3
0
        private static async Task MainAsync()
        {
            MongoDBConfig.RegisterClassMaps();

            var builder = new ContainerBuilder();

            builder.RegisterModule(new Common.AutofacModule());
            builder.RegisterModule(new AutofacModule());

            var container = builder.Build();

            while (true)
            {
                var jobs = container.Resolve <IEnumerable <IJob> >();
                foreach (var job in jobs)
                {
                    Console.WriteLine($"Starting {job.GetType().Name} job.");
                    var sw = Stopwatch.StartNew();

                    await job.ExecuteJob();

                    sw.Stop();
                    Console.WriteLine($"Took {TimeSpan.FromMilliseconds(sw.ElapsedMilliseconds):dd\\.hh\\:mm\\:ss\\.fff} to execute {job.GetType().Name} job.");
                    Console.WriteLine();
                }

                Thread.Sleep(TimeSpan.FromDays(7));
            }
        }
Esempio n. 4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services
            .AddCors(options => {
                options.AddPolicy("CorsPolicy",
                                  builder => builder
                                  .AllowAnyOrigin()
                                  .AllowAnyMethod()
                                  .AllowAnyHeader()
                                  .AllowCredentials());
            })
            .AddMvc()
            .AddControllersAsServices()
            .AddJsonOptions(options =>
            {
                var settings = options.SerializerSettings;

                settings.ContractResolver  = new CamelCasePropertyNamesContractResolver();
                settings.NullValueHandling = NullValueHandling.Ignore;
                settings.Formatting        = Formatting.Indented;
            });

            services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(jwt =>
            {
                jwt.Audience  = this.Configuration["Auth0:ApiIdentifier"];
                jwt.Authority = $"https://{this.Configuration["Auth0:Domain"]}/";
            });

            services.AddAuthorization(auth =>
            {
                auth.AddPolicy("LocalOnly", policy => policy.Requirements.Add(new LocalAuthorizationOnlyRequirement()));
            });

            MongoDBConfig.RegisterClassMaps();

            return(IocConfig.RegisterComponents(services));
        }
Esempio n. 5
0
        private static async Task MainAsync()
        {
            MongoDBConfig.RegisterClassMaps();

            var builder = new ContainerBuilder();

            builder.Register <ILoggerFactory>(c => new LoggerFactory());
            builder.RegisterModule(new Common.AutofacModule());

            var container         = builder.Build();
            var productRepository = container.Resolve <IProductRepository>();

            var changes = new List <ChangeInfo>();


            var sw = Stopwatch.StartNew();

            var allProducts = await productRepository.GetAllAsync();

            foreach (var product in allProducts)
            {
                var orderedPriceHistory = product.PriceHistory.OrderBy(x => x.ModifiedAt).ToList();
                for (var i = 0; i < orderedPriceHistory.Count - 1; i++)
                {
                    if (orderedPriceHistory[i].Price != orderedPriceHistory[i + 1].Price)
                    {
                        changes.Add(new ChangeInfo {
                            Domain = new Uri(product.Uri).Authority, DateTime = orderedPriceHistory[i + 1].ModifiedAt
                        });
                    }
                }
            }

            sw.Stop();
            Console.WriteLine($"Took {TimeSpan.FromMilliseconds(sw.ElapsedMilliseconds):dd\\.hh\\:mm\\:ss\\.fff} to process price changes.");
            Console.WriteLine();

            var changeDick = changes.ToLookup(x => x.Domain);

            foreach (var domain in changeDick)
            {
                Console.WriteLine("Changes for " + domain.Key + ": ");

                /*
                 * var domainChanges = domain.ToList().OrderBy(x => x.DateTime);
                 * foreach (var change in domainChanges)
                 * {
                 *  Console.WriteLine($"A change happened on {change.DateTime.Date.ToShortDateString()} which was a {change.DateTime.DayOfWeek}");
                 * }
                 *
                 * Console.WriteLine();
                 */

                var dayChanges = domain.ToLookup(x => x.DateTime.DayOfWeek);
                foreach (var dayChange in dayChanges)
                {
                    Console.WriteLine($"{dayChange.Count()} changes happened on {dayChange.Key}");
                }

                Console.WriteLine();
            }
        }
Esempio n. 6
0
        static async Task MainAsync()
        {
            MongoDBConfig.RegisterClassMaps();

            var builder = new ContainerBuilder();

            builder.RegisterModule(new E133.Business.AutofacModule());
            builder.RegisterModule(new E133.Crawler.AutofacModule());
            builder.RegisterModule(new E133.Database.AutofacModule());
            builder.RegisterModule(new E133.Parser.AutofacModule());

            var container = builder.Build();

            var repo          = container.Resolve <IQuickRecipeRepository>();
            var knownCrawlers = container.Resolve <IEnumerable <IHtmlCrawler> >();
            var recipeCount   = 0;
            var parserFactory = container.Resolve <IParserFactory>();

            var sw = Stopwatch.StartNew();

            foreach (var crawler in knownCrawlers)
            {
                // TODO Start these assholes asynchronously
                var sw1          = Stopwatch.StartNew();
                var allSiteLinks = await crawler.GetAllSiteLinks();

                Console.WriteLine("Took " + sw1.Elapsed.ToString("c") + " to get all links.");
                sw1.Stop();

                var sw2 = Stopwatch.StartNew();
                foreach (var link in allSiteLinks)
                {
                    Uri result = null;
                    if (Uri.TryCreate(crawler.Base.Domain, link, out result))
                    {
                        if (await crawler.IsRecipeLink(result))
                        {
                            Console.WriteLine("Found a recipe! " + result.AbsoluteUri);
                            recipeCount++;

                            try
                            {
                                var recipe = await parserFactory.CreateParser(result).ParseHtmlAsync(result);

                                if (recipe != null)
                                {
                                    var recipeByTitle = await repo.GetByUrlAsync(result.AbsoluteUri);

                                    if (recipeByTitle == null)
                                    {
                                        var response = await repo.InsertAsync(recipe);

                                        if (response)
                                        {
                                            Console.WriteLine("Recipe was successfully added to repo.");
                                        }
                                        else
                                        {
                                            Console.WriteLine("Could not add recipe for some reason");
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("Recipe already exists.");
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("Found a problem with recipe " + result.AbsoluteUri + ": " + e.Message);
                            }
                        }
                    }
                }
                Console.WriteLine("Took " + sw2.Elapsed.ToString("c") + " to check recipe links.");
                sw2.Stop();
            }

            sw.Stop();

            Console.WriteLine("All in all, found " + recipeCount + " recipes.");
            Console.WriteLine("Took " + sw.Elapsed.ToString("c") + ".");

            Console.ReadLine();
        }