public SyncDataViewModel() { StartSyncDataCommand = new RelayCommand(OnStartSyncData); _configModel = ConfigJsonHelper.GetConfigModel(); InitData(); }
private CommonStockDataManager() { _url = "http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?cb=jQuery11240010094446092091314_1536245516320&type=CT&token=4f1862fc3b5e77c150a2b985b12db0fd&sty=FCOIATC&js=(%7Bdata%3A%5B(x)%5D%2CrecordsFiltered%3A(tot)%7D)&cmd=C._A&st=(ChangePercent)&sr=-1&p=1&ps=3700&_=1536245516321"; _configModel = ConfigJsonHelper.GetConfigModel(); _storageSectionPath = LocalDataPath.TopicStockLinkDataPath; _stockBasicPath = LocalDataPath.LocalStockBasicDataPath; }
private void InitData() { CanStart = true; CanStop = false; //获取配置信息 _configModel = ConfigJsonHelper.GetConfigModel(); }
/// <summary> /// 获取所有产品配置信息 /// </summary> /// <returns></returns> private static List <ProductItem> GetProducts() { List <ProductItem> list = new List <ProductItem>(); //文件读取方式己经取消 list = ConfigJsonHelper.GetTypes <ProductItem>("products"); return(list); }
/// <summary> /// 系统定义的平台类型 /// </summary> /// <returns></returns> public static List <TypeItem> GetPlatform() { if (platform_list.Count == 0) { platform_list = ConfigJsonHelper.GetTypes("PlatformType"); } //list.Add(new TypeItem() { Name = "不限", Id = 0 }); //list.Add(new TypeItem() { Name = "APP平台", Id = 1 }); //list.Add(new TypeItem() { Name = "微信平台", Id = 2 }); return(platform_list); }
/// <summary> /// 开始监控 /// </summary> private async void OnStart() { this.CanStart = false; StockModels.Clear(); PageStockModels.Clear(); _count = 0; this.TotalCrawledCount = 0; this.BigDealRemainCount = 0; this.QuickRemainCount = 0; StockModel.StaticIndex = 1; try { _configModel = ConfigJsonHelper.GetConfigModel(); Messenger.Default.Send(new StatusBarArgs(true, ResourceHelper.FindKey("StatusBar_Message_CheckingConfig")), StatusBarToken.UpdateStatus); bool isConfigUseful = await Task.Run(() => CheckConfig()); if (!isConfigUseful) { this.CanStart = true; return; } Messenger.Default.Send(new StatusBarArgs(true, ResourceHelper.FindKey("StatusBar_Message_FilteringStock")), StatusBarToken.UpdateStatus); List <string> monitorStocks = GetMonitorStocks(); if (monitorStocks == null) { this.CanStart = true; return; } if (!monitorStocks.Any()) { Messenger.Default.Send(new StatusBarArgs(true, ResourceHelper.FindKey("StatusBar_ConfigError_FilterNone")), StatusBarToken.UpdateStatus); this.CanStart = true; return; } CanStart = false; CanStop = true; WebProxy proxy = null; if (_configModel.IsUseProxy) { proxy = new WebProxy(_configModel.ProxyData.IP, _configModel.ProxyData.Port); } try { _iStockRealTimeDealCrawler?.StartCrawler(proxy, _configModel.ClawlerConfigData.QueryInterval, monitorStocks); } catch (ClawlerConfigException clawlerConfigException) { Messenger.Default.Send(new StatusBarArgs(true, clawlerConfigException.Message), StatusBarToken.UpdateStatus); await Task.Run(() => _iStockRealTimeDealCrawler?.StopCrawler()); this.CanStart = true; this.CanStop = false; return; } catch (Exception ex) { LogBuilder.Logger.Error("start crawler error.", ex); } if (false) { try { var stockAnalysisService = ServiceManager <StockTransactionModel> .Instance.GetStockAnalysisService(); _stockAnalysisService = stockAnalysisService; _stockAnalysisService.StockAnalysisResultUpdatedEvent += StockAnalysisService_StockAnalysisResultUpdatedEvent; _stockAnalysisService.RemainderCountUpdatedEvent += _stockAnalysisService_RemainderCountUpdatedEvent; _stockAnalysisService.StartAnalysis(new BigDealAnalysisStrategy(), _cacheBigDealAnalysisStrategy); } catch (StrategyConfigException strategyConfigException) { _stockAnalysisService.StockAnalysisResultUpdatedEvent -= StockAnalysisService_StockAnalysisResultUpdatedEvent; _stockAnalysisService.RemainderCountUpdatedEvent -= _stockAnalysisService_RemainderCountUpdatedEvent; _stockAnalysisService.StopAnalysis(); Messenger.Default.Send(new StatusBarArgs(true, strategyConfigException.Message), StatusBarToken.UpdateStatus); await Task.Run(() => _iStockRealTimeDealCrawler?.StopCrawler()); this.CanStart = true; this.CanStop = false; return; } catch (Exception ex) { LogBuilder.Logger.Error("start moniter BigOrderStrategy error.", ex); } } if (true) { try { var quickAnalysisService = ServiceManager <StockTransactionModelExtern> .Instance.GetStockAnalysisService(); _quickAnalysisService = quickAnalysisService; _quickAnalysisService.StockAnalysisResultUpdatedEvent += _quickAnalysisService_StockAnalysisResultUpdatedEvent; _quickAnalysisService.RemainderCountUpdatedEvent += _quickAnalysisService_RemainderCountUpdatedEvent; _quickAnalysisService.StartAnalysis(new QuickUpAnalysisStrategy(), _cacheQuickUpAnalysisStrategy); } catch (StrategyConfigException strategyConfigException) { _quickAnalysisService.StockAnalysisResultUpdatedEvent -= _quickAnalysisService_StockAnalysisResultUpdatedEvent; _quickAnalysisService.RemainderCountUpdatedEvent -= _quickAnalysisService_RemainderCountUpdatedEvent; _quickAnalysisService.StopAnalysis(); Messenger.Default.Send(new StatusBarArgs(true, strategyConfigException.Message), StatusBarToken.UpdateStatus); await Task.Run(() => _iStockRealTimeDealCrawler?.StopCrawler()); this.CanStart = true; this.CanStop = false; return; } catch (Exception ex) { LogBuilder.Logger.Error("start moniter RapidRiseStrategy error.", ex); } } MonitoringStockCount = monitorStocks.Count; Messenger.Default.Send(new StatusBarArgs(true, ResourceHelper.FindKey("StatusBar_Message_Monitoring")), StatusBarToken.UpdateStatus); } catch (Exception ex) { CanStart = true; CanStop = false; Messenger.Default.Send(new StatusBarArgs(true, string.Format(ResourceHelper.FindKey("StatusBar_Message_StartMonitorFailed"), ex.Message)), StatusBarToken.UpdateStatus); LogBuilder.Logger.Error("开始监控 - 失败", ex); } }
/// <summary> /// 开始爬取数据 /// </summary> /// <param name="interval">单位:毫秒</param> public void StartCrawler(WebProxy webProxy, int interval, IEnumerable <string> stockCodes) { if (_isStarted) { return; } _isStarted = true; _isStoped = false; _interval = interval; _webProxy = webProxy; _configModel = ConfigJsonHelper.GetConfigModel(); CheckConfig(); _pageSize = GetPageSize(); _taskWorkCodeCount = _configModel.ClawlerConfigData.CodeCountPerThread; _monitorStocks = new List <string>(stockCodes); //分配一个线程专门从缓存中取出数据及数据转换并通过事件通知订阅者 Task.Factory.StartNew(() => { while (!_isStoped) { try { Tuple <string, string> tuple; var dequeueSuccesful = _responseJsonQueue.TryDequeue(out tuple); if (dequeueSuccesful) { var queryStockTransactionResponseModel = JsonHelper.DeserializeJsonToObject <QueryStockTransactionResponseModel>(tuple.Item2); var addedStr = new List <string>(); if (_datasDic.ContainsKey(tuple.Item1)) { addedStr = queryStockTransactionResponseModel.Value.Datas.Except(_datasDic[tuple.Item1]).ToList(); _datasDic[tuple.Item1].AddRange(addedStr); } else { addedStr = new List <string>(queryStockTransactionResponseModel.Value.Datas); _datasDic.Add(tuple.Item1, addedStr); } if (addedStr.Any()) { List <StockTransactionModel> addedModels = DtoHelper.GetStockTransactionModels(addedStr); addedModels.ForEach(x => x.Code = tuple.Item1); StockRealTimeDealUpdatedEven?.Invoke(new StockRealTimeDealUpdatedEventArgs(addedModels)); } } } catch (Exception) { } Thread.Sleep(1); } }, TaskCreationOptions.LongRunning); var count = _monitorStocks.Count % _taskWorkCodeCount == 0 ? _monitorStocks.Count / _taskWorkCodeCount : _monitorStocks.Count / _taskWorkCodeCount + 1; for (int i = 1; i <= count; i++) { var codes = _monitorStocks.Take(i * _taskWorkCodeCount).Skip((i - 1) * _taskWorkCodeCount); WorkTaskModel model = new WorkTaskModel(); model.StockCodes = new List <string>(codes); model.Cts = new CancellationTokenSource(); //System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)0X007F; //指定线程在哪个cpu上运行,由于此程序的cpu占用率过高,想通过这种方式降低cpu //此方式确实可以降低cpu,但是数据处理速度慢了很多,所以还是放弃这种方式 int userCount = 4; int core = i % userCount == 0 ? 1 + i % userCount : i % userCount; model.WorkTask = new Task(() => { DoCrawler(model.StockCodes, model.Cts.Token, core); }, model.Cts.Token, TaskCreationOptions.LongRunning); _workTasks.Add(model); } _workTasks.ForEach(work => { work.WorkTask.Start(); }); }
public void Start(IEnumerable <StockTransactionModelExtern> stockBases) { if (_isStarted) { return; } _isStarted = true; _isStoped = false; _cacheStockTransactionModelExterns = stockBases as ConcurrentQueue <StockTransactionModelExtern>; if (_cacheStockTransactionModelExterns == null) { throw new ArgumentException($"'stockBases' type must be ConcurrentQueue<StockTransactionModelExtern>."); } _configModel = ConfigJsonHelper.GetConfigModel(); CheckConfig(); _itemWorkCount = _configModel.QuickUpStrategyConfigData.StockMaxCountEachGroup; int threadCount = _configModel.QuickUpStrategyConfigData.ThreadCount; this._forwardSeconds = _configModel.QuickUpStrategyConfigData.ForwardSeconds; this._afterSeconds = _configModel.QuickUpStrategyConfigData.AfterSeconds; this._dealAmountThreshold = (int)_configModel.QuickUpStrategyConfigData.DealAmountThreshold; this._quickUpThreshold = _configModel.QuickUpStrategyConfigData.QuickUpThreshold / 100; for (int ntask = 1; ntask <= threadCount; ntask++) { Task.Factory.StartNew(() => { //System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)ntask; var remainCount = 0; while (!_isStoped) { try { //剩余待处理的数量 int count = _cacheStockTransactionModelExterns.Count; if (remainCount != count) { remainCount = count; RemainderCountUpdatedEvent.Invoke(this, count); } bool isDequeueSuccess = _cacheStockTransactionModelExterns.TryDequeue(out var model); if (isDequeueSuccess) { if (!_modelsCaches.Any()) { _modelsCaches.TryAdd(0, new ConcurrentDictionary <string, ConcurrentQueue <StockTransactionModelExtern> >()); StockGroupWork(_modelsCaches[0], _modelsCaches.Count); } for (int i = 0; ; i++) { //如果字典已有此个股 if (_modelsCaches[i].ContainsKey(model.Code)) { _modelsCaches[i][model.Code].Enqueue(model); break; } //如果未包含此个股,且字典容量小于所设定的容量限制 if (_modelsCaches[i].Count < _itemWorkCount) { _modelsCaches[i].TryAdd(model.Code, new ConcurrentQueue <StockTransactionModelExtern>(new List <StockTransactionModelExtern> { model })); break; } //如果字典容量超过所设定的容量限制 if (!_modelsCaches.ContainsKey(i + 1)) { _modelsCaches.TryAdd(i + 1, new ConcurrentDictionary <string, ConcurrentQueue <StockTransactionModelExtern> >()); StockGroupWork(_modelsCaches[i + 1], _modelsCaches.Count); } continue; } } Thread.Sleep(1); } catch { } } }, TaskCreationOptions.LongRunning); } }
/// <summary> /// 初始化数据 /// </summary> private void InitData() { ConfigJsonHelper.LoadedAppConfig(); var config = ConfigJsonHelper.GetConfigModel(); if (config == null) { return; } IsUseProxy = config.IsUseProxy; IPString = config.ProxyData.IP; Port = (ushort)config.ProxyData.Port; IsFromConditionFilter = config.IsFromConditionFilter; IsFilterPrice = config.FromConditionFilterData.IsFilterPrice; PriceSmall = config.FromConditionFilterData.PriceSmall; PriceMax = config.FromConditionFilterData.PriceMax; IsFilterFlowValue = config.FromConditionFilterData.IsFilterFlowValue; FlowValueSmall = config.FromConditionFilterData.FlowValueSmall; FlowValueMax = config.FromConditionFilterData.FlowValueMax; IsFilterTopic = config.FromConditionFilterData.IsFilterTopic; IsLogicAnd = config.FromConditionFilterData.IsLogicAnd; FilterTopicText = config.FromConditionFilterData.FilterTopicText; IsFromLocalFile = config.IsFromLocalFile; MonitorFilePath = config.MonitorFilePath; IsExceptMonitorRange = config.IsExceptMonitorRange; ExceptMonitorFilePath = config.ExceptMonitorFilePath; IsUseBigOrderStrategy = config.IsUseBigOrderStrategy; IsUseRapidRiseStrategy = config.IsUseRapidRiseStrategy; ClawlerSettingData = new ClawlerSettingModel(); ClawlerSettingData.CodeCountPerThread = config.ClawlerConfigData.CodeCountPerThread; ClawlerSettingData.QueryCountPerTime = config.ClawlerConfigData.QueryCountPerTime; ClawlerSettingData.QueryInterval = config.ClawlerConfigData.QueryInterval; BigDealStrategySettingData = new BigDealStrategySettingModel(); BigDealStrategySettingData.StockMaxCountEachGroup = config.BigDealStrategyConfigData.StockMaxCountEachGroup; BigDealStrategySettingData.ThreadCount = config.BigDealStrategyConfigData.ThreadCount; BigDealStrategySettingData.ForwardSeconds = config.BigDealStrategyConfigData.ForwardSeconds; BigDealStrategySettingData.AfterSeconds = config.BigDealStrategyConfigData.AfterSeconds; BigDealStrategySettingData.BigDealAmountThreshold = config.BigDealStrategyConfigData.BigDealAmountThreshold; BigDealStrategySettingData.BigDealCountThreshold = config.BigDealStrategyConfigData.BigDealCountThreshold; QuickUpStrategySettingData = new QuickUpStrategySettingModel(); QuickUpStrategySettingData.StockMaxCountEachGroup = config.QuickUpStrategyConfigData.StockMaxCountEachGroup; QuickUpStrategySettingData.ThreadCount = config.QuickUpStrategyConfigData.ThreadCount; QuickUpStrategySettingData.ForwardSeconds = config.QuickUpStrategyConfigData.ForwardSeconds; QuickUpStrategySettingData.AfterSeconds = config.QuickUpStrategyConfigData.AfterSeconds; QuickUpStrategySettingData.DealAmountThreshold = config.QuickUpStrategyConfigData.DealAmountThreshold; QuickUpStrategySettingData.QuickUpThreshold = config.QuickUpStrategyConfigData.QuickUpThreshold; ClawlerSettingData.PropertyChanged += SettingViewModel_PropertyChanged; BigDealStrategySettingData.PropertyChanged += SettingViewModel_PropertyChanged; QuickUpStrategySettingData.PropertyChanged += SettingViewModel_PropertyChanged; _cacheDataString = JsonHelper.SerializeObject(this); }
/// <summary> /// 保存 /// </summary> private void OnSave() { var config = ConfigJsonHelper.GetConfigModel(); if (config == null) { return; } config.IsUseProxy = IsUseProxy; config.ProxyData.IP = IPString; config.ProxyData.Port = Port; config.IsFromConditionFilter = IsFromConditionFilter; config.FromConditionFilterData.IsFilterPrice = IsFilterPrice; config.FromConditionFilterData.PriceSmall = PriceSmall; config.FromConditionFilterData.PriceMax = PriceMax; config.FromConditionFilterData.IsFilterFlowValue = IsFilterFlowValue; config.FromConditionFilterData.FlowValueSmall = FlowValueSmall; config.FromConditionFilterData.FlowValueMax = FlowValueMax; config.FromConditionFilterData.IsFilterTopic = IsFilterTopic; config.FromConditionFilterData.IsLogicAnd = IsLogicAnd; config.FromConditionFilterData.FilterTopicText = FilterTopicText; config.IsFromLocalFile = IsFromLocalFile; config.MonitorFilePath = MonitorFilePath; config.IsExceptMonitorRange = IsExceptMonitorRange; config.ExceptMonitorFilePath = ExceptMonitorFilePath; config.IsUseBigOrderStrategy = IsUseBigOrderStrategy; config.IsUseRapidRiseStrategy = IsUseRapidRiseStrategy; config.ClawlerConfigData.CodeCountPerThread = ClawlerSettingData.CodeCountPerThread; config.ClawlerConfigData.QueryCountPerTime = ClawlerSettingData.QueryCountPerTime; config.ClawlerConfigData.QueryInterval = ClawlerSettingData.QueryInterval; config.BigDealStrategyConfigData.StockMaxCountEachGroup = BigDealStrategySettingData.StockMaxCountEachGroup; config.BigDealStrategyConfigData.ThreadCount = BigDealStrategySettingData.ThreadCount; config.BigDealStrategyConfigData.ForwardSeconds = BigDealStrategySettingData.ForwardSeconds; config.BigDealStrategyConfigData.AfterSeconds = BigDealStrategySettingData.AfterSeconds; config.BigDealStrategyConfigData.BigDealAmountThreshold = BigDealStrategySettingData.BigDealAmountThreshold; config.BigDealStrategyConfigData.BigDealCountThreshold = BigDealStrategySettingData.BigDealCountThreshold; config.QuickUpStrategyConfigData.StockMaxCountEachGroup = QuickUpStrategySettingData.StockMaxCountEachGroup; config.QuickUpStrategyConfigData.ThreadCount = QuickUpStrategySettingData.ThreadCount; config.QuickUpStrategyConfigData.ForwardSeconds = QuickUpStrategySettingData.ForwardSeconds; config.QuickUpStrategyConfigData.AfterSeconds = QuickUpStrategySettingData.AfterSeconds; config.QuickUpStrategyConfigData.DealAmountThreshold = QuickUpStrategySettingData.DealAmountThreshold; config.QuickUpStrategyConfigData.QuickUpThreshold = QuickUpStrategySettingData.QuickUpThreshold; ConfigJsonHelper.SaveConfig(); _cacheDataString = JsonHelper.SerializeObject(this); this.PropertyChanged -= SettingViewModel_PropertyChanged; CanSave = false; CanCancel = false; this.PropertyChanged += SettingViewModel_PropertyChanged; //停止正在进行的监控 Messenger.Default.Send(string.Empty, MonitorOptions.StopMonitor); }
/// <summary> /// 获取充值类型 /// </summary> /// <returns></returns> public List <TypeItem> GetMoneyTypes() { return(ConfigJsonHelper.GetTypes("RechargeType")); }