This repository has been archived by the owner on Mar 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeature.js
executable file
·107 lines (100 loc) · 2.96 KB
/
feature.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
var redis_client = require('./make_redis_client')(4)
var Promise = require('bluebird')
// TODO DECIDE ON A BETTER PRIMARY KEY
// this presumes that platform+channel is unique
// var PRIMARYKEY = stream.platform+'/'+stream.channel
var NUMBER_FIELDS = [
'rustlers',
'viewers',
'expire_at'
]
var BOOLEAN_FIELDS = [
'featured',
'live'
]
var featureMod = {
rawGet: function(key){
return new Promise(function (resolve, reject){
redis_client.hgetall(key, function (err, returned){
if (err) {
reject(err)
}
// convert fields to proper types
NUMBER_FIELDS.forEach(function (n){
if (returned.hasOwnProperty(n)) {
returned[n] = parseInt(returned[n], 10)
}
})
BOOLEAN_FIELDS.forEach(function (n){
if (returned.hasOwnProperty(n)) {
returned[n] = (returned[n] == 'true' || returned[n] == true)
}
})
resolve(returned)
})
})
},
get: function(name){
return featureMod.rawGet('stream:'+name)
},
set: function(name, stream){
return new Promise(function (resolve, reject){
redis_client.hmset('stream:'+name, stream, function (err, result){
if(err){
return reject(err)
}
resolve(result)
})
})
},
featureByStream: function(stream){
return featureMod.featureByName(stream.platform+'/'+stream.channel)
},
featureByName: function(name){
name = 'stream:'+name
return featureMod.get(name).then(function (stream){
stream.featured = true
return featureMod.set(name, stream)
})
},
upsert: function(primary_key, stream){
// TODO DECIDE ON A BETTER PRIMARY KEY
// this presumes that platform+channel is unique
// var PRIMARYKEY = stream.platform+'/'+stream.channel
return featureMod.set(primary_key, stream).then(function (stream){
return featureMod.get(primary_key)
})
},
all: function(){
// console.log('getting all')
var streams = []
return new Promise(function (resolve, reject){
redis_client.keys('stream:*', function (err, keys) {
if (err) reject(err);
// console.log('all keys', keys, typeof keys)
resolve(keys)
})
}).then(function (keys) {
// console.log('all keys', keys, typeof keys)
return Promise.all(keys.map(function (key) {
// console.log('inside Primse.all keys.map', key)
return new Promise(function (resolve, reject) {
featureMod.rawGet(key).then(function (res, derp){
console.log(res, derp)
streams.push(res);
resolve();
})
// redis_client.hgetall(key, function (err, res) {
// if (err) reject(err);
// })
});
}))
}).then(function(){
// console.log('final THEN for .all. Streams is', streams)
return new Promise(function (resolve, reject){
resolve(streams)
})
})
}
}
module.exports = featureMod