Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //Create dependecies
            var _elastic          = new ElasticContext();
            var iishelper         = new IISLogHelper();
            var receiver          = new RabbitReceiver();
            var pipeline          = new MessagePipeline(_elastic, iishelper);
            var correlationEngine = new CorrelationEngine(_elastic);

            //Init
            _elastic.Init();
            Console.WriteLine("init");
            Console.ReadKey();
            // pipeline.OnReceiveMessage_Pipeline("IISConnector:2021-05-10 18:13:04 192.168.0.103 GET /Home/Bad1avLogin - 80 - 192.168.0.116 200 0 0 3");
            //  pipeline.OnReceiveMessage_Pipeline("IISConnector:2021-05-11 18:13:04 192.168.0.103 GET /Home/Bad3avLogin - 80 - 192.168.0.116 200 0 0 3");
            // pipeline.OnReceiveMessage_Pipeline("IISConnector:2021-05-12 18:13:04 192.168.0.103 GET /Home/Bad4avLogin - 80 - 192.168.0.116 200 0 0 3");

            //Start proccesses
            Task.Run(() => correlationEngine.Start());
            //receiver.onMessageReceived += pipeline.OnReceiveMessage_Pipeline;
            // receiver.onMessageReceived += Receiver_onMessageReceived_InFile;
            // receiver.onMessageReceived += Receiver_onMessageReceived_InConsole;
            //receiver.Receive();
            Console.ReadKey();
        }
 public async Task <IActionResult> Update(Product product)
 {
     using (ElasticContext dbContext = new ElasticContext())
     {
         if (product.Id == 0)
         {
             var newProduct = new Product();
             newProduct.Name       = product.Name;
             newProduct.SeriNumber = product.SeriNumber;
             newProduct.Price      = product.Price;
             newProduct.Count      = product.Count;
             await dbContext.Products.AddAsync(newProduct);
         }
         else
         {
             var updateProduct = dbContext.Products.FirstOrDefault(prod => prod.Id == product.Id);
             updateProduct.Name       = product.Name;
             updateProduct.SeriNumber = product.SeriNumber;
             updateProduct.Price      = product.Price;
             updateProduct.Count      = product.Count;
         }
         dbContext.SaveChanges();
     }
     return(RedirectToAction("Index"));
 }
        public void ExecuteRethrowsAggregateException()
        {
            var context = new ElasticContext(connection, mapping, log, new ThrowsRetryPolicy());
            var query   = context.Query <Sample2>();

            Assert.Throws <NotImplementedException>(() => query.Provider.Execute(query.Expression));
        }
 public IActionResult Index()
 {
     using (ElasticContext dbContext = new ElasticContext())
     {
         var data = dbContext.Products.ToList();
         return(View(data));
     }
 }
Exemplo n.º 5
0
        public DataContext(string connection, string userName = null, string password = null)
        {
            var client = new ElasticClient(new ConnectionSettings(new Uri(connection)));

            var linqClient = new ElasticContext(new ElasticConnection(new Uri(connection), userName, password));

            _facade = new ElasticFacade(linqClient, client);
        }
Exemplo n.º 6
0
 public virtual TElastic GetElastic(object id)
 {
     using (var context = new ElasticContext(ElasticConnectionString, Config))
     {
         var elastic = context.GetDocument<TElastic>(id);
         return elastic;
     }
 }
Exemplo n.º 7
0
 public virtual bool IsExistElastic(object id)
 {
     using (var context = new ElasticContext(ElasticConnectionString, Config))
     {
         bool isExist = context.DocumentExists<TElastic>(id);
         return isExist;
     }
 }
Exemplo n.º 8
0
        public static void DefaultValuesFromConstructorAreProvided()
        {
            var context = new ElasticContext(connection);

            Assert.IsType<TrivialElasticMapping>(context.Mapping);
            Assert.Same(NullLog.Instance, context.Log);
            var policy = Assert.IsType<RetryPolicy>(context.RetryPolicy);
            Assert.Same(Delay.Instance, policy.Delay);
            Assert.Equal(100, policy.InitialRetryMilliseconds);
            Assert.Same(NullLog.Instance, policy.Log);
            Assert.Equal(10, policy.MaxAttempts);
        }
Exemplo n.º 9
0
        public static void QueryPropertyReturnsElasticQueryWithConnectionAndMapping()
        {
            var context = new ElasticContext(connection);

            var query = context.Query<Sample>();

            Assert.NotNull(query);
            Assert.IsType<ElasticQueryProvider>(query.Provider);
            var elasticProvider = (ElasticQueryProvider)query.Provider;
            Assert.Same(context.Connection, elasticProvider.Connection);
            Assert.Same(context.Mapping, elasticProvider.Mapping);
        }
Exemplo n.º 10
0
 public virtual bool IsExistElasticMap()
 {
     using (var context = new ElasticContext(ElasticConnectionString, Config))
     {
         if (!EnvironmentHelper.IsProduction())
         {
             context.TraceProvider = new ConsoleTraceProvider();
         }
         IsExistElasticIndex = context.IndexExists<TElastic>();
         return IsExistElasticIndex == true;
     }
 }
Exemplo n.º 11
0
        public static void ConstructorSetsPropertiesFromParameters()
        {
            var mapping = Substitute.For<IElasticMapping>();
            var log = Substitute.For<ILog>();
            var retryPolicy = Substitute.For<IRetryPolicy>();

            var context = new ElasticContext(connection, mapping, log, retryPolicy);

            Assert.Same(connection, context.Connection);
            Assert.Same(mapping, context.Mapping);
            Assert.Same(log, context.Log);
            Assert.Same(retryPolicy, context.RetryPolicy);
        }
Exemplo n.º 12
0
        public void ConstructorSetsPropertiesFromParameters()
        {
            var mapping     = Substitute.For <IElasticMapping>();
            var log         = Substitute.For <ILog>();
            var retryPolicy = Substitute.For <IRetryPolicy>();

            var context = new ElasticContext(connection, mapping, log, retryPolicy);

            Assert.Same(connection, context.Connection);
            Assert.Same(mapping, context.Mapping);
            Assert.Same(log, context.Log);
            Assert.Same(retryPolicy, context.RetryPolicy);
        }
Exemplo n.º 13
0
        public ElasticSearchService(IOptions <ElasticSearchSettings> elasticSearchSettings)
        {
            this.elasticSearchSettings = elasticSearchSettings;

            indexName = string.Format("{0}_{1}", elasticSearchSettings.Value.IndexName,
                                      DateTime.Now.ToString("yyyyMMddHHss"));

            aliasName = elasticSearchSettings.Value.IndexName;

            elasticClient = new ElasticClient(ElasticHelper.Instance.GetConnectionSettings(elasticSearchSettings.Value.URI));

            elasticContext = new ElasticContext <T>(elasticClient);
        }
Exemplo n.º 14
0
        public void QueryPropertyReturnsElasticQueryWithConnectionAndMapping()
        {
            var context = new ElasticContext(connection);

            var query = context.Query <Sample>();

            Assert.NotNull(query);
            Assert.IsType <ElasticQueryProvider>(query.Provider);
            var elasticProvider = (ElasticQueryProvider)query.Provider;

            Assert.Same(context.Connection, elasticProvider.Connection);
            Assert.Same(context.Mapping, elasticProvider.Mapping);
        }
Exemplo n.º 15
0
        public virtual void InitElasticMap()
        {
            if (IsExistElasticMap()) return;

            using (var context = new ElasticContext(ElasticConnectionString, Config))
            {
                if (!EnvironmentHelper.IsProduction())
                {
                    context.TraceProvider = new ConsoleTraceProvider();
                }
                context.IndexCreate<TElastic>();
            }
        }
Exemplo n.º 16
0
        public void DefaultValuesFromConstructorAreProvided()
        {
            var context = new ElasticContext(connection);

            Assert.IsType <TrivialElasticMapping>(context.Mapping);
            Assert.Same(NullLog.Instance, context.Log);
            var policy = Assert.IsType <RetryPolicy>(context.RetryPolicy);

            Assert.Same(Delay.Instance, policy.Delay);
            Assert.Equal(100, policy.InitialRetryMilliseconds);
            Assert.Same(NullLog.Instance, policy.Log);
            Assert.Equal(10, policy.MaxAttempts);
        }
Exemplo n.º 17
0
        public virtual IEnumerable<TElastic> GetElastic(string fieldName, List<object> listValue)
        {
            if (string.IsNullOrWhiteSpace(fieldName))
            {
                throw new ArgumentException($"{nameof(fieldName)} is null or white space.");
            }

            listValue = listValue?.Where(x => x != null).ToList();

            if (listValue == null || !listValue.Any())
            {
                throw new ArgumentException($"{nameof(listValue)} is null or empty.");
            }

            fieldName = fieldName.ToLower();

            var search = new Search
            {
                Query = new Query(
                    new Filtered(
                        new Filter(
                            // filter by value
                            new TermsFilter(fieldName, listValue.ToList())))
                    {
                        Query = new Query(new MatchAllQuery())
                    })
            };

            var listElastic = new List<TElastic>();

            using (var context = new ElasticContext(ElasticConnectionString, Config))
            {
                var scanScrollConfig =
                    new ScanAndScrollConfiguration(new TimeUnitMinute(1), Constants.ElasticSearch.MaxTakeRecord);
                var searchResult = context.SearchCreateScanAndScroll<TElastic>(search, scanScrollConfig);
                var scrollId = searchResult.PayloadResult?.ScrollId;

                var total = searchResult.PayloadResult?.Hits?.Total ?? 0;
                var processedResults = 0;
                while (total > processedResults)
                {
                    var resultCollection = context.SearchScanAndScroll<TElastic>(scrollId, scanScrollConfig);
                    scrollId = resultCollection.PayloadResult?.ScrollId;

                    listElastic.AddRange(resultCollection.PayloadResult?.Hits?.HitsResult?.Select(x => x.Source));
                    processedResults = listElastic.Count;
                }

                return listElastic;
            }
        }
Exemplo n.º 18
0
        static void Main(string[] args)
        {
            //appconfig'den veri okumaya yarıyor. Appconfig içinde eklenen keylerde değişiklik yapıldığında tekrar projede herhangi bir kod satırını değiştirmeden burdan value okunabilir. ConfigurationManager sınıfının AppSettings metodundan girilen key değeri ile value döndürülebilir.
            string indexName = string.Format("{0}_{1}", ConfigurationManager.AppSettings["ElasticSearchIndexName"], DateTime.Now.ToString("yyyyMMddHHss"));

            string aliasName = ConfigurationManager.AppSettings["ElasticSearchIndexName"];

            var elasticClient = new ElasticClient(ElasticHelper.Instance.GetConnectionSettings());

            var elasticContext = new ElasticContext(elasticClient);

            CreateIndex(elasticContext, indexName, aliasName);

            Console.ReadKey();
        }
Exemplo n.º 19
0
        public virtual void AddUpdateElastic(IEnumerable<TElastic> listElastic)
        {
            using (var context = new ElasticContext(ElasticConnectionString, Config))
            {
                if (!EnvironmentHelper.IsProduction())
                {
                    context.TraceProvider = new ConsoleTraceProvider();
                }

                foreach (var elastic in listElastic)
                    context.AddUpdateDocument(elastic, elastic.Id);

                context.SaveChanges();
            }
        }
Exemplo n.º 20
0
        public virtual void DeleteElastic(IEnumerable<int> listElasticId)
        {
            using (var context = new ElasticContext(ElasticConnectionString, Config))
            {
                if (!EnvironmentHelper.IsProduction())
                {
                    context.TraceProvider = new ConsoleTraceProvider();
                }

                foreach (var elasticId in listElasticId)
                {
                    context.DeleteDocument<TElastic>(elasticId);
                }

                context.SaveChanges();
            }
        }
        public IActionResult Detail(int?ID)
        {
            Product product = null;

            if (ID == null)
            {
                product = new Product();
            }
            else
            {
                using (ElasticContext dbContext = new ElasticContext())
                {
                    product = dbContext.Products.FirstOrDefault(prod => prod.Id == ID);
                }
            }
            return(View(product));
        }
Exemplo n.º 22
0
        public virtual IEnumerable<TElastic> GetElastic(out int total, int skip, int take = Constants.ElasticSearch.MaxTakeRecord)
        {
            var search = new Search
            {
                Query = new Query(new MatchAllQuery()),
                From = skip,
                Size = take
            };

            using (var context = new ElasticContext(ElasticConnectionString, Config))
            {
                var searchResult = context.Search<TElastic>(search);
                var listElastic = searchResult.PayloadResult?.Hits?.HitsResult?.Select(x => x.Source);
                total = searchResult.PayloadResult?.Hits?.Total ?? 0;
                return listElastic;
            }
        }
Exemplo n.º 23
0
        public void DeleteElasticMap()
        {
            using (var context = new ElasticContext(ElasticConnectionString, Config))
            {
                if (!EnvironmentHelper.IsProduction())
                {
                    context.TraceProvider = new ConsoleTraceProvider();
                }

                context.AllowDeleteForIndex = true;

                if (!IsExistElasticMap())
                {
                    return;
                }

                context.DeleteIndex<TElastic>();
                IsExistElasticIndex = false;
            }
        }
Exemplo n.º 24
0
        static void Main(string[] args)
        {
            var connection = new FakeConnection(new Uri("http://localhost:9200"));

            IEnumerable <int> r = new[] { 1, 2, 3 };
            int v = 2;

            var context = new ElasticContext(connection);

            var queryable = context
                            .Query <tata>("toto", "tata")
                            .Query(x => x
                                   .Where(y => y.Int == 2)
                                   .Query(y => y
                                          .Where(z => z.String == "tutu")
                                          )
                                   )
                            .Where(y => true && y.Int == 2 || false)
                            .Where(y => y.Int == 2)
                            .Where(z => z.String == "tutu")
                            .Where(z => z.Array.Contains(1) || r.Contains(z.Int))
                            //.Where(z => z.Array.ContainsAll(r))
                            .Where(z => z.Array.ContainsAll(1, 2, 3))
                            .OrderBy(x => x.Int)
                            .Where(x => x.Int > 4 && x.Int > 3)
                            .Where(x => x.Int.Exists())
                            .Where(x => x.String.Missing())
                            .Where(x => x.Tutu.Double.Equals(42))
                            .Where(x => x.Tutu.Double > 2 + Settings.A + 4)
                            .Where(x => (x.Int < 2 || x.String == "tutu") == false)
                            .Where(x => x.Int.Exists() == true)
                            //.Where(x => bbb() == bbb())
                            //.Where(x => !bbb())
                            //.Where(x => true)
                            .Where(x => x.Int.Missing() == false)
                            .Where(x => !!!x.String.Missing())
                            .Skip(0)
                            .Take(10)
                            .ToList();
        }
Exemplo n.º 25
0
        private static async Task Scheduler()
        {
            try
            {
                NameValueCollection props = new NameValueCollection
                {
                    { "quartz.serializer.type", "binary" },
                    { "quartz.threadPool.threadCount", "20" }
                };
                StdSchedulerFactory factory   = new StdSchedulerFactory(props);
                IScheduler          scheduler = await factory.GetScheduler();

                List <Queries> list = new List <Queries>();
                using (ElasticContext ctx = new ElasticContext())
                {
                    list = ctx.Queries.Where(x => x.Active == 1).ToList();
                    foreach (var item in list)
                    {
                        IJobDetail job = JobBuilder.Create <AddQueryToQueueJob>().
                                         WithIdentity(item.Id.ToString())
                                         .Build();
                        job.JobDataMap["query"] = item;


                        ITrigger trigger = TriggerBuilder.Create()

                                           .WithIdentity(item.Id.ToString())
                                           .WithSimpleSchedule(x => x.WithIntervalInMinutes(item.MinutePeriod).RepeatForever())
                                           .Build();
                        Console.WriteLine("Dodano: " + item.IndexName);
                        await scheduler.ScheduleJob(job, trigger);
                    }
                }
                await scheduler.Start();
            }
            catch (SchedulerException se)
            {
                Console.WriteLine(se);
            }
        }
Exemplo n.º 26
0
 public CorrelationEngine(ElasticContext elastic)
 {
     _rules   = new List <Rule>();
     _elastic = elastic;
 }
Exemplo n.º 27
0
        private static void CreateIndex(ElasticContext elasticContext, string indexName, string aliasName)
        {
            var response = elasticContext.CreateIndex <Product>(indexName, aliasName);

            Console.WriteLine(response.StatusMessage);
        }
Exemplo n.º 28
0
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("1. Zobacz wszystkie zapytania");
                Console.WriteLine("2. Dodaj nowe zapytanie");
                Console.WriteLine("3. Wykonaj testowo zapytanie");
                int choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                case 1:
                    using (var ctx = new ElasticContext())
                    {
                        foreach (var item in ctx.Queries.ToList())
                        {
                            Console.WriteLine(item.Query);
                        }
                    }
                    break;

                case 2:
                    int    minutePeriod;
                    string Query, IndexName;
                    bool   Active;
                    Console.WriteLine("Podaj nazwe indeksu pod jakim będzie zapisywane zapytanie\n");
                    IndexName = Console.ReadLine().ToLower();
                    Console.WriteLine("Podaj tresc zapytania ktore zrobi tabele cache - select ID,X,Y into ElasticCache.elastic_cache from X\n");
                    Query = Console.ReadLine();
                    Console.WriteLine("Czy na starcie zapytanie ma byc aktywne? 0/1");
                    int z = int.Parse(Console.ReadLine());
                    if (z == 1)
                    {
                        Active = true;
                    }
                    else
                    {
                        Active = false;
                    }
                    Console.WriteLine("Podaj czas w minutach (bez liter) co ile zapytanie ma sie wykonywac");
                    minutePeriod = int.Parse(Console.ReadLine());
                    using (var ctx = new ElasticContext())
                    {
                        ctx.Queries.Add(new Queries()
                        {
                            Active       = Active ? (byte)0x1 : (byte)0x0,
                            Query        = Query,
                            IndexName    = IndexName,
                            MinutePeriod = minutePeriod
                        });
                        ctx.SaveChanges();
                    }
                    break;

                case 3:
                    using (var ctx = new ElasticContext())
                    {
                        int index = 0;
                        foreach (var item in ctx.Queries.ToList())
                        {
                            Console.WriteLine(index + " -> " + item.IndexName);
                            index++;
                        }
                        int w     = int.Parse(Console.ReadLine());
                        var query = ctx.Queries.ToList().ElementAt(w);
                        ElasticController.Instance.StartImportToElastic(query.IndexName, query.Query);
                    }
                    break;
                }
                Console.ReadKey();
                Console.Clear();
            }
        }
Exemplo n.º 29
0
 internal ElasticFacade(ElasticContext linqClient, ElasticClient client)
 {
     LinqClient = linqClient;
     Client     = client;
 }
Exemplo n.º 30
0
 internal ElasticFacade(ElasticContext linqClient, ElasticClient client)
 {
     LinqClient = linqClient;
     Client = client;
 }