From c90b0bd994199a97d63c1898bf9d264eeec2c74a Mon Sep 17 00:00:00 2001 From: Untone Date: Sun, 28 Jan 2024 18:33:04 +0300 Subject: [PATCH] ga-metric-fieldname-fix --- services/viewed.py | 47 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/services/viewed.py b/services/viewed.py index 3ada4b1d..e70d6ccc 100644 --- a/services/viewed.py +++ b/services/viewed.py @@ -8,7 +8,12 @@ from typing import Dict # ga from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import DateRange, Dimension, Metric, RunReportRequest +from google.analytics.data_v1beta.types import ( + DateRange, + Dimension, + Metric, + RunReportRequest, +) from orm.author import Author from orm.shout import Shout, ShoutAuthor, ShoutTopic @@ -55,7 +60,9 @@ class ViewedStorage: if os.path.exists(VIEWS_FILEPATH): file_timestamp = os.path.getctime(VIEWS_FILEPATH) - self.start_date = datetime.fromtimestamp(file_timestamp).strftime('%Y-%m-%d') + self.start_date = datetime.fromtimestamp(file_timestamp).strftime( + '%Y-%m-%d' + ) # Запуск фоновой задачи asyncio.create_task(self.worker()) @@ -71,7 +78,9 @@ class ViewedStorage: with open(VIEWS_FILEPATH, 'r') as file: precounted_views = json.load(file) self.views_by_shout.update(precounted_views) - logger.info(f' * {len(precounted_views)} публикаций с просмотрами успешно загружены.') + logger.info( + f' * {len(precounted_views)} публикаций с просмотрами успешно загружены.' + ) except Exception as e: logger.error(f'Ошибка загрузки предварительно подсчитанных просмотров: {e}') @@ -88,14 +97,19 @@ class ViewedStorage: request = RunReportRequest( property=f'properties/{GOOGLE_PROPERTY_ID}', dimensions=[Dimension(name='pagePath')], - metrics=[Metric(name='pageviews')], - date_ranges=[DateRange(start_date=self.start_date, end_date='today')], + metrics=[Metric(name='screenPageViews')], + date_ranges=[ + DateRange(start_date=self.start_date, end_date='today') + ], ) response = self.analytics_client.run_report(request) if response and isinstance(response.rows, list): slugs = set() for row in response.rows: - print(row.dimension_values[0].value, row.metric_values[0].value) + print( + row.dimension_values[0].value, + row.metric_values[0].value, + ) # Извлечение путей страниц из ответа Google Analytics if isinstance(row.dimension_values, list): page_path = row.dimension_values[0].value @@ -103,7 +117,9 @@ class ViewedStorage: views_count = int(row.metric_values[0].value) # Обновление данных в хранилище - self.views_by_shout[slug] = self.views_by_shout.get(slug, 0) + self.views_by_shout[slug] = self.views_by_shout.get( + slug, 0 + ) self.views_by_shout[slug] += views_count self.update_topics(slug) @@ -162,12 +178,20 @@ class ViewedStorage: # Обновление тем и авторов с использованием вспомогательной функции for [_shout_topic, topic] in ( - session.query(ShoutTopic, Topic).join(Topic).join(Shout).where(Shout.slug == shout_slug).all() + session.query(ShoutTopic, Topic) + .join(Topic) + .join(Shout) + .where(Shout.slug == shout_slug) + .all() ): update_groups(self.shouts_by_topic, topic.slug, shout_slug) for [_shout_topic, author] in ( - session.query(ShoutAuthor, Author).join(Author).join(Shout).where(Shout.slug == shout_slug).all() + session.query(ShoutAuthor, Author) + .join(Author) + .join(Shout) + .where(Shout.slug == shout_slug) + .all() ): update_groups(self.shouts_by_author, author.slug, shout_slug) @@ -192,7 +216,10 @@ class ViewedStorage: if failed == 0: when = datetime.now(timezone.utc) + timedelta(seconds=self.period) t = format(when.astimezone().isoformat()) - logger.info(' ⎩ Следующее обновление: %s' % (t.split('T')[0] + ' ' + t.split('T')[1].split('.')[0])) + logger.info( + ' ⎩ Следующее обновление: %s' + % (t.split('T')[0] + ' ' + t.split('T')[1].split('.')[0]) + ) await asyncio.sleep(self.period) else: await asyncio.sleep(10)