Skip to content

Commit b173a01

Browse files
Update background.js
1 parent 64bca45 commit b173a01

File tree

1 file changed

+224
-35
lines changed

1 file changed

+224
-35
lines changed

background.js

Lines changed: 224 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,13 @@ loading_buffer = `
8787

8888
function cacheStore(metric_string) {
8989
// caching the commit_id for 1000 days, but that can be modified accordingly
90+
console.log('Cache Store',metric_string)
91+
localStorage.setItem(latest_commit, metric_string)
9092
write_metric_values(metric_string)
91-
var today = new Date();
92-
today.setTime(today.getTime() + (1000 * 24 * 60 * 60 * 1000));
93-
var cookieExpiration = "cookieExpiration=" + today.toUTCString();
94-
document.cookie = latest_commit + "=" + metric_string + ";" + cookieExpiration + ";path=/";
93+
// var today = new Date();
94+
// today.setTime(today.getTime() + (1000 * 24 * 60 * 60 * 1000));
95+
// var cookieExpiration = "cookieExpiration=" + today.toUTCString();
96+
// document.cookie = latest_commit + "=" + metric_string + ";" + cookieExpiration + ";path=/";
9597
}
9698
var badges_final = `
9799
<link rel="preconnect" href="https://fonts.gstatic.com">
@@ -650,6 +652,185 @@ var badges_final = `
650652
651653
`;
652654
function add_insights() {
655+
var metricJSON = {
656+
"commit_id": "",
657+
"url": "",
658+
"N_Pack": "",
659+
"D_Pack": "",
660+
"N_Class": "",
661+
"I_Class": "",
662+
"N_Methods": "",
663+
"O_Methods": "",
664+
"Bugs": "",
665+
"Code_Impact": "",
666+
"Code_Churn": "",
667+
"Percentage_Active_Contributors": ""
668+
}
669+
metricJSON["commit_id"] = latest_commit
670+
metricJSON["url"] = commit_url
671+
metricJSON["N_Pack"] = document.getElementById('f1').innerHTML
672+
metricJSON["D_Pack"] = document.getElementById('f2').innerHTML
673+
metricJSON["N_Methods"] = document.getElementById("f3").innerHTML
674+
metricJSON["O_Methods"] = document.getElementById("f4").innerHTML
675+
metricJSON["N_Class"] = document.getElementById("f5").innerHTML
676+
metricJSON["I_Class"] = document.getElementById("f6").innerHTML
677+
metricJSON["Percentage_Active_Contributors"] = document.getElementById('active').innerHTML
678+
metricJSON["Code_Churn"] = document.getElementById('impacted_loc').innerHTML
679+
metricJSON["Code_Impact"] = document.getElementById('impacted_files').innerHTML
680+
metricJSON["Bugs"] = document.getElementById('bugs').innerHTML
681+
682+
var xhttp = new XMLHttpRequest();
683+
xhttp.onreadystatechange = function () {
684+
if (this.readyState == 4 && this.status == 200) {
685+
console.log("Results have been successfully stored in the global database")
686+
}
687+
}
688+
xhttp.open("POST", "https://gitq.herokuapp.com/store", "true");
689+
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
690+
xhttp.send('id=' + metricJSON["url"] +
691+
"&url=" + metricJSON["url"] +
692+
"&npack=" + metricJSON["N_Pack"] +
693+
"&dpack=" + metricJSON["D_Pack"] +
694+
"&nclass=" + metricJSON["N_Class"] +
695+
"&iclass=" + metricJSON["I_Class"] +
696+
"&nmethods=" + metricJSON["N_Methods"] +
697+
"&omethods=" + metricJSON["O_Methods"] +
698+
"&paar=" + metricJSON["Percentage_Active_Contributors"] +
699+
"&cimpact=" + metricJSON["Code_Impact"] +
700+
"&churn=" + metricJSON["Code_Churn"] +
701+
"&bugs=" + metricJSON["Bugs"])
702+
cacheStore(JSON.stringify(metricJSON))
703+
var bug_insight = "";
704+
var bugs = document.getElementById('bugs').innerHTML.split(" : ");
705+
var open_bugs = parseInt(bugs[0]);
706+
var closed_bugs = parseInt(bugs[1]);
707+
if (closed_bugs >= (2 * open_bugs)) {
708+
bug_insight = `
709+
<div class="icon icon__safe" style="margin-left: 43rem;" >
710+
<div style="font-family: 'Varela Round', sans-serif;" class="insight insight__safe">
711+
Excellent bug handling. Project is safe.
712+
</div>
713+
<img style="width: 2rem;height: 2rem;" src="https://img.icons8.com/nolan/64/bug.png"/>
714+
</div>`;
715+
}
716+
else if (closed_bugs < open_bugs) {
717+
bug_insight = `
718+
<div class="icon icon__ok" style="margin-left: 43rem;" >
719+
<div style="font-family: 'Varela Round', sans-serif;" class="insight insight__ok">
720+
Good bug handling. Good to go about using the project.
721+
</div>
722+
<img style="width: 2rem;height: 2rem;" src="https://img.icons8.com/nolan/64/bug.png"/>
723+
</div>`;
724+
}
725+
else {
726+
bug_insight = `
727+
<div class="icon icon__danger" style="margin-left: 43rem;">
728+
<div style="font-family: 'Varela Round', sans-serif;" class="insight insight__danger">
729+
Poor bug handling - Could you take a step to resolve one of the bugs?
730+
</div>
731+
<img style="width: 2rem;height: 2rem;" src="https://img.icons8.com/nolan/64/bug.png"/>
732+
</div>`;
733+
}
734+
var file_insight = ""
735+
var ifiles = parseInt(document.getElementById('impacted_files').innerHTML.split("%")[0]);
736+
console.log(ifiles)
737+
if (ifiles <= 10) {
738+
file_insight = `
739+
<div class="icon icon__safe" style="margin-left: 10rem;" >
740+
<div style="font-family: 'Varela Round', sans-serif;" class="insight insight__safe">
741+
Greatly modularized. New version will minimally affect the project.
742+
</div>
743+
<img style="width: 2rem;height: 2rem;" src="https://img.icons8.com/color/48/000000/code-file.png"/>
744+
</div>`;
745+
}
746+
else if (ifiles >= 11) {
747+
if (ifiles > 40) {
748+
file_insight = `
749+
<div class="icon icon__danger" style="margin-left: 10rem;">
750+
<div style="font-family: 'Varela Round', sans-serif;" class="insight insight__danger">
751+
Heavy proportion of the project gets affected for every commit. Older versions may not be compatible.
752+
</div>
753+
<img style="width: 2rem;height: 2rem;" src="https://img.icons8.com/color/48/000000/code-file.png"/>
754+
</div>`;
755+
}
756+
else {
757+
file_insight = `
758+
<div class="icon icon__ok" style="margin-left: 10rem;">
759+
<div style="font-family: 'Varela Round', sans-serif;" class="insight insight__ok">
760+
Good modularization. Less than 40% of the project is affected for every commit.
761+
</div>
762+
<img style="width: 2rem;height: 2rem;" src="https://img.icons8.com/color/48/000000/code-file.png"/>
763+
</div>`;
764+
}
765+
}
766+
var loc_insight = "";
767+
var iloc = parseInt(document.getElementById('impacted_loc').innerHTML.split("%")[0]);
768+
if (iloc < 16) {
769+
loc_insight = `
770+
<div class="icon icon__safe" style="margin-left: 10rem;">
771+
<div style="font-family: 'Varela Round', sans-serif;" class="insight insight__safe">
772+
Well coded. New version will minimally affect the code inside the files of the project.
773+
</div>
774+
<img style="width: 2rem;height: 2rem;" src="https://img.icons8.com/pastel-glyph/64/000000/code--v1.png"/>
775+
</div>`;
776+
}
777+
else if (iloc >= 16) {
778+
if (iloc < 50) {
779+
loc_insight = `
780+
<div class="icon icon__ok" style="margin-left: 10rem;">
781+
<div style="font-family: 'Varela Round', sans-serif;" class="insight insight__ok">
782+
Good code composition. Less than 50% of code in a file is affected for every commit.
783+
</div>
784+
<img style="width: 2rem;height: 2rem;" src="https://img.icons8.com/pastel-glyph/64/000000/code--v1.png"/>
785+
</div>`;
786+
}
787+
else {
788+
loc_insight = `
789+
<div class="icon icon__danger" style="margin-left: 10rem;">
790+
<div style="font-family: 'Varela Round', sans-serif;" class="insight insight__danger">
791+
Poor code composition. Atleast 50% of the code is affected for every commit.
792+
</div>
793+
<img style="width: 2rem;height: 2rem;" src="https://img.icons8.com/pastel-glyph/64/000000/code--v1.png"/>
794+
</div>`;
795+
}
796+
797+
}
798+
var contribution_insight = "";
799+
var active_contr = parseInt(document.getElementById('active').innerHTML.split("%")[0]);
800+
if (active_contr <= 10) {
801+
contribution_insight = `
802+
<div class="icon icon__danger" style="margin-left: 10rem;">
803+
<div style="font-family: 'Varela Round', sans-serif;" class="insight insight__danger">
804+
Fairly inactive community. Less than 10% of the authors remain active at any time.
805+
</div>
806+
<img style="width: 2rem;height: 2rem;" src="https://img.icons8.com/color/48/000000/myspace.png"/>
807+
</div>`;
808+
}
809+
else if (active_contr >= 11) {
810+
if (active_contr > 50) {
811+
contribution_insight = `
812+
<div class="icon icon__ok" style="margin-left: 10rem;">
813+
<div style="font-family: 'Varela Round', sans-serif;" class="insight insight__ok">
814+
Highly active community. Atleast 50% of the authors remain active at any time.
815+
</div>
816+
<img style="width: 2rem;height: 2rem;" src="https://img.icons8.com/color/48/000000/myspace.png"/>
817+
</div>`;
818+
}
819+
else {
820+
contribution_insight = `
821+
<div class="icon icon__ok" style="margin-left: 10rem;">
822+
<div style="font-family: 'Varela Round', sans-serif;" class="insight insight__ok">
823+
Fairly active community. Atleast 10% of the authors remain active at any time.
824+
</div>
825+
<img style="width: 2rem;height: 2rem;" src="https://img.icons8.com/color/48/000000/myspace.png"/>
826+
</div>`;
827+
}
828+
829+
}
830+
document.getElementById('insight_box').innerHTML = `<div class="icon-box">` + bug_insight + file_insight + loc_insight + contribution_insight + `</div>`;
831+
}
832+
833+
function add_insights_repeat() {
653834
var bug_insight = "";
654835
var bugs = document.getElementById('bugs').innerHTML.split(" : ");
655836
var open_bugs = parseInt(bugs[0]);
@@ -823,7 +1004,7 @@ function write_metric_values(metric_string) {
8231004
document.getElementById('f5').style.background = '#44CC11'
8241005
document.getElementById('f6').innerHTML = response["I_Class"]
8251006
document.getElementById('f6').style.background = '#44CC11'
826-
add_insights()
1007+
add_insights_repeat()
8271008
}
8281009
function analyze_boc() {
8291010
var user_repo = window.location.href.replace("https://github.com/", "");
@@ -832,6 +1013,8 @@ function analyze_boc() {
8321013
document.body.style.background = "#ffffff";
8331014
setTimeout(() => {
8341015
var xhttp = new XMLHttpRequest();
1016+
xhttp.open("POST", req_url, "true");
1017+
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
8351018
xhttp.onreadystatechange = function () {
8361019
if (this.readyState == 4 && this.status == 200) {
8371020
console.log("Bug Issues Open & Closed Metric Analysis Completed successfully")
@@ -846,8 +1029,6 @@ function analyze_boc() {
8461029
delete_repo()
8471030
}, 1500);
8481031
}
849-
xhttp.open("POST", req_url, "true");
850-
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
8511032
xhttp.send('boc_request');
8521033
}, 300);
8531034

@@ -860,6 +1041,8 @@ function analyze_aar() {
8601041
document.body.style.background = "#ffffff";
8611042
setTimeout(() => {
8621043
var xhttp = new XMLHttpRequest();
1044+
xhttp.open("POST", req_url, "true");
1045+
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
8631046
xhttp.onreadystatechange = function () {
8641047
if (this.readyState == 4 && this.status == 200) {
8651048
console.log("Active Authors Rate Metrics Analysis Complete successfully")
@@ -878,8 +1061,6 @@ function analyze_aar() {
8781061
console.log("Problems in calculating Active Authors Rate metric")
8791062
}
8801063
}
881-
xhttp.open("POST", req_url, "true");
882-
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
8831064
xhttp.send('aar_request');
8841065
}, 300);
8851066

@@ -893,6 +1074,8 @@ function analyze_po() {
8931074
document.body.style.background = "#ffffff";
8941075
setTimeout(() => {
8951076
var xhttp = new XMLHttpRequest();
1077+
xhttp.open("POST", req_url, "true");
1078+
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
8961079
xhttp.onreadystatechange = function () {
8971080
if (this.readyState == 4 && this.status == 200) {
8981081
console.log("Portion Impacted Metrics Analysis Complete successfully")
@@ -916,12 +1099,10 @@ function analyze_po() {
9161099
Code_churn = Code_churn.toString()
9171100
document.getElementById('impacted_loc').innerHTML = Code_churn + "%"
9181101
document.getElementById('impacted_loc').style.background = '#feb60a'
919-
} else {
1102+
} else {
9201103
console.log("Problems in calculating portion impacted metrics")
921-
}
1104+
}
9221105
}
923-
xhttp.open("POST", req_url, "true");
924-
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
9251106
xhttp.send('po_request');
9261107
}, 300);
9271108

@@ -934,6 +1115,8 @@ function analyze_scm() {
9341115
document.body.style.background = "#ffffff";
9351116
setTimeout(() => {
9361117
var xhttp = new XMLHttpRequest();
1118+
xhttp.open("POST", req_url, "true");
1119+
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
9371120
xhttp.onreadystatechange = function () {
9381121
if (this.readyState == 4 && this.status == 200) {
9391122
console.log("Source Code Metric Analysis Complete successfully")
@@ -955,8 +1138,6 @@ function analyze_scm() {
9551138
console.log("Problems in calculating source code metrics")
9561139
}
9571140
}
958-
xhttp.open("POST", req_url, "true");
959-
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
9601141
xhttp.send('scm_request');
9611142
}, 300);
9621143

@@ -969,15 +1150,15 @@ function delete_repo() {
9691150
document.body.style.background = "#ffffff";
9701151
setTimeout(() => {
9711152
var xhttp = new XMLHttpRequest();
1153+
xhttp.open("POST", req_url, "true");
1154+
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
9721155
xhttp.onreadystatechange = function () {
9731156
if (this.readyState == 4 && this.status == 200) {
9741157
console.log("Repo Has been deleted Successfully")
9751158
} else {
9761159
console.log("Problems in deleting the repository in the remote server")
9771160
}
9781161
}
979-
xhttp.open("POST", req_url, "true");
980-
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
9811162
xhttp.send('delete_request');
9821163
}, 300);
9831164

@@ -992,6 +1173,8 @@ function download_repository() {
9921173
document.body.style.background = "#ffffff";
9931174
setTimeout(() => {
9941175
var xhttp = new XMLHttpRequest();
1176+
xhttp.open("POST", req_url, "true");
1177+
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
9951178
xhttp.onreadystatechange = function () {
9961179
if (this.readyState == 4 && this.status == 200) {
9971180
if (this.responseText == "true") {
@@ -1007,15 +1190,13 @@ function download_repository() {
10071190
setTimeout(() => {
10081191
analyze_boc()
10091192
}, 2500);
1010-
1193+
10111194
} else {
10121195
console.log("Problems in downloading the repo!")
10131196
}
10141197

10151198
}
10161199
}
1017-
xhttp.open("POST", req_url, "true");
1018-
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
10191200
xhttp.send('download_request');
10201201
}, 3000);
10211202

@@ -1024,12 +1205,16 @@ function download_repository() {
10241205
function fetch_from_database() {
10251206
document.getElementsByClassName("Box mb-3")[0].innerHTML = loading_buffer + entire_code;
10261207
var xhttp = new XMLHttpRequest();
1208+
xhttp.open("POST", "https://gitq.herokuapp.com/read_database", "true");
1209+
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
10271210
xhttp.onreadystatechange = function () {
10281211
if (this.readyState == 4 && this.status == 200) {
10291212
if (this.responseText != "False") {
10301213
document.getElementsByClassName("Box mb-3")[0].innerHTML = badges_final + entire_code;
10311214
setTimeout(() => {
1032-
// cacheStore(this.responseText)
1215+
cacheStore(this.responseText)
1216+
// generate badges on to screen
1217+
// add_insights()
10331218
}, 800);
10341219
}
10351220
else {
@@ -1042,8 +1227,6 @@ function fetch_from_database() {
10421227
document.getElementsByClassName("Box mb-3")[0].innerHTML = '<iframe style="margin-bottom:2rem;width:100%;height:18rem;border:none;" src="https://kowndinya2000.github.io/buffer-2.github.io/restart-dynos.html"></iframe>' + entire_code;
10431228
}
10441229
}
1045-
xhttp.open("POST", "https://gitq.herokuapp.com/read_database", "true");
1046-
xhttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
10471230
xhttp.send('commit_id=' + latest_commit);
10481231
}
10491232
function fetch_from_cache() {
@@ -1062,19 +1245,25 @@ function fetch_from_cache() {
10621245
}
10631246

10641247
function getCache(commit_id) {
1065-
var commitKey = commit_id + "=";
1066-
var decodedCommitCookie = decodeURIComponent(document.cookie);
1067-
var commitSplit = decodedCommitCookie.split(';');
1068-
for (var iter = 0; iter < commitSplit.length; iter++) {
1069-
var arr = commitSplit[iter];
1070-
while (arr.charAt(0) == ' ') {
1071-
arr = arr.substring(1);
1072-
}
1073-
if (arr.indexOf(commitKey) == 0) {
1074-
return arr.substring(commitKey.length, arr.length);
1075-
}
1248+
// var commitKey = commit_id + "=";
1249+
if(localStorage.getItem(commit_id)){
1250+
return localStorage.getItem(commit_id)
1251+
} else{
1252+
return ""
10761253
}
1077-
return "";
1254+
// console.log("cache retrieval",document.cookie)
1255+
// var decodedCommitCookie = decodeURIComponent(document.cookie);
1256+
// var commitSplit = decodedCommitCookie.split(';');
1257+
// for (var iter = 0; iter < commitSplit.length; iter++) {
1258+
// var arr = commitSplit[iter];
1259+
// while (arr.charAt(0) == ' ') {
1260+
// arr = arr.substring(1);
1261+
// }
1262+
// if (arr.indexOf(commitKey) == 0) {
1263+
// return arr.substring(commitKey.length, arr.length);
1264+
// }
1265+
// }
1266+
// return "";
10781267
}
10791268

10801269
function browseCache(commit_id) {

0 commit comments

Comments
 (0)