Skip to content

Commit 7c2e940

Browse files
committed
Add start and end date to PostgreSQL where clause.
1 parent 6a83edc commit 7c2e940

File tree

2 files changed

+52
-28
lines changed

2 files changed

+52
-28
lines changed

src/Serilog.Ui.MsSqlServerProvider/SqlServerDataProvider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private async Task<IEnumerable<LogModel>> GetLogsAsync(
7575
}
7676
}
7777

78-
public async Task<int> CountLogsAsync(
78+
private async Task<int> CountLogsAsync(
7979
string level,
8080
string searchCriteria,
8181
DateTime? startDate = null,
@@ -108,8 +108,7 @@ private void GenerateWhereClause(
108108
string level,
109109
string searchCriteria,
110110
DateTime? startDate = null,
111-
DateTime? endDate = null
112-
)
111+
DateTime? endDate = null)
113112
{
114113
var whereIncluded = false;
115114

src/Serilog.Ui.PostgreSqlProvider/PostgreDataProvider.cs

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,28 @@ public PostgresDataProvider(RelationalDbOptions options)
2727
DateTime? endDate = null
2828
)
2929
{
30-
var logsTask = GetLogsAsync(page - 1, count, logLevel, searchCriteria);
30+
var logsTask = GetLogsAsync(page - 1, count, logLevel, searchCriteria, startDate, endDate);
3131
var logCountTask = CountLogsAsync(logLevel, searchCriteria);
3232

3333
await Task.WhenAll(logsTask, logCountTask);
3434

3535
return (await logsTask, await logCountTask);
3636
}
3737

38-
private async Task<IEnumerable<LogModel>> GetLogsAsync(int page, int count, string level, string searchCriteria)
38+
private async Task<IEnumerable<LogModel>> GetLogsAsync(int page,
39+
int count,
40+
string level,
41+
string searchCriteria,
42+
DateTime? startDate,
43+
DateTime? endDate)
3944
{
4045
var queryBuilder = new StringBuilder();
4146
queryBuilder.Append("SELECT message, message_template, level, timestamp, exception, log_event AS \"Properties\" FROM ");
4247
queryBuilder.Append(_options.Schema);
4348
queryBuilder.Append(".");
4449
queryBuilder.Append(_options.TableName);
4550

46-
var whereIncluded = false;
47-
48-
if (!string.IsNullOrEmpty(level))
49-
{
50-
queryBuilder.Append(" WHERE level = @Level ");
51-
whereIncluded = true;
52-
}
53-
54-
if (!string.IsNullOrEmpty(searchCriteria))
55-
{
56-
queryBuilder.Append(whereIncluded
57-
? " AND message LIKE @Search OR exception LIKE @Search "
58-
: " WHERE message LIKE @Search OR exception LIKE @Search ");
59-
}
51+
GenerateWhereClause(queryBuilder, level, searchCriteria, startDate, endDate);
6052

6153
queryBuilder.Append(" ORDER BY timestamp DESC LIMIT @Count OFFSET @Offset ");
6254

@@ -67,7 +59,9 @@ private async Task<IEnumerable<LogModel>> GetLogsAsync(int page, int count, stri
6759
Offset = page * count,
6860
Count = count,
6961
Level = LogLevelConverter.GetLevelValue(level),
70-
Search = searchCriteria != null ? "%" + searchCriteria + "%" : null
62+
Search = searchCriteria != null ? "%" + searchCriteria + "%" : null,
63+
StartDate = startDate,
64+
EndDate = endDate
7165
});
7266

7367
var index = 1;
@@ -77,14 +71,38 @@ private async Task<IEnumerable<LogModel>> GetLogsAsync(int page, int count, stri
7771
return logs;
7872
}
7973

80-
public async Task<int> CountLogsAsync(string level, string searchCriteria)
74+
private async Task<int> CountLogsAsync(
75+
string level,
76+
string searchCriteria,
77+
DateTime? startDate = null,
78+
DateTime? endDate = null)
8179
{
8280
var queryBuilder = new StringBuilder();
8381
queryBuilder.Append("SELECT COUNT(message) FROM ");
8482
queryBuilder.Append(_options.Schema);
8583
queryBuilder.Append(".");
8684
queryBuilder.Append(_options.TableName);
8785

86+
GenerateWhereClause(queryBuilder, level, searchCriteria, startDate, endDate);
87+
88+
using IDbConnection connection = new NpgsqlConnection(_options.ConnectionString);
89+
return await connection.ExecuteScalarAsync<int>(queryBuilder.ToString(),
90+
new
91+
{
92+
Level = LogLevelConverter.GetLevelValue(level),
93+
Search = searchCriteria != null ? "%" + searchCriteria + "%" : null,
94+
StartDate = startDate,
95+
EndDate = endDate
96+
});
97+
}
98+
99+
private void GenerateWhereClause(
100+
StringBuilder queryBuilder,
101+
string level,
102+
string searchCriteria,
103+
DateTime? startDate = null,
104+
DateTime? endDate = null)
105+
{
88106
var whereIncluded = false;
89107

90108
if (!string.IsNullOrEmpty(level))
@@ -100,13 +118,20 @@ public async Task<int> CountLogsAsync(string level, string searchCriteria)
100118
: " WHERE message LIKE @Search OR exception LIKE @Search ");
101119
}
102120

103-
using IDbConnection connection = new NpgsqlConnection(_options.ConnectionString);
104-
return await connection.ExecuteScalarAsync<int>(queryBuilder.ToString(),
105-
new
106-
{
107-
Level = LogLevelConverter.GetLevelValue(level),
108-
Search = searchCriteria != null ? "%" + searchCriteria + "%" : null
109-
});
121+
if (startDate != null)
122+
{
123+
queryBuilder.Append(whereIncluded
124+
? " AND timestamp >= @StartDate "
125+
: " WHERE timestamp >= @StartDate ");
126+
whereIncluded = true;
127+
}
128+
129+
if (endDate != null)
130+
{
131+
queryBuilder.Append(whereIncluded
132+
? " AND timestamp < @EndDate "
133+
: " WHERE timestamp < @EndDate ");
134+
}
110135
}
111136
}
112137
}

0 commit comments

Comments
 (0)