ga-metric-fieldname-fix
All checks were successful
Deploy to core / deploy (push) Successful in 1m44s
All checks were successful
Deploy to core / deploy (push) Successful in 1m44s
This commit is contained in:
parent
ef9fbe7c88
commit
c90b0bd994
|
@ -8,7 +8,12 @@ from typing import Dict
|
||||||
|
|
||||||
# ga
|
# ga
|
||||||
from google.analytics.data_v1beta import BetaAnalyticsDataClient
|
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.author import Author
|
||||||
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
||||||
|
@ -55,7 +60,9 @@ class ViewedStorage:
|
||||||
|
|
||||||
if os.path.exists(VIEWS_FILEPATH):
|
if os.path.exists(VIEWS_FILEPATH):
|
||||||
file_timestamp = os.path.getctime(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())
|
asyncio.create_task(self.worker())
|
||||||
|
@ -71,7 +78,9 @@ class ViewedStorage:
|
||||||
with open(VIEWS_FILEPATH, 'r') as file:
|
with open(VIEWS_FILEPATH, 'r') as file:
|
||||||
precounted_views = json.load(file)
|
precounted_views = json.load(file)
|
||||||
self.views_by_shout.update(precounted_views)
|
self.views_by_shout.update(precounted_views)
|
||||||
logger.info(f' * {len(precounted_views)} публикаций с просмотрами успешно загружены.')
|
logger.info(
|
||||||
|
f' * {len(precounted_views)} публикаций с просмотрами успешно загружены.'
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f'Ошибка загрузки предварительно подсчитанных просмотров: {e}')
|
logger.error(f'Ошибка загрузки предварительно подсчитанных просмотров: {e}')
|
||||||
|
|
||||||
|
@ -88,14 +97,19 @@ class ViewedStorage:
|
||||||
request = RunReportRequest(
|
request = RunReportRequest(
|
||||||
property=f'properties/{GOOGLE_PROPERTY_ID}',
|
property=f'properties/{GOOGLE_PROPERTY_ID}',
|
||||||
dimensions=[Dimension(name='pagePath')],
|
dimensions=[Dimension(name='pagePath')],
|
||||||
metrics=[Metric(name='pageviews')],
|
metrics=[Metric(name='screenPageViews')],
|
||||||
date_ranges=[DateRange(start_date=self.start_date, end_date='today')],
|
date_ranges=[
|
||||||
|
DateRange(start_date=self.start_date, end_date='today')
|
||||||
|
],
|
||||||
)
|
)
|
||||||
response = self.analytics_client.run_report(request)
|
response = self.analytics_client.run_report(request)
|
||||||
if response and isinstance(response.rows, list):
|
if response and isinstance(response.rows, list):
|
||||||
slugs = set()
|
slugs = set()
|
||||||
for row in response.rows:
|
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
|
# Извлечение путей страниц из ответа Google Analytics
|
||||||
if isinstance(row.dimension_values, list):
|
if isinstance(row.dimension_values, list):
|
||||||
page_path = row.dimension_values[0].value
|
page_path = row.dimension_values[0].value
|
||||||
|
@ -103,7 +117,9 @@ class ViewedStorage:
|
||||||
views_count = int(row.metric_values[0].value)
|
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.views_by_shout[slug] += views_count
|
||||||
self.update_topics(slug)
|
self.update_topics(slug)
|
||||||
|
|
||||||
|
@ -162,12 +178,20 @@ class ViewedStorage:
|
||||||
|
|
||||||
# Обновление тем и авторов с использованием вспомогательной функции
|
# Обновление тем и авторов с использованием вспомогательной функции
|
||||||
for [_shout_topic, topic] in (
|
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)
|
update_groups(self.shouts_by_topic, topic.slug, shout_slug)
|
||||||
|
|
||||||
for [_shout_topic, author] in (
|
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)
|
update_groups(self.shouts_by_author, author.slug, shout_slug)
|
||||||
|
|
||||||
|
@ -192,7 +216,10 @@ class ViewedStorage:
|
||||||
if failed == 0:
|
if failed == 0:
|
||||||
when = datetime.now(timezone.utc) + timedelta(seconds=self.period)
|
when = datetime.now(timezone.utc) + timedelta(seconds=self.period)
|
||||||
t = format(when.astimezone().isoformat())
|
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)
|
await asyncio.sleep(self.period)
|
||||||
else:
|
else:
|
||||||
await asyncio.sleep(10)
|
await asyncio.sleep(10)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user