1
|
|
|
""" |
2
|
|
|
byceps.metrics.service |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2021 Jochen Kupperschmidt |
6
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
7
|
|
|
""" |
8
|
|
|
|
9
|
1 |
|
from __future__ import annotations |
10
|
1 |
|
from typing import Iterator |
11
|
|
|
|
12
|
1 |
|
from ...services.brand import service as brand_service |
13
|
1 |
|
from ...services.board import ( |
14
|
|
|
board_service, |
15
|
|
|
topic_query_service as board_topic_query_service, |
16
|
|
|
posting_query_service as board_posting_query_service, |
17
|
|
|
) |
18
|
1 |
|
from ...services.consent import consent_service |
19
|
1 |
|
from ...services.metrics.models import Label, Metric |
20
|
1 |
|
from ...services.party.transfer.models import Party |
21
|
1 |
|
from ...services.party import service as party_service |
22
|
1 |
|
from ...services.seating import seat_service |
23
|
1 |
|
from ...services.shop.order import service as order_service |
24
|
1 |
|
from ...services.shop.article import service as shop_article_service |
25
|
1 |
|
from ...services.shop.shop import service as shop_service |
26
|
1 |
|
from ...services.shop.shop.transfer.models import Shop, ShopID |
27
|
1 |
|
from ...services.ticketing import ticket_service |
28
|
1 |
|
from ...services.user import stats_service as user_stats_service |
29
|
1 |
|
from ...typing import BrandID, PartyID |
30
|
|
|
|
31
|
|
|
|
32
|
1 |
|
def serialize(metrics: Iterator[Metric]) -> Iterator[str]: |
33
|
|
|
"""Serialize metric objects to text lines.""" |
34
|
1 |
|
for metric in metrics: |
35
|
1 |
|
yield metric.serialize() + '\n' |
36
|
|
|
|
37
|
|
|
|
38
|
1 |
|
def collect_metrics() -> Iterator[Metric]: |
39
|
1 |
|
brand_ids = [brand.id for brand in brand_service.get_all_brands()] |
40
|
|
|
|
41
|
1 |
|
active_parties = party_service.get_active_parties() |
42
|
1 |
|
active_party_ids = [p.id for p in active_parties] |
43
|
|
|
|
44
|
1 |
|
active_shops = shop_service.get_active_shops() |
45
|
1 |
|
active_shop_ids = {shop.id for shop in active_shops} |
46
|
|
|
|
47
|
1 |
|
yield from _collect_board_metrics(brand_ids) |
48
|
1 |
|
yield from _collect_consent_metrics() |
49
|
1 |
|
yield from _collect_shop_ordered_article_metrics(active_shop_ids) |
50
|
1 |
|
yield from _collect_shop_order_metrics(active_shops) |
51
|
1 |
|
yield from _collect_seating_metrics(active_party_ids) |
52
|
1 |
|
yield from _collect_ticket_metrics(active_parties) |
53
|
1 |
|
yield from _collect_user_metrics() |
54
|
|
|
|
55
|
|
|
|
56
|
1 |
|
def _collect_board_metrics(brand_ids: list[BrandID]) -> Iterator[Metric]: |
57
|
1 |
|
for brand_id in brand_ids: |
58
|
1 |
|
boards = board_service.get_boards_for_brand(brand_id) |
59
|
1 |
|
board_ids = [board.id for board in boards] |
60
|
|
|
|
61
|
1 |
|
for board_id in board_ids: |
62
|
1 |
|
labels = [Label('board', board_id)] |
63
|
|
|
|
64
|
1 |
|
topic_count = board_topic_query_service.count_topics_for_board( |
65
|
|
|
board_id |
66
|
|
|
) |
67
|
1 |
|
yield Metric('board_topic_count', topic_count, labels=labels) |
68
|
|
|
|
69
|
1 |
|
posting_count = ( |
70
|
|
|
board_posting_query_service.count_postings_for_board(board_id) |
71
|
|
|
) |
72
|
1 |
|
yield Metric('board_posting_count', posting_count, labels=labels) |
73
|
|
|
|
74
|
|
|
|
75
|
1 |
|
def _collect_consent_metrics() -> Iterator[Metric]: |
76
|
1 |
|
consents_per_subject = consent_service.count_consents_by_subject() |
77
|
1 |
|
for subject_name, consent_count in consents_per_subject.items(): |
78
|
|
|
yield Metric( |
79
|
|
|
'consent_count', |
80
|
|
|
consent_count, |
81
|
|
|
labels=[Label('subject_name', subject_name)], |
82
|
|
|
) |
83
|
|
|
|
84
|
|
|
|
85
|
1 |
|
def _collect_shop_ordered_article_metrics( |
86
|
|
|
shop_ids: set[ShopID], |
87
|
|
|
) -> Iterator[Metric]: |
88
|
|
|
"""Provide ordered article quantities for shops.""" |
89
|
1 |
|
stats = shop_article_service.sum_ordered_articles_by_payment_state(shop_ids) |
90
|
|
|
|
91
|
1 |
|
for shop_id, article_number, description, payment_state, quantity in stats: |
92
|
|
|
yield Metric( |
93
|
|
|
'shop_ordered_article_quantity', |
94
|
|
|
quantity, |
95
|
|
|
labels=[ |
96
|
|
|
Label('shop', shop_id), |
97
|
|
|
Label('article_number', article_number), |
98
|
|
|
Label('description', description), |
99
|
|
|
Label('payment_state', payment_state.name), |
100
|
|
|
], |
101
|
|
|
) |
102
|
|
|
|
103
|
|
|
|
104
|
1 |
|
def _collect_shop_order_metrics(shops: list[Shop]) -> Iterator[Metric]: |
105
|
|
|
"""Provide order counts grouped by payment state for shops.""" |
106
|
1 |
|
for shop in shops: |
107
|
|
|
order_counts_per_payment_state = ( |
108
|
|
|
order_service.count_orders_per_payment_state(shop.id) |
109
|
|
|
) |
110
|
|
|
|
111
|
|
|
for payment_state, quantity in order_counts_per_payment_state.items(): |
112
|
|
|
yield Metric( |
113
|
|
|
'shop_order_quantity', |
114
|
|
|
quantity, |
115
|
|
|
labels=[ |
116
|
|
|
Label('shop', shop.id), |
117
|
|
|
Label('payment_state', payment_state.name), |
118
|
|
|
], |
119
|
|
|
) |
120
|
|
|
|
121
|
|
|
|
122
|
1 |
|
def _collect_seating_metrics( |
123
|
|
|
active_party_ids: list[PartyID], |
124
|
|
|
) -> Iterator[Metric]: |
125
|
|
|
"""Provide seat occupation counts per party and category.""" |
126
|
1 |
|
for party_id in active_party_ids: |
127
|
1 |
|
occupied_seat_counts_by_category = ( |
128
|
|
|
seat_service.count_occupied_seats_by_category(party_id) |
129
|
|
|
) |
130
|
|
|
|
131
|
1 |
|
for category, count in occupied_seat_counts_by_category: |
132
|
1 |
|
yield Metric( |
133
|
|
|
'occupied_seat_count', |
134
|
|
|
count, |
135
|
|
|
labels=[ |
136
|
|
|
Label('party', party_id), |
137
|
|
|
Label('category_title', category.title), |
138
|
|
|
], |
139
|
|
|
) |
140
|
|
|
|
141
|
|
|
|
142
|
1 |
|
def _collect_ticket_metrics(active_parties: list[Party]) -> Iterator[Metric]: |
143
|
|
|
"""Provide ticket counts for active parties.""" |
144
|
1 |
|
for party in active_parties: |
145
|
1 |
|
party_id = party.id |
146
|
1 |
|
labels = [Label('party', party_id)] |
147
|
|
|
|
148
|
1 |
|
max_ticket_quantity = party.max_ticket_quantity |
149
|
1 |
|
if max_ticket_quantity is not None: |
150
|
1 |
|
yield Metric('tickets_max', max_ticket_quantity, labels=labels) |
151
|
|
|
|
152
|
1 |
|
tickets_revoked_count = ticket_service.count_revoked_tickets_for_party( |
153
|
|
|
party_id |
154
|
|
|
) |
155
|
1 |
|
yield Metric( |
156
|
|
|
'tickets_revoked_count', tickets_revoked_count, labels=labels |
157
|
|
|
) |
158
|
|
|
|
159
|
1 |
|
tickets_sold_count = ticket_service.count_sold_tickets_for_party( |
160
|
|
|
party_id |
161
|
|
|
) |
162
|
1 |
|
yield Metric('tickets_sold_count', tickets_sold_count, labels=labels) |
163
|
|
|
|
164
|
1 |
|
tickets_checked_in_count = ( |
165
|
|
|
ticket_service.count_tickets_checked_in_for_party(party_id) |
166
|
|
|
) |
167
|
1 |
|
yield Metric( |
168
|
|
|
'tickets_checked_in_count', tickets_checked_in_count, labels=labels |
169
|
|
|
) |
170
|
|
|
|
171
|
|
|
|
172
|
1 |
|
def _collect_user_metrics() -> Iterator[Metric]: |
173
|
1 |
|
users_active = user_stats_service.count_active_users() |
174
|
1 |
|
users_uninitialized = user_stats_service.count_uninitialized_users() |
175
|
1 |
|
users_suspended = user_stats_service.count_suspended_users() |
176
|
1 |
|
users_deleted = user_stats_service.count_deleted_users() |
177
|
1 |
|
users_total = user_stats_service.count_users() |
178
|
|
|
|
179
|
1 |
|
yield Metric('users_active_count', users_active) |
180
|
1 |
|
yield Metric('users_uninitialized_count', users_uninitialized) |
181
|
1 |
|
yield Metric('users_suspended_count', users_suspended) |
182
|
1 |
|
yield Metric('users_deleted_count', users_deleted) |
183
|
|
|
yield Metric('users_total_count', users_total) |
184
|
|
|
|