adding functionality to detect user download speed

This commit is contained in:
Anthony 2021-09-13 23:03:19 +02:00
parent 4c72a563da
commit 3eb52c647b
No known key found for this signature in database
GPG key ID: C386D3C93D50E356
3 changed files with 49 additions and 1 deletions

View file

@ -10,6 +10,13 @@ import ElectronCookies from '@exponent/electron-cookies';
import { generateInitialUrl } from 'util/url'; import { generateInitialUrl } from 'util/url';
// @endif // @endif
import { MATOMO_ID, MATOMO_URL } from 'config'; import { MATOMO_ID, MATOMO_URL } from 'config';
import getConnectionSpeed from 'util/detect-user-bandwidth';
let downloadSpeed;
getConnectionSpeed(function(speedInMbps){
downloadSpeed = speedInMbps;
console.log(downloadSpeed);
});
const isProduction = process.env.NODE_ENV === 'production'; const isProduction = process.env.NODE_ENV === 'production';
const devInternalApis = process.env.LBRY_API_URL && process.env.LBRY_API_URL.includes('dev'); const devInternalApis = process.env.LBRY_API_URL && process.env.LBRY_API_URL.includes('dev');
@ -202,8 +209,10 @@ async function sendWatchmanData(body) {
} }
const analytics: Analytics = { const analytics: Analytics = {
// receive buffer events from tracking plugin and jklj // receive buffer events from tracking plugin and save buffer amounts and times for backend call
videoBufferEvent: async (claim, data) => { videoBufferEvent: async (claim, data) => {
console.log('running here!');
console.log(data);
amountOfBufferEvents = amountOfBufferEvents + 1; amountOfBufferEvents = amountOfBufferEvents + 1;
amountOfBufferTimeInMS = amountOfBufferTimeInMS + data.bufferDuration; amountOfBufferTimeInMS = amountOfBufferTimeInMS + data.bufferDuration;
}, },

View file

@ -505,6 +505,7 @@ export function doAnalyticsBuffer(uri, bufferData) {
const fileSizeInBits = fileSize * 8; const fileSizeInBits = fileSize * 8;
const bitRate = parseInt(fileSizeInBits / fileDurationInSeconds); const bitRate = parseInt(fileSizeInBits / fileDurationInSeconds);
const userId = user && user.id.toString(); const userId = user && user.id.toString();
// if there's a logged in user, send buffer event data to watchman
if (userId) { if (userId) {
analytics.videoBufferEvent(claim, { analytics.videoBufferEvent(claim, {
timeAtBuffer, timeAtBuffer,

View file

@ -0,0 +1,38 @@
var startTime;
var endTime;
var testConnectionSpeed = {
imageAddr: 'https://upload.wikimedia.org/wikipedia/commons/a/a6/Brandenburger_Tor_abends.jpg', // this is just an example, you rather want an image hosted on your server
downloadSize: 2707459, // this must match with the image above
getConnectionSpeed: function(callback) {
testConnectionSpeed.InitiateSpeedDetection();
testConnectionSpeed.callback = callback;
},
InitiateSpeedDetection: function() {
window.setTimeout(testConnectionSpeed.MeasureConnectionSpeed, 1);
},
result: function() {
var duration = (endTime - startTime) / 1000;
var bitsLoaded = testConnectionSpeed.downloadSize * 8;
var speedBps = (bitsLoaded / duration).toFixed(2);
var speedKbps = (speedBps / 1024).toFixed(2);
var speedMbps = (speedKbps / 1024).toFixed(2);
testConnectionSpeed.callback(speedMbps);
},
MeasureConnectionSpeed: function() {
var download = new Image();
download.onload = function() {
endTime = (new Date()).getTime();
testConnectionSpeed.result();
};
startTime = (new Date()).getTime();
var cacheBuster = '?nnn=' + startTime;
download.src = testConnectionSpeed.imageAddr + cacheBuster;
},
};
// start test immediatly, you could also call this on any event or whenever you want
// testConnectionSpeed.getConnectionSpeed(function(time) { console.log(time) });
module.exports = testConnectionSpeed.getConnectionSpeed;