Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add way to get all instances of a type of model from dashboard #649

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/dashboard/dashboard-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,19 @@ describe('Dashboard manager', () => {
expect(mockModelManager.destroy).toHaveBeenCalledWith(mockOriginalDataSource);
expect(mockDataSourceManager.setRootDataSource).toHaveBeenCalledWith(mockNewDataSource, dashboard.root);
});

test('can retrieve model instances', () => {
const dashboard = dashboardManager.create({
type: 'serialized-model'
});

const mockDataSource = class MockDataSource {
public readonly value: number = Math.random();
};

mockModelManager.getModelInstances = jest.fn().mockReturnValue([new mockDataSource()]);

expect(dashboard.getModelInstances(mockDataSource).length).toBe(1);
expect(mockModelManager.getModelInstances).toHaveBeenCalledWith(mockDataSource, {});
});
});
8 changes: 7 additions & 1 deletion src/dashboard/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface Dashboard<TRoot extends object = object> {
setTimeRange(timeRange: TimeRange): this;

/**
* Returns a serialized from of this dashboard. Note this does not
* Returns a serialized form of this dashboard. Note this does not
* affect the dashboard in any way, it must be explicitly destroyed
* if it is no longer in use.
*/
Expand Down Expand Up @@ -56,4 +56,10 @@ export interface Dashboard<TRoot extends object = object> {
*/
// tslint:disable-next-line: no-any
getRootDataSource<T extends DataSource<any>>(): T | undefined;

/**
* Returns a shallow copy array of model instances within a dashboard that match the
* argument model class
*/
getModelInstances<T extends object>(modelClass: Constructable<T>): object[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought your use case was to instantiate a model without a renderer? If that's the case, no changes are needed in this repo, just in hyperdash-angular to export certain APIs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(i,e. model manager's programmatic instantiation APIs)

}
7 changes: 7 additions & 0 deletions src/dashboard/default-dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,11 @@ export class DefaultDashboard<TRoot extends object> implements Dashboard<TRoot>
public getRootDataSource<T extends DataSource<any>>(): T | undefined {
return this.dataSourceManager.getRootDataSource(this.root) as T | undefined;
}

/**
* @inheritdoc
*/
public getModelInstances<T extends object>(modelClass: Constructable<T>): object[] {
return this.modelManager.getModelInstances<T>(modelClass, this.root);
}
}
20 changes: 20 additions & 0 deletions src/model/manager/model-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ describe('Model manager', () => {
// Value used to disambiguate equality between instances
public readonly value: number = Math.random();
};
const testClass1 = class TestClass1 {
// Value used to disambiguate equality between instances
public readonly value: number = Math.random();
};
const testClass2 = class TestClass2 {
// Value used to disambiguate equality between instances
public readonly value: number = Math.random();
};
let manager: ModelManager;
let mockLogger: PartialObjectMock<Logger>;
let mockApiBuilder: PartialObjectMock<ModelApiBuilder<ModelApi>>;
Expand Down Expand Up @@ -59,6 +67,18 @@ describe('Model manager', () => {
manager.registerModelApiBuilder(mockApiBuilder as ModelApiBuilder<ModelApi>);
});

test('returns array of instances matching arg', () => {
expect(manager.getModelInstances(testClass, {})).toStrictEqual([]);

const instance = manager.construct(testClass);
const instance1 = manager.construct(testClass1, instance);
const instance2 = manager.construct(testClass2, instance1);
expect(manager.getModelInstances(testClass, instance)).toStrictEqual([instance]);
expect(manager.getModelInstances(testClass1, instance)).toStrictEqual([instance1]);
expect(manager.getModelInstances(testClass2, instance)).toStrictEqual([instance2]);
expect(manager.getModelInstances(testClass2, instance1)).toStrictEqual([instance2]);
});

test('allows constructing new models', () => {
const instance = manager.construct(testClass);
expect(instance.constructor).toBe(testClass);
Expand Down
17 changes: 17 additions & 0 deletions src/model/manager/model-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ export class ModelManager {
private readonly beforeModelDestroyedEvent: BeforeModelDestroyedEvent
) {}

/**
* Returns a shallow copy array of model instances that match the argument model class
*/
public getModelInstances<T extends object>(modelClass: Constructable<T>, root: object): object[] {
let found: object[] = [];

if (root instanceof modelClass) {
found = [...found, root];
}

this.modelInstanceMap
.get(root)
?.children.forEach(child => (found = [...found, ...this.getModelInstances(modelClass, child)]));

return found;
}

/**
* Constructs (@see `ModelManager.construct`) then initializes (@see `ModelManager.initialize`) it
*
Expand Down