2019-01-21 21:55:50 +01:00
|
|
|
import asyncio
|
2017-04-26 20:15:38 +02:00
|
|
|
import collections
|
|
|
|
import logging
|
2019-01-09 01:02:49 +01:00
|
|
|
import aiohttp
|
2019-03-01 20:48:49 +01:00
|
|
|
import typing
|
|
|
|
import binascii
|
2019-01-21 21:55:50 +01:00
|
|
|
from lbrynet import utils
|
2019-01-28 01:26:18 +01:00
|
|
|
from lbrynet.conf import Config
|
2019-01-09 01:02:49 +01:00
|
|
|
from lbrynet.extras import system_info
|
2017-04-26 20:15:38 +02:00
|
|
|
|
2019-01-28 01:26:18 +01:00
|
|
|
ANALYTICS_ENDPOINT = 'https://api.segment.io/v1'
|
|
|
|
ANALYTICS_TOKEN = 'Ax5LZzR1o3q3Z3WjATASDwR5rKyHH0qOIRIbLmMXn2H='
|
|
|
|
|
2017-04-26 20:15:38 +02:00
|
|
|
# Things We Track
|
|
|
|
SERVER_STARTUP = 'Server Startup'
|
|
|
|
SERVER_STARTUP_SUCCESS = 'Server Startup Success'
|
|
|
|
SERVER_STARTUP_ERROR = 'Server Startup Error'
|
|
|
|
DOWNLOAD_STARTED = 'Download Started'
|
|
|
|
DOWNLOAD_ERRORED = 'Download Errored'
|
|
|
|
DOWNLOAD_FINISHED = 'Download Finished'
|
|
|
|
HEARTBEAT = 'Heartbeat'
|
2017-04-27 02:02:00 +02:00
|
|
|
CLAIM_ACTION = 'Claim Action' # publish/create/update/abandon
|
|
|
|
NEW_CHANNEL = 'New Channel'
|
|
|
|
CREDITS_SENT = 'Credits Sent'
|
2018-10-18 23:41:49 +02:00
|
|
|
UPNP_SETUP = "UPnP Setup"
|
2017-04-26 20:15:38 +02:00
|
|
|
|
|
|
|
BLOB_BYTES_UPLOADED = 'Blob Bytes Uploaded'
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-03-01 20:48:49 +01:00
|
|
|
def _event_properties(installation_id: str, session_id: str,
|
|
|
|
event_properties: typing.Optional[typing.Dict]) -> typing.Dict:
|
|
|
|
properties = {
|
|
|
|
'lbry_id': installation_id,
|
|
|
|
'session_id': session_id,
|
|
|
|
}
|
|
|
|
properties.update(event_properties or {})
|
|
|
|
return properties
|
|
|
|
|
|
|
|
|
|
|
|
def _download_properties(download_id: str, name: str, sd_hash: str) -> typing.Dict:
|
|
|
|
p = {
|
|
|
|
'download_id': download_id,
|
|
|
|
'name': name,
|
|
|
|
'stream_info': sd_hash
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
|
|
|
|
|
|
|
|
def _make_context(platform):
|
|
|
|
# see https://segment.com/docs/spec/common/#context
|
|
|
|
# they say they'll ignore fields outside the spec, but evidently they don't
|
|
|
|
context = {
|
|
|
|
'app': {
|
|
|
|
'version': platform['lbrynet_version'],
|
|
|
|
'build': platform['build'],
|
|
|
|
},
|
|
|
|
# TODO: expand os info to give linux/osx specific info
|
|
|
|
'os': {
|
|
|
|
'name': platform['os_system'],
|
|
|
|
'version': platform['os_release']
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if 'desktop' in platform and 'distro' in platform:
|
|
|
|
context['os']['desktop'] = platform['desktop']
|
|
|
|
context['os']['distro'] = platform['distro']
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class AnalyticsManager:
|
2019-01-21 21:55:50 +01:00
|
|
|
|
|
|
|
def __init__(self, conf: Config, installation_id: str, session_id: str):
|
|
|
|
self.cookies = {}
|
|
|
|
self.url = ANALYTICS_ENDPOINT
|
|
|
|
self._write_key = utils.deobfuscate(ANALYTICS_TOKEN)
|
|
|
|
self._enabled = conf.share_usage_data
|
2017-04-26 20:15:38 +02:00
|
|
|
self._tracked_data = collections.defaultdict(list)
|
2019-03-01 20:48:49 +01:00
|
|
|
self.context = _make_context(system_info.get_platform())
|
2019-01-21 21:55:50 +01:00
|
|
|
self.installation_id = installation_id
|
|
|
|
self.session_id = session_id
|
|
|
|
self.task: asyncio.Task = None
|
|
|
|
|
2019-01-22 05:28:26 +01:00
|
|
|
@property
|
|
|
|
def is_started(self):
|
|
|
|
return self.task is not None
|
|
|
|
|
2019-01-21 21:55:50 +01:00
|
|
|
def start(self):
|
|
|
|
if self._enabled and self.task is None:
|
|
|
|
self.task = asyncio.create_task(self.run())
|
|
|
|
|
|
|
|
async def run(self):
|
|
|
|
while True:
|
|
|
|
await self._send_heartbeat()
|
|
|
|
await asyncio.sleep(1800)
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
if self.task is not None and not self.task.done():
|
|
|
|
self.task.cancel()
|
|
|
|
|
2019-03-01 20:48:49 +01:00
|
|
|
async def _post(self, data: typing.Dict):
|
2019-01-21 21:55:50 +01:00
|
|
|
request_kwargs = {
|
|
|
|
'method': 'POST',
|
2019-03-01 20:48:49 +01:00
|
|
|
'url': self.url + '/track',
|
2019-01-21 21:55:50 +01:00
|
|
|
'headers': {'Connection': 'Close'},
|
|
|
|
'auth': aiohttp.BasicAuth(self._write_key, ''),
|
|
|
|
'json': data,
|
|
|
|
'cookies': self.cookies
|
|
|
|
}
|
|
|
|
try:
|
2019-02-28 18:40:11 +01:00
|
|
|
async with utils.aiohttp_request(**request_kwargs) as response:
|
2019-01-21 21:55:50 +01:00
|
|
|
self.cookies.update(response.cookies)
|
|
|
|
except Exception as e:
|
2019-03-01 20:48:49 +01:00
|
|
|
log.exception('Encountered an exception while POSTing to %s: ', self.url + '/track', exc_info=e)
|
2019-01-21 21:55:50 +01:00
|
|
|
|
2019-03-01 20:48:49 +01:00
|
|
|
async def track(self, event: typing.Dict):
|
2019-01-21 21:55:50 +01:00
|
|
|
"""Send a single tracking event"""
|
|
|
|
if self._enabled:
|
2019-03-01 21:26:45 +01:00
|
|
|
log.debug('Sending track event: %s', event)
|
2019-03-01 20:48:49 +01:00
|
|
|
await self._post(event)
|
2017-04-26 20:15:38 +02:00
|
|
|
|
2019-01-09 01:02:49 +01:00
|
|
|
async def send_upnp_setup_success_fail(self, success, status):
|
2019-01-21 21:55:50 +01:00
|
|
|
await self.track(
|
2018-10-18 23:41:49 +02:00
|
|
|
self._event(UPNP_SETUP, {
|
|
|
|
'success': success,
|
|
|
|
'status': status,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2019-01-09 01:02:49 +01:00
|
|
|
async def send_server_startup(self):
|
2019-01-21 21:55:50 +01:00
|
|
|
await self.track(self._event(SERVER_STARTUP))
|
2017-04-26 20:15:38 +02:00
|
|
|
|
2019-01-09 01:02:49 +01:00
|
|
|
async def send_server_startup_success(self):
|
2019-01-21 21:55:50 +01:00
|
|
|
await self.track(self._event(SERVER_STARTUP_SUCCESS))
|
2017-04-26 20:15:38 +02:00
|
|
|
|
2019-01-09 01:02:49 +01:00
|
|
|
async def send_server_startup_error(self, message):
|
2019-01-21 21:55:50 +01:00
|
|
|
await self.track(self._event(SERVER_STARTUP_ERROR, {'message': message}))
|
2017-04-26 20:15:38 +02:00
|
|
|
|
2019-03-01 20:48:49 +01:00
|
|
|
async def send_download_started(self, download_id, name, sd_hash):
|
2019-01-21 21:55:50 +01:00
|
|
|
await self.track(
|
2019-03-01 20:48:49 +01:00
|
|
|
self._event(DOWNLOAD_STARTED, _download_properties(download_id, name, sd_hash))
|
2017-04-26 20:15:38 +02:00
|
|
|
)
|
|
|
|
|
2019-03-01 20:48:49 +01:00
|
|
|
async def send_download_finished(self, download_id, name, sd_hash):
|
|
|
|
await self.track(self._event(DOWNLOAD_FINISHED, _download_properties(download_id, name, sd_hash)))
|
2017-04-26 20:15:38 +02:00
|
|
|
|
2019-03-01 20:48:49 +01:00
|
|
|
async def send_download_errored(self, error: Exception, name: str):
|
|
|
|
await self.track(self._event(DOWNLOAD_ERRORED, {
|
|
|
|
'download_id': binascii.hexlify(utils.generate_id()).decode(),
|
|
|
|
'name': name,
|
|
|
|
'stream_info': None,
|
|
|
|
'error': type(error).__name__,
|
|
|
|
'reason': str(error),
|
|
|
|
'report': None
|
|
|
|
}))
|
2017-04-26 20:15:38 +02:00
|
|
|
|
2019-01-09 01:02:49 +01:00
|
|
|
async def send_claim_action(self, action):
|
2019-01-21 21:55:50 +01:00
|
|
|
await self.track(self._event(CLAIM_ACTION, {'action': action}))
|
2017-04-27 02:02:00 +02:00
|
|
|
|
2019-01-09 01:02:49 +01:00
|
|
|
async def send_new_channel(self):
|
2019-01-21 21:55:50 +01:00
|
|
|
await self.track(self._event(NEW_CHANNEL))
|
2017-04-27 02:02:00 +02:00
|
|
|
|
2019-01-09 01:02:49 +01:00
|
|
|
async def send_credits_sent(self):
|
2019-01-21 21:55:50 +01:00
|
|
|
await self.track(self._event(CREDITS_SENT))
|
2017-04-27 02:02:00 +02:00
|
|
|
|
2019-01-09 01:02:49 +01:00
|
|
|
async def _send_heartbeat(self):
|
2019-01-21 21:55:50 +01:00
|
|
|
await self.track(self._event(HEARTBEAT))
|
2017-04-26 20:15:38 +02:00
|
|
|
|
2019-03-01 20:48:49 +01:00
|
|
|
def _event(self, event, properties: typing.Optional[typing.Dict] = None):
|
2017-04-26 20:15:38 +02:00
|
|
|
return {
|
|
|
|
'userId': 'lbry',
|
|
|
|
'event': event,
|
2019-03-01 20:48:49 +01:00
|
|
|
'properties': _event_properties(self.installation_id, self.session_id, properties),
|
2017-04-26 20:15:38 +02:00
|
|
|
'context': self.context,
|
|
|
|
'timestamp': utils.isonow()
|
|
|
|
}
|