Skip to content

Commit

Permalink
Rename variables
Browse files Browse the repository at this point in the history
Signed-off-by: Olga Bulat <[email protected]>
  • Loading branch information
obulat committed Apr 8, 2024
1 parent 109365e commit e8d129e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
16 changes: 8 additions & 8 deletions frontend/src/data/media-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { Events } from "~/types/analytics"

import type { AxiosResponse } from "axios"

export type SearchTimeEvent =
export type SearchTimeEventPayload =
| Events["AUDIO_SEARCH_RESPONSE_TIME"]
| Events["IMAGE_SEARCH_RESPONSE_TIME"]

Expand All @@ -34,15 +34,15 @@ class MediaService<T extends Media> {
}

/**
* Processes AxiosResponse from a search query to
* construct SEARCH_RESPONSE_TIME analytics event.
* Processes AxiosResponse from a search query to construct
* SEARCH_RESPONSE_TIME analytics event payload.
* @param response - Axios response
* @param requestDatetime - datetime before request was sent
*/
buildSearchTimeEvent(
buildEventPayload(
response: AxiosResponse,
requestDatetime: Date
): SearchTimeEvent | undefined {
): SearchTimeEventPayload | undefined {
const REQUIRED_HEADERS = ["date", "cf-cache-status", "cf-ray"]

const responseHeaders = response.headers
Expand Down Expand Up @@ -107,7 +107,7 @@ class MediaService<T extends Media> {
async search(
params: PaginatedSearchQuery | PaginatedCollectionQuery
): Promise<{
searchTimeEvent: SearchTimeEvent | undefined
eventPayload: SearchTimeEventPayload | undefined
data: MediaResult<Record<string, Media>>
}> {
// Add the `peaks` param to all audio searches automatically
Expand All @@ -122,9 +122,9 @@ class MediaService<T extends Media> {
params as unknown as Record<string, string>
)

const searchTimeEvent = this.buildSearchTimeEvent(res, requestDatetime)
const eventPayload = this.buildEventPayload(res, requestDatetime)

return { searchTimeEvent, data: this.transformResults(res.data) }
return { eventPayload, data: this.transformResults(res.data) }
}

/**
Expand Down
14 changes: 7 additions & 7 deletions frontend/src/stores/media/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface SearchFetchState extends Omit<FetchState, "hasStarted"> {
hasStarted: boolean
}
import type { EventName } from "~/types/analytics"
import type { SearchTimeEvent } from "~/data/media-service"
import type { SearchTimeEventPayload } from "~/data/media-service"

export type MediaStoreResult = {
count: number
Expand Down Expand Up @@ -451,13 +451,13 @@ export const useMediaStore = defineStore("media", {
},

recordSearchTime(
event: SearchTimeEvent | undefined,
payload: SearchTimeEventPayload | undefined,
mediaType: SupportedMediaType
) {
if (event) {
const searchEvent =
if (payload) {
const eventName =
`${mediaType.toUpperCase()}_SEARCH_RESPONSE_TIME` as EventName
this.$nuxt.$sendCustomEvent(searchEvent, event)
this.$nuxt.$sendCustomEvent(eventName, payload)
}
},

Expand All @@ -483,8 +483,8 @@ export const useMediaStore = defineStore("media", {
try {
const accessToken = this.$nuxt.$openverseApiToken
const service = initServices[mediaType](accessToken)
const { searchTimeEvent, data } = await service.search(queryParams)
this.recordSearchTime(searchTimeEvent, mediaType)
const { eventPayload, data } = await service.search(queryParams)
this.recordSearchTime(eventPayload, mediaType)
const mediaCount = data.result_count
let errorData: FetchingError | undefined
/**
Expand Down
10 changes: 5 additions & 5 deletions frontend/test/unit/specs/data/media-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("Media Service search and recordSearchTime", () => {

const res = await initServices.image().search({})

expect(res.searchTimeEvent).not.toBeDefined()
expect(res.eventPayload).not.toBeDefined()
})

it("should not send a SEARCH_RESPONSE_TIME analytics event if the response was locally cached", async () => {
Expand All @@ -45,7 +45,7 @@ describe("Media Service search and recordSearchTime", () => {

const res = await initServices.audio().search({})

expect(res.searchTimeEvent).not.toBeDefined()
expect(res.eventPayload).not.toBeDefined()
})

it("should not send a SEARCH_RESPONSE_TIME analytics event if the cf-ray is malformed", async () => {
Expand All @@ -67,7 +67,7 @@ describe("Media Service search and recordSearchTime", () => {

const res = await initServices.audio().search({})

expect(res.searchTimeEvent).not.toBeDefined()
expect(res.eventPayload).not.toBeDefined()
})

it("should send SEARCH_RESPONSE_TIME analytics with correct parameters", async () => {
Expand Down Expand Up @@ -106,7 +106,7 @@ describe("Media Service search and recordSearchTime", () => {
const IMAGE_QUERY_PARAMS = { q: "apple" }
const imageRes = await initServices.image().search(IMAGE_QUERY_PARAMS)

expect(imageRes.searchTimeEvent).toEqual({
expect(imageRes.eventPayload).toEqual({
cfCacheStatus: "HIT",
cfRayIATA: "SJC",
elapsedTime: 2,
Expand All @@ -116,7 +116,7 @@ describe("Media Service search and recordSearchTime", () => {
const AUDIO_QUERY_PARAMS = { q: "table" }
const audioRes = await initServices.audio().search(AUDIO_QUERY_PARAMS)

expect(audioRes.searchTimeEvent).toEqual({
expect(audioRes.eventPayload).toEqual({
cfCacheStatus: "MISS",
cfRayIATA: "LHR",
elapsedTime: 3,
Expand Down

0 comments on commit e8d129e

Please sign in to comment.