disk space metrics

This commit is contained in:
Lex Berezhny 2021-09-30 21:38:02 -04:00 committed by Victor Shyba
parent e7b5b82909
commit 1004a83dae
3 changed files with 21 additions and 4 deletions

View file

@ -6,13 +6,14 @@ log = logging.getLogger(__name__)
class DiskSpaceManager: class DiskSpaceManager:
def __init__(self, config, db, blob_manager, cleaning_interval=30 * 60): def __init__(self, config, db, blob_manager, cleaning_interval=30 * 60, analytics=None):
self.config = config self.config = config
self.db = db self.db = db
self.blob_manager = blob_manager self.blob_manager = blob_manager
self.cleaning_interval = cleaning_interval self.cleaning_interval = cleaning_interval
self.running = False self.running = False
self.task = None self.task = None
self.analytics = analytics
async def get_space_used_bytes(self): async def get_space_used_bytes(self):
return await self.db.get_stored_blob_disk_usage() return await self.db.get_stored_blob_disk_usage()
@ -21,10 +22,14 @@ class DiskSpaceManager:
return int(await self.get_space_used_bytes()/1024.0/1024.0) return int(await self.get_space_used_bytes()/1024.0/1024.0)
async def clean(self): async def clean(self):
if not self.config.blob_storage_limit: space_used_bytes = await self.get_space_used_bytes()
storage_limit = self.config.blob_storage_limit*1024*1024 if self.config.blob_storage_limit else None
if self.analytics:
asyncio.create_task(self.analytics.send_disk_space_used(space_used_bytes, storage_limit))
if not storage_limit:
return 0 return 0
delete = [] delete = []
available = (self.config.blob_storage_limit*1024*1024) - await self.get_space_used_bytes() available = storage_limit - space_used_bytes
if available > 0: if available > 0:
return 0 return 0
for blob_hash, file_size, _ in await self.db.get_stored_blobs(is_mine=False): for blob_hash, file_size, _ in await self.db.get_stored_blobs(is_mine=False):

View file

@ -18,6 +18,7 @@ DOWNLOAD_STARTED = 'Download Started'
DOWNLOAD_ERRORED = 'Download Errored' DOWNLOAD_ERRORED = 'Download Errored'
DOWNLOAD_FINISHED = 'Download Finished' DOWNLOAD_FINISHED = 'Download Finished'
HEARTBEAT = 'Heartbeat' HEARTBEAT = 'Heartbeat'
DISK_SPACE = 'Disk Space'
CLAIM_ACTION = 'Claim Action' # publish/create/update/abandon CLAIM_ACTION = 'Claim Action' # publish/create/update/abandon
NEW_CHANNEL = 'New Channel' NEW_CHANNEL = 'New Channel'
CREDITS_SENT = 'Credits Sent' CREDITS_SENT = 'Credits Sent'
@ -169,6 +170,14 @@ class AnalyticsManager:
}) })
) )
async def send_disk_space_used(self, storage_used, storage_limit):
await self.track(
self._event(DISK_SPACE, {
'used': storage_used,
'limit': storage_limit,
})
)
async def send_server_startup(self): async def send_server_startup(self):
await self.track(self._event(SERVER_STARTUP)) await self.track(self._event(SERVER_STARTUP))

View file

@ -400,7 +400,10 @@ class DiskSpaceComponent(Component):
async def start(self): async def start(self):
db = self.component_manager.get_component(DATABASE_COMPONENT) db = self.component_manager.get_component(DATABASE_COMPONENT)
blob_manager = self.component_manager.get_component(BLOB_COMPONENT) blob_manager = self.component_manager.get_component(BLOB_COMPONENT)
self.disk_space_manager = DiskSpaceManager(self.conf, db, blob_manager) self.disk_space_manager = DiskSpaceManager(
self.conf, db, blob_manager,
analytics=self.component_manager.analytics_manager
)
await self.disk_space_manager.start() await self.disk_space_manager.start()
async def stop(self): async def stop(self):