1
0
mirror of https://github.com/BDDSM/YY.EventLogReaderAssistant.git synced 2024-11-21 10:05:51 +02:00

Рефакторинг модулей решения.

This commit is contained in:
YPermitin 2020-04-13 00:28:43 +05:00
parent cd309668c7
commit 34bcfa1f8a
11 changed files with 151 additions and 223 deletions

View File

@ -10,8 +10,8 @@ namespace YY.EventLogAssistant.Services.Tests
{
#region Private Member Variables
private string sampleDataDirectory;
private string sampleDatabaseFile;
private readonly string sampleDataDirectory;
private readonly string sampleDatabaseFile;
#endregion
@ -48,14 +48,12 @@ namespace YY.EventLogAssistant.Services.Tests
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
connection.Open();
using (SQLiteCommand command = new SQLiteCommand(queryText, connection))
using SQLiteCommand command = new SQLiteCommand(queryText, connection);
SQLiteDataReader reader = command.ExecuteReader();
if (reader.Read())
{
SQLiteDataReader reader = command.ExecuteReader();
if (reader.Read())
{
DataPresentation = SQLiteExtensions.GetStringOrDefault(reader, 0);
DataPresentationEmpty = SQLiteExtensions.GetStringOrDefault(reader, 1);
}
DataPresentation = SQLiteExtensions.GetStringOrDefault(reader, 0);
DataPresentationEmpty = SQLiteExtensions.GetStringOrDefault(reader, 1);
}
}
@ -83,14 +81,12 @@ namespace YY.EventLogAssistant.Services.Tests
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
connection.Open();
using (SQLiteCommand command = new SQLiteCommand(queryText, connection))
using SQLiteCommand command = new SQLiteCommand(queryText, connection);
SQLiteDataReader reader = command.ExecuteReader();
if (reader.Read())
{
SQLiteDataReader reader = command.ExecuteReader();
if (reader.Read())
{
connectionId = SQLiteExtensions.GetInt64OrDefault(reader, 0);
sessionId = SQLiteExtensions.GetInt64OrDefault(reader, 1);
}
connectionId = SQLiteExtensions.GetInt64OrDefault(reader, 0);
sessionId = SQLiteExtensions.GetInt64OrDefault(reader, 1);
}
}
@ -119,13 +115,11 @@ namespace YY.EventLogAssistant.Services.Tests
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
connection.Open();
using (SQLiteCommand command = new SQLiteCommand(queryText, connection))
using SQLiteCommand command = new SQLiteCommand(queryText, connection);
SQLiteDataReader reader = command.ExecuteReader();
if (reader.Read())
{
SQLiteDataReader reader = command.ExecuteReader();
if (reader.Read())
{
rowAsString = reader.GetRowAsString();
}
rowAsString = reader.GetRowAsString();
}
}
int countLines = rowAsString.Split('\n').Where(str => str != string.Empty).Count();
@ -153,16 +147,14 @@ namespace YY.EventLogAssistant.Services.Tests
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
connection.Open();
using (SQLiteCommand command = new SQLiteCommand(queryText, connection))
using SQLiteCommand command = new SQLiteCommand(queryText, connection);
SQLiteDataReader reader = command.ExecuteReader();
if (reader.Read())
{
SQLiteDataReader reader = command.ExecuteReader();
if (reader.Read())
object rowsCountObject = reader.GetValue(0);
if (rowsCountObject is long)
{
object rowsCountObject = reader.GetValue(0);
if (rowsCountObject is long)
{
rowsCount = Convert.ToInt64(rowsCountObject);
}
rowsCount = Convert.ToInt64(rowsCountObject);
}
}
}

View File

@ -10,9 +10,9 @@ namespace YY.EventLogAssistant.Services.Tests
{
#region Private Member Variables
private string sampleDataDirectory;
private string sampleDatabaseFile;
private string[] sampleFilesLGP;
private readonly string sampleDataDirectory;
private readonly string sampleDatabaseFile;
private readonly string[] sampleFilesLGP;
#endregion
@ -38,17 +38,13 @@ namespace YY.EventLogAssistant.Services.Tests
string lineContent = string.Empty;
using (StreamReader reader = new StreamReader(sampleDatabaseFile))
{
using(StreamLineReader lineReader = new StreamLineReader(reader.BaseStream, reader.CurrentEncoding))
using StreamLineReader lineReader = new StreamLineReader(reader.BaseStream, reader.CurrentEncoding);
if (lineReader.GoToLine(1))
{
if(lineReader.GoToLine(1))
{
lineContent = lineReader.ReadLine();
}
lineContent = lineReader.ReadLine();
}
}
Guid eventLogGuid = Guid.Empty;
Guid.TryParse(lineContent, out eventLogGuid);
Guid.TryParse(lineContent, out Guid eventLogGuid);
Assert.NotEqual(Guid.Empty, eventLogGuid);
}
@ -68,10 +64,8 @@ namespace YY.EventLogAssistant.Services.Tests
long lineCounterLibrary = 0;
using (StreamReader reader = new StreamReader(sampleDatabaseFile))
{
using (StreamLineReader lineReader = new StreamLineReader(reader.BaseStream, reader.CurrentEncoding))
{
lineCounterLibrary = lineReader.GetCount();
}
using StreamLineReader lineReader = new StreamLineReader(reader.BaseStream, reader.CurrentEncoding);
lineCounterLibrary = lineReader.GetCount();
}
Assert.Equal(lineCounterNative, lineCounterLibrary);
@ -97,15 +91,13 @@ namespace YY.EventLogAssistant.Services.Tests
List<string> resultLines = new List<string>();
using (StreamReader reader = new StreamReader(sampleDatabaseFile, utf8))
{
using (StreamLineReader lineReader = new StreamLineReader(reader.BaseStream, reader.CurrentEncoding))
using StreamLineReader lineReader = new StreamLineReader(reader.BaseStream, reader.CurrentEncoding);
string currentLine = lineReader.ReadLine();
do
{
string currentLine = lineReader.ReadLine();
do
{
resultLines.Add(currentLine);
currentLine = lineReader.ReadLine();
} while (currentLine != null);
}
resultLines.Add(currentLine);
currentLine = lineReader.ReadLine();
} while (currentLine != null);
}
Assert.Equal(correctLines, resultLines);
@ -134,36 +126,16 @@ namespace YY.EventLogAssistant.Services.Tests
using (StreamReader reader = new StreamReader(fileLGP, utf8))
{
using (StreamLineReader lineReader = new StreamLineReader(reader.BaseStream, reader.CurrentEncoding))
using StreamLineReader lineReader = new StreamLineReader(reader.BaseStream, reader.CurrentEncoding);
string currentLine = lineReader.ReadLine();
while (currentLine != null)
{
string currentLine = lineReader.ReadLine();
while (currentLine != null)
{
resultLines.Add(currentLine);
int cnt = resultLines.Count - 1;
if (correctLines[cnt] != resultLines[cnt])
{
int fff = 1;
} else if(cnt == 173)
{
int sdafdfa = 1;
}
currentLine = lineReader.ReadLine();
}
resultLines.Add(currentLine);
currentLine = lineReader.ReadLine();
}
}
}
//for (int i = 0; i < correctLines.Count; i++)
//{
// if(correctLines[i] != resultLines[i])
// {
// int fff = 1;
// }
//}
Assert.Equal(correctLines, resultLines);
}

View File

@ -8,8 +8,8 @@ namespace YY.EventLogAssistant.Services.Tests
{
#region Private Member Variables
private string sampleDataDirectory;
private string sampleDatabaseFile;
private readonly string sampleDataDirectory;
private readonly string sampleDatabaseFile;
#endregion

View File

@ -62,8 +62,6 @@ namespace YY.EventLogAssistant.Services.Tests
public void FromWin1251ToUTF8_Test()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding utf8 = Encoding.GetEncoding("UTF-8");
Encoding win1251 = Encoding.GetEncoding("windows-1251");
string sourceText = "Заказ звонка технической поддержки";
string checkValue = "Заказ звонка технической поддержки";

View File

@ -22,7 +22,7 @@ namespace YY.EventLogAssistant
}
}
private SQLiteConnection _connection;
private List<RowData> _readBuffer;
private readonly List<RowData> _readBuffer;
private long _lastRowId;
private const int _readBufferSize = 1000;
private long _lastRowNumberFromBuffer;
@ -107,28 +107,28 @@ namespace YY.EventLogAssistant
{
try
{
RowData bufferRowData = new RowData();
bufferRowData.RowID = reader.GetInt64OrDefault(0);
bufferRowData.Period = reader.GetInt64OrDefault(1).ToDateTimeFormat();
bufferRowData.ConnectId = reader.GetInt64OrDefault(2);
bufferRowData.Session = reader.GetInt64OrDefault(3);
bufferRowData.TransactionStatus = (TransactionStatus)reader.GetInt64OrDefault(4);
bufferRowData.TransactionDate = reader.GetInt64OrDefault(5).ToNullableDateTimeELFormat();
bufferRowData.TransactionId = reader.GetInt64OrDefault(6);
bufferRowData.Severity = (Severity)reader.GetInt64OrDefault(15);
bufferRowData.Comment = reader.GetStringOrDefault(16);
bufferRowData.Data = reader.GetStringOrDefault(17).FromWin1251ToUTF8();
bufferRowData.DataPresentation = reader.GetStringOrDefault(18);
bufferRowData.User = _users.Where(i => i.Code == reader.GetInt64OrDefault(7)).FirstOrDefault();
bufferRowData.Computer = _computers.Where(i => i.Code == reader.GetInt64OrDefault(8)).FirstOrDefault();
bufferRowData.Application = _applications.Where(i => i.Code == reader.GetInt64OrDefault(9)).FirstOrDefault();
bufferRowData.Event = _events.Where(i => i.Code == reader.GetInt64OrDefault(10)).FirstOrDefault();
bufferRowData.PrimaryPort = _primaryPorts.Where(i => i.Code == reader.GetInt64OrDefault(11)).FirstOrDefault();
bufferRowData.SecondaryPort = _secondaryPorts.Where(i => i.Code == reader.GetInt64OrDefault(12)).FirstOrDefault();
bufferRowData.WorkServer = _workServers.Where(i => i.Code == reader.GetInt64OrDefault(13)).FirstOrDefault();
bufferRowData.Metadata = _metadata.Where(i => i.Code == reader.GetInt64OrDefault(18)).FirstOrDefault();
_readBuffer.Add(bufferRowData);
_readBuffer.Add(new RowData
{
RowID = reader.GetInt64OrDefault(0),
Period = reader.GetInt64OrDefault(1).ToDateTimeFormat(),
ConnectId = reader.GetInt64OrDefault(2),
Session = reader.GetInt64OrDefault(3),
TransactionStatus = GetTransactionStatus(reader.GetInt64OrDefault(4)),
TransactionDate = reader.GetInt64OrDefault(5).ToNullableDateTimeELFormat(),
TransactionId = reader.GetInt64OrDefault(6),
User = GetUserByCode(reader.GetInt64OrDefault(7)),
Computer = GetComputerByCode(reader.GetInt64OrDefault(8)),
Application = GetApplicationByCode(reader.GetInt64OrDefault(9)),
Event = GetEventByCode(reader.GetInt64OrDefault(10)),
PrimaryPort = GetPrimaryPortByCode(reader.GetInt64OrDefault(11)),
SecondaryPort = GetSecondaryPortByCode(reader.GetInt64OrDefault(12)),
WorkServer = GetWorkServerByCode(reader.GetInt64OrDefault(13)),
Severity = GetSeverityByCode(reader.GetInt64OrDefault(14)),
Comment = reader.GetStringOrDefault(15),
Data = reader.GetStringOrDefault(16).FromWin1251ToUTF8(),
DataPresentation = reader.GetStringOrDefault(17),
Metadata = GetMetadataByCode(reader.GetInt64OrDefault(18))
});
}
catch (Exception ex)
{
@ -155,7 +155,7 @@ namespace YY.EventLogAssistant
_currentRow = _readBuffer
.Where(bufRow => bufRow.RowID > _lastRowId)
.First();
_lastRowNumberFromBuffer = _lastRowNumberFromBuffer + 1;
_lastRowNumberFromBuffer += 1;
_lastRowId = _currentRow.RowID;
RaiseAfterRead(new AfterReadEventArgs(_currentRow, _eventCount));
@ -237,9 +237,12 @@ namespace YY.EventLogAssistant
"From\n" +
" EventLog el\n");
SQLiteCommand cmd = new SQLiteCommand(_connection);
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = queryText;
SQLiteCommand cmd = new SQLiteCommand(_connection)
{
CommandType = System.Data.CommandType.Text,
CommandText = queryText
};
SQLiteDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{

View File

@ -14,11 +14,11 @@ namespace YY.EventLogAssistant
private const long _defaultBeginLineForLGF = 3;
private int _indexCurrentFile;
private string[] _logFilesWithData;
private readonly string[] _logFilesWithData;
private long _eventCount = -1;
StreamReader _stream;
StringBuilder _eventSource;
readonly StringBuilder _eventSource;
private LogParserLGF _logParser;
private LogParserLGF LogParser

View File

@ -16,17 +16,15 @@ namespace YY.EventLogAssistant
FileAttributes attr = File.GetAttributes(pathLogFile);
FileInfo logFileInfo = null;
string currentLogFilesPath;
string logFileWithReferences;
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
{
currentLogFilesPath = pathLogFile;
string currentLogFilesPath = pathLogFile;
logFileWithReferences = string.Format("{0}{1}{2}", currentLogFilesPath, Path.DirectorySeparatorChar, @"1Cv8.lgf");
}
else
{
logFileInfo = new FileInfo(pathLogFile);
currentLogFilesPath = logFileInfo.Directory.FullName;
logFileWithReferences = logFileInfo.FullName;
}
@ -210,6 +208,44 @@ namespace YY.EventLogAssistant
return severity;
}
public Severity GetSeverityByCode(long code)
{
try
{
return (Severity)code;
} catch
{
return Severity.Unknown;
}
}
public TransactionStatus GetTransactionStatus(string code)
{
TransactionStatus transactionStatus;
if (code == "R")
transactionStatus = TransactionStatus.Unfinished;
else if (code == "N")
transactionStatus = TransactionStatus.NotApplicable;
else if (code == "U")
transactionStatus = TransactionStatus.Committed;
else if (code == "C")
transactionStatus = TransactionStatus.RolledBack;
else
transactionStatus = TransactionStatus.Unknown;
return transactionStatus;
}
public TransactionStatus GetTransactionStatus(long code)
{
try
{
return (TransactionStatus)code;
} catch
{
return TransactionStatus.Unknown;
}
}
public Metadata GetMetadataByCode(string code)
{

View File

@ -32,11 +32,11 @@ namespace YY.EventLogAssistant
}
else if (Simb == "}" & !textBlockOpen)
{
count = count - 1;
count -= 1;
}
else if (Simb == "{" & !textBlockOpen)
{
count = count + 1;
count += 1;
}
}
@ -47,8 +47,8 @@ namespace YY.EventLogAssistant
#region Private Member Variables
private EventLogLGFReader _reader;
private Regex _regexDataUUID;
private readonly EventLogLGFReader _reader;
private readonly Regex _regexDataUUID;
#endregion
@ -176,29 +176,31 @@ namespace YY.EventLogAssistant
string[] parseResult = ParseEventLogString(eventSource);
string transactionSourceString = parseResult[2].RemoveBraces();
RowData eventData = new RowData();
eventData.RowID = _reader.CurrentFileEventNumber;
eventData.Period = DateTime.ParseExact(parseResult[0], "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
eventData.TransactionStatus = GetTransactionStatus(parseResult[1]);
eventData.TransactionDate = GetTransactionDate(transactionSourceString);
eventData.TransactionId = GetTransactionId(transactionSourceString);
eventData.User = _reader.GetUserByCode(parseResult[3]);
eventData.Computer = _reader.GetComputerByCode(parseResult[4]);
eventData.Application = _reader.GetApplicationByCode(parseResult[5]);
eventData.ConnectId = parseResult[6].ToInt32();
eventData.Event = _reader.GetEventByCode(parseResult[7]);
eventData.Severity = _reader.GetSeverityByCode(parseResult[8]);
eventData.Comment = parseResult[9].RemoveQuotes();
eventData.Metadata = _reader.GetMetadataByCode(parseResult[10]);
eventData.Data = GetData(parseResult[11]);
eventData.DataUUID = GetDataUUID(eventData.Data);
eventData.DataPresentation = parseResult[12].RemoveQuotes();
eventData.WorkServer = _reader.GetWorkServerByCode(parseResult[13]);
eventData.PrimaryPort = _reader.GetPrimaryPortByCode(parseResult[14]);
eventData.SecondaryPort = _reader.GetSecondaryPortByCode(parseResult[15]);
eventData.Session = parseResult[16].ToInt64();
RowData dataRow =new RowData()
{
RowID = _reader.CurrentFileEventNumber,
Period = DateTime.ParseExact(parseResult[0], "yyyyMMddHHmmss", CultureInfo.InvariantCulture),
TransactionStatus = _reader.GetTransactionStatus(parseResult[1]),
TransactionDate = GetTransactionDate(transactionSourceString),
TransactionId = GetTransactionId(transactionSourceString),
User = _reader.GetUserByCode(parseResult[3]),
Computer = _reader.GetComputerByCode(parseResult[4]),
Application = _reader.GetApplicationByCode(parseResult[5]),
ConnectId = parseResult[6].ToInt32(),
Event = _reader.GetEventByCode(parseResult[7]),
Severity = _reader.GetSeverityByCode(parseResult[8]),
Comment = parseResult[9].RemoveQuotes(),
Metadata = _reader.GetMetadataByCode(parseResult[10]),
Data = GetData(parseResult[11]),
DataPresentation = parseResult[12].RemoveQuotes(),
WorkServer = _reader.GetWorkServerByCode(parseResult[13]),
PrimaryPort = _reader.GetPrimaryPortByCode(parseResult[14]),
SecondaryPort = _reader.GetSecondaryPortByCode(parseResult[15]),
Session = parseResult[16].ToInt64()
};
dataRow.DataUUID = GetDataUUID(dataRow.Data);
return eventData;
return dataRow;
}
#endregion
@ -246,24 +248,6 @@ namespace YY.EventLogAssistant
return dataUUID;
}
private TransactionStatus GetTransactionStatus(string sourceString)
{
TransactionStatus transactionStatus;
if (sourceString == "R")
transactionStatus = TransactionStatus.Unfinished;
else if (sourceString == "N")
transactionStatus = TransactionStatus.NotApplicable;
else if (sourceString == "U")
transactionStatus = TransactionStatus.Committed;
else if (sourceString == "C")
transactionStatus = TransactionStatus.RolledBack;
else
transactionStatus = TransactionStatus.Unknown;
return transactionStatus;
}
private DateTime? GetTransactionDate(string sourceString)
{
DateTime? transactionDate;
@ -313,7 +297,7 @@ namespace YY.EventLogAssistant
while (delimIndex > 0)
{
bufferString = bufferString + preparedString.Substring(0, delimIndex).Trim();
bufferString += preparedString.Substring(0, delimIndex).Trim();
partNumber += 1;
preparedString = preparedString.Substring(delimIndex + 1);
if (partNumber == 1 && !String.IsNullOrEmpty(bufferString) && bufferString[0] == '\"')

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
[assembly: InternalsVisibleTo("YY.EventLogAssistant.Tests")]
namespace YY.EventLogAssistant.Services
@ -13,11 +12,11 @@ namespace YY.EventLogAssistant.Services
#region Private Member Variables
private const int _bufferLength = 1024;
private byte[] _utf8Preamble = Encoding.UTF8.GetPreamble();
private readonly byte[] _utf8Preamble = Encoding.UTF8.GetPreamble();
private Stream _base;
private Encoding _encoding;
private readonly Encoding _encoding;
private int _read = 0, _index = 0;
private byte[] _readBuffer = new byte[_bufferLength];
private readonly byte[] _readBuffer = new byte[_bufferLength];
#endregion
@ -150,7 +149,7 @@ namespace YY.EventLogAssistant.Services
Array.Copy(bufferString, 0, readyConvertData, 0, bufferSizeCopy);
string prepearedString;
if (_encoding == Encoding.UTF8 && byteArrayStartsWith(readyConvertData, 0, _utf8Preamble))
if (_encoding == Encoding.UTF8 && ByteArrayStartsWith(readyConvertData, 0, _utf8Preamble))
{
prepearedString = _encoding.GetString(readyConvertData, _utf8Preamble.Length, readyConvertData.Length - _utf8Preamble.Length);
}
@ -160,7 +159,7 @@ namespace YY.EventLogAssistant.Services
return prepearedString;
}
private bool byteArrayStartsWith(byte[] source, int offset, byte[] match)
private bool ByteArrayStartsWith(byte[] source, int offset, byte[] match)
{
if (match.Length > (source.Length - offset))
{

View File

@ -9,39 +9,6 @@ namespace YY.EventLogAssistant.Services
{
internal static class StringExtensions
{
#region Private Member Variables
private static readonly Dictionary<int, char> CharactersToMap = new Dictionary<int, char>
{
{130, '‚'},
{131, 'ƒ'},
{132, '„'},
{133, '…'},
{134, '†'},
{135, '‡'},
{136, 'ˆ'},
{137, '‰'},
{138, 'Š'},
{139, '‹'},
{140, 'Œ'},
{145, '‘'},
{146, '’'},
{147, '“'},
{148, '”'},
{149, '•'},
{150, '–'},
{151, '—'},
{152, '˜'},
{153, '™'},
{154, 'š'},
{155, '›'},
{156, 'œ'},
{159, 'Ÿ'},
{173, '-'}
};
#endregion
#region Public Methods
public static long From16To10(this string sourceValue)
@ -92,8 +59,7 @@ namespace YY.EventLogAssistant.Services
public static Guid ToGuid(this string sourceValue)
{
Guid guidFromString = Guid.Empty;
Guid.TryParse(sourceValue, out guidFromString);
Guid.TryParse(sourceValue, out Guid guidFromString);
return guidFromString;
}
@ -109,28 +75,6 @@ namespace YY.EventLogAssistant.Services
return source.GetString(resultBytes);
}
private static HashSet<char> CharListToSet(string charList)
{
HashSet<char> set = new HashSet<char>();
for (int i = 0; i < charList.Length; i++)
{
if ((i + 1) < charList.Length && charList[i + 1] == '-')
{
// Character range
char startChar = charList[i++];
i++; // Hyphen
char endChar = (char)0;
if (i < charList.Length)
endChar = charList[i++];
for (int j = startChar; j <= endChar; j++)
set.Add((char)j);
}
else set.Add(charList[i]);
}
return set;
}
#endregion
}
}

View File

@ -2,7 +2,7 @@
"profiles": {
"YY.EventLogAssistantConsoleApp": {
"commandName": "Project",
"commandLineArgs": "\"E:\\Dev\\YY\\YY.EventLogAssistant\\YY.EventLogAssistant\\YY.EventLogAssistantConsoleApp\\bin\\Debug\\netcoreapp3.1\\TestData\\1Cv8.lgd\" \"\\\\Srv-1c-01-vm\\жр lgf (генератор событий)\""
"commandLineArgs": "\"\\\\Srv-1c-01-vm\\жр lgf (генератор событий)\" \"E:\\Dev\\YY\\YY.EventLogAssistant\\YY.EventLogAssistant\\YY.EventLogAssistantConsoleApp\\bin\\Debug\\netcoreapp3.1\\TestData\\1Cv8.lgd\""
}
}
}