-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
168 lines (147 loc) · 4.86 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const csvtojson = require('csvtojson')
const fs = require('fs')
const csvfilepath = 'input.csv'
const csv = require('csv-parser')
const { convertArrayToCSV } = require('convert-array-to-csv');
const converter = require('convert-array-to-csv');
csvtojson ()
.fromFile(csvfilepath)
.then((json) => {
var len = json.length;
var alluser = []; // to store all user name
var list = new Array(len); //Create one dimensional array
for (var i=0; i<len; i++)
{
//console.log(json[i].length);
//Loop to create 2D array using 1D array
list[i] = new Array(5);
list[i][0] = json[i].date;
list[i][1] = json[i].shift;
list[i][2] = json[i].volunteerId;
list[i][3] = json[i].volunteerName;
list[i][4] = json[i].shiftReason;
/*
insert user in "alluser", if this user already exist there
then no need to insert this user */
var present = 0;
for(var j=0; j< alluser.length ; j++)
{
if(list[i][3] == alluser[j])present=1;
}
if(!present)alluser.push(list[i][3]);
}
//now I have every element in array.Playing with data would be easer than before
//let "graph_rep" (graph representation) is a list of list(2D array) to store conflicted person
var graph_rep= [];
//"uwsd" (user with shift and date) is list of a all user with their differnt shift and date
var uwsd = [];
for(var i=0; i<alluser.length; i++)
{
graph_rep[alluser[i]]=[];
uwsd[alluser[i]]=[];
}
for(let i=0; i<alluser.length; i++)
{
for(let j=0; j<len; j++)
{
if(list[j][3] == alluser[i])
{
let temp= "";
temp=temp.concat(list[j][0]);
temp=temp.concat('+');
temp=temp.concat(list[j][1]);
uwsd[alluser[i]].push(temp);
}
}
}
for(let i=0; i<alluser.length; i++)
{
for(let j=0; j<uwsd[alluser[i]].length; j++)
{
let cur_date_shift = uwsd[alluser[i]][j];
for(let k=0 ; k<len; k++)
{
let temp= "";
temp=temp.concat(list[k][0]);
temp=temp.concat('+');
temp=temp.concat(list[k][1]);
if(temp == cur_date_shift && list[k][3] != alluser[i])
{
var pre = 0;
for(let l=0; l<graph_rep[alluser[i]].length; l++)
{
if(graph_rep[alluser[i]][l] == list[k][3])
{
pre = 1;
}
}
graph_rep[alluser[i]].push(list[k][3]);
if(pre == 0)
{
graph_rep[alluser[i]][list[k][3]]=[];
}
graph_rep[alluser[i]][list[k][3]].push(temp);
}
}
}
}
/*
"vis" is the array I'm using to mark down which node I already visited.
I initialize every node as zero as I did not visited a single node yet
*/
var vis=[];
for(var i = 0; i<alluser.length; i++)vis[alluser[i]]=0;
var weighted_array_for_csv =[];
for(let i=0; i<alluser.length; i++)
{
for(let j=0; j<graph_rep[alluser[i]].length; j++)
{
let cur_member = graph_rep[alluser[i]][j];
if(vis[cur_member] == 0)
{
let chk = 0;
for( let k=0; k<j; k++)
{
if(cur_member == graph_rep[alluser[i]][k])
{
chk = 1;
break;
}
}
if(chk == 0)
{
let cnt = 0;
for(let k =0; k<graph_rep[alluser[i]].length; k++)
{
if(cur_member == graph_rep[alluser[i]][k])cnt++;
}
let temp=[alluser[i],cur_member,cnt];
weighted_array_for_csv.push(temp);
}
}
}
vis[alluser[i]]=1;
}
//coding phase finished,section is written bellow is for writting data onto the file
// write .csv file for table 2 and 3
const header = ['node1', 'Node2','Weight'];
const table3 = convertArrayToCSV(weighted_array_for_csv, {
header,
separator: ','
});
fs.writeFile("output.csv",table3, (err) => {
if (err)
console.log(err);
else {
console.log("File 2 written successfully\n");
}
});
//convert our .csv file into .json incase we need it future
/*
var encodedUri = encodeURI(csv);
fs.writeFileSync("out.json",JSON.stringify(json),"utf-8",(err) =>
{
if(err)console.log(err)
})
*/
})