Example #1
0
        public async Task <List <WatcherResponse> > GetAllWatchers(string userId = null, string currencyId = null, string indicatorId = null)
        {
            // Get user
            var user = await _userRepository.GetSingle(userId);

            // Check if it exists
            if (user == null)
            {
                throw new NotFoundException(UserMessage.UserNotFound);
            }

            // Get all watchers
            var userWatchers = await _watcherRepository.GetAll(WatcherExpression.WatcherFilter(userId, currencyId, indicatorId));

            // Get all default watchers
            var defaultWatchers = await _watcherRepository.GetAll(WatcherExpression.DefaultWatcher(currencyId, indicatorId));

            // Build with defaults
            userWatchers = WatcherBuilder.BuildWatchersWithDefaults(userWatchers, defaultWatchers);

            // Response
            var response = _mapper.Map <List <WatcherResponse> >(userWatchers);

            // Return
            return(response);
        }
        public async Task <Responses.Watcher> AddWatcher(AddWatcher request)
        {
            // Get user
            var user = await _userRepository.GetSingle(request.UserId);

            // Throw NotFound if the currency does not exist
            if (user == null)
            {
                throw new NotFoundException(UserMessage.UserNotFound);
            }

            // Get indicator
            var indicator = await _indicatorRepository.GetSingle(request.IndicatorId);

            // Throw NotFound if the currency does not exist
            if (indicator == null)
            {
                throw new NotFoundException(IndicatorMessage.IndicatorNotFound);
            }

            // Check if it exists
            var watcher = await _watcherRepository.GetSingle(WatcherExpression.Watcher(request.UserId, request.CurrencyId, request.IndicatorId));

            // Throw ConflictException if it exists
            if (watcher != null)
            {
                throw new ConflictException(WatcherMessage.WatcherAlreadyExists);
            }

            // Get default watcher
            var defaultWatcher = await _watcherRepository.GetSingle(WatcherExpression.DefaultWatcher(request.CurrencyId, request.IndicatorId));

            // Add
            watcher = new Watcher(
                request.UserId,
                request.CurrencyId,
                request.IndicatorId,
                request.IndicatorType,
                defaultWatcher?.Value,
                request.Buy,
                request.Sell,
                defaultWatcher?.AverageBuy,
                defaultWatcher?.AverageSell,
                request.Enabled,
                DateTime.Now);
            _watcherRepository.Add(watcher);

            // Save
            await _dbContext.SaveChangesAsync();

            // Log into Splunk
            _logger.LogSplunkInformation(request);

            // Response
            var response = _mapper.Map <Responses.Watcher>(watcher);

            // Return
            return(response);
        }
 public static void SyncWatchers(this List <Watcher> watchers, List <Watcher> defaultWatchers)
 {
     // Sync watcher
     foreach (var watcher in watchers)
     {
         var defaultWatcher = defaultWatchers.FirstOrDefault(WatcherExpression.DefaultWatcher(watcher.CurrencyId, watcher.IndicatorId).Compile());
         if (defaultWatcher != null)
         {
             watcher.Sync(defaultWatcher.Value, defaultWatcher.AverageBuy, defaultWatcher.AverageSell, defaultWatcher.Price);
         }
     }
 }
Example #4
0
        public async Task Run()
        {
            try
            {
                // Start watch
                var stopwatch = new Stopwatch();
                stopwatch.Start();

                // Get current lines
                var currentLines = await _lineRepository.GetAll(LineExpression.CurrentLine());

                // Build default watchers
                var newDefaultWatchers = WatcherBuilder.BuildDefaultWatchers(currentLines);

                // Get all default watchers
                var defaultWatchers = await _watcherRepository.GetAll(WatcherExpression.DefaultWatcher());

                // Update
                _watcherRepository.UpdateCollection(defaultWatchers, newDefaultWatchers);

                // Save
                await _mainDbContext.SaveChangesAsync();

                // Stop watch
                stopwatch.Stop();

                // Log into Splunk
                _logger.LogSplunkJob(new
                {
                    newDefaultWatchers.Count,
                    ExecutionTime = stopwatch.Elapsed.TotalSeconds
                });

                // Return
                await Task.CompletedTask;
            }
            catch (Exception ex)
            {
                // Log into Splunk
                _logger.LogSplunkJob(new
                {
                    JobFailed = ex.Message
                });

                // Log error into Splunk
                _logger.LogSplunkError(ex);
            }
        }
        public async Task Run()
        {
            try
            {
                // Start watch
                var stopwatch = new Stopwatch();
                stopwatch.Start();

                // Get all watchers
                var watchers = await _watcherRepository.GetAll();

                // Get all default watchers
                var defaultWatchers = await _watcherRepository.GetAll(WatcherExpression.DefaultWatcher());

                // Sync watchers
                watchers.SyncWatchers(defaultWatchers);

                // Save
                await _mainDbContext.SaveChangesAsync();

                // Stop watch
                stopwatch.Stop();

                // Log into Splunk
                _logger.LogSplunkJob(new
                {
                    watchers.Count,
                    ExecutionTime = stopwatch.Elapsed.TotalSeconds
                });

                // Return
                await Task.CompletedTask;
            }
            catch (Exception ex)
            {
                // Log into Splunk
                _logger.LogSplunkJob(new
                {
                    JobFailed = ex.Message
                });

                // Log error into Splunk
                _logger.LogSplunkError(ex);
            }
        }
        public async Task <List <Watcher> > UpdateDefaultWatchers(List <Line> lines)
        {
            // Return if there are no lines
            if (lines.Count == 0)
            {
                return(new List <Watcher>());
            }

            // Start watch
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            // Get newest time
            var newestTime = lines.Max(x => x.Time);

            // Get current lines
            var currentLines = await _lineRepository.GetAll(LineExpression.CurrentLine(newestTime));

            // Build default watchers
            var newDefaultWatchers = WatcherBuilder.BuildDefaultWatchers(currentLines);

            // Get all default watchers
            var defaultWatchers = await _watcherRepository.GetAll(WatcherExpression.DefaultWatcher());

            // Update
            _watcherRepository.UpdateCollection(defaultWatchers, newDefaultWatchers);

            // Save
            await _dbContext.SaveChangesAsync();

            // Stop watch
            stopwatch.Stop();

            // Log into Splunk
            _logger.LogSplunkInformation("UpdateDefaultWatchers", new
            {
                newDefaultWatchers.Count,
                ExecutionTime = stopwatch.Elapsed.TotalSeconds
            });

            // Return
            return(defaultWatchers);
        }
        public static void SyncWatchers(this List <Watcher> watchers, List <Watcher> defaultWatchers)
        {
            // Sync watcher
            foreach (var watcher in watchers)
            {
                // We skip default watchers
                if (watcher.UserId == "master")
                {
                    continue;
                }

                var currencyId     = watcher.CurrencyId;
                var indicatorId    = watcher.IndicatorId;
                var defaultWatcher = defaultWatchers.FirstOrDefault(WatcherExpression.DefaultWatcher(currencyId, indicatorId).Compile());
                if (defaultWatcher != null)
                {
                    watcher.Sync(defaultWatcher.Value, defaultWatcher.AverageBuy, defaultWatcher.AverageSell);
                }
            }
        }
Example #8
0
        public async Task <Responses.Watcher> AddWatcher(AddWatcher request)
        {
            // Get currency
            var currency = await _mainDbContext.Currencies.FindAsync(request.CurrencyId);

            // Watcher not found
            if (currency == null)
            {
                throw new NotFoundException(CurrencyMessage.CurrencyNotFound);
            }

            // Get user
            var user = await _mainDbContext.Users.FindAsync(request.UserId);

            // User not found
            if (user == null)
            {
                throw new NotFoundException(UserMessage.UserNotFound);
            }

            // Get indicator
            var indicator = await _mainDbContext.Indicators.FindAsync(request.IndicatorId);

            // Indicator not found
            if (indicator == null)
            {
                throw new NotFoundException(IndicatorMessage.IndicatorNotFound);
            }

            // Get watcher
            var watcher = await _mainDbContext.Watchers.FirstOrDefaultAsync(WatcherExpression.Unique(request.UserId, request.CurrencyId, indicator.UserId, indicator.IndicatorId));

            // Watcher already exists
            if (watcher != null)
            {
                throw new ConflictException(new Conflict <AddWatcherConflictReason>(AddWatcherConflictReason.WATCHER_ALREADY_EXISTS, WatcherMessage.WatcherAlreadyExists));
            }

            // Get default watcher
            var defaultWatcher = await _mainDbContext.Watchers.FirstOrDefaultAsync(WatcherExpression.DefaultWatcher(request.CurrencyId, request.IndicatorId));

            // Add watcher
            watcher = new Watcher(
                request.UserId,
                request.CurrencyId,
                indicator.IndicatorId,
                defaultWatcher?.Value,
                null,
                null,
                null,
                defaultWatcher?.AverageBuy,
                defaultWatcher?.AverageSell,
                defaultWatcher?.Price,
                request.Enabled,
                DateTime.UtcNow.StripSeconds());

            // Add
            _mainDbContext.Watchers.Add(watcher);

            // Save
            await _mainDbContext.SaveChangesAsync();

            // Response
            var response = _mapper.Map <Responses.Watcher>(watcher);

            // Log
            _logger.LogInformation("{@Event}, {@UserId}, {@Request}, {@Response}", "WatcherAdded", request.UserId, request, response);

            // Return
            return(response);
        }