Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start moving gatsby-telemetry to typescript #25812

Merged
merged 6 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/gatsby-core-utils/src/ci.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ci from "ci-info"

const CI_DEFINITIONS = [
envFromCIandCIName,
envFromCIAndCIName,
getEnvDetect({ key: `NOW_BUILDER_ANNOTATE`, name: `ZEIT Now` }),
getEnvDetect({ key: `NOW_REGION`, name: `ZEIT Now v1` }),
herokuDetect,
Expand Down Expand Up @@ -73,7 +73,7 @@ function herokuDetect(): false | "Heroku" {
)
}

function envFromCIandCIName(): string | null {
function envFromCIAndCIName(): string | null {
if (process.env.CI_NAME && process.env.CI) {
return process.env.CI_NAME
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
jest.mock(`../event-storage`)
const eventStore = require(`../event-storage`)
const Telemetry = require(`../telemetry`)
import { AnalyticsTracker } from "../telemetry"

let telemetry
beforeEach(() => {
eventStore.mockReset()
telemetry = new Telemetry()
telemetry = new AnalyticsTracker()
})

describe(`Telemetry`, () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/gatsby-telemetry/src/declarations.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type UUID = string

declare namespace NodeJS {
interface Process {
gatsbyTelemetrySessionId: UUID;
}
}

42 changes: 0 additions & 42 deletions packages/gatsby-telemetry/src/index.js

This file was deleted.

56 changes: 56 additions & 0 deletions packages/gatsby-telemetry/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { AnalyticsTracker, IAggregateStats } from "./telemetry"
import * as express from "express"

const instance = new AnalyticsTracker()

const flush = require(`./flush`)(instance.isTrackingEnabled())

process.on(`exit`, flush)

// For long running commands we want to occasionally flush the data
//
// The data is also sent on exit.

const intervalDuration = process.env.TELEMETRY_BUFFER_INTERVAL
const interval =
intervalDuration && Number.isFinite(+intervalDuration)
? Math.max(Number(intervalDuration), 1000)
: 10 * 60 * 1000 // 10 min

function tick(): void {
flush()
.catch(console.error)
.then(() => setTimeout(tick, interval))
}

module.exports = {
trackCli: (input, tags, opts): void =>
instance.captureEvent(input, tags, opts),
trackError: (input, tags): void => instance.captureError(input, tags),
trackBuildError: (input, tags): void =>
instance.captureBuildError(input, tags),
setDefaultTags: (tags): void => instance.decorateAll(tags),
decorateEvent: (event, tags): void => instance.decorateNextEvent(event, tags),
setTelemetryEnabled: (enabled): void => instance.setTelemetryEnabled(enabled),
startBackgroundUpdate: (): void => {
setTimeout(tick, interval)
},
isTrackingEnabled: (): boolean => instance.isTrackingEnabled(),
aggregateStats: (data): IAggregateStats => instance.aggregateStats(data),
addSiteMeasurement: (event, obj): void =>
instance.addSiteMeasurement(event, obj),
expressMiddleware: function (source: string) {
return function (
_req: express.Request,
_res: express.Response,
next
): void {
try {
instance.trackActivity(`${source}_ACTIVE`)
} catch (e) {
// ignore
}
next()
}
},
}
2 changes: 1 addition & 1 deletion packages/gatsby-telemetry/src/repository-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface IRepositoryData {
name?: string
}

interface IRepositoryId {
export interface IRepositoryId {
repositoryId: string
repositoryData?: IRepositoryData | null
}
Expand Down
10 changes: 0 additions & 10 deletions packages/gatsby-telemetry/src/send.js

This file was deleted.

10 changes: 10 additions & 0 deletions packages/gatsby-telemetry/src/send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { AnalyticsTracker } from "./telemetry"
const instance = new AnalyticsTracker()

function flush(): void {
instance.sendEvents().catch(_e => {
// ignore
})
}

flush()
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const boxen = require(`boxen`)
import boxen from "boxen"

const defaultConfig = {
padding: 1,
borderColor: `blue`,
borderStyle: `double`,
}
} as boxen.Options

const defaultMessage =
`Gatsby collects anonymous usage analytics\n` +
Expand All @@ -15,14 +15,10 @@ const defaultMessage =

/**
* Analytics notice for the end-user
* @param {Object} config - The configuration that boxen accepts. https://github.com/sindresorhus/boxen#api
* @param {string} message - Message shown to the end-user
*/
const showAnalyticsNotification = (
config = defaultConfig,
message = defaultMessage
) => {
export function showAnalyticsNotification(
config: boxen.Options = defaultConfig,
message: string = defaultMessage
): void {
console.log(boxen(message, config))
}

module.exports = showAnalyticsNotification
Loading