Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjyost committed Jan 18, 2020
1 parent faf4fd3 commit afa3a9d
Show file tree
Hide file tree
Showing 30 changed files with 1,412 additions and 175 deletions.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const fileUpload = require("express-fileupload");
const http = require("http");
const socketIo = require("socket.io");
const debug = require("debug")("deps:server");
require("./db");
require("dotenv").config();
require("./db");

const Handlers = require("./handlers");

Expand Down
9 changes: 9 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
severityLevels: {
NA: 0,
GOOD: 1,
FINE: 2,
WARNING: 3,
BAD: 4
}
};
9 changes: 1 addition & 8 deletions db.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
const mongoose = require("mongoose");
const URL = "mongodb://skoosh:skoosh2002@ds223509.mlab.com:23509/deps-client";

// const URL =
// "mongodb://skoosh:skoosh2002@ds127362.mlab.com:27362/newsbie-sandbox";

mongoose.connect(
URL,
{ useNewUrlParser: true }
);
mongoose.connect(process.env.DB_URL, { useNewUrlParser: true });
mongoose.Promise = global.Promise;
3 changes: 2 additions & 1 deletion handlers/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ module.exports = {
authorize: require("./authorize"),
addInstallation: require("./addInstallation"),
getInstallationRepos: require("./getInstallationRepos"),
updateInstallationRepos: require("./updateInstallationRepos")
updateInstallationRepos: require("./updateInstallationRepos"),
readPackageJSON: require("./readPackageJSON")
};
15 changes: 15 additions & 0 deletions handlers/routes/readPackageJSON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { GitHub } = require("../../services");

/**
* POST /read_package_json
* Read a json file and return object
*/
module.exports = async function(req, res) {
if (req.files) {
res.json(JSON.parse(req.files.file.data));
} else {
let url = req.repoURL;
const packageJSON = await GitHub.getPackageJSONFromRepoUrl(url);
res.json(packageJSON);
}
};
9 changes: 9 additions & 0 deletions handlers/socket/analyzePackageJSON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { fork } = require("child_process");

module.exports = function(socket, packageJson) {
const analyze = fork("./services/analyze");
analyze.send(packageJson);
analyze.on("message", msg => {
socket.emit(`basic/${msg.type}`, msg.data);
});
};
15 changes: 15 additions & 0 deletions handlers/socket/analyzeRepoUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { fork } = require("child_process");
const { GitHub } = require("../../services");

async function analyzeRepoUrl(socket, url) {
const packageJSON = await GitHub.getPackageJSONFromRepoUrl(url);
socket.emit("basic/packageJSON", packageJSON);

const analyze = fork("./services/analyze");
analyze.send(packageJSON);
analyze.on("message", msg => {
socket.emit(`basic/${msg.type}`, msg.data);
});
}

module.exports = analyzeRepoUrl;
11 changes: 0 additions & 11 deletions handlers/socket/analyzeSinglePackageJson.js

This file was deleted.

13 changes: 10 additions & 3 deletions handlers/socket/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
const analyzeSinglePackageJson = require("./analyzeSinglePackageJson");
const analyzePackageJSON = require("./analyzePackageJSON");
const analyzeInstallation = require("./analyzeInstallation");
const analyzeRepoUrl = require("./analyzeRepoUrl");

module.exports = function(socket) {
this.socket = socket;
// let the client know the socket id
socket.emit("socketId", socket.id);

// socket.on("analyzeSingle", analyzeSinglePackageJson);
socket.on("analyzeRepoUrl", url =>
analyzeRepoUrl(socket, url)
);

socket.on("analyzePackageJSON", packageJSON =>
analyzePackageJSON(socket, packageJSON)
);

socket.on("analyze", installationId =>
socket.on("analyzeInstallation", installationId =>
analyzeInstallation(socket, installationId)
);

Expand Down
99 changes: 58 additions & 41 deletions lib/analyze/calculateLevels.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,108 @@
const moment = require("moment");
const _ = require("lodash");
const { severityLevels } = require("../../constants");

module.exports = data => {
// 0 = good
// 1 = fine
// 2 = warning
// 3 = bad
// -1 = N/A
const { NA, GOOD, FINE, WARNING, BAD } = severityLevels;

function calculateLevels(data) {
const levels = {
versionsBehind: -1,
lastPublish: -1,
weeklyDownloads: -1,
stars: -1,
license: -1
versionsBehind: NA,
lastPublish: NA,
weeklyDownloads: NA,
stars: NA,
license: NA
};

levels.versionsBehind = levelMethods.versionsBehind(data.versionsBehind);
levels.lastPublish = levelMethods.lastPublish(data.time);
levels.lastPublish = levelMethods.lastPublish(data.time && data.time.latest);
levels.weeklyDownloads = levelMethods.weeklyDownloads(data.weeklyDownloads);
levels.stars = levelMethods.stars(data.stars);
levels.license = levelMethods.license(data.license);

return levels;
};
}

const levelMethods = {
versionsBehind: data => {
if (!data) return -1;
if (!data || _.isEmpty(data)) return NA;

if (data.major > 0) {
return 3;
return BAD;
} else if (data.minor > 0) {
return 2;
return WARNING;
} else if (data.patch > 0) {
return 1;
return FINE;
} else if (data.patch === 0) {
return GOOD;
}
return 0;

return NA;
},
lastPublish: data => {
if (!data.latest) return null;
const date = moment(data.latest);

lastPublish: lastPublishISOString => {
if (!lastPublishISOString) return null;
const date = moment(lastPublishISOString);

if (Math.abs(date.diff(moment(), "years")) > 0) {
return 3;
return BAD;
} else if (Math.abs(date.diff(moment(), "months")) > 6) {
return 2;
return WARNING;
} else if (Math.abs(date.diff(moment(), "months")) > 2) {
return 1;
return FINE;
} else if (Math.abs(date.diff(moment(), "second")) > 1) {
return GOOD;
}
return 0;

return NA;
},

weeklyDownloads: downloads => {
if (!downloads || isNaN(downloads)) return NA;

if (downloads < 1000) {
return 3;
return BAD;
} else if (downloads < 10000) {
return 2;
return WARNING;
} else if (downloads < 100000) {
return 1;
return FINE;
}
return 0;
return GOOD;
},

stars: stars => {
if (!stars || isNaN(stars)) return NA;

if (stars < 50) {
return 3;
return BAD;
} else if (stars < 300) {
return 2;
return WARNING;
} else if (stars < 1000) {
return 1;
return FINE;
}

return 0;
return GOOD;
},

license: license => {
if (!license) return -1;
if (!license || _.isEmpty(license)) return NA;

if (typeof license === "object" && "conditions" in license) {
if (typeof license === "object" && "key" in license) {
if (license.key === "mit") {
return 0;
} else if (license.name.includes("GNU")) {
return 3;
return GOOD;
} else if (license.name && license.name.includes("GNU")) {
return BAD;
} else {
return 1;
return FINE;
}
} else {
if (license === "MIT") {
return 0;
return GOOD;
}
}

return 4;
return BAD;
}
};

exports.calculateLevels = calculateLevels;
exports.levelMethods = levelMethods;
Loading

0 comments on commit afa3a9d

Please sign in to comment.