-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasift-historics.js
121 lines (102 loc) · 3.19 KB
/
datasift-historics.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
/*global module*/
/*jslint node:true*/
'use strict';
var async = require('async'),
request = require( 'request' ),
util = require( 'util' ),
events = require( 'events' ),
HistoricsApi = require( './datasift-historics-api' ),
moniker = require( 'moniker' );
var Historics = function( api, options ) {
/**
* Guide here:
* http://dev.datasift.com/docs/historics/historics-steps
*/
events.EventEmitter.call( this );
// DataSift API URI
this.api = api;
// Prepare settings for Datasift
this.options = options;
// Generate a name for this historics query
this.options.name = this.options.name || moniker.choose()+'-'+new Date().getTime();
this.prepare();
};
util.inherits( Historics, events.EventEmitter );
Historics.prototype.prepare = function() {
var api = this.api,
options = {
'hash': this.options['hash'],
'start': this.options['start'],
'end': this.options['end'],
'sources': this.options['sources'],
'sample': this.options['sample']
};
HistoricsApi.prepare( api, options, function( error, data ) {
if( !error ) {
this.options['playback_id'] = data.id;
this.emit( 'ready', data );
} else {
this.emit( 'error', error );
}
}.bind( this ));
};
Historics.prototype.start = function() {
var api = this.api,
createOptions = {
'playback_id': this.options['playback_id'],
'name': this.options['name'],
'output_type': this.options['output_type'],
'output_params': this.options['output_params']
},
startOptions = {
'playback_id': this.options['playback_id']
};
async.series(
[
function( callback ) {
HistoricsApi.create( api, createOptions, function( error, data ) {
callback( error, data );
});
},
function( callback ){
HistoricsApi.start( api, startOptions, function( error, data ) {
callback( error, data );
});
}
],
function( error, data ) {
if( !error ) {
this.emit( 'started', data );
} else {
this.emit( 'error', error );
}
}.bind( this )
);
};
Historics.prototype.stop = function() {
var api = this.api,
options = {
'playback_id': this.options['playback_id']
};
HistoricsApi.stop( api, options, function( error, data ) {
if( !error ) {
this.emit( 'stopped', data );
} else {
this.emit( 'error', error );
}
}.bind( this ));
};
Historics.prototype.remove = function() {
var api = this.api,
options = {
'playback_id': this.options['playback_id']
};
HistoricsApi.remove( api, options, function( error, data ) {
if( !error ) {
this.emit( 'removed', data );
} else {
this.emit( 'error', error );
}
}.bind( this ));
};
module.exports = Historics;