private bool TryReadLineInfo(out LineArgs lineInfo) { var attempt = 0; lineInfo.LineAmount = 0; lineInfo.DelayMs = 0; lineInfo.ReadCount = 0; do { var command = Console.ReadLine().Trim(); if (command == "q") { return(false); } var commandArgs = command.Split(' '); if (TryParseCommandArgs(commandArgs, out lineInfo)) { return(true); } attempt++; Console.WriteLine($"Invalid command: '{command}'. Please enter 'q' to quit, or a valid number or two or three."); } while (attempt < 5); Console.Error.WriteLine("Too many invalid attempts. Exiting program"); return(false); }
/// <summary> /// </summary> /// <param name="networkMapId">Network Map record identifier</param> /// <param name="target"></param> /// <param name="noNetworkMessages">When True only File Values are used in the Target</param> /// <returns></returns> public CriteriaMatchModel GetCriteriaMatches(int networkMapId, LineArgs target, bool noNetworkMessages) { return(NetworkMapService.GetCriteriaMatches(networkMapId, MainViewModel.Current.CurrentLineArgs, target, noNetworkMessages)); }
/// <summary> /// Returns a list of FileData that successfully match the Network Maps /// Note: This is only performing a FileData Value(s) match and does not look at EventPattern Value(s) /// </summary> public static List <NetworkMapFile> GetNetworkMapFiles(LineArgs model) { var stopwatch = new Stopwatch(MethodBase.GetCurrentMethod().Name, true); try { var sourceFile = XmlDal.CacheModel.GetFile(model.Path); if (sourceFile == null) { Log.WriteError("sourceFile should never be null!", typeof(NetworkMapService).FullName, MethodBase.GetCurrentMethod().Name); return(new List <NetworkMapFile>()); } var source = sourceFile.GetNetworkMessage(model.iLine); if (source == null) { Log.WriteError("source should never be null!", typeof(NetworkMapService).FullName, MethodBase.GetCurrentMethod().Name); return(new List <NetworkMapFile>()); } return(GetNetworkMapFiles(model.Line, source)); } finally { stopwatch.Stop(100); } }
/// <summary> /// Returns the properties for the current item /// </summary> public static List <SimpleProperty> GetCurrentLineValues(LineArgs item) { var property = PropertyService.GetProperty(item); var result = property.Properties.Select(prop => new SimpleProperty { Name = prop.Name, Value = (string)prop.Value ?? "", Type = "EventPattern" }).ToList(); result = result.OrderBy(n => n.Name).ToList(); var file = XmlDal.CacheModel.GetFile(item.Path); var result2 = file.FileValues.Where(n => n.FileInfo).Select(fileInfo => new SimpleProperty { Name = fileInfo.Name, Value = fileInfo.Value ?? "", Type = "FileData" }).ToList(); result2 = result2.OrderBy(n => n.Name).ToList(); result.AddRange(result2); return(result); }
/// <summary> /// Takes the source and finds the matching Netork Messages /// If the Source FileData has not cached it's Network Messages, then this method will Cache them /// If the Target FileData's have not cached their Network Messages, then this method will cache them /// If the Source Network Message has not cached it's Target's then this method will cache them /// Once all the cache items are out of the way, the system will call the Callback with the Source Network Message Target's /// </summary> /// <remarks> /// This method contains Locks and will block. Make sure you do not call this on the Main UI Thread! /// </remarks> public static NetworkTargets GetNetworkMessagesBySourceLine(LineArgs source) { var stopwatch = new Stopwatch(MethodBase.GetCurrentMethod().Name, true); try { var sourceFile = XmlDal.CacheModel.GetFile(source.Path); NetworkService.CheckNetworkMessages(sourceFile, source.StatusUpdate, "Source"); // Load the Source Network Message var sourceMessage = sourceFile.GetNetworkMessage(source.iLine); if (sourceMessage == null) { Log.Write("netSource should never be null!", typeof(NetworkMapService).FullName, MethodBase.GetCurrentMethod().Name); return(new NetworkTargets()); } lock (sourceMessage.InternalTargets) { // Make sure the Source Network Message Target's are cached if (!sourceMessage.IsCached_Targets) { sourceMessage.InternalTargets.Clear(); var targetFiles = GetNetworkMapFiles(source); foreach (var map in targetFiles) { NetworkService.CheckNetworkMessages(map.File, source.StatusUpdate, "Target"); // There is no need to show more then 50 Network Messages, since there should only be 1! if (sourceMessage.InternalTargets.Count >= 50) { break; } var result = GetNetworkMessageByTargetFile(source, sourceMessage, map.File); sourceMessage.InternalTargets.AddRange(result.Select(arg => new SourceLocator { Path = arg.Item.Source.FilePath, iLine = arg.Item.Source.iLine, NetworkMapId = arg.NetworkMapId })); } sourceMessage.HasTargetFiles = targetFiles.Count > 0; sourceMessage.IsCached_Targets = true; } } return(new NetworkTargets { HasTargetFiles = sourceMessage.HasTargetFiles, Targets = sourceMessage.Targets }); } finally { stopwatch.Stop(200); } }
/// <summary> /// Parse command args. If supplied, must be valid. If not supplied will default. /// </summary> private bool TryParseCommandArgs(string[] commandArgs, out LineArgs lineInfo) { lineInfo.LineAmount = 0; lineInfo.DelayMs = 0; lineInfo.ReadCount = 1; if (commandArgs.Length >= 3) { if (!int.TryParse(commandArgs[2], out lineInfo.ReadCount)) { return(false); } else if (lineInfo.ReadCount <= 0 || lineInfo.ReadCount > 100) { lineInfo.ReadCount = 1; } } else { lineInfo.ReadCount = 1; } if (commandArgs.Length >= 2) { if (!int.TryParse(commandArgs[1], out lineInfo.DelayMs)) { return(false); } else if (lineInfo.DelayMs < 0 || lineInfo.DelayMs > 5000) { lineInfo.DelayMs = 0; } } else { lineInfo.DelayMs = 0; } if (commandArgs.Length >= 1) { if (!int.TryParse(commandArgs[0], out lineInfo.LineAmount)) { return(false); } else if (lineInfo.LineAmount <= 0) { lineInfo.LineAmount = 1; } } else { lineInfo.LineAmount = 1; } return(true); }
public static List <CriteriaMatch> GetRecommendedMatches(LineArgs source, LineArgs target) { var result = new List <CriteriaMatch>(); var sourceMessage = XmlDal.CacheModel.GetFile(source.Path).GetNetworkMessage(source.iLine); if (sourceMessage == null) { return(new List <CriteriaMatch>()); } var targetMessage = XmlDal.CacheModel.GetFile(target.Path).GetNetworkMessage(target.iLine); if (targetMessage == null) { return(result); } foreach (var sourceValues in sourceMessage.Source.Values) { foreach (var targetValues in targetMessage.Source.Values) { if (sourceValues.Name == targetValues.Name || (sourceValues.Value == targetValues.Value && !string.IsNullOrEmpty(sourceValues.Value) && !string.IsNullOrEmpty(targetValues.Value))) { var model = new CriteriaMatch { SourceName = sourceValues.Name, SourceType = "N/A", SourceValue = sourceValues.Value, TargetName = targetValues.Name, TargetType = "N/A", TargetValue = targetValues.Value }; var exist = result.Any(item => String.Equals(item.SourceName, model.SourceName, StringComparison.CurrentCultureIgnoreCase) && String.Equals(item.SourceType, model.SourceType, StringComparison.CurrentCultureIgnoreCase) && String.Equals(item.SourceValue, model.SourceValue, StringComparison.CurrentCultureIgnoreCase) && String.Equals(item.TargetName, model.TargetName, StringComparison.CurrentCultureIgnoreCase) && String.Equals(item.TargetType, model.TargetType, StringComparison.CurrentCultureIgnoreCase) && String.Equals(item.TargetValue, model.TargetValue, StringComparison.CurrentCultureIgnoreCase)); if (!exist) { result.Add(model); } } } } return(result); }
private LogPropertyBaseModel GetProperty(LineArgs args) { lock (this) { var item = _properties.SingleOrDefault(n => n.iLine == args.iLine); if (item != null) { return(item); } item = PropertyService.GetProperty(args); _properties.Add(item); return(item); } }
/// <summary> /// Returns a list of EventPattern and FileData values /// </summary> public static List <SimpleProperty> GetPropertyValues(LineArgs model) { var result = new List <SimpleProperty>(); var file = XmlDal.CacheModel.GetFile(model.Path); var unload = false; try { if (!file.IsLoaded) { file.Load(true); unload = true; } var property = PropertyService.GetProperty(model); result.AddRange(property.Properties.Select(prop => new SimpleProperty { Name = prop.Name, Value = (string)prop.Value ?? "", Type = "EventPattern" })); result = result.OrderBy(n => n.Name).ToList(); var result2 = file.FileValues.Where(n => n.FileInfo).Select(item => new SimpleProperty { Name = item.Name, Value = item.Value ?? "", Type = "FileData" }).ToList(); result2 = result2.OrderBy(n => n.Name).ToList(); result.AddRange(result2); return(result); } finally { if (unload) { file.UnLoad(); } } }
private bool TryReadWriteLines(StreamReader sr, StreamWriter sw, LineArgs lineInfo, out int linesRead, out int linesWritten) { string line = null; linesRead = 0; linesWritten = 0; var totalLinesToRead = lineInfo.LineAmount * lineInfo.ReadCount; while (linesRead < totalLinesToRead && (line = sr.ReadLine()) != null) { if (linesRead > 0 && linesRead % lineInfo.LineAmount == 0) { Thread.Sleep(lineInfo.DelayMs); } linesRead++; sw.WriteLine(line); linesWritten++; } sw.Flush(); return(line != null); }
/// <summary> /// Returns a Property object that combines all Events into a single data object /// </summary> public static LogPropertyBaseModel GetProperty(LineArgs model) { return(LookupEvent(model.Path, new LogPropertyBaseModel { Line = model.Line, iLine = model.iLine })); }
public List <CriteriaMatch> GetRecommendedMatches(LineArgs target) { return(NetworkMapService.GetRecommendedMatches(MainViewModel.Current.CurrentLineArgs, target)); }
/// <summary> /// Returns a list of NetworkMessageInfoModels that map to the Source NetworkMessageInfo for the given Target FileData /// </summary> public static IEnumerable <NetworkMessageInfoModel> GetNetworkMessageByTargetFile(LineArgs sourceArgs, NetworkMessageInfo sourceMsg, FileData targetFile) { try { sourceArgs.StatusUpdate(StatusModel.StartStopWatch); sourceArgs.StatusUpdate(StatusModel.Update("Linking Network Messages", "")); lock (targetFile.Network) { var stopwatch = new Stopwatch(MethodBase.GetCurrentMethod().Name); try { var result = new List <NetworkMessageInfoModel>(); foreach (var map in GetNetworkMapsByLine(sourceArgs.Line).OrderBy(n => n.Priority)) { if (result.Count > 0 && map.OnlyUseFallThrough) { continue; } // If there is NO EventPattern Value(s) criteria, then add all Network Messages for the given FileData if (!map.Criteria.Exists(n => n.TargetType == MappingDataType.EventValue && n.Enabled)) { foreach (var message in targetFile.Network.NetworkMessages) { AddMessage(result, message, map); } } else { foreach (var targetMsg in targetFile.Network.NetworkMessages) { bool?success = null; foreach (var criteria in map.Criteria.Where(n => n.TargetType == MappingDataType.EventValue && n.Enabled)) { success = IsMatch(criteria, sourceMsg, targetMsg, targetFile.Path).IsMatch; if (success != null && (bool)!success) { break; } } if (success != null && (bool)success) { AddMessage(result, targetMsg, map); } } } } return(result); } finally { stopwatch.Stop(500); } } } finally { sourceArgs.StatusUpdate(StatusModel.Completed); } }
/// <summary> /// Using the Callback, returns a list of Target NetworkMessages based on the Source LineFS /// Runs on a background Thread /// </summary> public static void GetNetworkMessagesBySourceLine(LineArgs source, Action <NetworkTargets> callback) { ThreadPool.QueueUserWorkItem(GetNetworkMessagesBySourceLineAsync, new GetNetworkMessagesArgs { Source = source, Callback = callback }); }
public static CriteriaMatchModel GetCriteriaMatches(int networkMapId, LineArgs source, LineArgs target, bool noNetworkMessages) { var matches = new List <CriteriaMatch>(); var map = XmlDal.DataModel.GetNetworkMap(networkMapId); var sourceMessage = XmlDal.CacheModel.GetFile(source.Path).GetNetworkMessage(source.iLine); if (sourceMessage == null) { Log.WriteError("sourceMessage should never be null!!!!", typeof(NetworkMapService).FullName, MethodBase.GetCurrentMethod().Name); return(new CriteriaMatchModel()); } var targetMessage = XmlDal.CacheModel.GetFile(target.Path).GetNetworkMessage(target.iLine); foreach (var criteria in map.Criteria) { var match = IsMatch(criteria, sourceMessage, targetMessage, target.Path); if (criteria.UseSourceValue) { match.SourceValue = $"=={match.CriteriaSourceValue} [{match.SourceValue}]"; } if (criteria.UseTargetValue) { match.TargetValue = $"=={match.CriteriaTargetValue} [{match.TargetValue}]"; } if (criteria.TimeConditionMs != null) { match.TargetValue = $"{match.TargetValue} tc:{criteria.TimeConditionMs}"; } matches.Add(new CriteriaMatch { Enabled = criteria.Enabled, IsMatch = match.IsMatch, Operator = criteria.Operator, SourceName = criteria.SourceName, SourceType = criteria.SourceType.ToString(), SourceValue = match.SourceValue, TargetName = criteria.TargetName, TargetType = criteria.TargetType.ToString(), TargetValue = match.TargetValue }); } LogPropertyBaseModel targetProperty = null; if (!noNetworkMessages) { targetProperty = PropertyService.GetProperty(target); } var sourceProperty = PropertyService.GetProperty(source); var dateTimeDifference = CalculateDateTimeDifference(targetProperty, sourceProperty); return(new CriteriaMatchModel { DateTimeDifference = dateTimeDifference, Matches = matches }); }