|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { getOperationName } from '@apollo/client/utilities'; |
| 6 | +import { Inject, Injectable } from '@nestjs/common'; |
| 7 | +import { StatsD } from 'hot-shots'; |
| 8 | + |
| 9 | +import { StatsDService } from '@fxa/shared/metrics/statsd'; |
| 10 | +import { defaultCmsQuery } from '@fxa/shared/cms'; |
| 11 | +import { StrapiClient, StrapiClientEventResponse } from './strapi.client'; |
| 12 | +import { LOGGER_PROVIDER } from '@fxa/shared/log'; |
| 13 | +import type { Logger } from '@fxa/shared/log'; |
| 14 | + |
| 15 | +@Injectable() |
| 16 | +export class DefaultCmsConfigurationManager { |
| 17 | + constructor( |
| 18 | + private strapiClient: StrapiClient, |
| 19 | + @Inject(StatsDService) |
| 20 | + private statsd: StatsD, |
| 21 | + @Inject(LOGGER_PROVIDER) private readonly log: Logger |
| 22 | + ) { |
| 23 | + if (this.strapiClient.on) { |
| 24 | + this.strapiClient.on('response', this.onStrapiClientResponse.bind(this)); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + onStrapiClientResponse(response: StrapiClientEventResponse) { |
| 29 | + const defaultTags = { |
| 30 | + method: response.method, |
| 31 | + error: response.error ? 'true' : 'false', |
| 32 | + cache: `${response.cache}`, |
| 33 | + cacheType: `${response.cacheType}`, |
| 34 | + }; |
| 35 | + const operationName = response.query && getOperationName(response.query); |
| 36 | + const tags = operationName |
| 37 | + ? { ...defaultTags, operationName } |
| 38 | + : defaultTags; |
| 39 | + if (response.error) { |
| 40 | + this.log.error('cms.default.requestFailed', { |
| 41 | + operationName, |
| 42 | + error: response.error, |
| 43 | + }); |
| 44 | + } |
| 45 | + this.statsd.timing( |
| 46 | + 'cms_default_request', |
| 47 | + response.elapsed, |
| 48 | + undefined, |
| 49 | + tags |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + async fetchDefault() { |
| 54 | + return await this.strapiClient.query(defaultCmsQuery, {}); |
| 55 | + } |
| 56 | + |
| 57 | + async invalidateCache() { |
| 58 | + return await this.strapiClient.invalidateQueryCache(defaultCmsQuery, {}); |
| 59 | + } |
| 60 | +} |
0 commit comments