Debian Codegraph @

skins/Vector/resources/skins.vector.search/instrumentation.js

raw symbols SCIP
1 /**
2 * The value of the `inputLocation` property of any and all SearchSatisfaction events sent by the
3 * corresponding instrumentation.
4 *
5 * @see https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/skins/Vector/+/refs/heads/master/includes/Constants.php
6 */
7 const INPUT_LOCATION_MOVED = 'header-moved',
8 // T251544: Collect search performance metrics to compare Vue search with
9 // mediawiki.searchSuggest performance. Marks and Measures will only be
10 // recorded on the Vector skin and only if browser supported.
11 shouldTestSearchPerformance = !!( window.performance &&
12 !!window.requestAnimationFrame &&
13 !!performance.mark &&
14 !!performance.measure &&
15 !!performance.getEntriesByName &&
16 performance.clearMarks ),
17 loadStartMark = 'mwVectorVueSearchLoadStart',
18 queryMark = 'mwVectorVueSearchQuery',
19 renderMark = 'mwVectorVueSearchRender',
20 queryToRenderMeasure = 'mwVectorVueSearchQueryToRender',
21 loadStartToFirstRenderMeasure = 'mwVectorVueSearchLoadStartToFirstRender';
22
23 function onFetchStart() {
24 if ( !shouldTestSearchPerformance ) {
25 return;
26 }
27
28 // Clear past marks that are no longer relevant. This likely means that the
29 // search request failed or was cancelled. Whatever the reason, the mark
30 // is no longer needed since we are only interested in collecting the time
31 // from query to render.
32 if ( performance.getEntriesByName( queryMark ).length ) {
33 performance.clearMarks( queryMark );
34 }
35
36 performance.mark( queryMark );
37 }
38
39 /**
40 * @param {FetchEndEvent} event
41 */
42 function onFetchEnd( event ) {
43 mw.track( 'mediawiki.searchSuggest', {
44 action: 'impression-results',
45 numberOfResults: event.numberOfResults,
46 // resultSetType: '',
47 // searchId: '',
48 query: event.query,
49 inputLocation: INPUT_LOCATION_MOVED
50 } );
51
52 if ( shouldTestSearchPerformance ) {
53 // Schedule the mark after the search results have rendered and are
54 // visible to the user. Two rAF's are needed for this since rAF will
55 // execute before the rendering steps happen (e.g. layout and paint). A
56 // nested rAF will execute after these rendering steps have completed
57 // and ensure the search results are visible to the user.
58 requestAnimationFrame( () => {
59 requestAnimationFrame( () => {
60 if ( !performance.getEntriesByName( queryMark ).length ) {
61 return;
62 }
63
64 performance.mark( renderMark );
65 performance.measure( queryToRenderMeasure, queryMark, renderMark );
66
67 // Measure from the start of the lazy load to the first render if we
68 // haven't already captured that info.
69 if ( performance.getEntriesByName( loadStartMark ).length &&
70 !performance.getEntriesByName( loadStartToFirstRenderMeasure ).length ) {
71 performance.measure( loadStartToFirstRenderMeasure, loadStartMark, renderMark );
72 }
73
74 // The measures are the most meaningful info so we remove the marks
75 // after we have the measure.
76 performance.clearMarks( queryMark );
77 performance.clearMarks( renderMark );
78 } );
79 } );
80 }
81 }
82
83 /**
84 * @param {SuggestionClickEvent|SearchSubmitEvent} event
85 */
86 function onSuggestionClick( event ) {
87 mw.track( 'mediawiki.searchSuggest', {
88 action: 'click-result',
89 numberOfResults: event.numberOfResults,
90 index: event.index
91 } );
92 }
93
94 /**
95 * Generates the value of the `wprov` parameter to be used in the URL of a search result and the
96 * `wprov` hidden input.
97 *
98 * See https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/WikimediaEvents/+/refs/heads/master/modules/ext.wikimediaEvents/searchSatisfaction.js
99 * and also the top of that file for additional detail about the shape of the parameter.
100 *
101 * @param {number} index
102 * @return {string}
103 */
104 function getWprovFromResultIndex( index ) {
105 // result looks like: acrw1_0, acrw1_1, acrw1_2, etc.;
106 // or acrw1_-1 for index -1 (user did not highlight an autocomplete result)
107 return 'acrw1_' + index;
108 }
109
110 /**
111 * @typedef {Object} SearchResultPartial
112 * @property {string} title
113 * @property {string} [url]
114 */
115
116 /**
117 * Return a new list of search results,
118 * with the `wprov` parameter added to each result's url (if any).
119 *
120 * @param {SearchResultPartial[]} results Not modified.
121 * @param {number} offset Offset to add to the index of each result.
122 * @return {SearchResultPartial[]}
123 */
124 function addWprovToSearchResultUrls( results, offset ) {
125 return results.map( ( result, index ) => {
126 if ( result.url ) {
127 const url = new URL( result.url, location.href );
128 url.searchParams.set( 'wprov', getWprovFromResultIndex( index + offset ) );
129 result = Object.assign( {}, result, { url: url.toString() } );
130 }
131 return result;
132 } );
133 }
134
135 /**
136 * @typedef {Object} Instrumentation
137 * @property {Object} listeners
138 * @property {Function} getWprovFromResultIndex
139 * @property {Function} addWprovToSearchResultUrls
140 */
141
142 /**
143 * @type {Instrumentation}
144 */
145 module.exports = {
146 listeners: {
147 onFetchStart,
148 onFetchEnd,
149 onSuggestionClick,
150
151 // As of writing (2020/12/08), both the "click-result" and "submit-form" kind of
152 // mediawiki.searchSuggestion events result in a "click" SearchSatisfaction event being
153 // logged [0]. However, when processing the "submit-form" kind of mediawiki.searchSuggestion
154 // event, the SearchSatisfaction instrument will modify the DOM, adding a hidden input
155 // element, in order to set the appropriate provenance parameter (see [1] for additional
156 // detail).
157 //
158 // In this implementation of the mediawiki.searchSuggestion protocol, we don't want to
159 // trigger the above behavior as we're using Vue.js, which doesn't expect the DOM to be
160 // modified underneath it.
161 //
162 // [0] https://gerrit.wikimedia.org/g/mediawiki/extensions/WikimediaEvents/+/df97aa9c9407507e8c48827666beeab492fd56a8/modules/ext.wikimediaEvents/searchSatisfaction.js#735
163 // [1] https://phabricator.wikimedia.org/T257698#6416826
164 onSubmit: onSuggestionClick
165 },
166 getWprovFromResultIndex,
167 addWprovToSearchResultUrls
168 };