-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0380b01
commit e6d6681
Showing
13 changed files
with
354 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// getCommentData fetches Zendesk comment data from the data lake via Data Query API. | ||
// https://internal-docs.vtex.com/Analytics/VTEX-Data-Platform/data-query-API/ | ||
// DQAPI Metric: | ||
// https://github.com/vtex/analytics-platform/blob/main/lambdas/functions/data_consumption_entrypoint_v2/metrics/docs-in-tickets.yaml | ||
|
||
import { ExternalClient } from '@vtex/api' | ||
import type { IOContext, InstanceOptions } from '@vtex/api' | ||
|
||
// These types must be `string | string[]` because technically querystrings can be arrays of strings. But we plan to deal only with simple strings. | ||
export interface fetchCommentsParams { | ||
startDate: string | string[], | ||
endDate: string | string[], | ||
containsHelpArticle?: string | string[], | ||
containsDevArticle?: string | string[], | ||
articleUrl?: string | string[] | ||
} | ||
|
||
export default class DataQueryClient extends ExternalClient { | ||
constructor(ctx: IOContext, options?: InstanceOptions) { | ||
super(`https://data-consumption.vtex.com`, ctx, { | ||
...options, | ||
retries: 2, | ||
timeout: 6000, | ||
headers: { | ||
...options?.headers | ||
} | ||
}) | ||
} | ||
|
||
public async fetchCommentData (params: fetchCommentsParams, token: string | undefined | string[]) { | ||
|
||
// Assembling URL considering optional query params | ||
var queryParams = `an=vtexhelp&startDate=${params.startDate}&endDate=${params.endDate}` | ||
|
||
if (params.containsHelpArticle) { | ||
queryParams = `${queryParams}&contains_help_article=${params.containsHelpArticle}` | ||
} | ||
if (params.containsDevArticle) { | ||
queryParams = `${queryParams}&contains_dev_article=${params.containsDevArticle}` | ||
} | ||
if (params.articleUrl) { | ||
queryParams = `${queryParams}&article_url=${params.articleUrl}` | ||
} | ||
|
||
const url = `https://data-consumption.vtex.com/api/analytics/consumption/docs-in-tickets?${queryParams}` | ||
|
||
console.log(url) | ||
|
||
// Making the request | ||
const dataQuery = this.http.get(url, | ||
{ | ||
headers: { | ||
cookie: `VtexIdclientAutCookie=${token}` | ||
} | ||
} | ||
) | ||
return dataQuery | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Getting comment data from the Data Lake | ||
|
||
import { returnErrorUrl } from '../middlewares/errorLogs' | ||
|
||
export async function getCommentData( | ||
ctx: Context, | ||
next: () => Promise<Record<string, unknown>> | ||
) { | ||
console.log('Running getCommentData') | ||
|
||
const dataQuery = ctx.clients.dataQuery | ||
|
||
const params = ctx.state.body | ||
try{ | ||
const dataQueryResponse = await dataQuery.fetchCommentData(params, ctx.request.headers.vtexidclientautcookie) | ||
|
||
// Passing the data to the body to get it in other middlewares | ||
ctx.state.body = { | ||
articleData: dataQueryResponse, | ||
queryParams: params | ||
} | ||
|
||
} catch (error) { | ||
returnErrorUrl(params.articleUrl, 500, `Error trying to get article data from the DQAPI >>> ${error}`, ctx) | ||
console.log('error: '+error) | ||
return | ||
} | ||
|
||
await next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Getting comment data from the Data Lake | ||
|
||
interface Comment { | ||
ticketid: string | ||
commentid: number | ||
authorid: number | ||
createdat: string | ||
containshelparticle: boolean | ||
containsdevarticle: boolean | ||
numberofdocsportalsurls: number | ||
docsportalsurls: string[] | ||
numberofarticleurls: number | ||
articleurls: string[] | ||
} | ||
|
||
export async function processCommentData( | ||
ctx: Context, | ||
next: () => Promise<Record<string, unknown>> | ||
) { | ||
console.log('Running processUniqueTicketIds') | ||
|
||
const articleData = ctx.state.body.articleData | ||
|
||
// Get unique tickets | ||
const uniqueTicketIds = [...new Set(articleData.map((item: Comment) => item.ticketid))]; | ||
|
||
// Calculate number of tickets comments given that "empty" means an empty object in the array and still counts as 1. | ||
var numberOfComments = 0 | ||
var numberOfTickets | ||
if (articleData[0].ticketid) { | ||
numberOfComments = articleData.length | ||
numberOfTickets = uniqueTicketIds.length | ||
} | ||
|
||
// Set a max quantity and return a sample of the comments. The goal is to set up pagination to return all results, but first we must set up pagination in the DQAPI metric. | ||
const sampleSize = 20 | ||
var sampleQuantity = 0 | ||
var sampleData: Comment[] = [] | ||
if (numberOfComments > sampleSize) { | ||
sampleQuantity = sampleSize, | ||
sampleData = articleData.slice(0,sampleSize) | ||
} else { | ||
sampleQuantity = numberOfComments, | ||
sampleData = articleData | ||
} | ||
|
||
// Assembling response | ||
|
||
// Making sure the returned url is not between %% | ||
const queryParams = ctx.state.body.queryParams | ||
var urlToReturn: string | undefined | ||
if (queryParams.articleUrl) { | ||
urlToReturn = queryParams.articleUrl.slice(1, -1) | ||
} else { | ||
urlToReturn = queryParams.articleUrl | ||
} | ||
|
||
ctx.status = 200 | ||
ctx.response.body = { | ||
query: { | ||
startDate: queryParams.startDate, | ||
endDate: queryParams.endDate, | ||
containsHelpArticle: queryParams.containsHelpArticle, | ||
containsDevArticle: queryParams.containsDevArticle, | ||
articleUrl: urlToReturn | ||
}, | ||
tickets : { | ||
quantity: numberOfTickets, | ||
ids: uniqueTicketIds, | ||
}, | ||
comments: { | ||
quantity: numberOfComments, | ||
sample: { | ||
quantity: sampleQuantity, | ||
data: sampleData | ||
} | ||
} | ||
} | ||
await next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.