public async Task <AsyncDownloadResultModel> DoWork(IProgress <DownloadProgressReportModel> progressSent, CancellationToken cancellationToken) { List <FormattedTradesModel> resultData = new List <FormattedTradesModel>(); List <string[]> filteredItems = StaticMethods.GetFilteredItems(); progress = progressSent; token = cancellationToken; ItemFiltersModel filters = SqliteDataAccess.LoadItemFilters(); totalItems = filteredItems.Count; startTime = DateTimeOffset.Now.ToUnixTimeSeconds(); startedItem = 0; finishedItem = 0; maxAsync = 0; List <Task <FormattedTradesModel> > tasks = new List <Task <FormattedTradesModel> >(); while (startedItem < totalItems) { token.ThrowIfCancellationRequested(); if (maxAsync < filters.max_async_tasks) { maxAsync++; tasks.Add(Task.Run(() => downloadSite(filteredItems[startedItem]))); } Thread.Sleep(10); } FormattedTradesModel[] results = await Task.WhenAll(tasks); foreach (FormattedTradesModel model in results) { if (model.has_data) { resultData.Add(model); } } AsyncDownloadResultModel result = new AsyncDownloadResultModel(); result.item_list = resultData; result.finished_with_errors = finishedWithErrors; return(result); }
public ObservableCollection <CalculateStationTradeModel> DoWork(IProgress <DownloadProgressReportModel> progress, CancellationToken cancellationToken, List <FormattedTradesModel> formatedTrades) { List <string[]> filteredItems = StaticMethods.GetFilteredItems(); int totalItems = filteredItems.Count; if (formatedTrades.Count < totalItems) { totalItems = formatedTrades.Count; } filters = SqliteDataAccess.LoadItemFilters(); allStations = StaticMethods.GetAllStations(); ObservableCollection <CalculateStationTradeModel> cTrades = new ObservableCollection <CalculateStationTradeModel>(); long startTime = DateTimeOffset.Now.ToUnixTimeSeconds(); for (int i = 0; i < totalItems; i++) { cancellationToken.ThrowIfCancellationRequested(); int type_id = Int32.Parse(filteredItems[i][0]); if (formatedTrades[i].type_id == type_id) { CalculateStationTradeModel temp = TradePrepare(type_id, filteredItems[i][1], filteredItems[i][2], formatedTrades[i]); if (temp != null) { cTrades.Add(temp); } } int progressPercentage = Convert.ToInt32(((double)i / filteredItems.Count) * 100); DownloadProgressReportModel report = new DownloadProgressReportModel(); report.PercentageComplete = progressPercentage; report.MessageRemaining = "Step 2/2: Calculating data" + StaticMethods.EstimatedTime(startTime, i, totalItems); progress.Report(report); } return(cTrades); }
public async Task <bool> DoWork(IProgress <DownloadProgressReportModel> progressSent, CancellationToken cancellationToken) { DownloadProgressReportModel report; token = cancellationToken; progress = progressSent; string fileName = "invTypes.xls.bz2"; if (File.Exists(fileName)) { File.Delete(fileName); } WebClient wb = new WebClient(); wb.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"); wb.DownloadFile("https://www.fuzzwork.co.uk/dump/latest/invTypes.xls.bz2", fileName); var fileNameXls = Path.GetFileNameWithoutExtension(fileName); if (File.Exists(fileNameXls)) { File.Delete(fileNameXls); } using (Stream fs = File.OpenRead(fileName), output = File.Create(fileNameXls), decompressor = new Ionic.BZip2.BZip2InputStream(fs)) { byte[] buffer = new byte[2048]; int n; while ((n = decompressor.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, n); } } File.Delete(fileName); List <List <string> > allItemList = new List <List <string> >(); startTime = DateTimeOffset.Now.ToUnixTimeSeconds(); using (var stream = File.Open(fileNameXls, FileMode.Open, FileAccess.Read)) { using (var reader = ExcelReaderFactory.CreateReader(stream)) { var conf = new ExcelDataSetConfiguration { ConfigureDataTable = _ => new ExcelDataTableConfiguration { UseHeaderRow = true } }; var dataSet = reader.AsDataSet(conf); var dataTable = dataSet.Tables[0]; int totalItems = dataTable.Rows.Count; for (int i = 0; i < totalItems; i++) { List <string> singleRow = new List <string>(); singleRow.Add(dataTable.Rows[i][0].ToString()); singleRow.Add(dataTable.Rows[i][2].ToString()); string itemVolumeLocal = dataTable.Rows[i][5].ToString(); string itemVolumeInternational = itemVolumeLocal.Replace(',', '.'); singleRow.Add(itemVolumeInternational); allItemList.Add(singleRow); token.ThrowIfCancellationRequested(); int progressPercentage = Convert.ToInt32(((double)i / totalItems) * 100); report = new DownloadProgressReportModel(); report.PercentageComplete = progressPercentage; report.MessageRemaining = "Step 2/4: Processing new item list" + StaticMethods.EstimatedTime(startTime, i, totalItems); progress.Report(report); } } } File.Delete(fileNameXls); if (allItemList.Any()) { List <ItemModel> itemsDB = SqliteDataAccess.LoadItems(); List <ItemModel> todoItems = new List <ItemModel>(); ItemFiltersModel filters = SqliteDataAccess.LoadItemFilters(); long currentTImestamp = DateTimeOffset.Now.ToUnixTimeSeconds(); startedItem = 0; totalItems = allItemList.Count; startTime = DateTimeOffset.Now.ToUnixTimeSeconds(); foreach (List <string> newItem in allItemList) { token.ThrowIfCancellationRequested(); bool isNewItem = true; foreach (ItemModel item in itemsDB) { if (Int32.Parse(newItem[0]) == item.type_id) { isNewItem = false; long oldTimestamp = item.updated_at + (filters.updated_item_max_age * 24 * 60 * 60); if (currentTImestamp > oldTimestamp) { ItemModel newTodoItem = new ItemModel(); newTodoItem.type_id = item.type_id; newTodoItem.name = newItem[1]; newTodoItem.volume = Decimal.Parse(newItem[2], CultureInfo.InvariantCulture); todoItems.Add(newTodoItem); } } } if (isNewItem) { ItemModel newTodoItem = new ItemModel(); newTodoItem.type_id = Int32.Parse(newItem[0]); newTodoItem.name = newItem[1]; newTodoItem.volume = Decimal.Parse(newItem[2], CultureInfo.InvariantCulture); newTodoItem.sell_price = 0; newTodoItem.trade_volume = 0; newTodoItem.updated_at = 0; SqliteDataAccess.SaveItem(newTodoItem); todoItems.Add(newTodoItem); } report = new DownloadProgressReportModel(); report.PercentageComplete = Convert.ToInt32(((double)startedItem / totalItems) * 100); report.MessageRemaining = "Step 3/4: Checking items in database" + StaticMethods.EstimatedTime(startTime, startedItem, totalItems); progress.Report(report); startedItem++; } startTime = DateTimeOffset.Now.ToUnixTimeSeconds(); totalItems = todoItems.Count(); startedItem = 0; finishedItem = 0; maxAsync = 0; while (startedItem < totalItems) { token.ThrowIfCancellationRequested(); if (maxAsync < filters.max_async_tasks) { maxAsync++; Task.Run(() => GetAndUpdateItem(todoItems[startedItem])); } Thread.Sleep(100); } } return(finishedWithErrors); }
public static List <string[]> GetFilteredItems() { List <ItemModel> items = SqliteDataAccess.LoadItems(); ItemFiltersModel filters = SqliteDataAccess.LoadItemFilters(); List <string[]> filteredItems = new List <string[]>(); foreach (ItemModel item in items) { bool isIgnorePassed = false; bool isFilterVolumePassed = false; bool isFilterPricePassed = false; if (filters.ignore_zero > 0) { if (item.volume > 0 && item.sell_price > 0) { isIgnorePassed = true; } } else { isIgnorePassed = true; } decimal filterMaxVolume = filters.max_volume; if (filters.user_cargo_capacity < filters.max_volume && filters.user_cargo_capacity > 0) { filterMaxVolume = filters.user_cargo_capacity; } if (filterMaxVolume > 0) { if (item.volume < filterMaxVolume) { isFilterVolumePassed = true; } } else { isFilterVolumePassed = true; } decimal filterMaxPrice = filters.max_price; if (filters.user_available_money < filters.max_price && filters.user_available_money > 0) { filterMaxPrice = filters.user_available_money; } if (filterMaxPrice > 0) { if (item.sell_price < filterMaxPrice) { isFilterPricePassed = true; } } else { isFilterPricePassed = true; } if (isIgnorePassed && isFilterVolumePassed && isFilterPricePassed && item.trade_volume > filters.min_trade_volume) { filteredItems.Add(new string[] { item.type_id.ToString(), item.name, item.volume.ToString(CultureInfo.InvariantCulture) }); } } return(filteredItems); }