public static void appendToDownloadedStatistics(DownloadStatistics dwnldStats)
        {
            retrieveStatistics();
            downloadedStatistics.Add(dwnldStats);
            Stream fileStream = File.Open(Application.StartupPath + "/DownloadStats/Downloads.bin", FileMode.Create, FileAccess.ReadWrite);

            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(fileStream, downloadedStatistics);
            fileStream.Close();
        }
Пример #2
0
        public Task <DownloadStatistics> GetDownloadStatisticsAsync(string agentId)
        {
            var statistics = new DownloadStatistics();

            if (_downloadStatisticsDict.ContainsKey(agentId))
            {
                statistics = _downloadStatisticsDict[agentId];
            }

            return(Task.FromResult(statistics));
        }
        public static void appendToDownloadedStatistics(ControlledDownloader Downloader)
        {
            DownloadStatistics downloadStats = new DownloadStatistics();
            downloadStats.contentLength = Downloader.ContentLength;
            downloadStats.fileName = Downloader.SaveFileName;
            downloadStats.saveLocation = Downloader.FileSaveLocation;
            downloadStats.completeSaveFile = Downloader.CompleteSaveFileName;
            downloadStats.url = Downloader.Url;
               // downloadStats.responseHeaders = Downloader.ResponseHeaders;

            MD5Calculator md5Calculator = new MD5Calculator();
            downloadStats.MD5Checksum = md5Calculator.computeHash(Downloader.CompleteSaveFileName);

            appendToDownloadedStatistics(downloadStats);
        }
Пример #4
0
        public async Task DownloadFile(string url, FileInfo file)
        {
            try
            {
                var httpClient = new HttpClient();
                var response   = await httpClient.GetAsync(url);


                var n      = response.Content.Headers.ContentLength;
                var stream = await response.Content.ReadAsStreamAsync();

                using (var fileStream = file.Create())
                    using (stream)
                    {
                        byte[]   buffer     = new byte[1024 * 50];
                        var      readLength = 0;
                        int      length;
                        DateTime _startTime = DateTime.Now;
                        while ((length = await stream.ReadAsync(buffer, 0, buffer.Length)) != 0)
                        {
                            readLength += length;

                            //var interval = DateTime.Now - _startTime;

                            //var downLoadSpeed = interval.Seconds < 1
                            //    ? NetUtil.ToUnit(readLength - (readLength - length))
                            //    : NetUtil.ToUnit(readLength - (readLength - length) / interval.Seconds);

                            //var size = (n - readLength) / (1024 * 1024);
                            //var remainingTime = new DateTime().AddSeconds(Convert.ToDouble(size));

                            var args = new DownloadStatisticsEventArgs();
                            //args.Remaining = remainingTime;
                            args.Speed = Math.Round((double)(((double)readLength) / n * 100), 2).ToString();
                            DownloadStatistics.Invoke(this, args);

                            // 写入到文件
                            fileStream.Write(buffer, 0, length);

                            _startTime = DateTime.Now;
                        }
                    }
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
            }
        }
        public Boolean StatisticsAddDownload(Int32 idPerson, RepositoryIdentifier rIdentifier, long idItem, Guid uniqueIdItem, long idVersion, Guid uniqueIdVersion, ItemType type, Int32 idCommunity)
        {
            Boolean result = false;

            try{
                DownloadStatistics stat = new DownloadStatistics();
                stat.CreatedIPaddress      = UC.IpAddress;
                stat.CreatedOn             = DateTime.Now;
                stat.CreatedProxyIPaddress = UC.ProxyIpAddress;
                stat.IdCommunity           = idCommunity;
                stat.IdItem                = idItem;
                stat.IdPerson              = idPerson;
                stat.IdVersion             = idVersion;
                stat.ItemType              = type;
                stat.UniqueIdItem          = uniqueIdItem;
                stat.UniqueIdVersion       = uniqueIdVersion;
                stat.RepositoryIdCommunity = rIdentifier.IdCommunity;
                stat.RepositoryIdPerson    = rIdentifier.IdPerson;
                stat.RepositoryType        = rIdentifier.Type;
                Manager.SaveOrUpdate(stat);
                result = true;
                Boolean isIntransaction = Manager.IsInTransaction();
                if (!isIntransaction)
                {
                    Manager.BeginTransaction();
                }
                liteRepositoryItem item = Manager.Get <liteRepositoryItem>(idItem);
                if (item != null)
                {
                    item.Downloaded++;
                    Manager.SaveOrUpdate(item);
                    liteRepositoryItemVersion version = Manager.Get <liteRepositoryItemVersion>(idVersion);
                    if (version != null)
                    {
                        version.Downloaded++;
                        Manager.SaveOrUpdate(version);
                    }
                }
                if (!isIntransaction)
                {
                    Manager.Commit();
                }
            }
            catch (Exception ex)
            {
            }
            return(result);
        }
Пример #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="agentId"></param>
        /// <returns></returns>
        public Task <DownloadStatistics> GetDownloadStatisticsAsync(string agentId)
        {
            var Success      = SetRedisQueue(agentId, Const.RedisStatisticsDownload, SpiderOptions.DownloadSuccess);
            var Failed       = SetRedisQueue(agentId, Const.RedisStatisticsDownload, SpiderOptions.DownloadFailed);
            var DownloadTime = SetRedisQueue(agentId, Const.RedisStatisticsDownload, SpiderOptions.DownloadSuccess + Const.RedisStatisticsTime) + SetRedisQueue(agentId, Const.RedisStatisticsDownload, SpiderOptions.DownloadFailed + Const.RedisStatisticsTime);

            var info = new DownloadStatistics()
            {
                AgentId             = agentId,
                Success             = Success,
                Failed              = Failed,
                ElapsedMilliseconds = DownloadTime
            };

            return(Task.FromResult <DownloadStatistics>(info));
        }