|
| 1 | +const axios = require('axios'); |
| 2 | +const fs = require('fs'); |
| 3 | +const crypto = require('crypto'); |
| 4 | +const path = require('path'); |
| 5 | +const { v4: uuidv4 } = require('uuid'); |
| 6 | +const {PYTHAGORA_METADATA_DIR, CONFIG_FILENAME, PYTHAGORA_API_SERVER} = require("../const/common"); |
| 7 | +const packageJson = require('../../package.json'); |
| 8 | + |
| 9 | +const pythagoraVersion = packageJson.version; |
| 10 | +const configPath = path.join(process.cwd(), '../..', PYTHAGORA_METADATA_DIR, CONFIG_FILENAME); |
| 11 | + |
| 12 | +// Calculate the SHA-256 hash of the installation directory |
| 13 | +const installationDirectory = path.join(process.cwd(), '../..'); |
| 14 | +const hash = crypto.createHash('sha256'); |
| 15 | +hash.update(installationDirectory); |
| 16 | +const pathId = hash.digest('hex'); |
| 17 | + |
| 18 | +// Try to read the config file and get the userId |
| 19 | +let userId; |
| 20 | +try { |
| 21 | + const config = JSON.parse(fs.readFileSync(configPath)); |
| 22 | + userId = config.userId; |
| 23 | +} catch (err) { |
| 24 | + // Config file doesn't exist or is not valid JSON |
| 25 | +} |
| 26 | + |
| 27 | +// If there's no userId, generate one and save it to the config file |
| 28 | +if (!userId) { |
| 29 | + userId = uuidv4(); |
| 30 | + const configDir = path.dirname(configPath); |
| 31 | + if (!fs.existsSync(configDir)) { |
| 32 | + fs.mkdirSync(configDir, { recursive: true }); |
| 33 | + } |
| 34 | + fs.writeFileSync(configPath, JSON.stringify({ userId }, null, 2)); |
| 35 | +} |
| 36 | + |
| 37 | +// Send the request to the telemetry endpoint |
| 38 | +const telemetryData = { |
| 39 | + userId, |
| 40 | + pathId, |
| 41 | + event: 'install', |
| 42 | + pythagoraVersion |
| 43 | +}; |
| 44 | + |
| 45 | +axios.post(`${PYTHAGORA_API_SERVER}/telemetry`, telemetryData) |
| 46 | + .then((res) => { |
| 47 | + console.log('Telemetry data sent successfully'); |
| 48 | + }) |
| 49 | + .catch((err) => { |
| 50 | + console.error(`Failed to send telemetry data: ${err.message}`); |
| 51 | + }); |
0 commit comments