Skip to content

Commit

Permalink
Merge 538a0e4 into 05f08f5
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilesh26 committed Mar 20, 2018
2 parents 05f08f5 + 538a0e4 commit f82cb45
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 25 deletions.
68 changes: 62 additions & 6 deletions src/client/components/pages/statistics.js
Expand Up @@ -35,18 +35,20 @@ function TopEditorsTable(props) {
const {editors} = props;
return (
<div>
<div> <h2 className="text-center">Top 10 Editors</h2> </div>
<div>
<h2>Top 10 Editors</h2>
</div>
<Table
bordered
condensed
striped
>
<thead>
<tr>
<th >#</th>
<th>Editor&apos;s Name</th>
<th>Total Revisions</th>
<th>Registration Date</th>
<th className="col-sm-1" >#</th>
<th className="col-sm-5" >Editor&apos;s Name</th>
<th className="col-sm-3" >Total Revisions</th>
<th className="col-sm-3" >Registration Date</th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -76,24 +78,78 @@ TopEditorsTable.propTypes = {
editors: PropTypes.array.isRequired
};

/**
* Renders the document and displays the EntityCountTable table.
* @returns {ReactElement} a HTML document which displays the
* EntityCountTable table in the statistics page
*/

function EntityCountTable(props) {
const {allEntities, last30DaysEntities} = props;
return (
<div>
<div>
<h2>Total Entities</h2>
</div>
<Table
bordered
condensed
striped
>
<thead>
<tr>
<th className="col-sm-1" >#</th>
<th className="col-sm-5" >Entity Type</th>
<th className="col-sm-3" >Total</th>
<th className="col-sm-3" >Added in Last 30 days</th>
</tr>
</thead>
<tbody>
{
allEntities.map((entity, i) => (
<tr key={i}>
<td>{i + 1}</td>
<td>{entity.modelName}</td>
<td>{entity.Count}</td>
<td>{last30DaysEntities[i].Count}</td>
</tr>
))
}
</tbody>
</Table>
</div>
);
}

EntityCountTable.propTypes = {
allEntities: PropTypes.array.isRequired,
last30DaysEntities: PropTypes.array.isRequired
};

/**
* Renders the document and displays the 'Statistics' page.
* @returns {ReactElement} a HTML document which displays the statistics
* page
*/

function StatisticsPage(props) {
const {topEditors} = props;
const {allEntities, last30DaysEntities, topEditors} = props;
return (
<div>
<PageHeader>Statistics of BookBrainz</PageHeader>
<TopEditorsTable editors={topEditors}/>
<EntityCountTable
allEntities={allEntities}
last30DaysEntities={last30DaysEntities}
/>
</div>
);
}

StatisticsPage.displayName = 'StatisticsPage';
StatisticsPage.propTypes = {
allEntities: PropTypes.array.isRequired,
last30DaysEntities: PropTypes.array.isRequired,
topEditors: PropTypes.array.isRequired
};

Expand Down
6 changes: 5 additions & 1 deletion src/client/controllers/statistics.js
Expand Up @@ -27,7 +27,11 @@ const propsTarget = document.getElementById('props');
const props = propsTarget ? JSON.parse(propsTarget.innerHTML) : {};
const markup = (
<Layout {...extractLayoutProps(props)}>
<StatisticsPage topEditors={props.topEditors}/>
<StatisticsPage
allEntities={props.allEntities}
last30DaysEntities={props.last30DaysEntities}
topEditors={props.topEditors}
/>
</Layout>
);

Expand Down
6 changes: 6 additions & 0 deletions src/server/helpers/utils.js
Expand Up @@ -48,6 +48,12 @@ export function getEntityModels(orm: Object): Object {
};
}

export function getDateBeforeDays(days) {
const date = new Date();
date.setDate(date.getDate() - days);
return date;
}

export function filterIdentifierTypesByEntityType(
identifierTypes: Array<Object>,
entityType: string
Expand Down
101 changes: 83 additions & 18 deletions src/server/routes/statistics.js
Expand Up @@ -17,21 +17,82 @@
*/

import * as propHelpers from '../../client/helpers/props';
import * as utils from '../helpers/utils';
import {escapeProps, generateProps} from '../helpers/props';
import Layout from '../../client/containers/layout';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import StatisticsPage from '../../client/components/pages/statistics';
import _ from 'lodash';
import express from 'express';


const router = express.Router();

/* Get Statistics Page */
router.get('/', (req, res) => {
router.get('/', async (req, res) => {
const {orm} = req.app.locals;
const {Editor} = orm;

const entityModels = utils.getEntityModels(orm);

// queryPromises1 is used to extract total count of all entities
const queryPromises1 = [];

// queryPromises2 is used for total count of entities added in 30 days
const queryPromises2 = [];

/*
* Here We are fetching count of master revisions
* for every type of entities added from beginning
*/

// eslint-disable-next-line guard-for-in
for (const modelName in entityModels) {
const model = entityModels[modelName];
queryPromises1.push(
model.query((qb) => {
qb
.leftJoin(
'bookbrainz.revision',
`bookbrainz.${_.snakeCase(modelName)}.revision_id`,
'bookbrainz.revision.id'
)
.where('master', true);
})
.count().then((Count) =>
({Count, modelName}))
);
}
const allEntities = await Promise.all(queryPromises1);

/*
* Here We are fetching count of master revision
* for every type of entities added in last 30 days
*/

// eslint-disable-next-line guard-for-in
for (const modelName in entityModels) {
const model = entityModels[modelName];

queryPromises2.push(
model.query((qb) => {
qb
.leftJoin(
'bookbrainz.revision',
`bookbrainz.${_.snakeCase(modelName)}.revision_id`,
'bookbrainz.revision.id'
)
.where('master', true)
.where('bookbrainz.revision.created_at', '>=',
utils.getDateBeforeDays(30));
})
.count().then((Count) =>
({Count, modelName}))
);
}
const last30DaysEntities = await Promise.all(queryPromises2);

/*
* Here We are fetching top 10 Edirors
* from database on Basis of totalRevisions
Expand All @@ -45,23 +106,27 @@ router.get('/', (req, res) => {
collection.models.map((model) =>
model.attributes));

getTopEditors.then((topEditors) => {
const props = generateProps(req, res, {
topEditors
});
const markup = ReactDOMServer.renderToString(
<Layout {...propHelpers.extractLayoutProps(props)}>
<StatisticsPage
topEditors={topEditors}
/>
</Layout>
);
res.render('target', {
markup,
props: escapeProps(props),
script: '/js/statistics.js',
title: 'Statistics'
});
const topEditors = await getTopEditors;

const props = generateProps(req, res, {
allEntities,
last30DaysEntities,
topEditors
});
const markup = ReactDOMServer.renderToString(
<Layout {...propHelpers.extractLayoutProps(props)}>
<StatisticsPage
allEntities={allEntities}
last30DaysEntities={last30DaysEntities}
topEditors={topEditors}
/>
</Layout>
);
res.render('target', {
markup,
props: escapeProps(props),
script: '/js/statistics.js',
title: 'Statistics'
});
});

Expand Down

0 comments on commit f82cb45

Please sign in to comment.