Skip to content

Commit 757cc48

Browse files
authored
Merge pull request #26 from demokratie-live/sprint#6/memory-issue
Sprint#6/memory issue
2 parents d5a20aa + 43e085d commit 757cc48

File tree

8 files changed

+44
-44
lines changed

8 files changed

+44
-44
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2-
.env
2+
*.env
3+
*.log

Dockerfile

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
FROM node:carbon
1+
FROM node:latest
22

33
WORKDIR /app
44

5-
COPY package*.json ./
6-
7-
RUN yarn
8-
9-
# Bundle app source
105
COPY . .
116

12-
# EXPOSE 8080
7+
RUN yarn install
138

14-
CMD [ "yarn", "start" ]
9+
ENTRYPOINT [ "yarn", "start" ]

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
"license": "Apache2",
77
"private": true,
88
"scripts": {
9-
"start": "nodemon --exec babel-node src/index.js",
9+
"start": "babel-node src/index.js",
1010
"dev":
11-
"nodemon -e js,graphql src/index.js --exec \"node --require dotenv/config --require babel-register\"",
12-
"imp": "nodemon -e js,graphql --exec \"babel-node scripts/import.js\"",
13-
"impAll": "nodemon -e js,graphql --exec \"babel-node scripts/importAll.js\"",
11+
"nodemon -L -e js,graphql src/index.js --exec \"node --require dotenv/config --require babel-register\"",
1412
"lint": "eslint .",
1513
"clean": "rm -rf build && mkdir build",
1614
"build": "babel -d build src -s",

src/config/constants.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
export default {
22
PORT: process.env.PORT || 3000,
33
db: {
4-
development: {
5-
app: 'mongodb://localhost/democracy_development',
6-
},
7-
url: process.env.DB || 'mongodb://localhost/democracy_development',
4+
url: process.env.DB_URL || 'mongodb://localhost/democracy_development',
85
},
96
GRAPHIQL_PATH: '/graphiql',
107
GRAPHQL_PATH: '/graphql',

src/graphql/client.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { InMemoryCache } from 'apollo-cache-inmemory';
44
import fetch from 'node-fetch';
55
import CONSTANTS from '../config/constants';
66

7-
const client = new ApolloClient({
7+
8+
const createClient = () => new ApolloClient({
89
link: new HttpLink({
910
// ssrMode: true,
1011
uri: CONSTANTS.BUNDESTAGIO_SERVER_URL,
1112
fetch,
1213
}),
1314
cache: new InMemoryCache(),
1415
});
15-
export default client;
16+
export default createClient;

src/graphql/schemas/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { fileLoader, mergeTypes } from 'merge-graphql-schemas';
22
import path from 'path';
3+
import _ from 'lodash';
34

4-
const typesArray = fileLoader(path.join(__dirname, './'));
5+
const typesArray = _.uniq(fileLoader(path.join(__dirname, './')));
56

67
export default mergeTypes(typesArray);

src/scripts/import.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import ProgressBar from 'cli-progress'; // eslint-disable-line
22
import program from 'commander'; // eslint-disable-line
33

4-
import client from '../graphql/client';
4+
import createClient from '../graphql/client';
55
import Procedure from '../models/Procedure';
66
import getProcedures from '../graphql/queries/getProcedures';
77

88
export default async (procedureIds) => {
9+
const client = createClient();
910
// Start Importing
1011
const { data: { procedures } } = await client.query({
1112
query: getProcedures,
1213
variables: { IDs: procedureIds },
14+
fetchPolicy: 'network-only',
1315
});
1416
// Start Inserting
1517
const promises = await procedures.map(async (bIoProcedure) => {

src/scripts/webhook.js

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
import importProcedures from './import';
22
import getProcedureUpdates from '../graphql/queries/getProcedureUpdates';
3-
import client from '../graphql/client';
3+
import createClient from '../graphql/client';
44
import ProcedureModel from '../models/Procedure';
55

66
export default async (data) => {
7+
const client = createClient();
8+
79
// Count local Data in groups
8-
const groups = await ProcedureModel.aggregate([{
9-
// Group by Period & Type
10-
$group: {
11-
_id: { period: '$period', type: '$type' },
12-
count: { $sum: 1 },
10+
const groups = await ProcedureModel.aggregate([
11+
{
12+
// Group by Period & Type
13+
$group: {
14+
_id: { period: '$period', type: '$type' },
15+
count: { $sum: 1 },
16+
},
17+
},
18+
{
19+
// Group by Period
20+
$group: {
21+
_id: '$_id.period',
22+
types: { $push: { type: '$_id.type', count: '$count' } },
23+
},
1324
},
14-
},
15-
{
16-
// Group by Period
17-
$group: {
18-
_id: '$_id.period',
19-
types: { $push: { type: '$_id.type', count: '$count' } },
25+
{
26+
// Rename _id Field to period
27+
$project: { _id: 0, period: '$_id', types: 1 },
2028
},
21-
},
22-
{
23-
// Rename _id Field to period
24-
$project: { _id: 0, period: '$_id', types: 1 },
25-
}]);
29+
]);
2630

2731
const update = [];
2832
await Promise.all(data.map(async (d) => {
2933
const period = parseInt(d.period, 10);
3034
const { type, countBefore, changedIds } = d.types.find(t => t.type === 'Gesetzgebung');
3135
const group = groups.find(c => c.period === period);
32-
const localCount = group ? group.types
33-
.find(ct => ct.type === type).count : 0;
36+
const localCount = group ? group.types.find(ct => ct.type === type).count : 0;
3437
// Append Changed IDs
3538
update.concat(changedIds);
3639
// Compare Counts Remote & Local
@@ -40,10 +43,12 @@ export default async (data) => {
4043
query: getProcedureUpdates,
4144
variables: { period, type },
4245
});
43-
// Find local Procedure Updates
44-
const localProcedureUpdates = await ProcedureModel
45-
.find({ period, type }, { procedureId: 1, lastUpdateDate: 1 });
46-
// Compare
46+
// Find local Procedure Updates
47+
const localProcedureUpdates = await ProcedureModel.find(
48+
{ period, type },
49+
{ procedureId: 1, lastUpdateDate: 1 },
50+
);
51+
// Compare
4752
procedureUpdates.forEach((pu) => {
4853
const localData = localProcedureUpdates.find(ld => ld.procedureId === pu.procedureId);
4954
if (!localData || new Date(localData.lastUpdateDate) < new Date(pu.updatedAt)) {

0 commit comments

Comments
 (0)