Skip to content

Commit

Permalink
refactor(library storage)!: optimized dependents retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
sr258 committed Jun 24, 2020
1 parent eb2c41a commit 477e536
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/LibraryAdministration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ export default class LibraryAdministration {
)
).sort((a, b) => a.compare(b));

const dependents = await this.libraryManager.libraryStorage.getAllDependentsCount();

return Promise.all(
libraryMetadata.map(async (metadata) => {
const usage = await this.contentManager.contentStorage.getUsage(
metadata
);
const dependentsCount = await this.libraryManager.libraryStorage.getDependentsCount(
metadata
);
const dependentsCount =
dependents[LibraryName.toUberName(metadata)] ?? 0;
return {
title: metadata.title,
machineName: metadata.machineName,
Expand Down
39 changes: 32 additions & 7 deletions src/implementation/fs/FileLibraryStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,46 @@ export default class FileLibraryStorage implements ILibraryStorage {
return fsExtra.pathExists(this.getFilePath(library, filename));
}

// TODO: optimize
public async getDependentsCount(library: ILibraryName): Promise<number> {
let counter = 0;

/**
* Counts how often libraries are listed in the dependencies of other
* libraries and returns a list of the number.
* @returns an object with ubernames as key.
* Example:
* {
* 'H5P.Example': 10
* }
* This means that H5P.Example is used by 10 other libraries.
*/
public async getAllDependentsCount(): Promise<{
[ubername: string]: number;
}> {
const returnObject = {};
const librariesNames = await this.getInstalledLibraryNames();
const librariesMetadata = await Promise.all(
librariesNames.map((lib) => this.getLibrary(lib))
);
for (const libraryMetadata of librariesMetadata) {
if (hasDependencyOn(libraryMetadata, library)) {
counter += 1;
for (const dependency of (
libraryMetadata.preloadedDependencies ?? []
)
.concat(libraryMetadata.editorDependencies ?? [])
.concat(libraryMetadata.dynamicDependencies ?? [])) {
const ubername = LibraryName.toUberName(dependency);
returnObject[ubername] = (returnObject[ubername] ?? 0) + 1;
}
}

return counter;
return returnObject;
}

/**
* Returns the number of libraries that depend on this (single) library.
* @param library the library to check
* @returns the number of libraries that depend on this library.
*/
public async getDependentsCount(library: ILibraryName): Promise<number> {
const allDependencies = await this.getAllDependentsCount();
return allDependencies[LibraryName.toUberName(library)] ?? 0;
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,19 @@ export interface ILibraryStorage {
fileExists(library: ILibraryName, filename: string): Promise<boolean>;

/**
* Returns the number of libraries that depend on this library.
* Counts how often libraries are listed in the dependencies of other
* libraries and returns a list of the number.
* @returns an object with ubernames as key.
* Example:
* {
* 'H5P.Example': 10
* }
* This means that H5P.Example is used by 10 other libraries.
*/
getAllDependentsCount(): Promise<{ [ubername: string]: number }>;

/**
* Returns the number of libraries that depend on this (single) library.
* @param library the library to check
* @returns the number of libraries that depend on this library.
*/
Expand Down

0 comments on commit 477e536

Please sign in to comment.