Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Adding telemetry for the new features #112

Merged
merged 38 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1233f50
adding new telemetry to track
Aug 6, 2019
9d03efb
adding telemetry handling for sensor
Aug 6, 2019
f70ab85
Merge branch 'dev' into users/t-famoun/sensor_telemetry
Aug 12, 2019
08f6b2a
add telemetry for sensor calls
Aug 12, 2019
77864db
adding telemetryu for python side
Aug 12, 2019
ff8ba76
solved typo
Aug 12, 2019
abb8a43
renamed constants
Aug 12, 2019
e7732e5
rename constants
Aug 12, 2019
f35cd9b
adding appinsight
Aug 13, 2019
a9578d0
Merge branch 'users/t-famoun/sensor_telemetry' into users/t-famoun/ap…
Aug 13, 2019
99c13ac
removed previous telemetry method
Aug 13, 2019
b890510
adding extension name constant
Aug 13, 2019
11b1a3e
replacing activation key
Aug 13, 2019
ff924b1
replace extension name const
Aug 13, 2019
5b0004a
merging with dev
Aug 14, 2019
fcfb042
change according to review
Aug 15, 2019
4275a43
change due to review
Aug 15, 2019
eaad182
solved typo
Aug 15, 2019
d9a9349
add more event
Aug 15, 2019
96bedfe
replace key by value
Aug 15, 2019
9c97f96
replaced f strings
Aug 15, 2019
202f2c1
Merged with dev
Aug 15, 2019
d9ab5d5
adding license info
Aug 15, 2019
6229cd5
adding a prefix for telemetry
Aug 15, 2019
0687c64
removing spaces
Aug 15, 2019
0d508fd
merged with dev
Aug 15, 2019
0a47bf4
merged with dev
Aug 15, 2019
9ae3b40
adapt to dev chages
Aug 15, 2019
70412b3
chnage way sliders send telemetry
Aug 15, 2019
18e2c17
Merge branch 'dev' into users/t-famoun/appinsight-telemetry
Aug 15, 2019
271cd12
change to override
Aug 15, 2019
4a2e20d
change overwrite to override
Aug 15, 2019
53894ac
merged with dev
Aug 19, 2019
864d3d5
merged with dev
Aug 19, 2019
774ee6e
change tracking method in API
Aug 19, 2019
7e266e7
putting back default variables
Aug 19, 2019
c5a1736
change according to reviews
Aug 19, 2019
d222d61
merged with dev
Aug 19, 2019
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
}
},
{
"id": "badgeForegroundOverwrite",
"id": "badgeForegroundOverride",
"description": "Color that fixes the issue with midnight blue ",
"defaults": {
"dark": "#FFFFFF",
Expand Down
12 changes: 12 additions & 0 deletions src/adafruit_circuitplayground/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@

VALID_PIXEL_ASSIGN_ERROR = "The pixel color value should be a tuple with three values between 0 and 255 or a hexadecimal color between 0x000000 and 0xFFFFFF."

TELEMETRY_EVENT_NAMES = {
'TAPPED': "API.TAPPED",
'PLAY_FILE': "API.PLAY.FILE",
'PLAY_TONE': "API.PLAY.TONE",
'START_TONE': "API.START.TONE",
'STOP_TONE': "API.STOP.TONE",
'DETECT_TAPS': "API.DETECT.TAPS",
'ADJUST_THRESHOLD': "API.ADJUST.THRESHOLD",
'RED_LED': "API.RED.LED",
'PIXELS': "API.PIXELS"
}
ERROR_SENDING_EVENT = "Error trying to send event to the process : "

TIME_DELAY = 0.03

DEFAULT_PORT = "5577"


EVENTS_BUTTON_PRESS = ['button_a', 'button_b', 'switch']
EVENTS_SENSOR_CHANGED = ['temperature',
'light', 'motion_x', 'motion_y', 'motion_z']
46 changes: 40 additions & 6 deletions src/adafruit_circuitplayground/express.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from . import utils
from . import constants as CONSTANTS
from collections import namedtuple
from applicationinsights import TelemetryClient

Acceleration = namedtuple('acceleration', ['x', 'y', 'z'])

Expand Down Expand Up @@ -40,11 +41,18 @@ def __init__(self):
'motion_y': 0,
'motion_z': 0,
'touch': [False]*7,
'detect_taps': 1,
'tapped': False,
'shake': False,
}

self.telemetry_state = {
"DETECT_TAPS": False,
"TAPPED": False,
"RED_LED": False,
"ADJUST_THRESHOLD": False,
"PLAY_FILE": False,
"PLAY_TONE": False,
"START_TONE": False,
"STOP_TONE": False,
}
self.__debug_mode = False
self.__abs_path_to_code_file = ''
self.pixels = Pixel(self.__state, self.__debug_mode)
Expand All @@ -63,6 +71,9 @@ def button_b(self):

@property
def detect_taps(self):
if(not self.telemetry_state["DETECT_TAPS"]):
utils.send_telemetry("DETECT_TAPS")
self.telemetry_state["DETECT_TAPS"] = True
return self.__state['detect_taps']

@detect_taps.setter
Expand All @@ -75,15 +86,23 @@ def detect_taps(self, value):
def tapped(self):
""" Not Implemented!
"""

if(not self.telemetry_state["TAPPED"]):
utils.send_telemetry("TAPPED")
self.telemetry_state["TAPPED"] = True
raise NotImplementedError(CONSTANTS.NOT_IMPLEMENTED_ERROR)

@property
def red_led(self):
if(not self.telemetry_state["RED_LED"]):
utils.send_telemetry("RED_LED")
self.telemetry_state["RED_LED"] = True
return self.__state['red_led']

@red_led.setter
def red_led(self, value):
if(not self.telemetry_state["RED_LED"]):
utils.send_telemetry("RED_LED")
self.telemetry_state["RED_LED"] = True
self.__state['red_led'] = bool(value)
self.__show()

Expand Down Expand Up @@ -137,13 +156,20 @@ def adjust_touch_threshold(self, adjustement):
"""Not implemented!
The CPX Simulator doesn't use capacitive touch threshold.
"""
if(not self.telemetry_state["ADJUST_THRESHOLD"]):
utils.send_telemetry("ADJUST_THRESHOLD")
self.telemetry_state["ADJUST_THRESHOLD"] = True

raise NotImplementedError(
"this method is not supported by the simulator")
CONSTANTS.NOT_IMPLEMENTED_ERROR)

def shake(self, shake_threshold=30):
return self.__state['shake']

def play_file(self, file_name):
if(not self.telemetry_state["PLAY_FILE"]):
utils.send_telemetry("PLAY_FILE")
self.telemetry_state["PLAY_FILE"] = True
file_name = utils.remove_leading_slashes(file_name)
abs_path_parent_dir = os.path.abspath(
os.path.join(self.__abs_path_to_code_file, os.pardir))
Expand All @@ -165,19 +191,27 @@ def play_file(self, file_name):
def play_tone(self, frequency, duration):
""" Not Implemented!
"""
if(not self.telemetry_state["PLAY_TONE"]):
utils.send_telemetry("PLAY_TONE")
self.telemetry_state["PLAY_TONE"] = True
raise NotImplementedError(
CONSTANTS.NOT_IMPLEMENTED_ERROR)

def start_tone(self, frequency):
""" Not Implemented!
"""
if(not self.telemetry_state["START_TONE"]):
utils.send_telemetry("START_TONE")
self.telemetry_state["START_TONE"] = True
raise NotImplementedError(
CONSTANTS.NOT_IMPLEMENTED_ERROR)

def stop_tone(self):
""" Not Implemented!
"""
# Stop playing any tones.
if(not self.telemetry_state["STOP_TONE"]):
utils.send_telemetry("STOP_TONE")
self.telemetry_state["STOP_TONE"] = True
raise NotImplementedError(
CONSTANTS.NOT_IMPLEMENTED_ERROR)

Expand Down
11 changes: 9 additions & 2 deletions src/adafruit_circuitplayground/pixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import sys
from . import constants as CONSTANTS
from . import utils
from applicationinsights import TelemetryClient
from . import constants as CONSTANTS


class Pixel:
def __init__(self, state, debug_mode=False):
self.__state = state
self.auto_write = True
self.__debug_mode = debug_mode
self.telemetry_state = False

def show(self):
# Send the state to the extension so that React re-renders the Webview
Expand All @@ -28,9 +31,15 @@ def __getitem__(self, index):
if type(index) is not slice:
if not self.__valid_index(index):
raise IndexError(CONSTANTS.INDEX_ERROR)
if(not self.telemetry_state):
utils.send_telemetry("PIXELS")
self.telemetry_state = True
return self.__state['pixels'][index]

def __setitem__(self, index, val):
if(not self.telemetry_state):
utils.send_telemetry("PIXELS")
self.telemetry_state = True
is_slice = False
if type(index) is slice:
is_slice = True
Expand Down Expand Up @@ -80,7 +89,6 @@ def __extract_pixel_value(self, val, is_slice=False):
if len(rgb_value) != 3 or any(not self.__valid_rgb_value(pix) for pix in rgb_value):
raise ValueError(CONSTANTS.VALID_PIXEL_ASSIGN_ERROR)
extracted_values.append(rgb_value)

return rgb_value if not is_slice else extracted_values

def __hex_to_rgb(self, hexValue):
Expand All @@ -90,7 +98,6 @@ def __hex_to_rgb(self, hexValue):
hexToRgbValue[0] = int(hexColor[0:2], 16) # R
hexToRgbValue[1] = int(hexColor[2:4], 16) # G
hexToRgbValue[2] = int(hexColor[4:6], 16) # B

return tuple(hexToRgbValue)
else:
raise ValueError(CONSTANTS.PIXEL_RANGE_ERROR)
Expand Down
11 changes: 11 additions & 0 deletions src/adafruit_circuitplayground/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
import time
from . import constants as CONSTANTS
from . import debugger_communication_client
from applicationinsights import TelemetryClient


previous_state = {}


telemetry_client = TelemetryClient('__AIKEY__')
EXTENSION_NAME = '__EXTENSIONNAME__'


def show(state, debug_mode=False):
global previous_state
if state != previous_state:
Expand All @@ -28,3 +33,9 @@ def show(state, debug_mode=False):
def remove_leading_slashes(string):
string = string.lstrip('\\/')
return string


def send_telemetry(event_name):
telemetry_client.track_event(
'{}/{}'.format(EXTENSION_NAME, CONSTANTS.TELEMETRY_EVENT_NAMES[event_name]))
telemetry_client.flush()
10 changes: 9 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ export enum TelemetryEventName {
SIMULATOR_BUTTON_AB = "SIMULATOR.BUTTON.AB",
SIMULATOR_SWITCH = "SIMULATOR.SWITCH",

//Sensors
SIMULATOR_TEMPERATURE_SENSOR = "SIMULATOR.TEMPERATURE",
SIMULATOR_LIGHT_SENSOR = " SIMULATOR.LIGHT",
SIMULATOR_MOTION_SENSOR = "SIMULATOR.MOTION",
SIMULATOR_SHAKE = "SIMULATOR.SHAKE",
SIMULATOR_CAPACITIVE_TOUCH = "SIMULATOR.CAPACITIVE.TOUCH",

// Pop-up dialog
CLICK_DIALOG_DONT_SHOW = "CLICK.DIALOG.DONT.SHOW",
CLICK_DIALOG_EXAMPLE_CODE = "CLICK.DIALOG.EXAMPLE.CODE",
Expand All @@ -267,7 +274,8 @@ export enum WebviewMessages {
BUTTON_PRESS = "button-press",
PLAY_SIMULATOR = "play-simulator",
SENSOR_CHANGED = "sensor-changed",
REFRESH_SIMULATOR = "refresh-simulator"
REFRESH_SIMULATOR = "refresh-simulator",
SLIDER_TELEMETRY = "slider-telemetry"
}

// tslint:disable-next-line: no-namespace
Expand Down
44 changes: 44 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export async function activate(context: vscode.ExtensionContext) {
}
break;
case WebviewMessages.SENSOR_CHANGED:
checkForTelemetry(message.text);
console.log(`Sensor changed ${messageJson} \n`);
if (inDebugMode && debuggerCommunicationHandler) {
debuggerCommunicationHandler.emitSensorChanged(messageJson);
Expand All @@ -142,6 +143,9 @@ export async function activate(context: vscode.ExtensionContext) {
console.log("Refresh button");
runSimulatorCommand();
break;
case WebviewMessages.SLIDER_TELEMETRY:
handleSensorTelemetry(message.text);
break;
default:
vscode.window.showInformationMessage(
CONSTANTS.ERROR.UNEXPECTED_MESSAGE
Expand Down Expand Up @@ -743,6 +747,45 @@ const handleButtonPressTelemetry = (buttonState: any) => {
}
};

const handleSensorTelemetry = (sensor: string) => {
switch (sensor) {
case "temperature":
telemetryAI.trackFeatureUsage(
TelemetryEventName.SIMULATOR_TEMPERATURE_SENSOR
);
break;
case "light":
telemetryAI.trackFeatureUsage(TelemetryEventName.SIMULATOR_LIGHT_SENSOR);
break;
case "motion_x":
telemetryAI.trackFeatureUsage(TelemetryEventName.SIMULATOR_MOTION_SENSOR);
break;
case "motion_y":
telemetryAI.trackFeatureUsage(TelemetryEventName.SIMULATOR_MOTION_SENSOR);
break;
case "motion_z":
telemetryAI.trackFeatureUsage(TelemetryEventName.SIMULATOR_MOTION_SENSOR);
break;
case "shake":
telemetryAI.trackFeatureUsage(TelemetryEventName.SIMULATOR_SHAKE);
break;
case "touch":
telemetryAI.trackFeatureUsage(
TelemetryEventName.SIMULATOR_CAPACITIVE_TOUCH
);
break;
}
};

const checkForTelemetry = (sensorState: any) => {
if (sensorState["shake"]) {
console.log(`telemtry sending`);
handleSensorTelemetry("shake");
} else if (sensorState["touch"]) {
handleSensorTelemetry("touch");
}
};

const updatePythonExtraPaths = () => {
const pathToLib: string = __dirname;
const currentExtraPaths: string[] =
Expand Down Expand Up @@ -780,6 +823,7 @@ function getWebviewContent(context: vscode.ExtensionContext) {
</html>`;
}

// this method is called when your extension is deactivated
export async function deactivate() {
const monitor: SerialMonitor = SerialMonitor.getInstance();
await monitor.closeSerialMonitor(null, false);
Expand Down
1 change: 0 additions & 1 deletion src/process_user_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def __init__(self):
threading.Thread.__init__(self)

def run(self):
from adafruit_circuitplayground.express import cpx
while True:
read_val = sys.stdin.readline()
sys.stdin.flush()
Expand Down
3 changes: 2 additions & 1 deletion src/python_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"shake",
"motion_x",
"motion_y",
"motion_z"
"motion_z",
"touch"
]

EXEC_COMMAND = "exec"
Expand Down
1 change: 1 addition & 0 deletions src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
playsound
pytest
applicationinsights
python-socketio
Loading