Last active
March 10, 2017 19:49
-
-
Save sunnygleason/b18419701db58a2df6e74b6f26f9b95c to your computer and use it in GitHub Desktop.
PubNub Metrics UI w/ Librato API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.5.0.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> | |
<script src="https://cdn.pubnub.com/sdk/pubnub-angular/pubnub-angular-4.0.2.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> | |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css" /> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" /> | |
</head> | |
<body> | |
<div class="container" ng-app="PubNubAngularApp" ng-controller="MyMetricsCtrl"> | |
<pre> | |
NOTE: make sure to update the PubNub keys below with your keys, | |
and ensure that the metrics BLOCK is configured properly! | |
</pre> | |
<h3>MyUser Metrics Graphing</h3> | |
<p>User : {{userid}}</p> | |
<div> | |
Sentiment: | |
<select ng-model="sentiment"> | |
<option value="1">happy</option> | |
<option value="0">neutral</option> | |
<option value="-1">sad</option> | |
</select> | |
</div> | |
<div class="librato-display-media" | |
data-chart_id="YOUR_LIBRATO_CHARTID" | |
data-duration="3600" | |
data-width="400" | |
data-height="300" | |
data-source="*"> | |
</div> | |
<script type="text/javascript" | |
src="https://sdk.librato.com/librato-sdk-v1.0.0-min.js" | |
charset="utf-8" | |
data-librato_email="YOUR_LIBRATO_EMAIL" | |
data-librato_token="YOUR_LIBRATO_APIKEY"> | |
</script> | |
<br /><br /> | |
<ul> | |
<li ng-repeat="message in messages track by $index"> | |
userid: {{message.userid}} | |
<br /> | |
sentiment: {{message.sentiment}} | |
</li> | |
</ul> | |
</div> | |
<script> | |
angular.module('PubNubAngularApp', ["pubnub.angular.service"]) | |
.controller('MyMetricsCtrl', function($rootScope, $scope, $interval, Pubnub) { | |
$scope.userid = 'user' + parseInt(Math.random() * 10000); | |
$scope.sentiment = '1'; | |
$scope.messages = []; | |
$scope.channel = 'librato-channel'; | |
if (!$rootScope.initialized) { | |
Pubnub.init({ | |
publishKey: 'YOUR_PUB_KEY', | |
subscribeKey: 'YOUR_SUB_KEY', | |
ssl:true | |
}); | |
$rootScope.initialized = true; | |
} | |
var msgCallback = function(payload) { | |
$scope.$apply(function() { | |
$scope.messages.unshift(payload); | |
}); | |
}; | |
$scope.publish = function() { | |
Pubnub.publish({ | |
channel: $scope.channel, | |
message: {userid:$scope.userid, sentiment:$scope.sentiment} | |
}); | |
}; | |
$interval($scope.publish, 30000); | |
Pubnub.subscribe({ channels: [$scope.channel] }); | |
Pubnub.addListener({ message: msgCallback }); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment