Skip to content

Commit e776262

Browse files
committed
add insight.js
1 parent 8c54df4 commit e776262

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

lib/insight.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
(function(root) {
2+
root.AV = root.AV || {};
3+
var AV = root.AV;
4+
var _ = AV._;
5+
6+
/**
7+
* @namespace 包含了使用了 LeanCloud
8+
* <a href='/docs/bigquery_guide.html'>离线数据分析功能</a>的函数。
9+
* <p><strong><em>
10+
* 部分函数仅在云引擎运行环境下有效。
11+
* </em></strong></p>
12+
*/
13+
AV.BigQuery = AV.BigQuery || {};
14+
15+
_.extend(AV.BigQuery, /** @lends AV.BigQuery */ {
16+
17+
/**
18+
* 开始一个 BigQuery 任务。结果里将返回 Job id,你可以拿得到的 id 使用
19+
* AV.BigQuery.JobQuery 查询任务状态和结果。
20+
* @param {Object} jobConfig 任务配置的 JSON 对象,例如:<code><pre>
21+
* { "sql" : "select count(*) as c,gender from _User group by gender",
22+
* "saveAs": {
23+
* "className" : "UserGender",
24+
* "limit": 1
25+
* }
26+
* }
27+
* </pre></code>
28+
* sql 指定任务执行的 SQL 语句, saveAs(可选) 指定将结果保存在哪张表里,limit 最大 1000。
29+
* @param {Object} options A Backbone-style options object
30+
* options.success, if set, should be a function to handle a successful
31+
* call to a cloud function. options.error should be a function that
32+
* handles an error running the cloud function. Both functions are
33+
* optional. Both functions take a single argument.
34+
* @return {AV.Promise} A promise that will be resolved with the result
35+
* of the function.
36+
*/
37+
startJob: function(jobConfig, options) {
38+
if(!jobConfig || !jobConfig.sql) {
39+
throw new Error('Please provide the sql to run the job.');
40+
}
41+
var data = {
42+
jobConfig: jobConfig,
43+
appId: AV.applicationId
44+
}
45+
var request = AV._request("bigquery", 'jobs', null, 'POST',
46+
AV._encode(data, null, true));
47+
48+
return request.then(function(resp) {
49+
return AV._decode(null, resp).id;
50+
})._thenRunCallbacks(options);
51+
},
52+
53+
/**
54+
* 监听 BigQuery 任务事件,目前仅支持 end 事件,表示任务完成。
55+
* <p><strong><em>
56+
* 仅在云引擎运行环境下有效。
57+
* </em></strong></p>
58+
* @param {String} event 监听的事件,目前仅支持 'end' ,表示任务完成
59+
* @param {Function} 监听回调函数,接收 (err, id) 两个参数,err 表示错误信息,
60+
* id 表示任务 id。接下来你可以拿这个 id 使用AV.BigQuery.JobQuery 查询任务状态和结果。
61+
*
62+
*/
63+
on: function(event, cb) {
64+
}
65+
});
66+
67+
/**
68+
* 创建一个对象,用于查询 BigQuery 任务状态和结果。
69+
* @class
70+
* @param {String} id 任务 id
71+
* @since 0.5.5
72+
*/
73+
AV.BigQuery.JobQuery = function(id, className) {
74+
if(!id) {
75+
throw new Error('Please provide the job id.');
76+
}
77+
this.id = id;
78+
this.className = className;
79+
this._skip = 0;
80+
this._limit = 100;
81+
};
82+
83+
AV.BigQuery.JobQuery.prototype = {
84+
85+
/**
86+
* Sets the number of results to skip before returning any results.
87+
* This is useful for pagination.
88+
* Default is to skip zero results.
89+
* @param {Number} n the number of results to skip.
90+
* @return {AV.Query} Returns the query, so you can chain this call.
91+
*/
92+
skip: function(n) {
93+
this._skip = n;
94+
return this;
95+
},
96+
97+
/**
98+
* Sets the limit of the number of results to return. The default limit is
99+
* 100, with a maximum of 1000 results being returned at a time.
100+
* @param {Number} n the number of results to limit to.
101+
* @return {AV.Query} Returns the query, so you can chain this call.
102+
*/
103+
limit: function(n) {
104+
this._limit = n;
105+
return this;
106+
},
107+
108+
/**
109+
* 查询任务状态和结果,任务结果为一个 JSON 对象,包括 status 表示任务状态, totalCount 表示总数,
110+
* results 数组表示任务结果数组,previewCount 表示可以返回的结果总数,任务的开始和截止时间
111+
* startTime、endTime 等信息。
112+
*
113+
* @param {Object} options A Backbone-style options object
114+
* options.success, if set, should be a function to handle a successful
115+
* call to a cloud function. options.error should be a function that
116+
* handles an error running the cloud function. Both functions are
117+
* optional. Both functions take a single argument.
118+
* @return {AV.Promise} A promise that will be resolved with the result
119+
* of the function.
120+
*
121+
*/
122+
find: function(options) {
123+
var params = {
124+
skip: this._skip,
125+
limit: this._limit
126+
};
127+
128+
var request = AV._request("bigquery", 'jobs', this.id, "GET",
129+
params);
130+
var self = this;
131+
return request.then(function(response) {
132+
if(response.error) {
133+
return AV.Promise.error(new AV.Error(response.code, response.error));
134+
}
135+
return AV.Promise.as(response);
136+
})._thenRunCallbacks(options);
137+
}
138+
139+
};
140+
141+
}(this));

0 commit comments

Comments
 (0)