1 import fs from "fs"
2 import isDocker from "is-docker"
3 import os from "os"
4 import { join, sep } from "path"
5 import { v4 as uuidv4 } from "uuid"
6
7 import Store from "./store"
8 import createFlush from "./util/create-flush"
9 import getTermProgram from "./util/get-term-program"
10 import { getCIName, isCI } from "./util/is-ci"
11 import isTruthy from "./util/is-truthy"
12 import showAnalyticsNotification from "./util/show-notification"
13
14 const MEDUSA_TELEMETRY_VERBOSE = process.env.MEDUSA_TELEMETRY_VERBOSE || false
15
16 class Telemeter {
17 constructor(options = {}) {
18 this.store_ = new Store()
19
20 this.flushAt = Math.max(options.flushAt, 1) || 20
21 this.maxQueueSize = options.maxQueueSize || 1024 * 500
22 this.flushInterval = options.flushInterval || 10 * 1000
23 this.flushed = false
24
25 this.queueSize_ = this.store_.getQueueSize()
26 this.queueCount_ = this.store_.getQueueCount()
27
28 this.featureFlags_ = new Set()
29 this.modules_ = new Set()
30 this.plugins_ = []
31 }
32
33 getMachineId() {
34 if (this.machineId) {
35 return this.machineId
36 }
37 let machineId = this.store_.getConfig(`telemetry.machine_id`)
38 if (typeof machineId !== `string`) {
39 machineId = uuidv4()
40 this.store_.setConfig(`telemetry.machine_id`, machineId)
41 }
42 this.machineId = machineId
43 return machineId
44 }
45
46 isTrackingEnabled() {
47 // Cache the result
48 if (this.trackingEnabled !== undefined) {
49 return this.trackingEnabled
50 }
51 let enabled = this.store_.getConfig(`telemetry.enabled`)
52 if (enabled === undefined || enabled === null) {
53 if (!isCI()) {
54 showAnalyticsNotification()
55 }
56 enabled = true
57 this.store_.setConfig(`telemetry.enabled`, enabled)
58 }
59 this.trackingEnabled = enabled
60 return enabled
61 }
62
63 getOsInfo() {
64 if (this.osInfo) {
65 return this.osInfo
66 }
67 const cpus = os.cpus()
68 const osInfo = {
69 node_version: process.version,
70 platform: os.platform(),
71 release: os.release(),
72 cpus: (cpus && cpus.length > 0 && cpus[0].model) || undefined,
73 is_ci: isCI(),
74 ci_name: getCIName(),
75 arch: os.arch(),
76 docker: isDocker(),
77 term_program: getTermProgram(),
78 }
79 this.osInfo = osInfo
80 return osInfo
81 }
82
83 getMedusaVersion() {
84 try {
85 const packageJson = require.resolve(`@medusajs/medusa/package.json`)
86 const { version } = JSON.parse(fs.readFileSync(packageJson, `utf-8`))
87 return version
88 } catch (e) {
89 if (isTruthy(MEDUSA_TELEMETRY_VERBOSE)) {
90 console.error("failed to get medusa version", e)
91 }
92 }
93 return `-0.0.0`
94 }
95
96 getCliVersion() {
97 try {
98 const jsonfile = join(
99 require.resolve(`@medusajs/cli`).split(`${sep}dist`).shift(),
100 "package.json"
101 )
102 const { version } = require(jsonfile)
103 return version
104 } catch (e) {
105 if (isTruthy(MEDUSA_TELEMETRY_VERBOSE)) {
106 console.error("failed to get medusa version", e)
107 }
108 }
109
110 return `-0.0.0`
111 }
112
113 setTelemetryEnabled(enabled) {
114 this.trackingEnabled = enabled
115 this.store_.setConfig(`telemetry.enabled`, enabled)
116 }
117
118 track(event, data) {
119 return this.enqueue_(event, data)
120 }
121
122 enqueue_(type, data) {
123 const event = {
124 id: `te_${uuidv4()}`,
125 type,
126 properties: data,
127 timestamp: new Date(),
128 machine_id: this.getMachineId(),
129 os_info: this.getOsInfo(),
130 medusa_version: this.getMedusaVersion(),
131 cli_version: this.getCliVersion(),
132 feature_flags: Array.from(this.featureFlags_),
133 modules: Array.from(this.modules_),
134 plugins: this.plugins_,
135 }
136
137 this.store_.addEvent(event)
138
139 this.queueCount_ += 1
140 this.queueSize_ += JSON.stringify(event).length
141
142 const hasReachedFlushAt = this.queueCount_ >= this.flushAt
143 const hasReachedQueueSize = this.queueSize_ >= this.maxQueueSize
144
145 if (hasReachedQueueSize || hasReachedFlushAt) {
146 const flush = createFlush(this.isTrackingEnabled())
147 flush && flush()
148 }
149
150 if (this.flushInterval && !this.timer) {
151 const flush = createFlush(this.isTrackingEnabled())
152 if (flush) {
153 this.timer = setTimeout(flush, this.flushInterval)
154 }
155 }
156 }
157
158 trackFeatureFlag(flag) {
159 if (flag) {
160 this.featureFlags_.add(flag)
161 }
162 }
163
164 trackModule(module) {
165 if (module) {
166 this.modules_.add(module)
167 }
168 }
169
170 trackPlugin(plugin) {
171 if (plugin) {
172 this.plugins_.push(plugin)
173 }
174 }
175 }
176
177 export default Telemeter