public ActionResult Import(HttpPostedFileBase file) { if( file.ContentLength>0 ) { using(var sr = new StreamReader(file.InputStream)) { var engine = new DelimitedFileEngine<WareCatalogItem>(Encoding.UTF8); var items = engine.ReadStream(sr); var existingWares = Session.QueryOver<Ware>().List(); var wareMap = existingWares.ToDictionary(x => x.Sku); foreach(var item in items) { if( wareMap.ContainsKey(item.Sku) ) { var ware = wareMap[item.Sku]; ware.Name = item.Name; } else { var ware = new Ware {Sku = item.Sku, Name = item.Name}; Session.Save(ware); } } } } return RedirectToAction("Import"); }
public void AddRegisteredCustomMetrics() { if (File.Exists(AgentContext.CustomMetricRegisterFilePath)) { // Acquire lock FileStream csvFile = null; for (var i = 0; i < 10; i++) { try { csvFile = File.Open(AgentContext.CustomMetricRegisterFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); break; } catch { Thread.Sleep(1000); } } if (csvFile == null) { Log.Warn(string.Format("Could not acquire lock on {0}; Skipping registered custom metrics", AgentContext.CustomMetricRegisterFilePath)); return; } // Read current metrics var engine = new DelimitedFileEngine(typeof(RegisterHelper)); try { var registeredMetrics = engine.ReadStream(new StreamReader(csvFile)) as RegisterHelper[]; Register = registeredMetrics.GroupBy(r => r.Textkey).ToDictionary(g => g.Key, g => g.First().Description); } catch { Log.Warn(string.Format("There is a problem with the format of {0}; Skipping registered custom metrics", AgentContext.CustomMetricRegisterFilePath)); csvFile.Close(); return; } // Remove all synced metrics csvFile.SetLength(0); // Release lock csvFile.Close(); } }
public Dictionary<string, List<ReportHelper>> GetMetricValues() { if (File.Exists(AgentContext.CustomMetricReportFilePath)) { // Acquire lock FileStream csvFile = null; for (var i = 0; i < 10; i++) { try { csvFile = File.Open(AgentContext.CustomMetricReportFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); break; } catch { System.Threading.Thread.Sleep(1000); } } if (csvFile == null) { Log.Warn(string.Format("Could not acquire lock on {0}; Skipping reported custom metrics", AgentContext.CustomMetricReportFilePath)); return new Dictionary<string, List<ReportHelper>>(); } // Read current metrics var uniqueValues = new Dictionary<string, ReportHelper>(); var engine = new DelimitedFileEngine(typeof(ReportHelper)); try { var reportMetrics = engine.ReadStream(new StreamReader(csvFile)) as ReportHelper[]; foreach (var item in reportMetrics) { try { var timestamp = DateTime.ParseExact(item.Timestamp, "yyyy-MM-dd HH:mm:ss", null); var value = Double.Parse(item.Value); uniqueValues[string.Format("{0}:{1}", item.Textkey, timestamp.ToString("yyyy-MM-ddHH:mm"))] = item; } catch { continue; } } var uniqueMetricList = uniqueValues.Values.ToList(); uniqueMetricList = uniqueMetricList.OrderBy(s => s.Timestamp).ToList(); var customValues = new Dictionary<string, List<ReportHelper>>(); foreach (var metric in uniqueMetricList) { if (!customValues.ContainsKey(metric.Textkey)) { customValues[metric.Textkey] = new List<ReportHelper> { metric }; } else { customValues[metric.Textkey].Add(metric); } } // Remove all synced metrics csvFile.SetLength(0); // Release lock csvFile.Close(); return customValues; } catch { Log.Warn(string.Format("There is a problem with the format of {0}; Skipping reported custom metrics", AgentContext.CustomMetricReportFilePath)); csvFile.Close(); return new Dictionary<string, List<ReportHelper>>(); } } else { return new Dictionary<string, List<ReportHelper>>(); } }