Esempio n. 1
0
        private void ConfigureDependencies(IServiceCollection services)
        {
            //see: https://github.com/aspnet/Hosting/issues/793
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.AddSingleton(sp => sp.GetService <IOptions <SiteConfig> >().Value);
            services.AddTransient(sp => ElasticClientFactory.GetClient(sp));

            // Add DependencyAttribute (ScopedDependency, SingletonDependency, TransientDependency) to the class in order to be scanned
            services.ConfigureAttributedDependencies();

            {
                var options = ConfigurationOptions.Parse(Configuration["SlambyApi:Redis:Configuration"]);
                //HACK: https://github.com/dotnet/corefx/issues/8768
                //this should be removed when https://github.com/dotnet/corefx/issues/11564 is closed
                options = RedisDnsHelper.CorrectOption(options);
                if (options == null)
                {
                    Log.Logger.Error("Can't resolve the name of the Redis server!");
                    return;
                }
                services.AddSingleton(RedisDnsHelper.CorrectOption(options));
                services.AddSingleton <ConnectionMultiplexer>(sp => ConnectionMultiplexer.Connect(sp.GetService <ConfigurationOptions>()));
            }
        }
Esempio n. 2
0
        public void Configure(IWebJobsBuilder builder)
        {
            string environment = Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT");

            builder.Services.AddSingleton(_ => ElasticClientFactory.Create());
            builder.Services.AddSingleton(_ => DocumentClientFactory.Create());
            builder.Services.AddSingleton <IAttemptService, AttemptService>();
            builder.Services.AddSingleton <IZoneService, ZoneService>();
            builder.Services.AddSingleton <IToyDistributionProblemRepository, ToyDistributionProblemRepository>();

            if (!string.Equals(environment, "Development", StringComparison.OrdinalIgnoreCase))
            {
                builder.Services.AddLogging(loggingBuilder => loggingBuilder
                                            .AddSerilog(new LoggerConfiguration()
                                                        .WriteTo
                                                        .Elasticsearch(new ElasticsearchSinkOptions(ElasticClientFactory.Url)
                {
                    AutoRegisterTemplate        = true,
                    AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
                    MinimumLogEventLevel        = LogEventLevel.Information,
                    ModifyConnectionSettings    = connection =>
                    {
                        connection.BasicAuthentication(ElasticClientFactory.Username, ElasticClientFactory.Password);

                        return(connection);
                    }
                })
                                                        .CreateLogger()));
            }
        }
            protected override void When()
            {
                var conf = new ElasticClientConfiguration(new Uri(ElasticUrl), ElasticUsername, ElasticPassword);

                _factory = conf.CreateClientFactory() as ElasticClientFactory;
                _factory.CreateClient();
            }
        public static void GetInstance_Should_Return_Write_Instance()
        {
            // arrange
            ElasticsearchOptions options = new ElasticsearchOptions
            {
                DefaultIndexName = "index-d",
                DefaultTypeName  = "type-d",
                Environment      = "my-env",
                MaximumRetries   = 4,
                ReadUrl          = "http://esread:9200",
                WriteUrl         = "http://eswrite:9200",
                TimeoutInSeconds = 30
            };

            // act
            var client = ElasticClientFactory.GetInstance(ConnectionMode.Write, options);

            // assert
            Assert.Equal("type-d", client.ConnectionSettings.DefaultTypeName);
            Assert.Equal("index-d", client.ConnectionSettings.DefaultIndex);
            Assert.Equal(30, client.ConnectionSettings.RequestTimeout.TotalSeconds);
            Assert.Equal(4, client.ConnectionSettings.MaxRetries);
            Assert.Equal("my-env", client.ConnectionSettings.Headers.Get("Environment"));
            Assert.Equal("http://eswrite:9200/", client.ConnectionSettings.ConnectionPool.Nodes.First().Uri.AbsoluteUri);
        }
Esempio n. 5
0
 protected BaseElasticsearch(
     ConnectionMode mode,
     ElasticsearchOptions options,
     Func <string, T, string> generateIndexName)
 {
     this.ElasticClient     = ElasticClientFactory.GetInstance(mode, options);
     this.GenerateIndexName = generateIndexName;
     this.Options           = options;
 }
        public static void GetInstance_Should_Throws_Exception_When_Options_Is_Null()
        {
            // arrange
            ElasticsearchOptions options = null;

            // act & assert
            var exception = Assert.Throws <ArgumentNullException>(() =>
                                                                  ElasticClientFactory.GetInstance(ConnectionMode.Write, options));
        }
        public static IClientFactory GetClientFactory()
        {
            var connectionFactory = ConnectionSettingsFactoryHelper.GetHTTPConnection();
            var connectionPool    = ConnectionSettingsFactoryHelper.GetSingleNodePool();
            var settingsProvider  = new ConnectionSettingsProvider(connectionFactory, connectionPool);
            var connectionManager = new ConnectionManager(settingsProvider);
            var clientFactory     = new ElasticClientFactory(connectionManager);

            return(clientFactory);
        }
Esempio n. 8
0
        /// <summary>
        /// Configures the identity builder with ElasticIdentity components,
        /// and registers dependencies for that component which have not already been
        /// registered.
        /// </summary>
        /// <param name="serverName">
        /// The server:port combination which the <see cref="ElasticUserStore"/> will connect to. E.g. localhost:9200
        /// </param>
        /// <param name="indexName">
        /// The index which will contain the user information.
        /// </param>
        public static IdentityBuilder AddElasticIdentity(
            this IdentityBuilder identityBuilder,
            string serverName,
            string indexName = DEFAULT_INDEX_NAME)
        {
            var node = new Uri("http://" + serverName.Replace("http://", ""));

            IElasticClient elasticClient = ElasticClientFactory.Create(node, indexName);

            return(AddElasticIdentity(identityBuilder, elasticClient));
        }
        public DataSetService(ServiceQuery serviceQuery, IQueryFactory queryFactory,
                              IndexQuery indexQuery, ElasticClientFactory clientFactory,
                              ILoggerFactory loggerFactory, IGlobalStoreManager globalStore)
        {
            GlobalStore        = globalStore;
            this.clientFactory = clientFactory;
            this.indexQuery    = indexQuery;
            this.queryFactory  = queryFactory;
            this.serviceQuery  = serviceQuery;

            this.logger = loggerFactory.CreateLogger <DataSetService>();
        }
Esempio n. 10
0
 public DBUpdateService(ElasticClientFactory clientFactory, IndexQuery indexQuery, IQueryFactory queryFactory,
                        ILoggerFactory loggerFactory, MetadataQuery metadataQuery, ServiceQuery serviceQuery, ProcessQuery processQuery,
                        ServiceManager serviceManager)
 {
     this.serviceManager = serviceManager;
     this.processQuery   = processQuery;
     this.serviceQuery   = serviceQuery;
     this.metadataQuery  = metadataQuery;
     this.queryFactory   = queryFactory;
     this.indexQuery     = indexQuery;
     this.clientFactory  = clientFactory;
     this.logger         = loggerFactory.CreateLogger <DBUpdateService>();
 }
Esempio n. 11
0
 public StartupService(SiteConfig siteConfig, ILogger <StartupService> logger,
                       ElasticClientFactory elasticClientFactory, DataSetService dataSetService,
                       DBUpdateService dbUpdateService, MachineResourceService machineResourceService,
                       ServiceManager serviceManager, ILicenseManager licenseManager)
 {
     this.licenseManager         = licenseManager;
     this.serviceManager         = serviceManager;
     this.dbUpdateService        = dbUpdateService;
     this.dataSetService         = dataSetService;
     this.logger                 = logger;
     this.elasticClientFactory   = elasticClientFactory;
     this.siteConfig             = siteConfig;
     this.machineResourceService = machineResourceService;
 }
Esempio n. 12
0
        public ElasticUserStoreTest()
        {
            _indexName = $"{nameof(ElasticUserStoreTest)}_{Guid.NewGuid()}".ToLowerInvariant();
            var node = new Uri("http://localhost:9200");

            _elasticClient = ElasticClientFactory.Create(node, _indexName);

            _store = new ElasticUserStore(_elasticClient);

            // the generic store refreshes immediately
            var store = new ElasticUserStore <string, ElasticIdentityUser>(_elasticClient);

            store.CreateAsync(_user1, CancellationToken.None).GetAwaiter().GetResult();

            _elasticClient.Indices.Refresh(_indexName);
        }
Esempio n. 13
0
        public void Foo()
        {
            var elasticClient = ElasticClientFactory.ElasticClient();

            elasticClient.Index(
                new Employee
            {
                FirstName = "John",
                LastName  = "Smith",
                Age       = 25,
                About     = "I love to go rock climbing",
                Interests = new[] { "sports", "music" }
            }, d => d.Index("megacorp").Type("employee"));


            elasticClient.Get <Employee>(1);
        }
        public async Task Can_join_channel()
        {
            ////Arrange
            var client = ElasticClientFactory.Create();
            var user   = TestExtensions.RandomUser();

            await new UserService().EnsureUser(user);

            ////Act
            await new ChannelService().Join(user, "global");

            ////Assert
            await client.Indices.RefreshAsync(Indices.Index <UserAccount>());

            var userAccount = await new UserService().FindById(user.UserId());

            Assert.IsTrue(userAccount.Channels.Contains("global"));
        }
        protected IntegrationTestsBase()
        {
            Fixture = new Fixture();

            var server   = GetSettingsValue("ElasticSearch.ReadModelNodeList", "http://localhost:9200");
            var username = GetSettingsValue("ElasticSearch.UserName", "");
            var password = GetSettingsValue("ElasticSearch.Password", "");

            ElasticClient = ElasticClientFactory.CreateElasticClient(server, username, password);

            if (ElasticClient.Indices.Exists(IndexName).Exists)
            {
                ElasticClient.Indices.Delete(IndexName);
            }

            ElasticClient.Indices.Create(IndexName, d => d.Settings(descriptor => descriptor).Map(m => m.AutoMap <T>()));

            Sut = new ElasticQueryable <T>(ElasticClient, IndexName);
        }
Esempio n. 16
0
        private void AddClassesToIoC(IServiceCollection services)
        {
            services.AddSingleton <IFileReader, FileReader>();
            services.AddSingleton <IDeSerializer <Account>, AccountDeSerializer>();
            services.AddSingleton <IDeSerializer <Transaction>, TransactionDeSerializer>();

            var client = new ElasticClientFactory(Configuration).CreateElasticClient();

            services.AddSingleton(client);
            services.AddSingleton <IndexCreator <Account> >();
            services.AddSingleton <IndexCreator <Transaction> >();
            services.AddSingleton <Importer <Account> >();
            services.AddSingleton <Importer <Transaction> >();

            services.AddSingleton <IAccountQueryCreator, AccountQueryCreator>();
            services.AddSingleton <ITransactionQueryCreator, TransactionQueryCreator>();

            services.AddSingleton <IAccountSearcher>(p => new AccountSearcher(client,
                                                                              p.GetService <IAccountQueryCreator>(), _accountIndex));
            services.AddSingleton <ITransactionSearcher>(p => new TransactionSearcher(client,
                                                                                      p.GetService <ITransactionQueryCreator>(), _transactionIndex));
        }
        public async Task Can_join_private_channel()
        {
            ////Arrange
            var client = ElasticClientFactory.Create();
            var user1  = TestExtensions.RandomUser();
            var user2  = TestExtensions.RandomUser();

            await new UserService().EnsureUser(user1);
            await new UserService().EnsureUser(user2);

            ////Act
            var channel = await new ChannelService().Create(user1, "tester", false, new[] { user1.UserId(), user2.UserId() });

            await new ChannelService().Join(user1, channel.Id);

            ////Assert
            await client.Indices.RefreshAsync(Indices.Index <UserAccount>());

            var userAccount = await new UserService().FindById(user1.UserId());

            Assert.IsTrue(userAccount.Channels.Contains(channel.Id));
        }
        private static void Main(string[] args)
        {
            IConfiguration config        = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
            var            directoryPath = config.GetValue <string>("DirectoryPath");

            var client       = new ElasticClientFactory(config).CreateElasticClient();
            var queryCreator = new QueryCreator();
            var searcher     = new Searcher(client, queryCreator, Index);

            var consoleView = new ConsoleView();
            var controller  = new Controller(consoleView, searcher);

            if (!IsIndexExisting(Index, client))
            {
                var indexCreator = new IndexCreator(client);
                indexCreator.CreateIndex(Index);
                var fileReader = new DocFileReader();
                var docCreator = new DocFactory(fileReader);
                var importer   = new Importer <Doc>(client);
                importer.Import(docCreator.GetAllDocuments(directoryPath), Index);
            }
            controller.Run();
        }
Esempio n. 19
0
 public BaseQuery(ElasticClientFactory elasticClientFactory, string indexName, SiteConfig siteConfig)
 {
     IndexName  = indexName;
     Client     = elasticClientFactory.GetClient(indexName);
     SiteConfig = siteConfig;
 }
Esempio n. 20
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                //TODO: Think about a more appropriate restriction policy
                options.AddPolicy("CorsPolicy",
                                  builder =>
                {
                    builder
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .DisallowCredentials();
                });
            });

            services.AddControllers();

            services.AddAutoMapper(config =>
            {
                config.CreateMap <Book, BookDto>().ReverseMap();
                config.CreateMap <OperationResult, OperationResultDto>().ReverseMap();
            }, typeof(Startup));

            services.AddMediatR(typeof(GetBooksQueryHandler).Assembly);
            services.AddTransient(typeof(IPipelineBehavior <,>), typeof(ValidationBehavior <,>));
            services.AddValidatorsFromAssembly(typeof(UpsertBookValidator).Assembly);


            services.AddTransient <IElasticRepository, ElasticRepository>();
            services.AddTransient <IBookService, BookService>();
            services.AddTransient <IBookRepository, ElasticBookRepository>();
            services.AddTransient <IFileService>(x => new BlobFileService("UseDevelopmentStorage=true"));
            services.AddTransient <IPdfParser, PdfParser>();


            services.AddRabbitMQ(Configuration["RabbitMq:Host"]);
            services.AddTransient <IMessageProducerService, MessageProducerService>();
            services.AddHostedService <BookUpsertSubscriberService>();
            services.AddHostedService <BookUpsertedSubscriberService>();


            var elasticHosts = Configuration.GetSection("Elasticsearch:Hosts")
                               .AsEnumerable()
                               .Where(x => x.Value != null)
                               .Select(x => x.Value);

            services.AddSingleton(x =>
                                  ElasticClientFactory.CreateElasticClient(elasticHosts, Configuration["Elasticsearch:Username"], Configuration["Elasticsearch:Password"]));
            services.AddTransient <IEnumerable <IElasticQuery> >(x => new List <IElasticQuery> {
                new GetBooksQuery()
            });

            services.AddTransient <IEnumerable <IElasticCommand> >(x => new List <IElasticCommand> {
                new UpsertBookCommand()
            });

            services.AddSignalR();


            var redisHosts = Configuration.GetSection("Redis:Hosts")
                             .AsEnumerable()
                             .Where(x => x.Value != null)
                             .Select(x => x.Value);

            services.UseRedisCaching(redisHosts, uint.Parse(Configuration["Redis:Database"]));

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc(
                    "v1",
                    info: new Microsoft.OpenApi.Models.OpenApiInfo()
                {
                    Title       = "Book Store",
                    Version     = "1.0",
                    Description = "Book Store"
                }
                    );
            });
        }
Esempio n. 21
0
 public ServiceQuery(ElasticClientFactory elasticClientFactory, IndexQuery indexQuery, SiteConfig siteConfig) :
     base(elasticClientFactory, Constants.SlambyServicesIndex, siteConfig)
 {
     this.indexQuery           = indexQuery;
     this.elasticClientFactory = elasticClientFactory;
 }
Esempio n. 22
0
        static void Main(string[] args)
        {
            Randomizer.Seed = new Random(3897234);
            var random     = new Bogus.Randomizer();
            var guildNames = new[] { null, "Guild A", "Guild B", "Guild C", "Guild D", "Guild E" };
            var faction    = new [] { "Horde", "Alliance", "Undefined" };
            var race       = new[]
            {
                "Human", "Kaldorei", "Gnome", "Dwarf", "Worgen", "Orc", "Tauren",
                "Forsaken", "Sindorei", "Goblin", "Pandaren"
            };

            var isActive = new[] { true, false };

            var dateInfo = new Faker <DateInfo>()
                           .StrictMode(true)
                           .RuleFor(x => x.CreatedDate, f => f.Date.Recent())
                           .RuleFor(x => x.UpdatedDate, f => f.Date.Recent());

            var story = new Faker <Story>()
                        .StrictMode(true)
                        .RuleFor(x => x.Body, f => f.Lorem.Paragraphs(3))
                        .RuleFor(x => x.Id, f => f.IndexFaker)
                        .RuleFor(x => x.Title, f => f.Lorem.Sentence())
                        .RuleFor(x => x.DateInfo, f => dateInfo.Generate());

            var picture = new Faker <Picture>()
                          .StrictMode(true)
                          .RuleFor(x => x.Id, f => f.IndexFaker)
                          .RuleFor(x => x.Caption, f => f.Lorem.Sentence())
                          .RuleFor(x => x.Url, f => f.Internet.Url());

            var guild = new Faker <Guild>()
                        .StrictMode(true)
                        .RuleFor(x => x.Name, f => f.PickRandom(guildNames))
                        .RuleFor(x => x.Description, f => f.Lorem.Paragraphs(3));

            var character = new Faker <Character>()
                            .StrictMode(true)
                            .RuleFor(x => x.Id, f => f.IndexFaker)
                            .RuleFor(x => x.FirstName, f => f.Name.FirstName())
                            .RuleFor(x => x.LastName, f => f.Name.LastName())
                            .RuleFor(x => x.EyeColor, f => f.Commerce.Color())
                            .RuleFor(x => x.EyeColor, f => f.Commerce.Color())
                            .RuleFor(x => x.Weight, f => f.Lorem.Sentence())
                            .RuleFor(x => x.Height, f => f.Lorem.Sentence())
                            .RuleFor(x => x.SkinColor, f => f.Commerce.Color())
                            .RuleFor(x => x.Race, f => f.PickRandom(race))
                            .RuleFor(x => x.Faction, f => f.PickRandom(faction))
                            .RuleFor(x => x.Background, f => f.Lorem.Paragraphs(7))
                            .RuleFor(x => x.Residence, f => f.Lorem.Word())
                            .RuleFor(x => x.Occupation, f => f.Lorem.Sentence())
                            .RuleFor(x => x.PhysicalAppearance, f => f.Lorem.Paragraphs(2))
                            .RuleFor(x => x.Friends, f => f.Lorem.Words().ToList())
                            .RuleFor(x => x.Stories, f => story.Generate(random.Number(1, 10)).ToList())
                            .RuleFor(x => x.Pictures, f => picture.Generate(random.Number(1, 10)).ToList())
                            .RuleFor(x => x.Guild, f => guild.Generate())
                            .RuleFor(x => x.DateInfo, f => dateInfo.Generate());



            var user = new Faker <User>()
                       .StrictMode(true)
                       .RuleFor(x => x.UserName, f => f.Internet.UserName())
                       .RuleFor(x => x.DateInfo, dateInfo.Generate())
                       .RuleFor(x => x.Email, f => f.Internet.Email())
                       .RuleFor(x => x.Image, f => f.Internet.Url())
                       .RuleFor(x => x.IsActive, f => f.PickRandom(isActive))
                       .RuleFor(x => x.Characters, f => character.Generate(random.Number(1, 10)).ToList());


            var users = user.Generate(1000);
            //string charsJson = JsonConvert.SerializeObject(users);
            ElasticClient client = ElasticClientFactory.GetClient();
            //File.WriteAllText(@"c:\dump\users.txt", charsJson);

            var result = client.Bulk(b => b.IndexMany(users));

            if (!result.IsValid)
            {
                foreach (var item in result.ItemsWithErrors)
                {
                    Console.WriteLine("Failed to index document {0}: {1}", item.Id, item.Error);
                }

                Console.WriteLine(result.DebugInformation);
                Console.Read();
                Environment.Exit(1);
            }

            Console.WriteLine("Done!");
            Console.ReadLine();
        }
Esempio n. 23
0
 public IndexQuery(ElasticClient client, ElasticClientFactory clientFactory, SiteConfig siteConfig) : base(client, siteConfig)
 {
     this.clientFactory = clientFactory;
     this.defaultClient = clientFactory.GetClient();
 }
 protected override void When()
 {
     _factory = new ElasticClientConfiguration(CloudId, ElasticUsername, ElasticPassword)
                .CreateClientFactory() as ElasticClientFactory;
 }
Esempio n. 25
0
 public void SetUp()
 {
     _elasticClient = ElasticClientFactory.CreateClientForTest();
 }
Esempio n. 26
0
 public MetadataQuery(ElasticClientFactory elasticClientFactory, IndexQuery indexQuery, SiteConfig siteConfig) :
     base(elasticClientFactory, Constants.SlambyMetadataIndex, siteConfig)
 {
     this.elasticClientFactory = elasticClientFactory;
     this.indexQuery           = indexQuery;
 }
 public LicenseServerClient(ElasticClientFactory clientFactory, MPService mpService)
 {
     this.clientFactory = clientFactory;
     this.mpService     = mpService;
 }
Esempio n. 28
0
 public ChatService(IElasticClient client = null, UserService userService = null)
 {
     _client      = client ?? ElasticClientFactory.Create();
     _userService = userService ?? new UserService();
 }
 public void SetUp()
 {
     _elasticClient     = ElasticClientFactory.CreateOfflineClient();
     _elasticConnection = new ElasticConnection(_elasticClient, new HangfireElasticSettings());
 }
 protected ElasticRepository()
 {
     _esClient = ElasticClientFactory.Create();
 }