-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
249 lines (191 loc) · 5.46 KB
/
db.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* Created by tony on 16/5/18.
*/
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/aso_tracking');
var async = require('async');
var _ = require('underscore');
var moment = require('moment')
var AppSchema = new mongoose.Schema({
appid: Number,
name: String
});
AppSchema.index({appid: 1}, {unique: true, dropDups: true});
var KeywordSchema = new mongoose.Schema({
appid: Number,
keyword: String
});
KeywordSchema.index({appid: 1});
KeywordSchema.index({appid: 1, keyword: 1}, {unique: true, dropDups: true});
var AsoRankSchema = new mongoose.Schema({
appid: Number,
keyword: String,
rank: Number,
date: String,
dt: String,
updated: Date,
});
AsoRankSchema.index({appid: 1});
AsoRankSchema.index({updated: -1});
AsoRankSchema.index({keyword: 1});
AsoRankSchema.index({dt: -1});
AsoRankSchema.index({date: -1});
AsoRankSchema.index({appid: 1, keyword: 1, dt: -1});
var SpiderQueueScheme = new mongoose.Schema({
dt: String,
keyword: String,
url: String,
status: String,
last_error: String
});
SpiderQueueScheme.index({status: 1});
SpiderQueueScheme.index({keyword: 1, dt: 1, url: 1}, {unique: true, dropDups: true});
//数据对象
var AppModel = mongoose.model('app', AppSchema);
var KeywordModel = mongoose.model('keyword', KeywordSchema);
var AsoRankModel = mongoose.model('aso_rank', AsoRankSchema);
var SpiderQueueModel = mongoose.model('spider_queue', SpiderQueueScheme);
db_obj = {}
db_obj.get_apps = function (cb) {
AppModel.find(function (err, data) {
cb(data)
});
}
db_obj.add_app = function (appinfo) {
var new_app = new AppModel(appinfo);
new_app.save();
return new_app._id;
}
db_obj.del_app = function (appid) {
AppModel.remove({appid: appid}, function (err) {
if (err) return handleError(err);
// removed!
});
}
db_obj.get_app_info = function (appid, cb) {
AppModel.findOne({appid: appid}).exec(function (err, data) {
cb(data)
});
}
db_obj.get_app_keywords = function (appid, cb) {
KeywordModel.find({appid: appid}).exec(function (err, data) {
cb(data)
});
}
db_obj.get_all_distinct_keywords = function (cb) {
KeywordModel.distinct('keyword').exec(function (err, data) {
cb(data)
});
}
db_obj.add_keyword = function (appid, keyword) {
var new_obj = new KeywordModel({appid: appid, keyword: keyword});
new_obj.save();
console.log('添加关键词' + appid + ' ' + keyword);
return new_obj._id;
}
db_obj.del_keyword = function (appid, keyword) {
KeywordModel.remove({appid: appid, keyword: keyword}, function (err) {
if (err) return handleError(err);
// removed!
});
}
db_obj.get_app_aso_rank_log = function (appid, cb) {
AsoRankModel.find({appid: appid}).sort({updated: 1}).exec(function (err, data) {
cb(data)
});
}
db_obj.get_app_keyword_aso_rank_log = function (appid, keyword, cb) {
AsoRankModel.find({appid: appid, keyword: keyword}).exec(function (err, data) {
cb(data)
});
}
db_obj.get_app_keyword_dt_rank = function (appid, keyword, dt, cb) {
AsoRankModel.findOne({
appid: appid,
keyword: keyword,
dt: {$eq: moment(new Date(dt)).format('Y-M-D H:00')}
}).exec(function (err, data) {
if (!_.isEmpty(data)) {
cb(data.rank);
}
else {
cb(null);
}
});
}
db_obj.get_app_keyword_date_rank = function (appid, keyword, date, cb) {
AsoRankModel.findOne({
appid: appid,
keyword: keyword,
date: {$eq: moment(new Date(date)).format('Y-M-D')}
}).exec(function (err, data) {
if (!_.isEmpty(data)) {
cb(data.rank);
}
else {
cb(null);
}
});
}
db_obj.get_app_aso_rank_log_distinct_dt = function (appid, cb) {
AsoRankModel.distinct('dt').exec(function (err, data) {
//按日期常规排序
data = _.sortBy(data,function(v)
{
return new Date(v);
});
cb(data)
});
}
db_obj.get_app_aso_rank_log_distinct_date = function (appid, cb) {
AsoRankModel.distinct('date').exec(function (err, data) {
//按日期常规排序
data = _.sortBy(data,function(v)
{
return new Date(v);
});
cb(data)
});
}
db_obj.add_spider_queue = function (queue_url_obj) {
var obj = new SpiderQueueModel({
keyword: queue_url_obj.keyword,
url: queue_url_obj.url,
dt: moment().format('Y-M-D H:00'),
status: 'queue',
last_error: ''
});
obj.save();
}
db_obj.get_spider_queue = function (cb) {
SpiderQueueModel.find({status: 'queue'}).sort({dt: 1}).exec(function (err, data) {
cb(data)
})
}
db_obj.set_spider_queue_result_ok = function (url) {
var update_obj = {
status: 'ok'
}
SpiderQueueModel.update({url: url}, update_obj, {multi: true}, function (err) {
});
}
db_obj.set_spider_queue_result_error = function (url, error_msg) {
var update_obj = {
status: 'queue',
last_error: error_msg
}
SpiderQueueModel.update({url: url}, update_obj, {multi: true}, function (err) {
});
}
db_obj.save_aso_log = function (logs) {
if (!_.isArray(logs)) {
logs = [logs]
}
logs.forEach(function (v, i) {
console.log('---准备保存aso_log----')
console.log(v)
var new_rank = new AsoRankModel(v);
new_rank.save();
});
}
module.exports = db_obj;