Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
j1348 authored and jfroffice committed Dec 25, 2018
1 parent 6b4ad9f commit 7403d33
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
3 changes: 1 addition & 2 deletions src/graphql/resolvers/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { getRepo, getReposFast, getReposFilter } from './repo';
import { getRepo, getReposFast } from './repo';

export default {
Query: {
getRepos: getReposFast,
getReposFilter: (_, { filter }) => getReposFilter(filter),
getRepo: (_, { id }) => getRepo(id),
},
Repo: {
Expand Down
27 changes: 21 additions & 6 deletions src/graphql/resolvers/repo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@ export function getManyRepos(ids) {
}

export function getRepoRefs() {
return Reporef.find({}).sort({ 'value.date': -1, 'value.starsByDay': -1 });
return Reporef.find({});
}

export function getRepo(id) {
return Repo.find({ _id: id }).map(
({ _id: id, name, author, language, href, description }) => {
return {
id,
name,
author,
language,
href,
description,
};
},
);
}

export function getReposFast() {
Expand All @@ -21,16 +36,16 @@ export function getReposFast() {

return getManyRepos(ids).then(repos => {
const newRepos = repos.map(
({ _id: id, name, author, language, href, description }) => {
const { stars, starsByDay, date } = metaInfo[id];
({ _id: id, name, description, author, language, href }) => {
const { date, stars, starsByDay } = metaInfo[id];
return {
id,
date,
author,
name,
description,
href,
author,
language,
href,
date,
stars,
starsByDay,
};
Expand Down
8 changes: 5 additions & 3 deletions src/graphql/resolvers/repo/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ const reporefSchema = mongoose.Schema({
const Reporef = mongoose.model('Reporef', reporefSchema);

function reduceRepo() {
console.log('running reduceRepo');

console.time('reduceRepos');
function map() {
var lastTicks = this.ticks.slice(Math.max(this.ticks.length - 40, 0));
for (var i = 0; i < lastTicks.length; i++) {
Expand Down Expand Up @@ -101,10 +100,13 @@ function reduceRepo() {
},
out: { replace: 'reporefs', inline: 1 },
},
err => {
(err, result) => {
if (err) {
console.log(err);
}

console.timeEnd('reduceRepos');
console.log(result.stats);
},
);
}
Expand Down
14 changes: 1 addition & 13 deletions src/graphql/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,13 @@ export default gql`
Ticks: [Tick]
stars: Int!
forks: Int!
date: Date
createdAt: Date!
updatedAt: Date!
}
type RepoFast {
id: ID!
author: String!
name: String!
language: String!
description: String
starsByDay: Int
href: String!
stars: Int!
}
type Query {
getRepos: [Repo]
getReposFast: [RepoFast]
getReposFilter(filter: JSON!): [Repo]
getRepo(id: ID!): Repo
}
Expand Down
8 changes: 5 additions & 3 deletions src/importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ function importer(toProcessed = 10) {
cb2(null, {});
});
},
function(err) {
cb(err, {});
},
cb,
);
},
function(err) {
Expand Down Expand Up @@ -125,3 +123,7 @@ module.exports = new CronJob({
start: true,
timeZone: 'Europe/Paris',
});

if (process.env.NODE_ENV !== 'production') {
setTimeout(reduceRepo, 3000);
}

0 comments on commit 7403d33

Please sign in to comment.