Skip to content

Commit

Permalink
report processed analytics event (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeknovy authored Feb 10, 2023
1 parent 9ad9e2b commit 41a458e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export const config = {
jwtToken: process.env.JWT_TOKEN,
jwtTokenLogin: process.env.JWT_TOKEN_LOGIN,
feUrl: process.env.FE_URL,
analyticsIdentifier: process.env.ANALYTICS_IDENTIFIER,
}
3 changes: 2 additions & 1 deletion src/server/controllers/item/shared/item-data-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getScenarioSettings, currentScenarioMetrics } from "../../../queries/sc
import { sendNotifications } from "../../../utils/notifications/send-notification"
import { scenarioThresholdsCalc } from "../utils/scenario-thresholds-calc"
import { extraIntervalMilliseconds } from "./extra-intervals-mapping"
import { AnalyticsEvent } from "../../../utils/analytics/anyltics-event"

export const itemDataProcessing = async ({ projectName, scenarioName, itemId }) => {
const MAX_LABEL_CHART_LENGTH = 100000
Expand Down Expand Up @@ -72,7 +73,6 @@ export const itemDataProcessing = async ({ projectName, scenarioName, itemId })
const overviewChart = await db.many(chartOverviewQuery(interval, itemId))
const statusCodeChart = await db.many(chartOverviewStatusCodesQuery(interval, itemId))
if (parseInt(index, 10) === 0) { // default interval
console.log(overviewChart)
chartData = prepareChartDataForSaving(
{
overviewData: overviewChart,
Expand Down Expand Up @@ -117,6 +117,7 @@ export const itemDataProcessing = async ({ projectName, scenarioName, itemId })
})

logger.info(`Item: ${itemId} processing finished`)
AnalyticsEvent.reportProcessingFinished()

if (scenarioSettings.deleteSamples) {
logger.info(`Item: ${itemId} deleting samples data`)
Expand Down
41 changes: 41 additions & 0 deletions src/server/utils/analytics/analytics-event.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { AnalyticsEvent } from "./anyltics-event"
import { analytics } from "../analytics"

jest.mock("../analytics")


describe("AnalyticEvents", () => {
describe("isAnalyticEnabled", () => {
it("should return true when OPT_OUT_ANALYTICS not set", function () {
const isEnabled = AnalyticsEvent.isAnalyticEnabled()
expect(isEnabled).toEqual(true)
})

it("should return true when OPT_OUT_ANALYTICS not disabled", function () {
process.env.OPT_OUT_ANALYTICS = "false"
const isEnabled = AnalyticsEvent.isAnalyticEnabled()
expect(isEnabled).toEqual(true)
})
it("should return false when OPT_OUT_ANALYTICS disabled", function () {
process.env.OPT_OUT_ANALYTICS = "true"
const isEnabled = AnalyticsEvent.isAnalyticEnabled()
expect(isEnabled).toEqual(false)
})
})
describe("reportProcessingFinished", () => {
it("should not track the event when analytics disabled", function () {
process.env.OPT_OUT_ANALYTICS = "true"
const trackMock = (analytics.track as any).mockResolvedValueOnce(undefined)
AnalyticsEvent.reportProcessingFinished()
expect(trackMock).not.toHaveBeenCalled()

})
it("should track the even only when analytics enabled", function () {
process.env.OPT_OUT_ANALYTICS = "false"
const trackMock = (analytics.track as any).mockResolvedValueOnce(undefined)
AnalyticsEvent.reportProcessingFinished()
expect(trackMock).toHaveBeenCalled()
})
})

})
18 changes: 18 additions & 0 deletions src/server/utils/analytics/anyltics-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { analytics } from "../analytics"

export class AnalyticsEvent {

static isAnalyticEnabled(): boolean {
return !(process.env.OPT_OUT_ANALYTICS === "true")
}

static reportProcessingFinished() {
if (this.isAnalyticEnabled()) {
console.log(process.env.ANALYTICS_IDENTIFIER)
analytics.track("reportProcessingFinished", {
// eslint-disable-next-line camelcase
distinct_id: process.env.ANALYTICS_IDENTIFIER,
})
}
}
}
4 changes: 1 addition & 3 deletions src/server/utils/scheduled-tasks/jobs/analytics-report.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { analytics } from "../../analytics"
import { config } from "../../../config";


(async function () {
await analytics.track("appAlive", {
$os: process.platform,
distinct_id: config.analyticsIdentifier,
distinct_id: process.env.ANALYTICS_IDENTIFIER,
})
}())

0 comments on commit 41a458e

Please sign in to comment.