コード例 #1
0
ファイル: BackupTask.cs プロジェクト: rudrp/StockSharp
        protected override TimeSpan OnProcess()
        {
            IBackupService service;

            switch (_settings.Service)
            {
            case BackupServices.AwsS3:
                service = new AmazonS3Service(AmazonExtensions.GetEndpoint(_settings.Address), _settings.ServiceRepo, _settings.Login, _settings.Password.To <string>());
                break;

            case BackupServices.AwsGlacier:
                service = new AmazonGlacierService(AmazonExtensions.GetEndpoint(_settings.Address), _settings.ServiceRepo, _settings.Login, _settings.Password.To <string>());
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            var hasSecurities = false;

            this.AddInfoLog(LocalizedStrings.Str2306Params.Put(_settings.StartFrom));

            var startDate = _settings.StartFrom;
            var endDate   = DateTime.Today - TimeSpan.FromDays(_settings.Offset);

            var allDates = startDate.Range(endDate, TimeSpan.FromDays(1)).ToArray();

            var pathEntry = ToEntry(new DirectoryInfo(_settings.Drive.Path));

            IEnumerable <Tuple <Type, object> > dataTypes = new[]
            {
                Tuple.Create(typeof(ExecutionMessage), (object)ExecutionTypes.Tick),
                Tuple.Create(typeof(ExecutionMessage), (object)ExecutionTypes.OrderLog),
                Tuple.Create(typeof(ExecutionMessage), (object)ExecutionTypes.Order),
                Tuple.Create(typeof(ExecutionMessage), (object)ExecutionTypes.Trade),
                Tuple.Create(typeof(QuoteChangeMessage), (object)null),
                Tuple.Create(typeof(Level1ChangeMessage), (object)null),
                Tuple.Create(typeof(NewsMessage), (object)null)
            };

            var workingSecurities = GetWorkingSecurities().ToArray();

            foreach (var date in allDates)
            {
                foreach (var security in workingSecurities)
                {
                    hasSecurities = true;

                    if (!CanProcess())
                    {
                        break;
                    }

                    var dateEntry = new BackupEntry
                    {
                        Name   = date.ToString("yyyy_MM_dd"),
                        Parent = new BackupEntry
                        {
                            Parent = new BackupEntry
                            {
                                Name   = security.Security.Id.Substring(0, 1),
                                Parent = pathEntry
                            },
                            Name = security.Security.Id,
                        }
                    };

                    var candleTypes = _settings.Drive.GetCandleTypes(security.Security.ToSecurityId(), _settings.StorageFormat);

                    var secDataTypes = dataTypes.Concat(candleTypes.SelectMany(t => t.Item2.Select(a => Tuple.Create(t.Item1, a))));

                    foreach (var tuple in secDataTypes)
                    {
                        var storage = StorageRegistry.GetStorage(security.Security, tuple.Item1, tuple.Item2, _settings.Drive, _settings.StorageFormat);

                        var drive = storage.Drive;

                        var stream = drive.LoadStream(date);

                        if (stream == Stream.Null)
                        {
                            continue;
                        }

                        var entry = new BackupEntry
                        {
                            Name   = LocalMarketDataDrive.CreateFileName(tuple.Item1, tuple.Item2) + LocalMarketDataDrive.GetExtension(StorageFormats.Binary),
                            Parent = dateEntry,
                        };

                        service.Upload(entry, stream, p => { });

                        this.AddInfoLog(LocalizedStrings.Str1580Params, GetPath(entry));
                    }
                }

                if (CanProcess())
                {
                    _settings.StartFrom += TimeSpan.FromDays(1);
                    SaveSettings();
                }
            }

            if (!hasSecurities)
            {
                this.AddWarningLog(LocalizedStrings.Str2292);
                return(TimeSpan.MaxValue);
            }

            if (CanProcess())
            {
                this.AddInfoLog(LocalizedStrings.Str2300);
            }

            return(base.OnProcess());
        }
コード例 #2
0
        public void Start(IMarketDataDrive destDrive, DateTime?startDate, DateTime?endDate, Security security, IMarketDataDrive sourceDrive, StorageFormats format, Type dataType, object arg)
        {
            CreateWorker(0, null);

            _worker.DoWork += (s, e) =>
            {
                var storageRegistry = ConfigManager.GetService <IStorageRegistry>();
                var storage         = storageRegistry.GetStorage(security, dataType, arg, sourceDrive, format);

                try
                {
                    var dates = storage.Dates.ToArray();

                    if (dates.IsEmpty())
                    {
                        return;
                    }

                    var allDates = (startDate ?? dates.First()).Range((endDate ?? dates.Last()), TimeSpan.FromDays(1));

                    var datesToExport = storage.Dates
                                        .Intersect(allDates)
                                        .Select(d =>
                    {
                        int count;

                        if (dataType == typeof(ExecutionMessage))
                        {
                            count = ((IMarketDataStorage <ExecutionMessage>)storage).Load(d).Count;
                        }
                        else if (dataType == typeof(QuoteChangeMessage))
                        {
                            count = ((IMarketDataStorage <QuoteChangeMessage>)storage).Load(d).Count;
                        }
                        else if (dataType == typeof(Level1ChangeMessage))
                        {
                            count = ((IMarketDataStorage <Level1ChangeMessage>)storage).Load(d).Count;
                        }
                        else if (dataType.IsSubclassOf(typeof(CandleMessage)))
                        {
                            count = ((IMarketDataStorage <CandleMessage>)storage).Load(d).Count;
                        }
                        else
                        {
                            throw new NotSupportedException(LocalizedStrings.Str2872Params.Put(dataType.Name));
                        }

                        return(Tuple.Create(d, count));
                    })
                                        .ToArray();

                    _worker.ReportProgress(0);

                    var currentValuesCount = 0;
                    var totalValuesCount   = datesToExport.Select(d => d.Item2).Sum();

                    if (!Directory.Exists(destDrive.Path))
                    {
                        Directory.CreateDirectory(destDrive.Path);
                    }

                    var dataPath = ((LocalMarketDataDrive)sourceDrive).GetSecurityPath(security.ToSecurityId());
                    var fileName = LocalMarketDataDrive.CreateFileName(dataType, arg) + LocalMarketDataDrive.GetExtension(StorageFormats.Binary);

                    foreach (var date in datesToExport)
                    {
                        var d    = date.Item1.ToString("yyyy_MM_dd");
                        var file = Path.Combine(dataPath, d, fileName);

                        if (File.Exists(file))
                        {
                            if (!Directory.Exists(Path.Combine(destDrive.Path, d)))
                            {
                                Directory.CreateDirectory(Path.Combine(destDrive.Path, d));
                            }

                            File.Copy(file, Path.Combine(destDrive.Path, d, Path.GetFileName(file)), true);
                        }

                        if (date.Item2 == 0)
                        {
                            continue;
                        }

                        currentValuesCount += date.Item2;
                        _worker.ReportProgress((int)Math.Round(currentValuesCount * 100m / totalValuesCount));
                        this.GuiAsync(() => UpdateCount(currentValuesCount));
                    }
                }
                finally
                {
                    _worker.ReportProgress(100);
                    Thread.Sleep(500);
                    _worker.ReportProgress(0);
                }
            };

            _worker.RunWorkerAsync();
        }