Skip to content

Commit

Permalink
Add mapper for new node comments. refs #6
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamarts committed Oct 26, 2020
1 parent ed22b18 commit e354f04
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 25 deletions.
6 changes: 3 additions & 3 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"server": {
"host": "0.0.0.0",
"host": "localhost",
"port": 3005,
"baseDir": "/"
},
Expand All @@ -15,8 +15,8 @@
"renewTokenInMinute": 800,
"endpoints": {
"auth": "/auth/realms/agrows/protocol/openid-connect/token",
"publishMetrics": "/v1/owner/gitfeed/thing/:provider/node/:node",
"metrics": "/v1/owner/gitfeed/thing/:provider/node/:node",
"publishMetrics": "/v1/owner/gitfeedV9/thing/:provider/node/:node",
"metrics": "/v1/owner/gitfeedV9/thing/:provider/node/:node",
"jsonDataAPI": "/v1/files/upload/jsondata"
}
}
Expand Down
12 changes: 9 additions & 3 deletions controller/metrics.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ const metrics = async (req, res) => {
});

if (
starwsResp.data &&
starwsResp.data.data &&
starwsResp.data.data.lenght == 0
!starwsResp ||
(starwsResp.data &&
starwsResp.data.data &&
starwsResp.data.data.lenght == 0)
) {
res.send({ message: 'No data found' });
return false;
}

if (starwsResp.status && starwsResp.status != 200) {
res.status(starwsResp.status).send({ message: 'Unknown error' });
return false;
}

const maker = nodeMapper[node];

if (!maker) {
Expand Down
10 changes: 6 additions & 4 deletions controller/publish.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const starws = require('../services/star-ws/controller');
const nodeMapper = require('../mapper/inbound.mapper');
const utils = require('../utils');

const publish = async (req, res) => {
const sourceData = req.body;
Expand Down Expand Up @@ -39,7 +40,7 @@ const publish = async (req, res) => {
// TODO: Means that is not formatted, lets mapper it
let rawDataValues;
if (createRawData) {
// Save JSON Data
// Save JSON Data
const rawDataPromise = [];
sourceData.forEach((theData, index) => {
rawDataPromise[index] = starws.saveJSONData(theData);
Expand All @@ -59,15 +60,16 @@ const publish = async (req, res) => {
const data = [];
sourceData.forEach((item, index) => {
let d = item;
if (rawDataValues){
if (rawDataValues) {
d = {
...item,
rawData: rawDataValues[index].link
}
rawData: rawDataValues[index].link,
};
}
data.push(theMaker(d));
});

utils.dateCounter = 0;
const starwsResp = await starws.publishMetrics(provider, node, data);
if (starwsResp) {
res.status(starwsResp.status).send(starwsResp.data);
Expand Down
38 changes: 33 additions & 5 deletions mapper/inbound.mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ const issues = JM.makeConverter({
},
fields: {
number: ['number', h.toString],
state: 'state',
state: 'state', //
createdAt: input => Utils.dateFormat4StarWS(input.createdAt),
closedAt: input => Utils.dateFormat4StarWS(input.closedAt),
updatedAt: input => Utils.dateFormat4StarWS(input.updatedAt),
closedAt: input => Utils.dateFormat4StarWS(input.closedAt), //
updatedAt: input => Utils.dateFormat4StarWS(input.updatedAt), //
author: input => Utils.prepareString4StarWS(`a:${input.author}`),
labels: input => Utils.concatArray4StarWS(input.labels),
participantsTotalCount: ['participants.totalCount', h.toString],
labels: input => Utils.concatArray4StarWS(input.labels), //
participantsTotalCount: ['participants.totalCount', h.toString], //
participants: input => {
const participants =
input.participants && input.participants.users
Expand Down Expand Up @@ -137,6 +137,33 @@ const commits = JM.makeConverter({
tags: {},
});

const comments = JM.makeConverter({
dateTime: input => Utils.dateFormat4StarWS(input.createdAt),
fields: {
url: 'url',
createdAt: data => Utils.dateFormat4StarWS(data.createdAt),
author: input => Utils.prepareString4StarWS(`a:${input.author}`),
number: ['number', h.toString],
url: 'url',
id: 'id',
rawData: input => {
if (input.rawData) {
return input.rawData;
}
return `https://datajson/empty`;
},
dono: input => {
return `o:${input.owner}`;
},
name: input => {
return `n:${input.name}`;
},
provider: 'provider',
type: JM.helpers.def('comments'),
},
tags: {},
});

const userStats = JM.makeConverter({
dateTime: input => {
if (input.dateTime) {
Expand Down Expand Up @@ -201,6 +228,7 @@ module.exports = {
pulls,
issues,
commits,
comments,
userStats,
repositoryStats,
};
19 changes: 19 additions & 0 deletions services/star-ws/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ const publishMetrics = async (provider, node, data) => {
endPoint = route.reverse({ provider, node });

try {
const dateTimes = data.map(val => val.dateTime);
const uniq = dateTimes
.map(d => {
return {
count: 1,
d: d,
};
})
.reduce((a, b) => {
a[b.d] = (a[b.d] || 0) + b.count;
return a;
}, {});

const duplicates = Object.keys(uniq).filter(a => uniq[a] > 1);
if (duplicates.length > 0) {
const dup = data.filter(val => duplicates.includes(val.dateTime));
console.log(dup);
}
console.log(duplicates);
const response = await httpClient.post(endPoint, data);
return response;
} catch (e) {
Expand Down
21 changes: 11 additions & 10 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const moment = require('moment');

const utils = (() => {
return {
dateCounter: 0,
dateFormat4StarWS: data => {
if (!data) {
return moment(0).format();
Expand All @@ -17,11 +18,9 @@ const utils = (() => {

dateFormat4Githunter: date => {
const theDate = moment(date);
if (!theDate.isValid())
return date;

if (theDate.year() <= 1970)
return '';
if (!theDate.isValid()) return date;

if (theDate.year() <= 1970) return '';

return date;
},
Expand All @@ -31,10 +30,10 @@ const utils = (() => {
return data.substring(0, shortStringLen);
},

prepareString4Githunter: (data) => {
prepareString4Githunter: data => {
if (!data) return data;
const regex = new RegExp('[a-z]{1}:');
if (regex.test(data)){
if (regex.test(data)) {
return data.substr(2, data.length);
}

Expand All @@ -49,9 +48,11 @@ const utils = (() => {
return str.substring(0, shortStringLen);
},
nanoSeconds: () => {
const hrTime = process.hrtime();
const nTime = `${hrTime[0] * 1000000000}${hrTime[1]}`;
const nSec = nTime.substr(nTime.length - 5);
// const hrTime = process.hrtime();
// const nTime = `${hrTime[0] * 1000000000}${hrTime[1]}`;
// const nSec = nTime.substr(nTime.length - 4);
const nSec = `${utils.dateCounter}`.padStart(3, '0');
utils.dateCounter += 1;
return moment().format(`YYYY-MM-DDTHH:mm:ss.SSS${nSec}Z`);
},
};
Expand Down

0 comments on commit e354f04

Please sign in to comment.