Skip to content

Commit 4f796f2

Browse files
committed
add telemetry on install
1 parent 6e7bfa5 commit 4f796f2

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"scripts": {
1717
"publish-pythagora": "src/bin/publish.bash",
1818
"prepublishOnly": "node src/bin/prepublish.js",
19+
"postinstall": "node src/bin/postinstall.js",
1920
"test": "npx jest ./pythagora_tests/ --coverage"
2021
},
2122
"bin": {

src/bin/postinstall.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
});

src/bin/run.bash

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,28 @@ do
5353
elif [[ "${args[$i]}" == "--config" ]]
5454
then
5555
# TODO refactor and make it flexible
56-
CONFIG_FILE="./.pythagora/config.json"
57-
rm "$CONFIG_FILE"
56+
CONFIG_DIR="./.pythagora"
57+
CONFIG_FILE="$CONFIG_DIR/config.json"
58+
TMP_FILE="$CONFIG_DIR/tmp.json"
5859

60+
# Create the config file if it doesn't exist
5961
if [ ! -f "$CONFIG_FILE" ]; then
60-
mkdir -p ./.pythagora
61-
touch "./$CONFIG_FILE"
62+
mkdir -p $CONFIG_DIR
63+
echo "{}" > $CONFIG_FILE
6264
fi
6365

6466
API_NAME="${args[$i+1]//--/}"
6567
API_NAME="${API_NAME//-/_}"
6668
API_KEY="${args[$i+2]}"
67-
echo "{" >> $CONFIG_FILE
68-
echo " \"$API_NAME\": \"$API_KEY\"" >> $CONFIG_FILE
69-
echo "}" >> $CONFIG_FILE
69+
70+
if [ "$API_NAME" == "pythagora_api_key" ]; then
71+
jq 'del(.openai_api_key)' $CONFIG_FILE > $TMP_FILE && mv $TMP_FILE $CONFIG_FILE
72+
elif [ "$API_NAME" == "openai_api_key" ]; then
73+
jq 'del(.pythagora_api_key)' $CONFIG_FILE > $TMP_FILE && mv $TMP_FILE $CONFIG_FILE
74+
fi
75+
76+
# Use jq to add the new key-value pair to the JSON object
77+
jq --arg key "$API_NAME" --arg value "$API_KEY" '. + {($key): $value}' $CONFIG_FILE > $TMP_FILE && mv $TMP_FILE $CONFIG_FILE
7078
echo "${green}${bold}API key added to config!${reset}"
7179
exit 0
7280
elif [[ "${args[$i]}" == "--review" ]]

0 commit comments

Comments
 (0)