-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathapp.js
150 lines (123 loc) · 4.22 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
/**
* 初始化性能优化任务模块
* @see http://baike.corp.taobao.com/index.php/CS_RD/Goodbench/Api
*/
var request = require('request');
var when = require('when');
var fs = require('fs');
var api = {
getValidProvinceList: "http://open.alibench.com/rest/v1/CronTask/GetValidProvinceList", //获取可用的省份列表
getValidISPList: "http://open.alibench.com/rest/v1/CronTask/GetValidISPList", //获取可用的运营商列表
//getValidTaskInterval: "http://open.alibench.com/rest/v1/CronTask/GetValidTaskInterval", //获取可用的任务间隔
newPageTask: "http://open.alibench.com/rest/v1/CronTask/NewPageTask",//新建页面探测任务
updatePageTask: "http://open.alibench.com/rest/v1/CronTask/UpdatePageTask", //更新页面任务
getTaskByID: "http://open.alibench.com/rest/v1/CronTask/GetTaskByID", //获取任务信息
getTaskList: "http://open.alibench.com/rest/v1/CronTask/GetTaskList", //获取任务列表
pagePerformanceCurve: "http://open.alibench.com/rest/v1/ChartData/PagePerformanceCurve", //Page性能曲线
pageChinaMap: "http://open.alibench.com/rest/v1/ChartData/PageChinaMap" //Page中国地图
};
var userLists = [];
function PerTasks(user, pwd) {
this.username = user;
this.password = pwd;
}
PerTasks.prototype.ready = function (fnc) {
var self = this;
if (!self.pros || !self.isp) {
this.getValidProvinceList().then(function (pros) {
self.pros = pros;
}).then(self.getValidISPList().then(function (isp) {
self.isp = isp;
})).then(function () {
fnc.call();
});
} else {
fnc.call();
}
}
PerTasks.prototype._post = function (url, param, fnc) {
var self = this;
param.username = self.username;
param.password = self.password;
request.post(
url,
{ form: param },
function (error, response, body) {
if (!error && response.statusCode == 200) {
fnc.call(this, JSON.parse(body));
}
}
);
};
//一劳永逸的函数
PerTasks.prototype.getValidProvinceList = function () {
var deferred = when.defer();
var self = this;
this._post(api.getValidProvinceList, {}, function (json) {
var temp = 0, pros = [];
if (json.code == 0) {
for (temp; temp < json.data.province.length; temp++) {
pros.push(json.data.province[temp].id);
}
deferred.resolve(pros);
} else {
console.log("province error!")
}
});
return deferred.promise;
};
//一劳永逸的函数
PerTasks.prototype.getValidISPList = function () {
var deferred = when.defer();
this._post(api.getValidISPList, {}, function (json) {
var temp = 0, isp = [];
if (json.code == 0) {
for (temp; temp < json.data.length; temp++) {
isp.push(json.data[temp].id);
}
deferred.resolve(isp);
} else {
console.log("province error!")
}
});
return deferred.promise;
};
//新建页面探测任务
PerTasks.prototype.newPageTask = function () {
var pam;
var self = this;
var taskIds = [];
for (var index = 0; index < userLists.length; index++) {
pam = {};
pam.task_name = "task" + userLists.id;
pam.task_target = userLists.id;
pam.end_time = "2013-12-01";
pam.interval = 180;
pam.locates = self.locates;
pam.isp = self.isp;
this._post(api.newPageTask, pam, function (json) {
if(json.code == 0){
fs.appendFile('taskIds.txt', json.data+",", function (err) {
if (err) throw err;
});
}
});
}
};
PerTasks.prototype.getTaskList = function(){
this._post(api.getTaskList, {task_type:6,page:1,rows:10000}, function (json) {
if(json.code == 0){
console.log(json)
}
});
}
PerTasks.prototype.pagePerformanceCurve = function(){
this._post(api.pagePerformanceCurve, {task_id:75,start_time:"2013-07-10 00:00:00",end_time:"2013-07-10 23:59:59",rows:10000}, function (json) {
if(json.code == 0){
console.log(json)
}
});
}
var perTask = new PerTasks("test/shiba", "shiba123456");
perTask.pagePerformanceCurve();
module.exports = PerTasks;