Exemple #1
0
        /// <summary>
        /// Executes this job.
        /// </summary>
        public void Execute()
        {
            //TODO: Duplicate code, see QuotationController.UpdateQuotation

            Status = ScheduledJobStatus.Running;

            try
            {
                foreach (var stock in _queryDispatcher.Execute(new StockAllQuery()))
                {
                    var latestUpdate = stock.Quotations != null && stock.Quotations.Any() ? stock.Quotations.Max(q => q.Changed) : DateTime.MinValue;

                    var quotations = _quotationServiceClient.Get(stock.Id, latestUpdate.Date).ToList();

                    if (quotations.Any())
                    {
                        var cmd = new StockQuotationsAddOrChangeCommand(
                            stock.Id,
                            stock.OriginalVersion,
                            quotations);

                        _commandDispatcher.Execute(cmd);
                    }
                }
            }
            finally
            {
                Status = ScheduledJobStatus.Stopped;
            }
        }
        public void Start()
        {
            //Import
            LoggingService.Info("Download Quotations ");

            var stocks = QueryDispatcher.Execute(new StockAllQuery());

            foreach (var stock in stocks)
            {
                var quotationsBefore = QueryDispatcher.Execute(new StockQuotationsCountByIdQuery(stock.Id));

                var quotations = new QuotationServiceClient(QueryDispatcher, new StockQuoteExternalService(LoggingService)).Get(stock.Id).ToList();

                if (!quotations.Any())
                {
                    LoggingService.Info($"No quotations for stock {stock.Name} imported (Qty Before: {quotationsBefore})");
                    continue;
                }

                var cmd = new StockQuotationsAddOrChangeCommand(
                    stock.Id,
                    stock.OriginalVersion,
                    quotations);

                CommandDispatcher.Execute(cmd);

                //Statistics
                var existentQuotations = QueryDispatcher.Execute(new StockQuotationsByIdQuery(stock.Id)).Count();
                var diff = existentQuotations - quotationsBefore;

                LoggingService.Info($"{diff} Quotation(s) for stock {stock.Name} imported (Qty After: {existentQuotations},Qty Before: {quotationsBefore})");
            }
        }
Exemple #3
0
        /// <summary>
        /// Downloads the quotes.
        /// </summary>
        /// <param name="stockId">The stock identifier.</param>
        private void DownloadQuotes(Guid stockId)
        {
            var quotes = _quoteServiceClient.Get(stockId).ToList();

            if (quotes.Any())
            {
                var cmd = new StockQuotationsAddOrChangeCommand(stockId, 0, quotes);
                _commandDispatcher.Execute(cmd);
            }
        }
        // GET: Stock/UpdateQuotation/5
        public IActionResult UpdateQuotation(Guid id)
        {
            //TODO: Duplicate code, see QuotationController.UpdateQuotation
            var stock = _queryDispatcher.Execute(new StockByIdQuery(id));

            if (stock == null)
            {
                return(PartialView("DisplayTemplates/UpdateStatus", new UpdateQuotationStatusViewModel()
                {
                    Message = Resources.NoSuchStock,
                    Successfull = false
                }));
            }

            //Quotations before
            var quotationsBefore = _queryDispatcher.Execute(new StockQuotationsCountByIdQuery(id));

            var quotations = _quotationServiceClient.Get(stock.Id).ToList();

            if (quotations.Any())
            {
                var cmd = new StockQuotationsAddOrChangeCommand(
                    stock.Id,
                    stock.OriginalVersion,
                    quotations);

                _commandDispatcher.Execute(cmd);
            }

            //Statistics
            var existentQuotations = _queryDispatcher.Execute(new StockQuotationsCountByIdQuery(id));

            return(PartialView("DisplayTemplates/UpdateStatus", new UpdateQuotationStatusViewModel()
            {
                Id = stock.Id,
                Message = string.Format(Resources.StatusQuotations, existentQuotations, existentQuotations - quotationsBefore),
                Successfull = quotations.Any()
            }));
        }
        public void Start()
        {
            const string queryString = "SELECT [Date],[High],[Close],[Open],[Low],[Stock_ID],[Changed] FROM [dbo].[Quotations] ORDER BY [Date] ASC";
            const string countString = "SELECT COUNT([Stock_ID]) AS COUNT FROM [dbo].[Quotations]";

            //Load from db
            using (var connection = new SqlConnection(SourceConnectionString))
            {
                connection.Open();

                using (var command = new SqlCommand(countString, connection))
                {
                    var count = (int)command.ExecuteScalar();
                    LoggingService.Info($" ({count})");
                }

                using (var command = new SqlCommand(queryString, connection))
                {
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var item = new Quotation(
                                DateTime.Parse(reader["Date"].ToString()),
                                DateTime.Parse(reader["Changed"].ToString()),
                                decimal.Parse(reader["Open"].ToString()),
                                decimal.Parse(reader["Close"].ToString()),
                                decimal.Parse(reader["High"].ToString()),
                                decimal.Parse(reader["Low"].ToString()));

                            //Stock_id
                            var stockId = int.Parse(reader["Stock_ID"].ToString());

                            if (!Items.ContainsKey(stockId))
                            {
                                Items.Add(int.Parse(reader["Stock_ID"].ToString()), new List <Quotation>());
                            }

                            Items[stockId].Add(item);
                        }
                    }
                }
            }

            //Import
            LoggingService.Info("Quotations ");

            foreach (var item in Items)
            {
                var stockOld = StockItems.FirstOrDefault(s => s.OldId == item.Key);

                if (stockOld == null || stockOld.IsDividend)
                {
                    continue;
                }

                var stock = QueryDispatcher.Execute(new StockByIdQuery(stockOld.Id));

                var version = stock.OriginalVersion;

                var cmd = new StockQuotationsAddOrChangeCommand(
                    stockOld.Id,
                    version,
                    item.Value);

                CommandDispatcher.Execute(cmd);

                LoggingService.Info($"{item.Value.Count} Quotation(s) for stock {stock.Name} ({stockOld.OldId})");
            }
        }