freeCodeCamp/server/boot/about.js

65 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-02-23 07:00:59 +00:00
import { Observable } from 'rx';
2016-01-15 08:10:13 +00:00
import dedent from 'dedent';
import moment from 'moment';
2016-04-05 21:45:09 +00:00
import { timeCache, observeMethod } from '../utils/rx';
2016-01-15 08:10:13 +00:00
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
2016-04-05 21:45:09 +00:00
// userCount(where: Object) => Observable[Number]
// getCertCount(userCount: userCount, cert: String) => Observable[Number]
function getCertCount(userCount, cert) {
return userCount({ [cert]: true })
// using This-Bind operator
::timeCache(2, 'hours');
}
2016-01-15 08:10:13 +00:00
export default function about(app) {
const router = app.loopback.Router();
const User = app.models.User;
2016-04-05 21:45:09 +00:00
const userCount = observeMethod(User, 'count');
const frontEndCount$ = getCertCount(userCount, 'isFrontEndCert');
const dataVisCount$ = getCertCount(userCount, 'isDataVisCert');
const backEndCount$ = getCertCount(userCount, 'isBackEndCert');
2016-01-15 08:10:13 +00:00
function showAbout(req, res, next) {
const daysRunning = moment().diff(new Date('10/15/2014'), 'days');
2016-02-23 07:00:59 +00:00
Observable.combineLatest(
2016-04-05 21:45:09 +00:00
frontEndCount$,
dataVisCount$,
backEndCount$,
(frontEndCount = 0, dataVisCount = 0, backEndCount = 0) => ({
2016-02-23 07:00:59 +00:00
frontEndCount,
dataVisCount,
backEndCount
})
)
2016-04-05 21:45:09 +00:00
.doOnNext(({ frontEndCount, dataVisCount, backEndCount }) => {
2016-01-15 08:10:13 +00:00
res.render('resources/about', {
frontEndCount: numberWithCommas(frontEndCount),
dataVisCount: numberWithCommas(dataVisCount),
backEndCount: numberWithCommas(backEndCount),
2016-01-15 08:10:13 +00:00
daysRunning,
title: dedent`
About our Open Source Community, our social media presence,
2016-02-23 06:28:32 +00:00
and how to contact us
`.split('\n').join(' '),
2016-01-15 08:10:13 +00:00
globalCompletedCount: numberWithCommas(
2016-02-23 06:28:32 +00:00
5612952 + (Math.floor((Date.now() - 1446268581061) / 1800))
2016-02-23 06:15:14 +00:00
),
2016-02-23 06:32:50 +00:00
globalPledgedAmount: numberWithCommas(Math.floor(
28000 +
((Date.now() - 1456207176902) / (2629746000 / 2000) * 8.30)
))
2016-01-15 08:10:13 +00:00
});
})
.subscribe(() => {}, next);
}
router.get('/about', showAbout);
app.use(router);
}