|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +namespace GVFS.Common |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Class responsible for the business logic involved in calculating the health statistics |
| 9 | + /// of a gvfs enlistment. Constructed with the lists of paths for the enlistment, and then |
| 10 | + /// internally stores the calculated information. Compute or recompute via CalculateStatistics |
| 11 | + /// with an optional parameter to only look for paths which are under the specified directory |
| 12 | + /// </summary> |
| 13 | + public class EnlistmentHealthCalculator |
| 14 | + { |
| 15 | + // In the context of this class, hydrated files are placeholders or modified paths |
| 16 | + // The total number of hydrated files is this.PlaceholderCount + this.ModifiedPathsCount |
| 17 | + private readonly EnlistmentPathData enlistmentPathData; |
| 18 | + |
| 19 | + public EnlistmentHealthCalculator(EnlistmentPathData pathData) |
| 20 | + { |
| 21 | + this.enlistmentPathData = pathData; |
| 22 | + } |
| 23 | + |
| 24 | + public EnlistmentHealthData CalculateStatistics(string parentDirectory) |
| 25 | + { |
| 26 | + int gitTrackedItemsCount = 0; |
| 27 | + int placeholderCount = 0; |
| 28 | + int modifiedPathsCount = 0; |
| 29 | + Dictionary<string, int> gitTrackedItemsDirectoryTally = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); |
| 30 | + Dictionary<string, int> hydratedFilesDirectoryTally = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); |
| 31 | + |
| 32 | + // Parent directory is a path relative to the root of the repository which is already in git format |
| 33 | + if (!parentDirectory.EndsWith(GVFSConstants.GitPathSeparatorString) && parentDirectory.Length > 0) |
| 34 | + { |
| 35 | + parentDirectory += GVFSConstants.GitPathSeparator; |
| 36 | + } |
| 37 | + |
| 38 | + if (parentDirectory.StartsWith(GVFSConstants.GitPathSeparatorString)) |
| 39 | + { |
| 40 | + parentDirectory = parentDirectory.TrimStart(GVFSConstants.GitPathSeparator); |
| 41 | + } |
| 42 | + |
| 43 | + gitTrackedItemsCount += this.CategorizePaths(this.enlistmentPathData.GitFolderPaths, gitTrackedItemsDirectoryTally, parentDirectory); |
| 44 | + gitTrackedItemsCount += this.CategorizePaths(this.enlistmentPathData.GitFilePaths, gitTrackedItemsDirectoryTally, parentDirectory); |
| 45 | + placeholderCount += this.CategorizePaths(this.enlistmentPathData.PlaceholderFolderPaths, hydratedFilesDirectoryTally, parentDirectory); |
| 46 | + placeholderCount += this.CategorizePaths(this.enlistmentPathData.PlaceholderFilePaths, hydratedFilesDirectoryTally, parentDirectory); |
| 47 | + modifiedPathsCount += this.CategorizePaths(this.enlistmentPathData.ModifiedFolderPaths, hydratedFilesDirectoryTally, parentDirectory); |
| 48 | + modifiedPathsCount += this.CategorizePaths(this.enlistmentPathData.ModifiedFilePaths, hydratedFilesDirectoryTally, parentDirectory); |
| 49 | + |
| 50 | + Dictionary<string, int> mostHydratedDirectories = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); |
| 51 | + |
| 52 | + // Map directory names to the corresponding health data from gitTrackedItemsDirectoryTally and hydratedFilesDirectoryTally |
| 53 | + foreach (KeyValuePair<string, int> pair in gitTrackedItemsDirectoryTally) |
| 54 | + { |
| 55 | + if (hydratedFilesDirectoryTally.TryGetValue(pair.Key, out int hydratedFiles)) |
| 56 | + { |
| 57 | + // In-lining this for now until a better "health" calculation is created |
| 58 | + // Another possibility is the ability to pass a function to use for health (might not be applicable) |
| 59 | + mostHydratedDirectories.Add(pair.Key, hydratedFiles); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + mostHydratedDirectories.Add(pair.Key, 0); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return new EnlistmentHealthData( |
| 68 | + parentDirectory, |
| 69 | + gitTrackedItemsCount, |
| 70 | + placeholderCount, |
| 71 | + modifiedPathsCount, |
| 72 | + this.CalculateHealthMetric(placeholderCount + modifiedPathsCount, gitTrackedItemsCount), |
| 73 | + mostHydratedDirectories.OrderByDescending(kp => kp.Value).ToList()); |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Take a file path and get the top level directory from it, or GVFSConstants.GitPathSeparator if it is not in a directory |
| 78 | + /// </summary> |
| 79 | + /// <param name="path">The path to a file to parse for the top level directory containing it</param> |
| 80 | + /// <returns>A string containing the top level directory from the provided path, or GVFSConstants.GitPathSeparator if the path is for an item in the root</returns> |
| 81 | + private string ParseTopDirectory(string path) |
| 82 | + { |
| 83 | + int whackLocation = path.IndexOf(GVFSConstants.GitPathSeparator); |
| 84 | + if (whackLocation == -1) |
| 85 | + { |
| 86 | + return GVFSConstants.GitPathSeparatorString; |
| 87 | + } |
| 88 | + |
| 89 | + return path.Substring(0, whackLocation); |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Categorizes a list of paths given as strings by mapping them to the top level directory in their path |
| 94 | + /// Modifies the directoryTracking dictionary to have an accurate count of the files underneath a top level directory |
| 95 | + /// </summary> |
| 96 | + /// <remarks> |
| 97 | + /// The distinction between files and directories is important -- |
| 98 | + /// If the path to a file doesn't contain a GVFSConstants.GitPathSeparator, then that means it falls within the root |
| 99 | + /// However if a directory's path doesn't contain a GVFSConstants.GitPathSeparator, it doesn't count towards its own hydration |
| 100 | + /// </remarks> |
| 101 | + /// <param name="paths">An enumerable containing paths as strings</param> |
| 102 | + /// <param name="directoryTracking">A dictionary used to track the number of files per top level directory</param> |
| 103 | + /// <param name="parentDirectory">Paths will only be categorized if they are descendants of the parentDirectory</param> |
| 104 | + private int CategorizePaths(IEnumerable<string> paths, Dictionary<string, int> directoryTracking, string parentDirectory) |
| 105 | + { |
| 106 | + int count = 0; |
| 107 | + foreach (string path in paths) |
| 108 | + { |
| 109 | + // Only categorize if descendent of the parentDirectory |
| 110 | + if (path.StartsWith(parentDirectory, StringComparison.OrdinalIgnoreCase)) |
| 111 | + { |
| 112 | + count++; |
| 113 | + |
| 114 | + // If the path is to the parentDirectory, ignore it to avoid adding string.Empty to the data structures |
| 115 | + if (!parentDirectory.Equals(path, StringComparison.OrdinalIgnoreCase)) |
| 116 | + { |
| 117 | + // Trim the path to parent directory |
| 118 | + string topDir = this.ParseTopDirectory(this.TrimDirectoryFromPath(path, parentDirectory)); |
| 119 | + if (!topDir.Equals(GVFSConstants.GitPathSeparatorString)) |
| 120 | + { |
| 121 | + this.IncreaseDictionaryCounterByKey(directoryTracking, topDir); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return count; |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Trim the relative path to a directory from the front of a specified path which is its child |
| 132 | + /// </summary> |
| 133 | + /// <remarks>Precondition: 'directoryTarget' must be an ancestor of 'path'</remarks> |
| 134 | + /// <param name="path">The path being trimmed</param> |
| 135 | + /// <param name="directoryTarget">The directory target whose path to trim from the path</param> |
| 136 | + /// <returns>The newly formatted path with the directory trimmed</returns> |
| 137 | + private string TrimDirectoryFromPath(string path, string directoryTarget) |
| 138 | + { |
| 139 | + return path.Substring(directoryTarget.Length); |
| 140 | + } |
| 141 | + |
| 142 | + private void IncreaseDictionaryCounterByKey(Dictionary<string, int> countingDictionary, string key) |
| 143 | + { |
| 144 | + if (!countingDictionary.TryGetValue(key, out int count)) |
| 145 | + { |
| 146 | + count = 0; |
| 147 | + } |
| 148 | + |
| 149 | + countingDictionary[key] = ++count; |
| 150 | + } |
| 151 | + |
| 152 | + private decimal CalculateHealthMetric(int hydratedFileCount, int totalFileCount) |
| 153 | + { |
| 154 | + if (totalFileCount == 0) |
| 155 | + { |
| 156 | + return 0; |
| 157 | + } |
| 158 | + |
| 159 | + return (decimal)hydratedFileCount / (decimal)totalFileCount; |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments