blob: 2db9f306b97809b61f8aa819810cde0fa39acc6b [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2018 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Copybara854996b2021-09-07 19:36:02 +00004
5"""ts_mon JavaScript proxy handler."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10from framework import authdata
11from framework import sql
12from framework import xsrf
13
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020014from gae_ts_mon.flask_handlers import TSMonJSFlaskHandler
Copybara854996b2021-09-07 19:36:02 +000015
16from google.appengine.api import users
17
18from infra_libs import ts_mon
19
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020020import flask
Copybara854996b2021-09-07 19:36:02 +000021
22STANDARD_FIELDS = [
23 ts_mon.StringField('client_id'),
24 ts_mon.StringField('host_name'),
25 ts_mon.BooleanField('document_visible'),
26]
27
28
29# User action metrics.
30ISSUE_CREATE_LATENCY_METRIC = ts_mon.CumulativeDistributionMetric(
31 'monorail/frontend/issue_create_latency', (
32 'Latency between Issue Entry form submission and page load of '
33 'the subsequent issue page.'
34 ), field_spec=STANDARD_FIELDS,
35 units=ts_mon.MetricsDataUnits.MILLISECONDS)
36ISSUE_UPDATE_LATENCY_METRIC = ts_mon.CumulativeDistributionMetric(
37 'monorail/frontend/issue_update_latency', (
38 'Latency between Issue Update form submission and page load of '
39 'the subsequent issue page.'
40 ), field_spec=STANDARD_FIELDS,
41 units=ts_mon.MetricsDataUnits.MILLISECONDS)
42AUTOCOMPLETE_POPULATE_LATENCY_METRIC = ts_mon.CumulativeDistributionMetric(
43 'monorail/frontend/autocomplete_populate_latency', (
44 'Latency between page load and autocomplete options loading.'
45 ), field_spec=STANDARD_FIELDS,
46 units=ts_mon.MetricsDataUnits.MILLISECONDS)
47CHARTS_SWITCH_DATE_RANGE_METRIC = ts_mon.CounterMetric(
48 'monorail/frontend/charts/switch_date_range', (
49 'Number of times user clicks frequency button.'
50 ), field_spec=STANDARD_FIELDS + [ts_mon.IntegerField('date_range')])
51
52# Page load metrics.
53ISSUE_COMMENTS_LOAD_EXTRA_FIELDS = [
54 ts_mon.StringField('template_name'),
55 ts_mon.BooleanField('full_app_load'),
56]
57ISSUE_COMMENTS_LOAD_LATENCY_METRIC = ts_mon.CumulativeDistributionMetric(
58 'monorail/frontend/issue_comments_load_latency', (
59 'Time from navigation or click to issue comments loaded.'
60 ), field_spec=STANDARD_FIELDS + ISSUE_COMMENTS_LOAD_EXTRA_FIELDS,
61 units=ts_mon.MetricsDataUnits.MILLISECONDS)
62DOM_CONTENT_LOADED_EXTRA_FIELDS = [
63 ts_mon.StringField('template_name')]
64DOM_CONTENT_LOADED_METRIC = ts_mon.CumulativeDistributionMetric(
65 'frontend/dom_content_loaded', (
66 'domContentLoaded performance timing.'
67 ), field_spec=STANDARD_FIELDS + DOM_CONTENT_LOADED_EXTRA_FIELDS,
68 units=ts_mon.MetricsDataUnits.MILLISECONDS)
69
70
71ISSUE_LIST_LOAD_EXTRA_FIELDS = [
72 ts_mon.StringField('template_name'),
73 ts_mon.BooleanField('full_app_load'),
74]
75ISSUE_LIST_LOAD_LATENCY_METRIC = ts_mon.CumulativeDistributionMetric(
76 'monorail/frontend/issue_list_load_latency', (
77 'Time from navigation or click to search issues list loaded.'
78 ), field_spec=STANDARD_FIELDS + ISSUE_LIST_LOAD_EXTRA_FIELDS,
79 units=ts_mon.MetricsDataUnits.MILLISECONDS)
80
81
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010082class MonorailTSMonJSHandler(TSMonJSFlaskHandler):
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020083
84 def __init__(self, services=None):
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010085 super(MonorailTSMonJSHandler, self).__init__(
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020086 flask=flask, services=services)
87 self.register_metrics(
88 [
89 ISSUE_CREATE_LATENCY_METRIC, ISSUE_UPDATE_LATENCY_METRIC,
90 AUTOCOMPLETE_POPULATE_LATENCY_METRIC,
91 CHARTS_SWITCH_DATE_RANGE_METRIC, ISSUE_COMMENTS_LOAD_LATENCY_METRIC,
92 DOM_CONTENT_LOADED_METRIC, ISSUE_LIST_LOAD_LATENCY_METRIC
93 ])
94
95 def xsrf_is_valid(self, body):
96 """This method expects the body dictionary to include two fields:
97 `token` and `user_id`.
98 """
99 cnxn = sql.MonorailConnection()
100 token = body.get('token')
101 user = users.get_current_user()
102 email = user.email() if user else None
103
104 services = self.service
105 auth = authdata.AuthData.FromEmail(cnxn, email, services, autocreate=False)
106 try:
107 xsrf.ValidateToken(token, auth.user_id, xsrf.XHR_SERVLET_PATH)
108 return True
109 except xsrf.TokenIncorrect:
110 return False
111
112 def PostMonorailTSMonJSHandler(self):
113 return self.post()