Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit65cbd47

Browse files
Copilotmo-esmp
andcommitted
Add backend dashboard API implementation
Co-authored-by: mo-esmp <1659032+mo-esmp@users.noreply.github.com>
1 parent8353e53 commit65cbd47

File tree

14 files changed

+362
-12
lines changed

14 files changed

+362
-12
lines changed

‎src/Serilog.Ui.Core/AggregateDataProvider.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,9 @@ public AggregateDataProvider(IEnumerable<IDataProvider> dataProviders)
7575
/// <inheritdoc/>
7676
publicTask<(IEnumerable<LogModel>,int)>FetchDataAsync(FetchLogsQueryqueryParams,CancellationTokencancellationToken=default)
7777
=>SelectedDataProvider.FetchDataAsync(queryParams,cancellationToken);
78+
79+
/// <inheritdoc/>
80+
publicTask<DashboardModel>FetchDashboardAsync(CancellationTokencancellationToken=default)
81+
=>SelectedDataProvider.FetchDashboardAsync(cancellationToken);
7882
}
7983
}

‎src/Serilog.Ui.Core/IDataProvider.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public interface IDataProvider
1515
/// </summary>
1616
Task<(IEnumerable<LogModel>results,inttotal)>FetchDataAsync(FetchLogsQueryqueryParams,CancellationTokencancellationToken=default);
1717

18+
/// <summary>
19+
/// Fetches dashboard statistics asynchronous.
20+
/// </summary>
21+
Task<DashboardModel>FetchDashboardAsync(CancellationTokencancellationToken=default);
22+
1823
/// <summary>
1924
/// Name of the provider, used to identify this provider when using multiple.
2025
/// </summary>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
usingSystem.Collections.Generic;
2+
3+
namespaceSerilog.Ui.Core.Models
4+
{
5+
/// <summary>
6+
/// Represents dashboard statistics for log data visualization.
7+
/// </summary>
8+
publicclassDashboardModel
9+
{
10+
/// <summary>
11+
/// Gets or sets the total count of logs.
12+
/// </summary>
13+
publicintTotalLogs{get;set;}
14+
15+
/// <summary>
16+
/// Gets or sets the count of logs by level.
17+
/// </summary>
18+
publicDictionary<string,int>LogsByLevel{get;set;}=new();
19+
20+
/// <summary>
21+
/// Gets or sets the count of logs for today.
22+
/// </summary>
23+
publicintTodayLogs{get;set;}
24+
25+
/// <summary>
26+
/// Gets or sets the count of error logs for today.
27+
/// </summary>
28+
publicintTodayErrorLogs{get;set;}
29+
}
30+
}

‎src/Serilog.Ui.ElasticSearchProvider/ElasticSearchDbDataProvider.cs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,54 @@ public class ElasticSearchDbDataProvider(IElasticClient client, ElasticSearchDbO
5656

5757
return(result?.Documents.Select((x,index)=>x.ToLogModel(rowNoStart,index)).ToList()??[],total);
5858
}
59+
60+
publicasyncTask<DashboardModel>FetchDashboardAsync(CancellationTokencancellationToken=default)
61+
{
62+
vardashboard=newDashboardModel();
63+
vartoday=DateTime.Today;
64+
vartomorrow=today.AddDays(1);
65+
66+
// Get total logs count
67+
vartotalResponse=await_client.CountAsync<ElasticSearchDbLogModel>(c=>c
68+
.Index(options.IndexName),cancellationToken);
69+
dashboard.TotalLogs=(int)(totalResponse?.Count??0);
70+
71+
// Get logs count by level
72+
varlevelResponse=await_client.SearchAsync<ElasticSearchDbLogModel>(s=>s
73+
.Index(options.IndexName)
74+
.Size(0)
75+
.Aggregations(aggs=>aggs
76+
.Terms("levels", t=>t.Field(f=>f.Level))
77+
),cancellationToken);
78+
79+
if(levelResponse?.Aggregations?.Terms("levels")is{}levelsAgg)
80+
{
81+
dashboard.LogsByLevel=levelsAgg.Buckets.ToDictionary(
82+
bucket=>bucket.Key.ToString()??"Unknown",
83+
bucket=>(int)bucket.DocCount);
84+
}
85+
86+
// Get today's logs count
87+
vartodayResponse=await_client.CountAsync<ElasticSearchDbLogModel>(c=>c
88+
.Index(options.IndexName)
89+
.Query(q=>q
90+
.DateRange(r=>r.Field(f=>f.Timestamp).GreaterThanOrEquals(today).LessThan(tomorrow))
91+
),cancellationToken);
92+
dashboard.TodayLogs=(int)(todayResponse?.Count??0);
93+
94+
// Get today's error logs count
95+
vartodayErrorResponse=await_client.CountAsync<ElasticSearchDbLogModel>(c=>c
96+
.Index(options.IndexName)
97+
.Query(q=>q
98+
.Bool(b=>b
99+
.Must(
100+
m=>m.Term(t=>t.Field(f=>f.Level).Value("Error")),
101+
m=>m.DateRange(r=>r.Field(f=>f.Timestamp).GreaterThanOrEquals(today).LessThan(tomorrow))
102+
)
103+
)
104+
),cancellationToken);
105+
dashboard.TodayErrorLogs=(int)(todayErrorResponse?.Count??0);
106+
107+
returndashboard;
108+
}
59109
}

‎src/Serilog.Ui.MongoDbProvider/MongoDbDataProvider.cs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,38 @@ private static SortDefinition<MongoDbLogModel> GenerateSortClause(SortProperty s
121121

122122
returnisDesc?Builders<MongoDbLogModel>.Sort.Descending(sortPropertyName):Builders<MongoDbLogModel>.Sort.Ascending(sortPropertyName);
123123
}
124+
125+
publicasyncTask<DashboardModel>FetchDashboardAsync(CancellationTokencancellationToken=default)
126+
{
127+
vardashboard=newDashboardModel();
128+
vartoday=DateTime.Today.ToUniversalTime();
129+
vartomorrow=today.AddDays(1);
130+
131+
// Get total logs count
132+
dashboard.TotalLogs=Convert.ToInt32(await_collection.CountDocumentsAsync(Builders<MongoDbLogModel>.Filter.Empty,cancellationToken:cancellationToken));
133+
134+
// Get logs count by level
135+
varlevelCounts=await_collection.Aggregate()
136+
.Group(x=>x.Level, g=>new{Level=g.Key,Count=g.Count()})
137+
.ToListAsync(cancellationToken);
138+
dashboard.LogsByLevel=levelCounts.ToDictionary(x=>x.Level??"Unknown", x=>x.Count);
139+
140+
// Get today's logs count
141+
vartodayFilter=Builders<MongoDbLogModel>.Filter.And(
142+
Builders<MongoDbLogModel>.Filter.Gte(x=>x.UtcTimeStamp,today),
143+
Builders<MongoDbLogModel>.Filter.Lt(x=>x.UtcTimeStamp,tomorrow)
144+
);
145+
dashboard.TodayLogs=Convert.ToInt32(await_collection.CountDocumentsAsync(todayFilter,cancellationToken:cancellationToken));
146+
147+
// Get today's error logs count
148+
vartodayErrorFilter=Builders<MongoDbLogModel>.Filter.And(
149+
Builders<MongoDbLogModel>.Filter.Eq(x=>x.Level,"Error"),
150+
Builders<MongoDbLogModel>.Filter.Gte(x=>x.UtcTimeStamp,today),
151+
Builders<MongoDbLogModel>.Filter.Lt(x=>x.UtcTimeStamp,tomorrow)
152+
);
153+
dashboard.TodayErrorLogs=Convert.ToInt32(await_collection.CountDocumentsAsync(todayErrorFilter,cancellationToken:cancellationToken));
154+
155+
returndashboard;
156+
}
124157
}
125158
}

‎src/Serilog.Ui.MsSqlServerProvider/SqlServerDataProvider.cs‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
usingSerilog.Ui.Core;
44
usingSerilog.Ui.Core.Models;
55
usingSerilog.Ui.MsSqlServerProvider.Extensions;
6+
usingSystem;
67
usingSystem.Collections.Generic;
78
usingSystem.Data;
89
usingSystem.Linq;
@@ -73,4 +74,40 @@ private async Task<int> CountLogsAsync(FetchLogsQuery queryParams)
7374
queryParams.EndDate
7475
});
7576
}
77+
78+
publicasyncTask<DashboardModel>FetchDashboardAsync(CancellationTokencancellationToken=default)
79+
{
80+
vardashboard=newDashboardModel();
81+
vartoday=DateTime.Today;
82+
vartomorrow=today.AddDays(1);
83+
84+
usingIDbConnectionconnection=newSqlConnection(options.ConnectionString);
85+
86+
// Get total logs count
87+
vartotalQuery=$"SELECT COUNT(*) FROM [{options.Schema}].[{options.TableName}]";
88+
dashboard.TotalLogs=awaitconnection.QueryFirstOrDefaultAsync<int>(totalQuery);
89+
90+
// Get logs count by level
91+
varlevelQuery=$"SELECT [{options.ColumnNames.Level}] as Level, COUNT(*) as Count FROM [{options.Schema}].[{options.TableName}] GROUP BY [{options.ColumnNames.Level}]";
92+
varlevelCounts=awaitconnection.QueryAsync<(stringLevel,intCount)>(levelQuery);
93+
dashboard.LogsByLevel=levelCounts.ToDictionary(x=>x.Level??"Unknown", x=>x.Count);
94+
95+
// Get today's logs count
96+
vartodayQuery=$"SELECT COUNT(*) FROM [{options.Schema}].[{options.TableName}] WHERE [{options.ColumnNames.Timestamp}] >= @StartDate AND [{options.ColumnNames.Timestamp}] < @EndDate";
97+
dashboard.TodayLogs=awaitconnection.QueryFirstOrDefaultAsync<int>(todayQuery,new
98+
{
99+
StartDate=today,
100+
EndDate=tomorrow
101+
});
102+
103+
// Get today's error logs count
104+
vartodayErrorQuery=$"SELECT COUNT(*) FROM [{options.Schema}].[{options.TableName}] WHERE [{options.ColumnNames.Level}] = 'Error' AND [{options.ColumnNames.Timestamp}] >= @StartDate AND [{options.ColumnNames.Timestamp}] < @EndDate";
105+
dashboard.TodayErrorLogs=awaitconnection.QueryFirstOrDefaultAsync<int>(todayErrorQuery,new
106+
{
107+
StartDate=today,
108+
EndDate=tomorrow
109+
});
110+
111+
returndashboard;
112+
}
76113
}

‎src/Serilog.Ui.MySqlProvider/Shared/DataProvider.cs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,40 @@ private async Task<int> CountLogsAsync(FetchLogsQuery queryParams)
7272
queryParams.EndDate
7373
});
7474
}
75+
76+
publicasyncTask<DashboardModel>FetchDashboardAsync(CancellationTokencancellationToken=default)
77+
{
78+
vardashboard=newDashboardModel();
79+
vartoday=DateTime.Today;
80+
vartomorrow=today.AddDays(1);
81+
82+
usingMySqlConnectionconnection=new(options.ConnectionString);
83+
84+
// Get total logs count
85+
vartotalQuery=$"SELECT COUNT(*) FROM `{options.Schema}`.`{options.TableName}`";
86+
dashboard.TotalLogs=awaitconnection.QueryFirstOrDefaultAsync<int>(totalQuery);
87+
88+
// Get logs count by level
89+
varlevelQuery=$"SELECT `{options.ColumnNames.Level}` as Level, COUNT(*) as Count FROM `{options.Schema}`.`{options.TableName}` GROUP BY `{options.ColumnNames.Level}`";
90+
varlevelCounts=awaitconnection.QueryAsync<(stringLevel,intCount)>(levelQuery);
91+
dashboard.LogsByLevel=levelCounts.ToDictionary(x=>x.Level??"Unknown", x=>x.Count);
92+
93+
// Get today's logs count
94+
vartodayQuery=$"SELECT COUNT(*) FROM `{options.Schema}`.`{options.TableName}` WHERE `{options.ColumnNames.Timestamp}` >= @StartDate AND `{options.ColumnNames.Timestamp}` < @EndDate";
95+
dashboard.TodayLogs=awaitconnection.QueryFirstOrDefaultAsync<int>(todayQuery,new
96+
{
97+
StartDate=today,
98+
EndDate=tomorrow
99+
});
100+
101+
// Get today's error logs count
102+
vartodayErrorQuery=$"SELECT COUNT(*) FROM `{options.Schema}`.`{options.TableName}` WHERE `{options.ColumnNames.Level}` = 'Error' AND `{options.ColumnNames.Timestamp}` >= @StartDate AND `{options.ColumnNames.Timestamp}` < @EndDate";
103+
dashboard.TodayErrorLogs=awaitconnection.QueryFirstOrDefaultAsync<int>(todayErrorQuery,new
104+
{
105+
StartDate=today,
106+
EndDate=tomorrow
107+
});
108+
109+
returndashboard;
110+
}
75111
}

‎src/Serilog.Ui.PostgreSqlProvider/PostgresDataProvider.cs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,42 @@ private async Task<int> CountLogsAsync(FetchLogsQuery queryParams)
7979
queryParams.EndDate
8080
});
8181
}
82+
83+
/// <inheritdoc/>
84+
publicasyncTask<DashboardModel>FetchDashboardAsync(CancellationTokencancellationToken=default)
85+
{
86+
vardashboard=newDashboardModel();
87+
vartoday=System.DateTime.Today;
88+
vartomorrow=today.AddDays(1);
89+
90+
awaitusingNpgsqlConnectionconnection=new(options.ConnectionString);
91+
92+
// Get total logs count
93+
vartotalQuery=$"SELECT COUNT(*) FROM\"{options.Schema}\".\"{options.TableName}\"";
94+
dashboard.TotalLogs=awaitconnection.QueryFirstOrDefaultAsync<int>(totalQuery);
95+
96+
// Get logs count by level
97+
varlevelQuery=$"SELECT{options.ColumnNames.Level} as Level, COUNT(*) as Count FROM\"{options.Schema}\".\"{options.TableName}\" GROUP BY{options.ColumnNames.Level}";
98+
varlevelCounts=awaitconnection.QueryAsync<(intLevel,intCount)>(levelQuery);
99+
dashboard.LogsByLevel=levelCounts.ToDictionary(x=>LogLevelConverter.GetLevelName(x.Level.ToString()), x=>x.Count);
100+
101+
// Get today's logs count
102+
vartodayQuery=$"SELECT COUNT(*) FROM\"{options.Schema}\".\"{options.TableName}\" WHERE\"{options.ColumnNames.Timestamp}\" >= @StartDate AND\"{options.ColumnNames.Timestamp}\" < @EndDate";
103+
dashboard.TodayLogs=awaitconnection.QueryFirstOrDefaultAsync<int>(todayQuery,new
104+
{
105+
StartDate=today,
106+
EndDate=tomorrow
107+
});
108+
109+
// Get today's error logs count (Error level = 3 in PostgreSQL)
110+
vartodayErrorQuery=$"SELECT COUNT(*) FROM\"{options.Schema}\".\"{options.TableName}\" WHERE{options.ColumnNames.Level} = @ErrorLevel AND\"{options.ColumnNames.Timestamp}\" >= @StartDate AND\"{options.ColumnNames.Timestamp}\" < @EndDate";
111+
dashboard.TodayErrorLogs=awaitconnection.QueryFirstOrDefaultAsync<int>(todayErrorQuery,new
112+
{
113+
ErrorLevel=LogLevelConverter.GetLevelValue("Error"),
114+
StartDate=today,
115+
EndDate=tomorrow
116+
});
117+
118+
returndashboard;
119+
}
82120
}

‎src/Serilog.Ui.RavenDbProvider/RavenDbDataProvider.cs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,35 @@ SortDirection sortBy
105105
_=>query.OrderByDescending(q=>q.Timestamp),
106106
};
107107
}
108+
109+
publicasyncTask<DashboardModel>FetchDashboardAsync(CancellationTokencancellationToken=default)
110+
{
111+
vardashboard=newDashboardModel();
112+
vartoday=DateTime.Today;
113+
vartomorrow=today.AddDays(1);
114+
115+
usingvarsession=_documentStore.OpenAsyncSession();
116+
117+
// Get total logs count
118+
dashboard.TotalLogs=awaitsession.Query<RavenDbLogModel>().CountAsync();
119+
120+
// Get logs count by level
121+
varlevelCounts=awaitsession.Query<RavenDbLogModel>()
122+
.GroupBy(x=>x.Level)
123+
.Select(g=>new{Level=g.Key,Count=g.Count()})
124+
.ToListAsync();
125+
dashboard.LogsByLevel=levelCounts.ToDictionary(x=>x.Level??"Unknown", x=>x.Count);
126+
127+
// Get today's logs count
128+
dashboard.TodayLogs=awaitsession.Query<RavenDbLogModel>()
129+
.Where(x=>x.Timestamp>=today&&x.Timestamp<tomorrow)
130+
.CountAsync();
131+
132+
// Get today's error logs count
133+
dashboard.TodayErrorLogs=awaitsession.Query<RavenDbLogModel>()
134+
.Where(x=>x.Level=="Error"&&x.Timestamp>=today&&x.Timestamp<tomorrow)
135+
.CountAsync();
136+
137+
returndashboard;
138+
}
108139
}

‎src/Serilog.Ui.SqliteDataProvider/SqliteDataProvider.cs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,42 @@ public class SqliteDataProvider(SqliteDbOptions options, SqliteQueryBuilder quer
3131

3232
publicstringName=>_options.GetProviderName(SqliteProviderName);
3333

34+
publicasyncTask<DashboardModel>FetchDashboardAsync(CancellationTokencancellationToken=default)
35+
{
36+
vardashboard=newDashboardModel();
37+
vartoday=DateTime.Today;
38+
vartomorrow=today.AddDays(1);
39+
40+
usingvarconnection=newSqliteConnection(_options.ConnectionString);
41+
42+
// Get total logs count
43+
vartotalQuery=$"SELECT COUNT(*) FROM{_options.TableName}";
44+
dashboard.TotalLogs=awaitconnection.QueryFirstOrDefaultAsync<int>(totalQuery);
45+
46+
// Get logs count by level
47+
varlevelQuery=$"SELECT{_options.ColumnNames.Level} as Level, COUNT(*) as Count FROM{_options.TableName} GROUP BY{_options.ColumnNames.Level}";
48+
varlevelCounts=awaitconnection.QueryAsync<(stringLevel,intCount)>(levelQuery);
49+
dashboard.LogsByLevel=levelCounts.ToDictionary(x=>x.Level??"Unknown", x=>x.Count);
50+
51+
// Get today's logs count
52+
vartodayQuery=$"SELECT COUNT(*) FROM{_options.TableName} WHERE{_options.ColumnNames.Timestamp} >= @StartDate AND{_options.ColumnNames.Timestamp} < @EndDate";
53+
dashboard.TodayLogs=awaitconnection.QueryFirstOrDefaultAsync<int>(todayQuery,new
54+
{
55+
StartDate=StringifyDate(today),
56+
EndDate=StringifyDate(tomorrow)
57+
});
58+
59+
// Get today's error logs count
60+
vartodayErrorQuery=$"SELECT COUNT(*) FROM{_options.TableName} WHERE{_options.ColumnNames.Level} = 'Error' AND{_options.ColumnNames.Timestamp} >= @StartDate AND{_options.ColumnNames.Timestamp} < @EndDate";
61+
dashboard.TodayErrorLogs=awaitconnection.QueryFirstOrDefaultAsync<int>(todayErrorQuery,new
62+
{
63+
StartDate=StringifyDate(today),
64+
EndDate=StringifyDate(tomorrow)
65+
});
66+
67+
returndashboard;
68+
}
69+
3470
privateasyncTask<IEnumerable<LogModel>>GetLogsAsync(FetchLogsQueryqueryParams)
3571
{
3672
varquery=queryBuilder.BuildFetchLogsQuery(_options.ColumnNames,_options.Schema,_options.TableName,queryParams);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp