@@ -903,5 +903,102 @@ public async Task ClearAllTasks()
903903
904904 #endregion
905905
906+ #region Maintenance Operations
907+
908+ /// <summary>
909+ /// Get all database names that represent Telegram channels (numeric IDs)
910+ /// Excludes system databases like TCCONFIG, TFM-SHARED, admin, local, config
911+ /// </summary>
912+ public async Task < List < string > > GetAllChannelDatabaseNames ( )
913+ {
914+ var excludedDatabases = new HashSet < string > ( StringComparer . OrdinalIgnoreCase )
915+ {
916+ CONFIG_DB_NAME ,
917+ SHARED_DB_NAME ,
918+ "admin" ,
919+ "local" ,
920+ "config" ,
921+ "default"
922+ } ;
923+
924+ var databaseNames = new List < string > ( ) ;
925+
926+ using ( var cursor = await client . ListDatabaseNamesAsync ( ) )
927+ {
928+ while ( await cursor . MoveNextAsync ( ) )
929+ {
930+ foreach ( var dbName in cursor . Current )
931+ {
932+ // Only include databases that are numeric (channel IDs) or start with "-" (group IDs)
933+ if ( ! excludedDatabases . Contains ( dbName ) &&
934+ ( long . TryParse ( dbName , out _ ) || ( dbName . StartsWith ( "-" ) && long . TryParse ( dbName , out _ ) ) ) )
935+ {
936+ databaseNames . Add ( dbName ) ;
937+ }
938+ }
939+ }
940+ }
941+
942+ _logger . LogInformation ( "Found {Count} channel databases" , databaseNames . Count ) ;
943+ return databaseNames ;
944+ }
945+
946+ /// <summary>
947+ /// Get statistics for a specific database including size, document count, and dates
948+ /// </summary>
949+ public async Task < DatabaseStats > GetDatabaseStats ( string dbName )
950+ {
951+ var stats = new DatabaseStats ( ) ;
952+
953+ try
954+ {
955+ var database = getDatabase ( dbName ) ;
956+
957+ // Get database stats using command
958+ var command = new BsonDocument { { "dbStats" , 1 } } ;
959+ var result = await database . RunCommandAsync < BsonDocument > ( command ) ;
960+
961+ if ( result . Contains ( "dataSize" ) )
962+ {
963+ stats . SizeInBytes = result [ "dataSize" ] . ToInt64 ( ) ;
964+ }
965+
966+ // Get document count from the directory collection
967+ var collection = database . GetCollection < BsonFileManagerModel > ( "directory" ) ;
968+ stats . DocumentCount = await collection . CountDocumentsAsync ( Builders < BsonFileManagerModel > . Filter . Empty ) ;
969+
970+ // Get creation date (oldest document) and last modified (newest document)
971+ var oldestDoc = await collection
972+ . Find ( Builders < BsonFileManagerModel > . Filter . Empty )
973+ . Sort ( Builders < BsonFileManagerModel > . Sort . Ascending ( x => x . DateCreated ) )
974+ . Limit ( 1 )
975+ . FirstOrDefaultAsync ( ) ;
976+
977+ var newestDoc = await collection
978+ . Find ( Builders < BsonFileManagerModel > . Filter . Empty )
979+ . Sort ( Builders < BsonFileManagerModel > . Sort . Descending ( x => x . DateModified ) )
980+ . Limit ( 1 )
981+ . FirstOrDefaultAsync ( ) ;
982+
983+ if ( oldestDoc != null )
984+ {
985+ stats . CreatedAt = oldestDoc . DateCreated ;
986+ }
987+
988+ if ( newestDoc != null )
989+ {
990+ stats . LastModified = newestDoc . DateModified ;
991+ }
992+ }
993+ catch ( Exception ex )
994+ {
995+ _logger . LogWarning ( ex , "Error getting stats for database {DbName}" , dbName ) ;
996+ }
997+
998+ return stats ;
999+ }
1000+
1001+ #endregion
1002+
9061003 }
9071004}
0 commit comments