Skip to content

Commit a46efcd

Browse files
committed
Merge branch 'issue-8075-display-database-size' into develop
# Conflicts: # src/Presentation/Nop.Web.Framework/Migrations/UpgradeTo500/LocalizationMigration.cs
2 parents 4f41b44 + 7f85791 commit a46efcd

File tree

12 files changed

+118
-1
lines changed

12 files changed

+118
-1
lines changed

src/Libraries/Nop.Data/DataProviders/MsSqlDataProvider.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,21 @@ public virtual async Task ShrinkDatabaseAsync()
261261
await currentConnection.ExecuteAsync($"DBCC SHRINKDATABASE ([{GetConnectionStringBuilder().InitialCatalog}]);");
262262
}
263263

264+
/// <summary>
265+
/// Gets the database size in Kb
266+
/// </summary>
267+
/// <returns>
268+
/// A task that represents the asynchronous operation
269+
/// The task result contains the database size
270+
/// </returns>
271+
public virtual async Task<long> GetDatabaseSizeAsync()
272+
{
273+
using var currentConnection = CreateDataConnection();
274+
var result = await currentConnection.QueryToListAsync<long>("SELECT dbSize = SUM(size) * 8 FROM sys.master_files WITH(NOWAIT) WHERE database_id = DB_ID() GROUP BY database_id;");
275+
276+
return result.FirstOrDefault();
277+
}
278+
264279
/// <summary>
265280
/// Build the connection string
266281
/// </summary>

src/Libraries/Nop.Data/DataProviders/MySqlDataProvider.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,21 @@ public virtual Task ShrinkDatabaseAsync()
264264
throw new DataException("This database provider does not support database shrinking. Instead, use Re-index operation to optimize database space. Optimization is only available when the 'innodb_file_per_table' setting is enabled");
265265
}
266266

267+
/// <summary>
268+
/// Gets the database size in Kb
269+
/// </summary>
270+
/// <returns>
271+
/// A task that represents the asynchronous operation
272+
/// The task result contains the database size
273+
/// </returns>
274+
public virtual async Task<long> GetDatabaseSizeAsync()
275+
{
276+
using var currentConnection = CreateDataConnection();
277+
var result = await currentConnection.QueryToListAsync<long>($"SELECT ROUND(SUM(data_length + index_length) / 1024, 1) FROM information_schema.tables where table_schema='{GetConnectionStringBuilder().Database}' GROUP BY table_schema");
278+
279+
return result.FirstOrDefault();
280+
}
281+
267282
/// <summary>
268283
/// Build the connection string
269284
/// </summary>

src/Libraries/Nop.Data/DataProviders/PostgreSqlDataProvider.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,21 @@ public virtual async Task ShrinkDatabaseAsync()
344344
await currentConnection.ExecuteAsync($"VACUUM FULL \"{table}\";");
345345
}
346346

347+
/// <summary>
348+
/// Gets the database size in Kb
349+
/// </summary>
350+
/// <returns>
351+
/// A task that represents the asynchronous operation
352+
/// The task result contains the database size
353+
/// </returns>
354+
public virtual async Task<long> GetDatabaseSizeAsync()
355+
{
356+
using var currentConnection = CreateDataConnection();
357+
var result = await currentConnection.QueryToListAsync<long>($"SELECT pg_database_size('{GetConnectionStringBuilder().Database}') / 1024 as sizebytes");
358+
359+
return result.FirstOrDefault();
360+
}
361+
347362
/// <summary>
348363
/// Build the connection string
349364
/// </summary>

src/Libraries/Nop.Data/INopDataProvider.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,15 @@ Task<ITempDataStorage<TItem>> CreateTempDataStorageAsync<TItem>(string storeKey,
232232
/// <returns>A task that represents the asynchronous operation</returns>
233233
Task ShrinkDatabaseAsync();
234234

235+
/// <summary>
236+
/// Gets the database size in Kb
237+
/// </summary>
238+
/// <returns>
239+
/// A task that represents the asynchronous operation
240+
/// The task result contains the database size
241+
/// </returns>
242+
Task<long> GetDatabaseSizeAsync();
243+
235244
/// <summary>
236245
/// Build the connection string
237246
/// </summary>

src/Presentation/Nop.Web.Framework/Migrations/UpgradeTo500/LocalizationMigration.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ public override void Up()
248248
//#8074
249249
["Admin.System.SystemInfo.UsedMemory"] = "Used memory (MB)",
250250
["Admin.System.SystemInfo.UsedMemory.Hint"] = "Total megabytes (MB) in use by the application.",
251+
252+
//#8075
253+
["Admin.System.Maintenance.ShrinkDatabase.DatabaseSize"] = "Current database size is {0} MB",
251254
});
252255

253256
#endregion

src/Presentation/Nop.Web/App_Data/Localization/defaultResources.nopres.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16046,11 +16046,14 @@
1604616046
<LocaleResource Name="Admin.System.Maintenance.ShrinkDatabase.Complete">
1604716047
<Value>Database shrinking completed</Value>
1604816048
</LocaleResource>
16049+
<LocaleResource Name="Admin.System.Maintenance.ShrinkDatabase.DatabaseSize">
16050+
<Value>Current database size is {0} MB</Value>
16051+
</LocaleResource>
1604916052
<LocaleResource Name="Admin.System.Maintenance.ShrinkDatabase.Progress">
1605016053
<Value>Processing...</Value>
1605116054
</LocaleResource>
1605216055
<LocaleResource Name="Admin.System.Maintenance.ShrinkDatabase.Text">
16053-
<Value>Reclaim disk space by reorganizing physical data storage</Value>
16056+
<Value>Reclaim disk space by reorganizing physical data storage.</Value>
1605416057
</LocaleResource>
1605516058
<LocaleResource Name="Admin.System.QueuedEmails">
1605616059
<Value>Message queue</Value>

src/Presentation/Nop.Web/Areas/Admin/Controllers/CommonController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ public virtual async Task<IActionResult> ReIndexTables(MaintenanceModel model)
280280
await _notificationService.ErrorNotificationAsync(exc);
281281
}
282282

283+
//prepare model
284+
model = await _commonModelFactory.PrepareMaintenanceModelAsync(model);
285+
283286
return View(model);
284287
}
285288

@@ -298,6 +301,9 @@ public virtual async Task<IActionResult> ShrinkDatabase(MaintenanceModel model)
298301
await _notificationService.ErrorNotificationAsync(exc);
299302
}
300303

304+
//prepare model
305+
model = await _commonModelFactory.PrepareMaintenanceModelAsync(model);
306+
301307
return View(model);
302308
}
303309

src/Presentation/Nop.Web/Areas/Admin/Factories/CommonModelFactory.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,10 @@ public virtual async Task<MaintenanceModel> PrepareMaintenanceModelAsync(Mainten
982982
model.DeleteAlreadySentQueuedEmails.EndDate = DateTime.UtcNow.AddDays(-7);
983983

984984
model.BackupSupported = _dataProvider.BackupSupported;
985+
model.DatabaseSize =
986+
string.Format(
987+
await _localizationService.GetResourceAsync("Admin.System.Maintenance.ShrinkDatabase.DatabaseSize"),
988+
Math.Round(await _dataProvider.GetDatabaseSizeAsync() / 1024.0M, 2));
985989

986990
//prepare nested search model
987991
PrepareBackupFileSearchModel(model.BackupFileSearchModel);

src/Presentation/Nop.Web/Areas/Admin/Models/Common/MaintenanceModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public MaintenanceModel()
3333

3434
public bool BackupSupported { get; set; }
3535

36+
public string DatabaseSize { get; set; }
37+
3638
#region Nested classes
3739

3840
public partial record DeleteGuestsModel : BaseNopModel

src/Presentation/Nop.Web/Areas/Admin/Views/Common/_Maintenance.ShrinkDatabase.cshtml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
<p>
77
@T("Admin.System.Maintenance.ShrinkDatabase.Text")
88
</p>
9+
<p>
10+
<ul>
11+
<input type="hidden" asp-for="DatabaseSize" />
12+
<li>@Html.Raw(Model.DatabaseSize)</li>
13+
</ul>
14+
</p>
915
</div>
1016
<div>
1117
<button type="submit" name="shrink-database" class="btn btn-default">

0 commit comments

Comments
 (0)