-
Notifications
You must be signed in to change notification settings - Fork 12
/
DataProcess.js
107 lines (101 loc) · 3.23 KB
/
DataProcess.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
/*
这是一个数据处理文件
我把数据库中的数据整理成了189个数据点
最后存储为json文件,供前端调用
*/
const MongoClient = require('mongodb').MongoClient;
const fs = require('fs');
const variables = require('./GlobalVariable');
//对于一个输入的工作对象
//返回其最高薪水和最低薪水
function processSalary(job){
var str = job.salary;
var salary = null;
if(str.indexOf('-') < 0){
salary = {
lowerSalary: parseInt(str),
higherSalary: parseInt(str)
};
}
else{
let arr = str.split('-');
salary = {
lowerSalary: parseFloat(arr[0]),
higherSalary: parseFloat(arr[1])
};
}
return salary;
}
//给定工作岗位和工作地点
//返回对应的薪水平均值
function fetchAvg(jobName, jobLocation) {
return new Promise((resolve, reject) => {
MongoClient.connect('mongodb://localhost:27017', (e, db) => {
if (e)
reject(e);
var jobs = db.db('jobs');
var collection = jobs.collection('jobs');
collection.find({ type: jobName, location: jobLocation }).toArray((e, arr) => {
if (e)
console.error(e);
let res = [];
let lowerSalary = 0;
let higherSalary = 0;
//用于测试空白数据
// if(arr.length === 0){
// console.log(jobName, jobLocation);
// }
arr.forEach((job) => {
let salary = processSalary(job);
res.push(salary);
});
db.close();
res.forEach(obj => {
lowerSalary += obj.lowerSalary;
higherSalary += obj.higherSalary;
});
resolve({
lowerSalary: (lowerSalary * 1.0 / res.length).toFixed(2),
higherSalary: (higherSalary * 1.0 / res.length).toFixed(2),
name: jobName,
city: jobLocation,
count:res.length
});
});
});
});
}
//处理数据结果的函数
//对于一个数组的结果
//返回一个处理好的数据对象
function processData(arr){
var resObj = {};
for(code in variables.cityCode){
for(index in arr){
if(variables.cityCode[code] === arr[index].city){
resObj[`${code}_${arr[index].name}`] = arr[index];
}
}
}
return resObj;
}
//主函数
function main() {
var resArr = [];
for (job in variables.jobs) {
for (city in variables.cities) {
(function (jobName, jobLocation, resArr) {
fetchAvg(jobName, jobLocation).then(obj => {
resArr.push(obj);
if(resArr.length === 189){
// console.log(processData(resArr));
fs.writeFileSync('./files/data.json', JSON.stringify(processData(resArr)));
}
}, e => {
console.error(e);
});
})(job, variables.cities[city], resArr)
}
}
}
main();