Skip to content

Fetch class counts using Parse Query #18

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

Merged
merged 1 commit into from
Feb 27, 2016
Merged
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
13 changes: 11 additions & 2 deletions dashboard/Data/Browser/Browser.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export default class Browser extends DashboardView {
}

componentWillMount() {
this.props.schema.dispatch(ActionTypes.FETCH);
this.props.schema.dispatch(ActionTypes.FETCH)
.then(() => this.fetchCollectionCounts());
if (!this.props.params.className && this.props.schema.data.get('classes')) {
this.redirectToFirstClass(this.props.schema.data.get('classes'));
} else if (this.props.params.className) {
Expand All @@ -83,7 +84,8 @@ export default class Browser extends DashboardView {

componentWillReceiveProps(nextProps, nextContext) {
if (this.context !== nextContext) {
nextProps.schema.dispatch(ActionTypes.FETCH);
nextProps.schema.dispatch(ActionTypes.FETCH)
.then(() => this.fetchCollectionCounts());
let changes = {
filters: new List(),
data: null,
Expand Down Expand Up @@ -220,6 +222,13 @@ export default class Browser extends DashboardView {
});
}

fetchCollectionCounts() {
this.props.schema.data.get('classes').forEach((_, className) => {
this.context.currentApp.getClassCount(className)
.then(count => this.setState({ counts: { [className]: count, ...this.state.counts } }));
})
Copy link
Contributor

Choose a reason for hiding this comment

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

;

Should be fine in the short term. For any of these requests where a batch doesn't exist we may consider having the dashboard node server make the request. But ideally we go straight to the api server without any application logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would rather not add any logic to the node thing that serves the dashboard. Adding logic to the server would be reasonable I think. But thats a debate for another time.

}

fetchInfo(app) {
app.getCollectionInfo().then(({ collections }) => {
let counts = {};
Expand Down
20 changes: 10 additions & 10 deletions dashboard/Data/Browser/BrowserToolbar.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import BrowserFilter from 'components/BrowserFilter/BrowserFilter.react';
import BrowserMenu from 'components/BrowserMenu/BrowserMenu.react';
import Icon from 'components/Icon/Icon.react';
import MenuItem from 'components/BrowserMenu/MenuItem.react';
import prettyNumber from 'lib/prettyNumber';
import React from 'react';
import SecurityDialog from 'dashboard/Data/Browser/SecurityDialog.react'
import Separator from 'components/BrowserMenu/Separator.react';
import Toolbar from 'components/Toolbar/Toolbar.react';
import styles from 'dashboard/Data/Browser/Browser.scss';
import BrowserFilter from 'components/BrowserFilter/BrowserFilter.react';
import BrowserMenu from 'components/BrowserMenu/BrowserMenu.react';
import Icon from 'components/Icon/Icon.react';
import MenuItem from 'components/BrowserMenu/MenuItem.react';
import prettyNumber from 'lib/prettyNumber';
import React from 'react';
import SecurityDialog from 'dashboard/Data/Browser/SecurityDialog.react';
import Separator from 'components/BrowserMenu/Separator.react';
import styles from 'dashboard/Data/Browser/Browser.scss';
import Toolbar from 'components/Toolbar/Toolbar.react';

let BrowserToolbar = ({
className,
Expand Down
21 changes: 21 additions & 0 deletions lib/ParseApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export default class ParseApp {
lastFetched: new Date(0)
};

this.classCounts = {
counts: {},
lastFetched: {},
}

this.hasCheckedForMigraton = false;
}

Expand Down Expand Up @@ -181,6 +186,22 @@ export default class ParseApp {
});
}

getClassCount(className) {
this.setParseKeys();
if (this.classCounts.counts[className] !== undefined) {
// Cache it for a minute
if (new Date() - this.classCounts.lastFetched[className] < 60000) {
return Parse.Promise.as(this.classCounts.counts[className]);
}
}
let p = new Parse.Query(className).count({ useMasterKey: true });
p.then(count => {
this.classCounts.counts[className] = count;
this.classCounts.lastFetched[className] = new Date();
})
return p;
}

getAnalyticsRetention(time) {
time = Math.round(time.getTime() / 1000);
return AJAX.abortableGet('/apps/' + this.slug + '/analytics_retention?at=' + time);
Expand Down