Skip to content

Commit a8bf6b6

Browse files
committed
add metric for data download rate
1 parent dabe2f4 commit a8bf6b6

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

modules/cache.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import uuid
66
import json
7+
import time
78

89
from pytubefix import YouTube
910

@@ -58,8 +59,10 @@ def add(self, url: str):
5859
MetricsHandler.cache_size.set(len(self.video_id_to_path))
5960
MetricsHandler.cache_size_bytes.set(self.current_size_bytes)
6061
video_file_name = video.default_filename
62+
start_time = time.time()
6163
with MetricsHandler.download_time.time():
6264
video.download(self.file_path)
65+
end_time = time.time()
6366
MetricsHandler.data_downloaded.inc(video.filesize)
6467
MetricsHandler.video_download_count.inc()
6568
video_id = self.get_video_id(url)
@@ -80,6 +83,9 @@ def add(self, url: str):
8083
self.current_size_bytes += video_info.size_bytes
8184
MetricsHandler.cache_size.set(len(self.video_id_to_path))
8285
MetricsHandler.cache_size_bytes.set(self.current_size_bytes)
86+
#download rate are currently listed in bytes / second
87+
download_rate = (video.filesize / (end_time-start_time))
88+
MetricsHandler.download_rate.set(download_rate)
8389

8490
def find(self, video_id: str):
8591
if video_id in self.video_id_to_path:
@@ -158,4 +164,4 @@ def write_cache(self):
158164
def get_video_id(url) -> str:
159165
parsed_url = urlparse(url)
160166
video_id = parse_qs(parsed_url.query)["v"][0]
161-
return video_id
167+
return video_id

modules/metrics.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class Metrics(enum.Enum):
3030
prometheus_client.Summary,
3131
)
3232

33+
DOWNLOAD_RATE = (
34+
"download_rate",
35+
"The rate of downloading videos in bytes per seconds",
36+
prometheus_client.Gauge,
37+
)
38+
3339
DATA_DOWNLOADED = (
3440
"data_downloaded",
3541
"Total video data downloaded in bytes",
@@ -100,4 +106,4 @@ def init(self) -> None:
100106
metric.prometheus_type(
101107
metric.title, metric.description, labelnames=metric.labels
102108
),
103-
)
109+
)

0 commit comments

Comments
 (0)