Ejemplo n.º 1
0
 public JsonResult DataCalculateDo(StatisticsMethodVM parameters)
 {
     try
     {
         _logRepository.Add("画布-统计分析");
         var data = _dataFlowRepository.DataCalculate(parameters);
         if (data != null && !string.IsNullOrEmpty(data.OutputHTML))
         {
             return(Json(new { success = true, data }));
         }
         else
         {
             return(Json(new { success = false, msg = "方法运行失败,请联系管理员!" }));
         }
     }
     catch (Exception ex)
     {
         return(Json(new { success = false, msg = ex.ToString() }));
     }
 }
Ejemplo n.º 2
0
        public StatisticsMethodResult DataCalculate(StatisticsMethodVM parameters)
        {
            var res = new StatisticsMethodResult();

            try
            {
                _logger.LogInformation($"开始计算");
                var userId     = GlobalSetting.RScriptAcount;
                var outputPath = $@"\wwwroot\Output\{userId}\{parameters.MethodCode}\{DateTime.Now.ToString("yyyyMMddHHmmss")}";
                _logger.LogInformation($"计算目录:{outputPath}");
                var target = _env.ContentRootPath + outputPath;
                //R 语言脚本文件夹
                var source = _env.ContentRootPath + Path.GetDirectoryName(parameters.MethodPath);
                Directory.CreateDirectory(target);
                var srcFiles = new DirectoryInfo(source).GetFiles();
                foreach (FileInfo item in srcFiles)
                {
                    item.CopyTo(Path.Combine(target, item.Name), true);
                }
                var cache = GetDataFlowCache(parameters.Node);
                _logger.LogInformation($"计算参数,node:{parameters.Node},projectId:{cache.ProjectInfo.Id},dataSetId:{cache.DataSetInfo.Id},dataResultId:{cache.Id}");
                var rMethod = CaculateMethodFactory.Build(cache, parameters.MethodCode, JsonConvert.SerializeObject(parameters));
                rMethod.RScriptWorkingDirectory = target;
                rMethod.RScriptFileName         = Path.GetFileName(parameters.MethodPath);
                rMethod.StaticLogger            = _staticlogger;
                var methodData = new MethodData()
                {
                    dt          = cache.DataTable,
                    DataColumns = cache.DataColumns,
                };
                var isOk = rMethod.Execute(methodData);

                if (isOk)
                {
                    var tick1 = DateTime.Now.Ticks;
                    var done  = false;
                    do
                    {
                        long   tickDiff = DateTime.Now.Ticks;
                        double n        = (tickDiff - tick1) / 10000000.0;
                        if (n > 30)
                        {
                            done = true;
                            _logger.LogError("统计操作超时!");
                        }
                        if (System.IO.Directory.Exists(target))
                        {
                            res.OutputHTML = CalculateResult(target);
                            if (!string.IsNullOrEmpty(res.OutputHTML))
                            {
                                var downLoadFolder = target + "\\download";
                                Directory.CreateDirectory(downLoadFolder);
                                var fileList = System.IO.Directory.EnumerateFiles(target).OrderBy(f => Path.GetExtension(f));
                                if (fileList != null)
                                {
                                    foreach (var item in fileList)
                                    {
                                        var fileName = System.IO.Path.GetFileName(item);
                                        var ext      = System.IO.Path.GetExtension(item);
                                        if (ext.ToLower() == ".png")
                                        {
                                            res.Pictures.Add(fileName);
                                            File.Copy($@"{target}\{fileName}", $@"{downLoadFolder}\{fileName}");
                                        }
                                        if (ext.ToLower() == ".htm")
                                        {
                                            File.Copy($@"{target}\{fileName}", $@"{downLoadFolder}\{fileName}");
                                        }
                                    }
                                }
                                ZipFile.CreateFromDirectory(downLoadFolder, target + "\\DownLoadZip.zip");
                                res.DownLoadZip = Path.Combine(outputPath.Replace("~", "").Replace(@"\wwwroot\", ""), "DownLoadZip.zip");
                                done            = true;
                            }
                        }
                        else
                        {
                            System.Threading.Thread.Sleep(100);
                        }
                    } while (!done);

                    if (res != null && !string.IsNullOrEmpty(res.OutputHTML))
                    {
                        res.OutputHTML = Path.Combine(outputPath.Replace("~", "").Replace(@"\wwwroot\", ""), res.OutputHTML);
                        if (res.Pictures.Any())
                        {
                            for (int i = 0; i < res.Pictures.Count; i++)
                            {
                                res.Pictures[i] = Path.Combine(outputPath.Replace("~", "").Replace(@"\wwwroot\", ""), res.Pictures[i]);
                            }
                        }
                    }
                }
                _logger.LogInformation($"计算结束");
            }
            catch (Exception ex)
            {
                _logger.LogError("Calculate 失败" + ex.ToString());
            }
            return(res);
        }