Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
Latest commit
113 lines (96 loc) · 2.68 KB
/
Copy pathdatabase.js
File metadata and controls
113 lines (96 loc) · 2.68 KB
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
constMongoClient=require('mongodb').MongoClient;
constassert=require('assert');
consturl='mongodb://sdpygl:sdp_ygl@3.36.175.233:27017/admin';
constdbName='visualization';
letProjectData=null,
db;
constclient=newMongoClient(url,{
useNewUrlParser: true,
useUnifiedTopology: true,
});
client.connect(async(err,client)=>{
assert.strictEqual(null,err);
console.log('Connected successfully to server');
db=client.db(dbName);
});
constgetProjectData=async(req,res)=>{
//not cached yet
if(ProjectData===null){
try{
ProjectData=awaitdb.collection('projects').find({}).toArray();
}catch(error){
console.log('fetching data from MongoDB Failed');
console.err(error);
//throw error;
}
}
returnProjectData;
};
/*
ppi_status : "Delayed" // status
sector : "Energy" // primary sector
subsector : "Electricity" // sub sector
income_group: "Low income" // income group
*/
constgetSearchCriteria=async(req,res,next)=>{
letdata=ProjectData;
if(data==null){
awaitgetProjectData();
data=ProjectData;
}
letsector={};
letppi_status=['Delayed','Canceled'];
letincome_group=newSet();
for(constdataofProjectData){
// ppi_status.add(data.properties.ppi_status);
if(data.properties.sectorinsector){
sector[data.properties.sector].add(data.properties.subsector);
}else{
sector[data.properties.sector]=newSet([data.properties.subsector]);
}
if(data.properties.income_group==null){
continue;
}
income_group.add(data.properties.income_group);
}
for(const[key,value]ofObject.entries(sector)){
sector[key]=Array.from(value);
}
constcriteria={
ppi_status,
sector,
income_group: Array.from(income_group),
};
res.locals.criteria=criteria;
next();
};
constsearchData=async(req)=>{
letdata=ProjectData;
if(data==null){
awaitgetProjectData();
data=ProjectData;
}
for(const[key,value]ofObject.entries(req.query)){
data=data.filter((datum)=>{
if(key==='name'){
returndatum.properties.project_name
.toLowerCase()
.includes(value.toLowerCase());
}elseif(key==='ppi_status'){
if(value==='Delayed'){
returndatum.properties.ppi_status.toLowerCase().includes('delay');
}elseif(value==='Canceled'){
returndatum.properties.ppi_status.toLowerCase().includes('cancel');
}
}else{
returndatum.properties[key]===value;
}
});
}
returndata;
};
module.exports={
getProjectData,
searchData,
getSearchCriteria,
};