Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Log stats for app directory (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
ua741 authored Oct 12, 2023
2 parents 72229d8 + c0fe1a5 commit 655c06b
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 30 deletions.
2 changes: 1 addition & 1 deletion android/app/.classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>
10 changes: 5 additions & 5 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PODS:
- connectivity_plus (0.0.1):
- Flutter
- ReachabilitySwift
- device_info (0.0.1):
- device_info_plus (0.0.1):
- Flutter
- Firebase/CoreOnly (10.9.0):
- FirebaseCore (= 10.9.0)
Expand Down Expand Up @@ -189,7 +189,7 @@ PODS:
DEPENDENCIES:
- background_fetch (from `.symlinks/plugins/background_fetch/ios`)
- connectivity_plus (from `.symlinks/plugins/connectivity_plus/ios`)
- device_info (from `.symlinks/plugins/device_info/ios`)
- device_info_plus (from `.symlinks/plugins/device_info_plus/ios`)
- firebase_core (from `.symlinks/plugins/firebase_core/ios`)
- firebase_messaging (from `.symlinks/plugins/firebase_messaging/ios`)
- fk_user_agent (from `.symlinks/plugins/fk_user_agent/ios`)
Expand Down Expand Up @@ -257,8 +257,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/background_fetch/ios"
connectivity_plus:
:path: ".symlinks/plugins/connectivity_plus/ios"
device_info:
:path: ".symlinks/plugins/device_info/ios"
device_info_plus:
:path: ".symlinks/plugins/device_info_plus/ios"
firebase_core:
:path: ".symlinks/plugins/firebase_core/ios"
firebase_messaging:
Expand Down Expand Up @@ -341,7 +341,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
background_fetch: bc9b44b0bf8b434e282a2ac9be8662800a0296ed
connectivity_plus: 53efb943fc2882c8512d84c45707bcabc4c36076
device_info: d7d233b645a32c40dfdc212de5cf646ca482f175
device_info_plus: 7545d84d8d1b896cb16a4ff98c19f07ec4b298ea
Firebase: bd152f0f3d278c4060c5c71359db08ebcfd5a3e2
firebase_core: ce64b0941c6d87c6ef5022ae9116a158236c8c94
firebase_messaging: 42912365e62efc1ea3e00724e5eecba6068ddb88
Expand Down
4 changes: 2 additions & 2 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
"${BUILT_PRODUCTS_DIR}/Toast/Toast.framework",
"${BUILT_PRODUCTS_DIR}/background_fetch/background_fetch.framework",
"${BUILT_PRODUCTS_DIR}/connectivity_plus/connectivity_plus.framework",
"${BUILT_PRODUCTS_DIR}/device_info/device_info.framework",
"${BUILT_PRODUCTS_DIR}/device_info_plus/device_info_plus.framework",
"${BUILT_PRODUCTS_DIR}/fk_user_agent/fk_user_agent.framework",
"${BUILT_PRODUCTS_DIR}/flutter_email_sender/flutter_email_sender.framework",
"${BUILT_PRODUCTS_DIR}/flutter_image_compress/flutter_image_compress.framework",
Expand Down Expand Up @@ -355,7 +355,7 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Toast.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/background_fetch.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/connectivity_plus.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/device_info.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/device_info_plus.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/fk_user_agent.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_email_sender.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_image_compress.framework",
Expand Down
27 changes: 23 additions & 4 deletions lib/ui/tools/debug/app_storage_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,40 @@ class _AppStorageViewerState extends State<AppStorageViewer> {
allowCacheClear: true,
),
]);
final List<String> directoryStatePath = [
appDocumentsDirectory.path,
appSupportDirectory.path,
appTemporaryDirectory.path,
];
if (!Platform.isAndroid) {
directoryStatePath.add(iosTempDirectoryPath);
}
if (internalUser) {
paths.addAll([
PathStorageItem.name(appDocumentsDirectory.path, "App Documents Dir"),
PathStorageItem.name(appSupportDirectory.path, "App Support Dir"),
PathStorageItem.name(appTemporaryDirectory.path, "App Temp Dir"),
PathStorageItem.name(appDocumentsDirectory.path, "Documents"),
PathStorageItem.name(appSupportDirectory.path, "Support"),
PathStorageItem.name(appTemporaryDirectory.path, "App Temp"),
]);
if (!Platform.isAndroid) {
paths.add(PathStorageItem.name(iosTempDirectoryPath, "/tmp directory"));
paths.add(PathStorageItem.name(iosTempDirectoryPath, "/tmp"));
}
}
prettyStringDirectoryStats(directoryStatePath).ignore();
if (mounted) {
setState(() => {});
}
}

Future<void> prettyStringDirectoryStats(List<String> paths) async {
for (var path in paths) {
final DirectoryStat state = await getDirectoryStat(Directory(path));
final content = prettyPrintDirectoryStat(state, path);
if (content.isNotEmpty) {
debugPrint(content);
}
}
}

@override
Widget build(BuildContext context) {
debugPrint("$runtimeType building");
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/tools/debug/path_storage_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class _PathStorageViewerState extends State<PathStorageViewer> {
@override
Widget build(BuildContext context) {
return FutureBuilder<DirectoryStat>(
future: getDirectorySize(Directory(widget.item.path)),
future: getDirectoryStat(Directory(widget.item.path)),
builder: (context, snapshot) {
if (snapshot.hasData) {
return _buildMenuItemWidget(snapshot.data, null);
Expand Down
100 changes: 84 additions & 16 deletions lib/utils/directory_content.dart
Original file line number Diff line number Diff line change
@@ -1,38 +1,106 @@
import 'dart:io';

import "package:path/path.dart";
import "package:photos/utils/data_util.dart";

class DirectoryStat {
final int subDirectoryCount;
final String path;
final List<DirectoryStat> subDirectory;
final Map<String, int> fileNameToSize;
final int size;
final int fileCount;

DirectoryStat(this.subDirectoryCount, this.size, this.fileCount);
DirectoryStat(this.path, this.subDirectory, this.fileNameToSize, this.size);

int get total => fileNameToSize.length + subDirectory.length;

int get fileCount => fileNameToSize.length;
}

const int _oneMB = 1048576;
const int _tenMB = 10485760;

String prettyPrintDirectoryStat(
DirectoryStat dirStat,
String rootPath, [
String indent = '',
int minDirSizeForPrint = _tenMB,
int minFileSizeForPrint = _oneMB,
]) {
final StringBuffer buffer = StringBuffer();
_buildPrettyString(
dirStat,
rootPath,
buffer,
indent,
minDirSizeForPrint,
minFileSizeForPrint,
);
return buffer.toString();
}

void _buildPrettyString(
DirectoryStat dirStat,
String rootPath,
StringBuffer buffer,
String indent,
int minDirSizeForPrint,
int minFileSizeForPrint,
) {
if (dirStat.size < minDirSizeForPrint) {
return;
}
final String relativePath = dirStat.path.replaceFirst(rootPath, '');

if (relativePath.isEmpty) {
buffer.writeln(
'Root: ${basename(dirStat.path)} [SubDir:${dirStat.subDirectory.length} Files:${dirStat.fileNameToSize.length}], Size:${formatBytes(dirStat.size)}');
} else {
buffer.writeln(
'${indent}Directory: $relativePath, [SubDir:${dirStat.subDirectory.length} Files:${dirStat.fileNameToSize.length}], Size: ${formatBytes(dirStat.size)}',
);
}

for (var subDir in dirStat.subDirectory) {
_buildPrettyString(
subDir,
rootPath,
buffer,
'$indent ',
minDirSizeForPrint,
minFileSizeForPrint,
);
}

for (var fileName in dirStat.fileNameToSize.keys) {
final int fSize = dirStat.fileNameToSize[fileName]!;
if (fSize <= minFileSizeForPrint) {
continue;
}
buffer.writeln('$indent File: $fileName, Size: ${formatBytes(fSize)}');
}
}

Future<DirectoryStat> getDirectorySize(Directory directory) async {
Future<DirectoryStat> getDirectoryStat(Directory directory) async {
int size = 0;
int subDirCount = 0;
int fileCount = 0;
final List<DirectoryStat> subDirectories = [];
final Map<String, int> fileNameToSize = {};

if (await directory.exists()) {
// Get a list of all the files and directories in the directory
final List<FileSystemEntity> entities = directory.listSync();
// Iterate through the list of entities and add the sizes of the files to the total size
for (FileSystemEntity entity in entities) {
if (entity is File) {
size += (await File(entity.path).length());
fileCount++;
final int fileSize = await File(entity.path).length();
size += fileSize;
fileNameToSize[entity.uri.pathSegments.last] = fileSize;
} else if (entity is Directory) {
subDirCount++;
// If the entity is a directory, recursively calculate its size
final DirectoryStat subDirStat =
await getDirectorySize(Directory(entity.path));
await getDirectoryStat(Directory(entity.path));
subDirectories.add(subDirStat);
size += subDirStat.size;
subDirCount += subDirStat.subDirectoryCount;
fileCount += subDirStat.fileCount;
}
}
}
return DirectoryStat(subDirCount, size, fileCount);
return DirectoryStat(directory.path, subDirectories, fileNameToSize, size);
}

Future<void> deleteDirectoryContents(String directoryPath) async {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ description: ente photos application
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html

version: 0.7.99+499
version: 0.7.100+500

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down

0 comments on commit 655c06b

Please sign in to comment.