1
+ using Raven . Client . Documents ;
2
+ using Raven . Client . Documents . Linq ;
3
+ using Serilog . Ui . Core ;
4
+ using Serilog . Ui . RavenDbProvider . Models ;
5
+
6
+ namespace Serilog . Ui . RavenDbProvider ;
7
+
8
+ /// <inheritdoc/>
9
+ public class RavenDbDataProvider : IDataProvider
10
+ {
11
+ private readonly string _collectionName ;
12
+ private readonly IDocumentStore _documentStore ;
13
+
14
+ public RavenDbDataProvider ( IDocumentStore documentStore , string collectionName )
15
+ {
16
+ _documentStore = documentStore ;
17
+ _collectionName = collectionName ;
18
+ }
19
+
20
+ /// <inheritdoc/>
21
+ public string Name => string . Join ( "." , "RavenDB" ) ;
22
+
23
+ /// <inheritdoc/>
24
+ public async Task < ( IEnumerable < LogModel > , int ) > FetchDataAsync (
25
+ int page ,
26
+ int count ,
27
+ string ? level = null ,
28
+ string ? searchCriteria = null ,
29
+ DateTime ? startDate = null ,
30
+ DateTime ? endDate = null
31
+ )
32
+ {
33
+ if ( startDate != null && startDate . Value . Kind != DateTimeKind . Utc )
34
+ {
35
+ startDate = DateTime . SpecifyKind ( startDate . Value , DateTimeKind . Utc ) ;
36
+ }
37
+
38
+ if ( endDate != null && endDate . Value . Kind != DateTimeKind . Utc )
39
+ {
40
+ endDate = DateTime . SpecifyKind ( endDate . Value , DateTimeKind . Utc ) ;
41
+ }
42
+
43
+ var logsTask = GetLogsAsync ( page - 1 , count , level , searchCriteria , startDate , endDate ) ;
44
+ var logCountTask = CountLogsAsync ( level , searchCriteria , startDate , endDate ) ;
45
+ await Task . WhenAll ( logsTask , logCountTask ) ;
46
+
47
+ return ( await logsTask , await logCountTask ) ;
48
+ }
49
+
50
+ private async Task < IEnumerable < LogModel > > GetLogsAsync (
51
+ int page ,
52
+ int count ,
53
+ string ? level ,
54
+ string ? searchCriteria ,
55
+ DateTime ? startDate ,
56
+ DateTime ? endDate )
57
+ {
58
+ using var session = _documentStore . OpenAsyncSession ( ) ;
59
+ var query = session . Advanced . AsyncDocumentQuery < RavenDbLogModel > ( collectionName : _collectionName ) . ToQueryable ( ) ;
60
+
61
+ GenerateWhereClause ( ref query , level , searchCriteria , startDate , endDate ) ;
62
+
63
+ var logs = await query . Skip ( count * page ) . Take ( count ) . ToListAsync ( ) ;
64
+
65
+ var index = 1 ;
66
+
67
+ return logs . Select ( log => log . ToLogModel ( ( page * count ) + index ++ ) ) . ToList ( ) ;
68
+ }
69
+
70
+ private async Task < int > CountLogsAsync (
71
+ string ? level ,
72
+ string ? searchCriteria ,
73
+ DateTime ? startDate = null ,
74
+ DateTime ? endDate = null )
75
+ {
76
+ using var session = _documentStore . OpenAsyncSession ( ) ;
77
+ var query = session . Advanced . AsyncDocumentQuery < RavenDbLogModel > ( collectionName : _collectionName ) . ToQueryable ( ) ;
78
+
79
+ GenerateWhereClause ( ref query , level , searchCriteria , startDate , endDate ) ;
80
+
81
+ return await query . CountAsync ( ) ;
82
+ }
83
+
84
+ private void GenerateWhereClause (
85
+ ref IRavenQueryable < RavenDbLogModel > query ,
86
+ string ? level ,
87
+ string ? searchCriteria ,
88
+ DateTime ? startDate ,
89
+ DateTime ? endDate )
90
+ {
91
+ if ( ! string . IsNullOrEmpty ( level ) )
92
+ {
93
+ query = query . Where ( q => q . Level == level ) ;
94
+ }
95
+
96
+ if ( ! string . IsNullOrEmpty ( searchCriteria ) )
97
+ {
98
+ query = query
99
+ . Search ( q => q . RenderedMessage , searchCriteria )
100
+ . Search ( q => q . Exception , searchCriteria ) ;
101
+ }
102
+
103
+ if ( startDate != null )
104
+ {
105
+ query = query . Where ( q => q . Timestamp >= startDate . Value ) ;
106
+ }
107
+
108
+ if ( endDate != null )
109
+ {
110
+ query = query . Where ( q => q . Timestamp <= endDate . Value ) ;
111
+ }
112
+ }
113
+ }
0 commit comments