Design a Dashboard of Database Widgets
Model a dashboard that places number, chart and list widgets on a 12-column grid without overlap, works out each widget's result from a database's current rows, and lets new widget kinds plug in without changing the layout code.
Design the classes for a dashboard that shows database results in widgets on a grid, give their responsibilities and method contracts, walk through a short session, then implement the core in a language of your choice.
For this exercise, assume the following product facts, modeled on a workspace tool where a team keeps its work in a database. A database has a schema of named properties, each either text or number, and a list of rows. Each row has an ID and one value for every property.
A dashboard belongs to one database and shows widgets on a grid that is 12 columns wide and grows downward without limit. Every widget has a title, a filter, a position (top row and left column, both counted from 0) and a size (width in columns and height in rows, both at least 1).
A filter is a list of conditions of the form "property equals value". A row matches when every condition holds, and an empty filter matches every row.
Three kinds of widget exist today: - Number: one figure, either the count of matching rows or the sum of one number property over the matching rows. - Chart: groups the matching rows by one property and gives each group's value with its row count, groups in ascending order of value.
Drawing the chart is out of scope; the result is the list of groups and counts. - List: the IDs of matching rows, at most a chosen limit of them, sorted by one property, ascending or descending, with ties broken by row ID ascending.
A user can: - add a widget, which returns a new widget ID that is never reused - move or resize a widget - remove a widget - view the dashboard
Viewing returns every widget's title and current result in reading order: by top row, then by left column. Results always reflect the database's rows at the moment of viewing.
The dashboard rejects these requests and stays unchanged after each one: - a change that would make two widgets overlap or push a widget past the 12th column - a widget whose filter, grouping, sort or sum names a property the database does not have - a sum over a property that is not a number - a list limit below 1 - a request that names a widget ID the dashboard does not have
The team plans more widget kinds later, such as a progress bar. Adding one should not require changing how the dashboard places widgets or builds its view.
Out of scope: permissions and sharing, several people editing at once, saving to disk, drawing pixels, formulas, and widgets that read from more than one database.