Ejemplo n.º 1
0
    public void ImportFromCSV()
    {
        var serializedObject = new SerializedObject(this);
        var list             = serializedObject.FindProperty("stock");

        CSVConverter.ImportCSV <ShopItem>("stock", list);
    }
Ejemplo n.º 2
0
    public void ExportToCSV()
    {
        var serializedObject = new SerializedObject(this);
        var list             = serializedObject.FindProperty("stock");

        CSVConverter.ExportCSV("stock", list);
    }
        public string GetCSV(string input)
        {
            var sentences = new TextParser().Parse(input);
            var csvString = new CSVConverter().Convert(sentences);

            return(csvString);
        }
Ejemplo n.º 4
0
        public static TDataConfig CreateDataByContent <TModel, TDataConfig>(string csvContent, string scriptObjectPath, bool refresh = true)
            where TModel : new() where TDataConfig : DataConfigBase <TModel>
        {
            var dataConfig = ScriptableObject.CreateInstance <TDataConfig>();

            dataConfig.Data = CSVConverter.Convert <TModel>(csvContent);

            CreateAsset(scriptObjectPath, dataConfig, refresh);
            return(dataConfig);
        }
Ejemplo n.º 5
0
    private static string GetCsvSerialize(TextAsset textAsset)
    {
        string[] csvType      = CSVConverter.SerializeCSVType(textAsset);
        string[] csvParameter = CSVConverter.SerializeCSVParameter(textAsset);

        int keyCount = csvParameter.Length;

        string csvSerialize = string.Empty;

        for (int i = 0; i < keyCount; i++)
        {
            string[] attributes = new string[] { csvType[i], csvParameter[i] };

            if (attributes[0] == "string")
            {
                csvSerialize += string.Format("m_tempData.{0}=m_datas[i][{1}];", attributes[1], i);
            }
            else if (attributes[0] == "bool")
            {
                csvSerialize += GetCsvSerialize(attributes, i, "0");
            }
            else if (attributes[0] == "int")
            {
                csvSerialize += GetCsvSerialize(attributes, i, "0");
            }
            else if (attributes[0] == "float")
            {
                csvSerialize += GetCsvSerialize(attributes, i, "0.0f");
            }
            else if (attributes[0] == "string[]")
            {
                csvSerialize += string.Format("m_tempData.{0}=CSVConverter.ConvertToArray<string>(m_datas[i][{1}].Trim());", attributes[1], i);
            }
            else if (attributes[0] == "bool[]")
            {
                csvSerialize += string.Format("m_tempData.{0}=CSVConverter.ConvertToArray<bool>(m_datas[i][{1}].Trim());", attributes[1], i);
            }
            else if (attributes[0] == "int[]")
            {
                csvSerialize += string.Format("m_tempData.{0}=CSVConverter.ConvertToArray<int>(m_datas[i][{1}].Trim());", attributes[1], i);
            }
            else if (attributes[0] == "float[]")
            {
                csvSerialize += string.Format("m_tempData.{0}=CSVConverter.ConvertToArray<float>(m_datas[i][{1}].Trim());", attributes[1], i);
            }

            if (i != keyCount - 1)
            {
                csvSerialize += "\n";
                csvSerialize += "\t\t\t\t\t";
            }
        }
        return(csvSerialize);
    }
Ejemplo n.º 6
0
    private static string GetCsvKey(TextAsset textAsset)
    {
        string[] csvType      = CSVConverter.SerializeCSVType(textAsset);
        string[] csvParameter = CSVConverter.SerializeCSVParameter(textAsset);

        if (csvType[0] == "string")
        {
            return(string.Format("temp => temp.{0} == key", csvParameter[0]));
        }
        return(string.Format("temp => temp.{0} == {1}.Parse(key)", csvParameter[0], csvType[0]));
    }
Ejemplo n.º 7
0
        public void TestCsvFile()
        {
            CSVConverter converter = new CSVConverter();

            string csvFile = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\..\\..\\", "Aux_Files", "Dealertrack-CSV-Example.csv"));

            Stream stream = new FileStream(csvFile, FileMode.Open);

            List <Sale> sales = converter.ConvertCSVToSales(stream);

            Assert.True(sales.Count == 13);
        }
Ejemplo n.º 8
0
        public void TestCSVExportSimple()
        {
            Stock         stockAsset       = Resources.Load <Stock>("TestExportStock");
            var           serializedObject = new SerializedObject(stockAsset);
            var           stock            = serializedObject.FindProperty("stock");
            List <string> csv = CSVConverter.ToCSV(stock);

            Assert.AreEqual(2, csv.Count);
            Assert.AreEqual("name,price,image,colour,available,category,material,sound", csv[0]);
            Assert.AreEqual("cookie,50,Cookie,FF000000,True,Baked,Special,Funny", csv[1]);
            Resources.UnloadAsset(stockAsset);
        }
Ejemplo n.º 9
0
        public static ScriptableObject CreateColumnDataByContent(Type modelType, Type dataConfigType, string csvContent, string scriptObjectPath, string headerName, bool refresh = true)
        {
            if (!typeof(DataConfigBase <>).MakeGenericType(modelType).IsAssignableFrom(dataConfigType))
            {
                Debug.LogErrorFormat("dataConfigType: {0} must inherited from DataConfigBase<{1}>!", dataConfigType, modelType);
                return(null);
            }
            var dataConfig = ScriptableObject.CreateInstance(dataConfigType);

            (dataConfig as IDataConfig).Assign(CSVConverter.ConvertColumn(modelType, csvContent, headerName));

            CreateAsset(scriptObjectPath, dataConfig, refresh);
            return(dataConfig);
        }
Ejemplo n.º 10
0
        public void TestCSVImportSimple()
        {
            Stock stockAsset       = Resources.Load <Stock>("TestImportStock");
            var   serializedObject = new SerializedObject(stockAsset);
            var   stock            = serializedObject.FindProperty("stock");

            stock.ClearArray();
            serializedObject.ApplyModifiedProperties();
            Assert.AreEqual(0, stockAsset.stock.Count);
            TextAsset file = Resources.Load <TextAsset>("TestStockCSV");
            var       csv  = new List <string>();

            using (var reader = new StringReader(file.text))
            {
                string line = null;
                do
                {
                    line = reader.ReadLine();
                    if (line != null)
                    {
                        csv.Add(line);
                    }
                }while (line != null);
            }
            CSVConverter.FromCSV(csv, typeof(ShopItem), stock);
            serializedObject.ApplyModifiedProperties();
            Assert.AreEqual(5, stockAsset.stock.Count);
            Assert.AreEqual("cookie", stockAsset.stock[0].name);
            Assert.AreEqual(50, stockAsset.stock[0].price);
            Assert.NotNull(stockAsset.stock[0].image);
            Assert.AreEqual("Cookie", stockAsset.stock[0].image.name);
            Assert.AreEqual(Color.red, stockAsset.stock[0].colour);
            Assert.IsTrue(stockAsset.stock[0].available);
            Assert.AreEqual(
                ShopItem.Category.Baked,
                stockAsset.stock[0].category);
            Assert.NotNull(stockAsset.stock[0].material);
            Assert.AreEqual("Special", stockAsset.stock[0].material.name);
            Assert.NotNull(stockAsset.stock[0].sound);
            Assert.AreEqual("Funny", stockAsset.stock[0].sound.name);
            Assert.AreEqual("apple", stockAsset.stock[1].name);
            Assert.AreEqual("chicken leg", stockAsset.stock[2].name);
            Assert.AreEqual("fish", stockAsset.stock[3].name);
            Assert.AreEqual("steak", stockAsset.stock[4].name);
            Resources.UnloadAsset(stockAsset);
            Resources.UnloadAsset(file);
        }
Ejemplo n.º 11
0
        public async Task <IActionResult> ViewDataTable(int datasetID)
        {
            Dataset dataset = await repository.GetDatasetByID(datasetID);

            ViewBag.TableName = dataset.DatasetNameVersion;

            // convert string to stream
            byte[] byteArray = Encoding.UTF8.GetBytes(dataset.TheData);

            using (MemoryStream stream = new MemoryStream(byteArray))
            {
                DataTable dataTable = CSVConverter.CSVDataToDataTable(stream); //convert the internally loaded data (which is in CSV format) to a datatable
                dataTable.TableName = dataset.DatasetID.ToString();

                Sheet sheet = new Sheet(dataTable);
                return(View(sheet));
            }
        }
Ejemplo n.º 12
0
        public List <SaveResult <UserDTO> > Import(UserImportDTO userImportDTO)
        {
            using (var userCreateHandler = new UserCreateHandler(db, user, new UserValidator(), new UserFactory(db, user), new UserQuery(db), new AccessControl(user)))
            {
                using (var transaction = new TransactionScope())
                {
                    List <SaveResult <UserDTO> > saveResults = new List <SaveResult <UserDTO> >();

                    var stream = new CSVConverter().GetStreamFromBase64(userImportDTO.File);
                    using (var reader = new StreamReader(stream))
                    {
                        int current      = 0;
                        var jabatanQuery = new KategoriJabatan.Queries.KategoriJabatanQuery(db);
                        while (!reader.EndOfStream)
                        {
                            var line = reader.ReadLine();
                            if (current == 0)
                            {
                                current++;
                                continue;
                            }
                            var     values  = line.Split(';');
                            UserDTO userDTO = CreateUserDTO(jabatanQuery, values);

                            var saveResult = userCreateHandler.Save(userDTO: userDTO, dateStamp: DateTime.UtcNow);
                            saveResults.Add(new SaveResult <UserDTO>()
                            {
                                Message          = saveResult.Message,
                                Model            = saveResult.Model?.Model,
                                Success          = saveResult.Success,
                                ValidationResult = saveResult.ValidationResult
                            });
                            current++;
                        }
                    }
                    transaction.Complete();

                    return(saveResults);
                }
            }
        }
Ejemplo n.º 13
0
    public static void ImportCSVFile <Type>(
        string filename,
        SerializedProperty list)
    {
        var csv = new List <string>();

        using (var reader = new StreamReader(filename))
        {
            string line;
            do
            {
                line = reader.ReadLine();
                if (line != null)
                {
                    csv.Add(line);
                }
            }while (line != null);
        }
        CSVConverter.FromCSV(csv, typeof(Type), list);
        list.serializedObject.ApplyModifiedProperties();
    }
Ejemplo n.º 14
0
        public void ConvertToDataTable_DECulture__ReturnsCorrectDataTable()
        {
            //Arrange
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            string theData = GetTestCSVData();

            //change the separator
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                theData = theData.Replace(',', ';'); //list separator for De on windows
                theData = theData.Replace('.', ','); //points replaced with commas
            }
            else
            {
                theData = theData.Replace(',', ';'); //list separator for De on windows
                theData = theData.Replace('.', ','); //points replaced with commas

                theData = theData.Replace(';', '.'); //list separator for De on linux
            }


            byte[] byteArray = Encoding.UTF8.GetBytes(theData);

            using (MemoryStream stream = new MemoryStream(byteArray))
            {
                //Act
                DataTable dataTable = CSVConverter.CSVDataToDataTable(stream, new System.Globalization.CultureInfo("de-DE"));

                //Assert
                Assert.Equal("SilveRSelected", dataTable.Columns[0].ColumnName);
                Assert.Equal("Resp1", dataTable.Columns[1].ColumnName);
                Assert.Equal("Trea t1", dataTable.Columns[2].ColumnName);

                Assert.True((bool)dataTable.Rows[0][0]);
                Assert.Equal("0,998758912", dataTable.Rows[0][1]);
                Assert.Equal(String.Empty, dataTable.Rows[10][4]);
                Assert.Equal("C", dataTable.Rows[10][5]);
            }
        }
Ejemplo n.º 15
0
    /// <summary>
    /// 获取配置文件属性
    /// </summary>
    /// <param name="textAsset"></param>
    /// <returns></returns>
    private static string GetClassParmeters(TextAsset textAsset)
    {
        string[] csvNote      = CSVConverter.SerializeCSVNote(textAsset);
        string[] csvType      = CSVConverter.SerializeCSVType(textAsset);
        string[] csvParameter = CSVConverter.SerializeCSVParameter(textAsset);
        int      keyCount     = csvParameter.Length;

        string classParamers = string.Empty;

        for (int i = 0; i < keyCount; i++)
        {
            classParamers += string.Format("/// <summary>\n\t\t///{0}\n\t\t/// </summary>\n", csvNote[i]);
            classParamers += string.Format("\t\tpublic {0} {1};", csvType[i], csvParameter[i]);

            if (i != keyCount - 1)
            {
                classParamers += "\n";
                classParamers += "\t\t";
            }
        }

        return(classParamers);
    }
Ejemplo n.º 16
0
        static void Main(string[] args)
        {
            DateTime startTime    = DateTime.Now;
            var      csvConverter = new CSVConverter <DraftStarSystem>()
            {
                Path      = @"C:\Users\Scott\Source\Repos\SSFleetCommand\SSFleetCommand\SSFleetCommandApi\Data\SystemLog.csv",
                SkipLines = 1,
                TakeLines = 1
            };

            var      result        = csvConverter.Execute();
            DateTime endBlocked    = DateTime.Now;
            var      convertResult = csvConverter.ExecuteAsync();

            result = convertResult.GetAwaiter().GetResult();
            DateTime endAsync = DateTime.Now;

            Console.WriteLine($"Processed: {csvConverter?.Results?.Count ?? 0} records -> Sync: {(endBlocked - startTime).TotalSeconds} secs Async -> {(endAsync - endBlocked).TotalSeconds} secs");
            var starSystemsList = csvConverter.Results.Select(ds => new StarSystem(ds));

            Console.WriteLine("Press any key to end...");
            Console.ReadKey();
        }
Ejemplo n.º 17
0
        public void ConvertToDataTable_GBCulture_ReturnsCorrectDataTable()
        {
            //Arrange
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            string theData = GetTestCSVData();

            byte[] byteArray = Encoding.UTF8.GetBytes(theData);

            using (MemoryStream stream = new MemoryStream(byteArray))
            {
                //Act
                DataTable dataTable = CSVConverter.CSVDataToDataTable(stream, new System.Globalization.CultureInfo("en-GB"));

                //Assert
                Assert.Equal("SilveRSelected", dataTable.Columns[0].ColumnName);
                Assert.Equal("Resp1", dataTable.Columns[1].ColumnName);
                Assert.Equal("Trea t1", dataTable.Columns[2].ColumnName);

                Assert.True((bool)dataTable.Rows[0][0]);
                Assert.Equal("0.998758912", dataTable.Rows[0][1]);
                Assert.Equal(String.Empty, dataTable.Rows[10][4]);
                Assert.Equal("C", dataTable.Rows[10][5]);
            }
        }
Ejemplo n.º 18
0
        private async Task <IActionResult> LoadFile(FileInfo selectedFile)
        {
            if (selectedFile.Extension.ToUpper() == ".CSV") //not necessary as already filtering...
            {
                //use the CSVReader to read in the data
                string    message;
                DataTable dataTable = CSVConverter.CSVDataToDataTable(selectedFile.OpenRead(), Program.UserCulture); //loading from disk so allow the CSV parser to just use the current culture

                if (dataTable == null)                                                                               //then failed to be read
                {
                    message = "The CSV data needs to be in the correct format.";
                }
                else //got datatable but need to check
                {
                    message = dataTable.CheckDataTable();
                }

                if (message == null)
                {
                    //save in database
                    await SaveDatasetToDatabase(selectedFile.Name, dataTable);

                    try //to delete but if fails doesn't matter
                    {
                        System.IO.File.Delete(selectedFile.FullName);
                    }
                    catch (IOException)
                    {
                        //ignore error
                    }

                    TempData["InfoMessage"] = "File imported successfully!";
                }
                else
                {
                    TempData["ErrorMessage"] = message;
                }

                return(RedirectToAction("Index"));                                                              //return to file loader/loaded files display screen
            }
            else if (selectedFile.Extension.ToUpper() == ".XLS" || selectedFile.Extension.ToUpper() == ".XLSX") //excel
            {
                //System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

                //read in xls file
                using (FileStream stream = selectedFile.OpenRead())
                {
                    using (IExcelDataReader excelReader = ExcelReaderFactory.CreateReader(stream))
                    {
                        DataSet excelDataSet = excelReader.AsDataSet(new ExcelDataSetConfiguration()
                        {
                            ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
                            {
                                UseHeaderRow = true
                            }
                        });

                        //if there is more than one worksheet then ask the user which sheet they want, otherwise automatically load it
                        if (excelDataSet.Tables.Count == 0)
                        {
                            TempData["ErrorMessage"] = "Error reading file. No sheets with valid data could be found in the Excel workbook.";
                            return(RedirectToAction("Index"));
                        }
                        else if (excelDataSet.Tables.Count == 1) //one excel sheet
                        {
                            DataTable dataTable = excelDataSet.Tables[0];
                            string    message   = dataTable.CheckDataTable();

                            if (message == null)
                            {
                                //save in database
                                await SaveDatasetToDatabase(selectedFile.Name, dataTable);

                                try //to delete...
                                {
                                    System.IO.File.Delete(selectedFile.FullName);
                                }
                                catch (IOException)
                                {
                                    //...if fails doesn't matter
                                }

                                TempData["InfoMessage"] = "File imported successfully!";
                            }
                            else
                            {
                                TempData["ErrorMessage"] = message;
                            }

                            return(RedirectToAction("Index")); //return to file loader/loaded files display screen
                        }
                        else //more than 1 excel sheet
                        {
                            List <string> tableNames = new List <string>();
                            foreach (DataTable t in excelDataSet.Tables)
                            {
                                tableNames.Add(t.TableName);
                            }

                            TempData["TableNames"] = tableNames;
                            return(RedirectToAction("SheetSelector", new { filename = selectedFile.FullName })); //go to sheet selection screen
                        }
                    }
                }
            }
            else
            {
                throw new ArgumentException("File format not recognised.");
            }
        }
Ejemplo n.º 19
0
        public void Load()
        {
            TextAsset textAsset = Resources.Load <TextAsset>(DataPath());

            m_datas = GetAllData(CSVConverter.SerializeCSVData(textAsset));
        }
Ejemplo n.º 20
0
        public async Task Execute(string analysisGuid)
        {
            using (IServiceScope scope = services.CreateScope())
            {
                ISilveRRepository silveRRepository = scope.ServiceProvider.GetRequiredService <ISilveRRepository>();
                AppSettings       appSettings      = scope.ServiceProvider.GetRequiredService <IOptions <AppSettings> >().Value;

                //declared here as used in exception handler
                string workingDir   = Path.GetTempPath();
                string theArguments = null;
                string rscriptPath  = null;

                try
                {
                    Stopwatch sw = Stopwatch.StartNew();

                    //get analysis
                    Analysis analysis = await silveRRepository.GetAnalysisComplete(analysisGuid);

                    //load the analysis entity into the model so that arguments can be extracted
                    AnalysisModelBase analysisModel = AnalysisFactory.CreateAnalysisModel(analysis);
                    analysisModel.LoadArguments(analysis.Arguments);

                    //save the useroptions to the working dir
                    UserOption userOptions = await silveRRepository.GetUserOptions();

                    File.WriteAllLines(Path.Combine(workingDir, analysisGuid + ".useroptions"), userOptions.GetOptionLines());

                    //combine script files into analysisGuid.R
                    string scriptFileName = Path.Combine(workingDir, analysisGuid + ".R");

                    List <string> scriptLines = new List <string>();
                    scriptLines.AddRange(File.ReadAllLines(Path.Combine(Startup.ContentRootPath, "Scripts", "Common_Functions.R")));
                    scriptLines.AddRange(File.ReadAllLines(Path.Combine(Startup.ContentRootPath, "Scripts", analysis.Script.ScriptFileName + ".R")));

                    if (analysisModel.CustomRCode != null)
                    {
                        scriptLines.Add(analysisModel.CustomRCode);
                    }

                    File.WriteAllLines(scriptFileName, scriptLines);

                    //csvfilename is built from the analysis guid and is also used in R to name the output at this time
                    string csvFileName = Path.Combine(workingDir, analysisGuid + ".csv");

                    if (analysisModel is AnalysisDataModelBase analysisDataModelBase) //then has data component
                    {
                        string[] csvData = analysisDataModelBase.ExportData();
                        File.WriteAllLines(csvFileName, csvData);
                    }

                    //setup the r process (way of calling rscript.exe is slightly different for each OS)
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        if (!String.IsNullOrEmpty(appSettings.CustomRScriptLocation))
                        {
                            rscriptPath = appSettings.CustomRScriptLocation;
                        }
                        else
                        {
                            rscriptPath = Path.Combine(Startup.ContentRootPath, "R", "bin", "Rscript.exe");
                        }
                    }
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                    {
                        if (!String.IsNullOrEmpty(appSettings.CustomRScriptLocation))
                        {
                            rscriptPath = appSettings.CustomRScriptLocation;
                        }
                        else
                        {
                            rscriptPath = "Rscript";
                        }
                    }
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                    {
                        if (!String.IsNullOrEmpty(appSettings.CustomRScriptLocation))
                        {
                            rscriptPath = appSettings.CustomRScriptLocation;
                        }
                        else
                        {
                            rscriptPath = "/usr/local/bin/Rscript";
                        }
                    }

                    ProcessStartInfo psi = new ProcessStartInfo();
                    psi.FileName         = rscriptPath;
                    psi.WorkingDirectory = workingDir;

                    theArguments  = analysisModel.GetCommandLineArguments();
                    psi.Arguments = FormatPreArgument(scriptFileName) + " --vanilla --args " + FormatPreArgument(csvFileName) + " " + theArguments;

                    //Configure some options for the R process
                    psi.UseShellExecute        = false;
                    psi.CreateNoWindow         = true;
                    psi.RedirectStandardOutput = true;
                    psi.RedirectStandardError  = true;

                    //start rscript.exe
                    Process R = Process.Start(psi);

                    //create a stringbuilder to hold the output, and get the output line by line until R process finishes
                    StringBuilder output = new StringBuilder();

                    bool completedOK = R.WaitForExit(10 * 60 * 1000); //10 minutes!

                    if (completedOK)
                    {
                        //need to make sure that we have got all the output so do a readtoend here
                        output.AppendLine(R.StandardOutput.ReadToEnd());
                        output.AppendLine();

                        //output the errors from R
                        string errorsFromR = R.StandardError.ReadToEnd().Trim();

                        R.Close();
                        R.Dispose();

                        if (!String.IsNullOrEmpty(errorsFromR))
                        {
                            output.AppendLine();
                            output.Append(errorsFromR);
                            output.AppendLine();
                        }
                    }
                    else //timed out, try and kill it (but usually doesnt work)
                    {
                        output.AppendLine("WARNING! The R process timed out before the script could complete");
                        output.AppendLine();

                        //get the id so can really check if it has died
                        int processID = R.Id;

                        //try and kill it
                        R.Kill();
                        R.WaitForExit(5000); //wait 5 seconds to exit, but this usually doesnt work
                        R.Dispose();

                        if (Process.GetProcesses().Any(x => x.Id == processID)) //then R failed to exit
                        {
                            throw new TimeoutException("R timed out and failed to exit gracefully, aborting analysis without reading results or log. You may need to manually kill the Rscript process. Partial results and log may be in the temp folder.");
                        }
                    }

                    TimeSpan timeElapsed = sw.Elapsed;
                    output.AppendLine();
                    output.AppendLine("Analysis by the R Processor took " + Math.Round(timeElapsed.TotalSeconds, 2) + " seconds.");

                    analysis.RProcessOutput = output.ToString().Trim();

                    //assemble the entire path and file to the html output
                    string htmlFile = Path.Combine(workingDir, analysisGuid + ".html");

                    if (File.Exists(htmlFile)) //won't exist if there is an error!
                    {
                        DirectoryInfo dir         = new DirectoryInfo(workingDir);
                        FileInfo[]    outputFiles = dir.GetFiles(analysis.AnalysisGuid + "*");

                        //first go through all the results files...
                        List <string> resultsFiles = new List <string>();

                        foreach (FileInfo file in outputFiles.Where(x => x.Extension == ".html" || x.Extension == ".png")) //go through all results output
                        {
                            resultsFiles.Add(file.FullName);
                        }

                        //generate the inline html
                        string inlineHtml = InlineHtmlCreator.CreateInlineHtml(resultsFiles);
                        analysis.HtmlOutput = inlineHtml;

                        //do a save at this point so that results can be shown (processing is checking for output at this point)
                        await silveRRepository.UpdateAnalysis(analysis);

                        //now go through any csv output to be imported into datasets
                        foreach (FileInfo file in outputFiles.Where(x => x.Extension == ".csv" && !x.FullName.EndsWith(analysis.AnalysisGuid + ".csv"))) //go through any dataset output (make sure dont import original csv file!)
                        {
                            DataTable dataTable = CSVConverter.CSVDataToDataTable(file.OpenRead());                                                      //because R will write out using , separator

                            string datasetName = file.Name.Replace(analysis.AnalysisGuid, String.Empty);
                            await SaveDatasetToDatabase(silveRRepository, datasetName, dataTable);
                        }
                    }
                    else //something not right then, throw exception
                    {
                        throw new InvalidOperationException("No html output found!" + Environment.NewLine + "RProcessOutput:" + Environment.NewLine + analysis.RProcessOutput);
                    }

#if !DEBUG
                    string[] filesToClean = Directory.GetFiles(workingDir, analysis.AnalysisGuid + "*");
                    foreach (string file in filesToClean)
                    {
                        File.Delete(file);
                    }
#endif
                }
                catch (System.ComponentModel.Win32Exception ex) when(ex.NativeErrorCode == 2)
                {
                    Analysis analysis = await silveRRepository.GetAnalysis(analysisGuid);

                    string message = "Rscript cannot be found, so no analysis was run." + Environment.NewLine;

                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        message = message + "Your R installation (specifically Rscript.exe) is missing - please reinstall this application.";
                    }
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                    {
                        message = message + "No R install could be found on this system. It is recommended that you copy and run the following shell script to install the correct version of R. Then, click the 'Install R packages' button under settings to install the correct R packages." + Environment.NewLine;
                        message = message + "apt-get install r-base -y";
                    }
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                    {
                        message = message + "No R install could be found on this system. It is recommended that you download and install the following R package (https://cran.r-project.org/bin/macosx/R-3.5.2.pkg) into your OSX system. Then, click the 'Install R packages' button under settings to install the correct R packages.";
                    }

                    analysis.RProcessOutput = message;
                    await silveRRepository.UpdateAnalysis(analysis);
                }
                catch (Exception ex)
                {
                    Analysis analysis = await silveRRepository.GetAnalysis(analysisGuid);

                    string message = "ContentRoot=" + Startup.ContentRootPath + Environment.NewLine + Environment.NewLine;
                    message = message + "TempFolder=" + workingDir + Environment.NewLine + Environment.NewLine;
                    message = message + "Arguments=" + theArguments + Environment.NewLine + Environment.NewLine;
                    message = message + "Rscript=" + rscriptPath + Environment.NewLine + Environment.NewLine;

                    message = message + ex.ToString();

                    analysis.RProcessOutput = message;
                    await silveRRepository.UpdateAnalysis(analysis);
                }
            }
        }
 public SalesBusiness(ISaleRepository saleRepository, CSVConverter converter)
 {
     _saleRepository = saleRepository;
     Converter       = converter;
 }
Ejemplo n.º 22
0
        public void CSVReadAndWriteTest()
        {
            //Support List<> data type.
            Debug.Log("Auto generate data list:");
            foreach (var data in CSVConverter.ConvertColumn <List <float> >(
                         new CSVTableWriter()
                         .AddHeader(new CSVRecordWriter {
                "Item1"
            })
                         .AddHeader(new CSVRecordWriter {
                "List<float>"
            })
                         .AddRecord(new CSVRecordWriter {
                "1.25|3.33|2.5|4"
            })
                         .AddRecord(new CSVRecordWriter {
                "2.5|4|5.1"
            })
                         .GetEncodeTable()))
            {
                Debug.Log(string.Join("|", data));
            }

            //Using ValueTuple as the data type.
            Debug.Log("Auto generate data list:");
            foreach (var data in CSVConverter.Convert <ValueTuple <float, string> >(
                         new CSVTableWriter()
                         .AddHeader(new CSVRecordWriter {
                "Item1", "Item2"
            })
                         .AddHeader(new CSVRecordWriter {
                "float", "string"
            })
                         .AddRecord(new CSVRecordWriter {
                "1.25", "first"
            })
                         .AddRecord(new CSVRecordWriter {
                "2.5", "second"
            })
                         .GetEncodeTable()))
            {
                Debug.Log(data);
            }

            //Custom cell separator.
            CSVTableWriter csvTableWriter = CSVGenerator.Class2CSVTable <ExampleTestData>(';');

            Debug.Log("Auto generate csv:\n" + csvTableWriter.GetEncodeTable(NewLineStyle.NonUnix));

            string classStr = CSVGenerator.CSV2Class(csvTableWriter.GetEncodeTable(NewLineStyle.NonUnix), null, nameof(ExampleTestData), csvTableWriter.CellSeparator);

            Debug.Log("Auto generate class:\n" + classStr);

            csvTableWriter.AddRecord(new CSVRecordWriter()
                                     .Add("1")
                                     .Add("#cccccc")
                                     .Add("2")
                                     .Add("normal string")
                                     .Add("\"string with double quote")
                                     .Add("1;2;3|4;5;6")
                                     .Add("#cccccc;string content")
                                     .Add("#cccccc;string content|#ffffff;second string")
                                     .Add("#cccccc;string content|#ffffff;second string"));
            csvTableWriter.AddRecord(new CSVRecordWriter()
                                     .Add("3")
                                     .Add("#dddddd")
                                     .Add("4")
                                     .Add("string with, comma")
                                     .Add("\"string with\", comma and \"double quote")
                                     .Add("7;8;9|10;11;12|7;7;7")
                                     .Add("#dddddd;string content2")
                                     .Add("#dddddd;string content2|#eeeeee;second string2")
                                     .Add("#dddddd;string content2|#eeeeee;second string2"));
            Debug.Log("csv add data:\n" + csvTableWriter.GetEncodeTable(NewLineStyle.NonUnix));

            var dataList = CSVConverter.Convert <ExampleTestData>(csvTableWriter.GetEncodeTable(NewLineStyle.NonUnix), csvTableWriter.CellSeparator);

            Debug.Log("Auto generate data list:");
            foreach (var data in dataList)
            {
                Debug.Log(data);
            }

            Debug.Log("Auto generate data IEnumerable:");
            foreach (var data in CSVConverter.ConvertEnumerator <ExampleTestData>(csvTableWriter.GetEncodeTable(NewLineStyle.NonUnix), csvTableWriter.CellSeparator))
            {
                Debug.Log(data);
            }

            string newContent = new CSVTableWriter()
                                .AddHeader(new CSVRecordWriter {
                "Name", "Age"
            })
                                .AddHeader(new CSVRecordWriter {
                "string", "int"
            })
                                .AddRecord(new CSVRecordWriter {
                "Name1", "10"
            })
                                .AddRecord(new CSVRecordWriter {
                "Name2", "20"
            })
                                .AddRecord(new CSVRecordWriter {
                "Name3", "30"
            })
                                .AddRecord(new CSVRecordWriter {
                "Name4", "40"
            })
                                .GetEncodeTable(NewLineStyle.NonUnix);

            Debug.Log("Auto generate column data by header name:");
            foreach (var name in CSVConverter.ConvertColumn <string>(newContent, "Name"))
            {
                Debug.Log(name);
            }

            Debug.Log("Auto generate column data by header index:");
            foreach (var age in CSVConverter.ConvertColumn <int>(newContent, 1))
            {
                Debug.Log(age);
            }
        }