From 687ef87cd986dc1738ec7dd17556606d6f2bec70 Mon Sep 17 00:00:00 2001 From: hadasi Date: Thu, 18 Jan 2018 10:41:13 +0200 Subject: [PATCH 1/3] updated the stream to not include since when pastdays is undefined, and by so bring data from BOT --- index.js | 23 +++++++++-------------- package.json | 9 +++++---- test.js | 38 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index c6b6aee..d0471f8 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ var stream = require( "stream" ); var extend = require( "extend" ); var request = require( "request" ); var Promise = require( "bluebird" ); +const queryString = require('query-string') request = Promise.promisifyAll( request ) @@ -90,23 +91,17 @@ FacebookInsightStream.prototype._init = function ( callback ) { "{metric}", ].join( "/" ) - var query = [ - "access_token=" + options.token, - "period=" + options.period, - "since=" + since, - "until=" + until, - ].join( "&" ); - var hasEvents = options.events && options.events.length; var breakdowns = options.breakdowns; - if ( hasEvents ) { - query += "&event_name={ev}" - } - - if ( options.aggregate ) { - query += "&aggregateBy={agg}"; - } + let query = queryString.stringify({ + since: options.pastdays ? since : undefined, + until: until, + period: options.period, + access_token: options.token, + event_name: hasEvents ? '{ev}' : undefined, + aggregateBy: options.aggregate ? '{agg}' : undefined + }) if ( breakdowns && breakdowns.length ) { for ( var i = 0; i < breakdowns.length; i += 1 ) { diff --git a/package.json b/package.json index 50f1e46..18d9476 100644 --- a/package.json +++ b/package.json @@ -21,10 +21,11 @@ }, "homepage": "https://github.com/panoplyio/facebook-insight-stream#readme", "dependencies": { - "sugar": "1.4.1", - "extend": "3.0.0", - "request": "2.53.0", - "bluebird": "2.9.32" + "bluebird": "2.9.32", + "extend": "3.0.0", + "query-string": "^5.0.1", + "request": "2.53.0", + "sugar": "1.4.1" }, "devDependencies": { "mocha": "2.3.4" diff --git a/test.js b/test.js index d8f4aaf..560f55f 100644 --- a/test.js +++ b/test.js @@ -162,8 +162,42 @@ describe( "collect", function () { }) }) +describe( "Fetch BOT", function () { + var result = {}; + var source = { + apps: [ 'myApp' ], + ignoreMissing: true + } + + var response = { "myApp": { error: {}, name: "myApp", data: dataGenerator( 1, null ) } } + + before( initialize( result, response, source, true ) ) + after( reset ) + + it( 'Skip missing data', function () { + assert.equal(result.stream.url.includes('&since'), false) + }) +}) + +describe( "Fetch x Days", function () { + var result = {}; + var source = { + apps: [ 'myApp' ], + ignoreMissing: true + } + + var response = { "myApp": { error: {}, name: "myApp", data: dataGenerator( 1, null ) } } + + before( initialize( result, response, source ) ) + after( reset ) + + it( 'Skip missing data', function () { + assert.equal(result.stream.url.includes('&since'), true) + }) +}) + -function initialize( result, response, source ) { +function initialize( result, response, source, fetchBOT ) { result.batchCount = 0; @@ -200,7 +234,7 @@ function initialize( result, response, source ) { } var options = { - pastdays: "30", + pastdays: fetchBOT ? undefined : "30", node: "app", period: "daily", metrics: METRICS, From d50abc98761b763d0bd5c050eaa80df2332d4111 Mon Sep 17 00:00:00 2001 From: hadasi Date: Sun, 21 Jan 2018 11:40:26 +0200 Subject: [PATCH 2/3] pr fixes --- README.md | 2 +- test.js | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 45df766..bd1de96 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ var pageStream = new FacebookInsightStream( options ) ### FacebookInsightStream options: -* `pastdays` (string-required)#number of collected days from today e.g "30" +* `pastdays` (string)#number of collected days from today e.g "30", defaults to BOT * `node` (string-required)#type of insight one of the two ( "app", "page" ) * `token` (string-required)#valid facebook oAuth token with 'read_insigt' scope * `period` (string-required)#the time period to collect according to the relevant api diff --git a/test.js b/test.js index 560f55f..4d13b68 100644 --- a/test.js +++ b/test.js @@ -7,6 +7,7 @@ var BASEURL = "https://graph.facebook.com/"; var METRICS = require( "./metric-list" ); var req_get = request.get; +let calledUrl describe( "Skip missing data", function () { var result = {}; @@ -174,8 +175,8 @@ describe( "Fetch BOT", function () { before( initialize( result, response, source, true ) ) after( reset ) - it( 'Skip missing data', function () { - assert.equal(result.stream.url.includes('&since'), false) + it( 'Fetch insights from BOT', function () { + assert.equal(calledUrl.includes('&since'), false) }) }) @@ -191,8 +192,8 @@ describe( "Fetch x Days", function () { before( initialize( result, response, source ) ) after( reset ) - it( 'Skip missing data', function () { - assert.equal(result.stream.url.includes('&since'), true) + it( 'Fetch insights for x days', function () { + assert.equal(calledUrl.includes('&since'), true) }) }) @@ -205,6 +206,7 @@ function initialize( result, response, source, fetchBOT ) { request.get = function ( url, callback ) { var metric; + calledUrl = url url = url.split( BASEURL )[ 1 ]; var params = url.split( "?" )[ 0 ].split( "/" ); var app = params[ 1 ]; From e4f141603f44a8bd19f76a26243d78f53ff4ce5d Mon Sep 17 00:00:00 2001 From: hadasi Date: Sun, 21 Jan 2018 11:45:25 +0200 Subject: [PATCH 3/3] pr fixes --- README.md | 2 +- test.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bd1de96..a1347b5 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ var pageStream = new FacebookInsightStream( options ) ### FacebookInsightStream options: -* `pastdays` (string)#number of collected days from today e.g "30", defaults to BOT +* `pastdays` (string)#number of collected days from today e.g "30", disregard to fetch beginning of time * `node` (string-required)#type of insight one of the two ( "app", "page" ) * `token` (string-required)#valid facebook oAuth token with 'read_insigt' scope * `period` (string-required)#the time period to collect according to the relevant api diff --git a/test.js b/test.js index 4d13b68..232fc35 100644 --- a/test.js +++ b/test.js @@ -163,7 +163,7 @@ describe( "collect", function () { }) }) -describe( "Fetch BOT", function () { +describe( "Fetch beginning of time", function () { var result = {}; var source = { apps: [ 'myApp' ], @@ -175,12 +175,12 @@ describe( "Fetch BOT", function () { before( initialize( result, response, source, true ) ) after( reset ) - it( 'Fetch insights from BOT', function () { + it( 'Fetch insights from beginning of time', function () { assert.equal(calledUrl.includes('&since'), false) }) }) -describe( "Fetch x Days", function () { +describe( "Fetch x Days ago", function () { var result = {}; var source = { apps: [ 'myApp' ], @@ -192,7 +192,7 @@ describe( "Fetch x Days", function () { before( initialize( result, response, source ) ) after( reset ) - it( 'Fetch insights for x days', function () { + it( 'Fetch insights for past x days', function () { assert.equal(calledUrl.includes('&since'), true) }) })