forked from leancloud/javascript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsight.js
More file actions
146 lines (132 loc) · 5.16 KB
/
Copy pathinsight.js
File metadata and controls
146 lines (132 loc) · 5.16 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
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
/**
* 每位工程师都有保持代码优雅的义务
* Each engineer has a duty to keep the code elegant
**/
const _ = require('underscore');
const AVError = require('./error');
const AVRequest = require('./request').request;
module.exports = function(AV) {
/**
* @namespace 包含了使用了 LeanCloud
* <a href='/docs/leaninsight_guide.html'>离线数据分析功能</a>的函数。
* <p><strong><em>
* 部分函数仅在云引擎运行环境下有效。
* </em></strong></p>
*/
AV.Insight = AV.Insight || {};
_.extend(AV.Insight, /** @lends AV.Insight */ {
/**
* 开始一个 Insight 任务。结果里将返回 Job id,你可以拿得到的 id 使用
* AV.Insight.JobQuery 查询任务状态和结果。
* @param {Object} jobConfig 任务配置的 JSON 对象,例如:<code><pre>
* { "sql" : "select count(*) as c,gender from _User group by gender",
* "saveAs": {
* "className" : "UserGender",
* "limit": 1
* }
* }
* </pre></code>
* sql 指定任务执行的 SQL 语句, saveAs(可选) 指定将结果保存在哪张表里,limit 最大 1000。
* @param {Object} options A Backbone-style options object
* options.success, if set, should be a function to handle a successful
* call to a cloud function. options.error should be a function that
* handles an error running the cloud function. Both functions are
* optional. Both functions take a single argument.
* @return {AV.Promise} A promise that will be resolved with the result
* of the function.
*/
startJob: function(jobConfig, options) {
if(!jobConfig || !jobConfig.sql) {
throw new Error('Please provide the sql to run the job.');
}
var data = {
jobConfig: jobConfig,
appId: AV.applicationId
};
var request = AVRequest("bigquery", 'jobs', null, 'POST',
AV._encode(data, null, true));
return request.then(function(resp) {
return AV._decode(null, resp).id;
})._thenRunCallbacks(options);
},
/**
* 监听 Insight 任务事件,目前仅支持 end 事件,表示任务完成。
* <p><strong><em>
* 仅在云引擎运行环境下有效。
* </em></strong></p>
* @param {String} event 监听的事件,目前仅支持 'end' ,表示任务完成
* @param {Function} 监听回调函数,接收 (err, id) 两个参数,err 表示错误信息,
* id 表示任务 id。接下来你可以拿这个 id 使用AV.Insight.JobQuery 查询任务状态和结果。
*
*/
on: function(event, cb) {
}
});
/**
* 创建一个对象,用于查询 Insight 任务状态和结果。
* @class
* @param {String} id 任务 id
* @since 0.5.5
*/
AV.Insight.JobQuery = function(id, className) {
if(!id) {
throw new Error('Please provide the job id.');
}
this.id = id;
this.className = className;
this._skip = 0;
this._limit = 100;
};
AV.Insight.JobQuery.prototype = {
/**
* Sets the number of results to skip before returning any results.
* This is useful for pagination.
* Default is to skip zero results.
* @param {Number} n the number of results to skip.
* @return {AV.Query} Returns the query, so you can chain this call.
*/
skip: function(n) {
this._skip = n;
return this;
},
/**
* Sets the limit of the number of results to return. The default limit is
* 100, with a maximum of 1000 results being returned at a time.
* @param {Number} n the number of results to limit to.
* @return {AV.Query} Returns the query, so you can chain this call.
*/
limit: function(n) {
this._limit = n;
return this;
},
/**
* 查询任务状态和结果,任务结果为一个 JSON 对象,包括 status 表示任务状态, totalCount 表示总数,
* results 数组表示任务结果数组,previewCount 表示可以返回的结果总数,任务的开始和截止时间
* startTime、endTime 等信息。
*
* @param {Object} options A Backbone-style options object
* options.success, if set, should be a function to handle a successful
* call to a cloud function. options.error should be a function that
* handles an error running the cloud function. Both functions are
* optional. Both functions take a single argument.
* @return {AV.Promise} A promise that will be resolved with the result
* of the function.
*
*/
find: function(options) {
var params = {
skip: this._skip,
limit: this._limit
};
var request = AVRequest("bigquery", 'jobs', this.id, "GET",
params);
var self = this;
return request.then(function(response) {
if(response.error) {
return AV.Promise.error(new AVError(response.code, response.error));
}
return AV.Promise.as(response);
})._thenRunCallbacks(options);
}
};
};