Commit 47f1894d authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Automatically add API requests to the perf bar

Ajax requests to API endpoints now show up in the performance bar
request dropdown automatically.
parent 1a7b8741
...@@ -32,10 +32,9 @@ export default class PerformanceBarService { ...@@ -32,10 +32,9 @@ export default class PerformanceBarService {
// Get the request URL from response.config for Axios, and response for // Get the request URL from response.config for Axios, and response for
// Vue Resource. // Vue Resource.
const requestUrl = (response.config || response).url; const requestUrl = (response.config || response).url;
const apiRequest = requestUrl && requestUrl.match(/^\/api\//);
const cachedResponse = const cachedResponse =
response.headers && parseBoolean(response.headers['x-gitlab-from-cache']); response.headers && parseBoolean(response.headers['x-gitlab-from-cache']);
const fireCallback = requestUrl !== peekUrl && requestId && !apiRequest && !cachedResponse; const fireCallback = requestUrl !== peekUrl && Boolean(requestId) && !cachedResponse;
return [fireCallback, requestId, requestUrl]; return [fireCallback, requestId, requestUrl];
} }
......
---
title: Automatically add AJAX API requests to the performance bar
merge_request: 39069
author:
type: added
...@@ -8,19 +8,13 @@ describe('PerformanceBarService', () => { ...@@ -8,19 +8,13 @@ describe('PerformanceBarService', () => {
} }
it('returns false when the request URL is the peek URL', () => { it('returns false when the request URL is the peek URL', () => {
expect( expect(fireCallback({ headers: { 'x-request-id': '123' }, url: '/peek' }, '/peek')).toBe(
fireCallback({ headers: { 'x-request-id': '123' }, url: '/peek' }, '/peek'), false,
).toBeFalsy(); );
}); });
it('returns false when there is no request ID', () => { it('returns false when there is no request ID', () => {
expect(fireCallback({ headers: {}, url: '/request' }, '/peek')).toBeFalsy(); expect(fireCallback({ headers: {}, url: '/request' }, '/peek')).toBe(false);
});
it('returns false when the request is an API request', () => {
expect(
fireCallback({ headers: { 'x-request-id': '123' }, url: '/api/' }, '/peek'),
).toBeFalsy();
}); });
it('returns false when the response is from the cache', () => { it('returns false when the response is from the cache', () => {
...@@ -29,13 +23,19 @@ describe('PerformanceBarService', () => { ...@@ -29,13 +23,19 @@ describe('PerformanceBarService', () => {
{ headers: { 'x-request-id': '123', 'x-gitlab-from-cache': 'true' }, url: '/request' }, { headers: { 'x-request-id': '123', 'x-gitlab-from-cache': 'true' }, url: '/request' },
'/peek', '/peek',
), ),
).toBeFalsy(); ).toBe(false);
}); });
it('returns true when all conditions are met', () => { it('returns true when the request is an API request', () => {
expect( expect(fireCallback({ headers: { 'x-request-id': '123' }, url: '/api/' }, '/peek')).toBe(
fireCallback({ headers: { 'x-request-id': '123' }, url: '/request' }, '/peek'), true,
).toBeTruthy(); );
});
it('returns true for all other requests', () => {
expect(fireCallback({ headers: { 'x-request-id': '123' }, url: '/request' }, '/peek')).toBe(
true,
);
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment