Skip to content

Commit

Permalink
refactor: telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
nextchamp-saqib committed Sep 3, 2024
1 parent 22ef756 commit 7dfd555
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 46 deletions.
1 change: 0 additions & 1 deletion frontend/src2/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ async function initialize(force: boolean = false) {
if (session.initialized && !force) return
Object.assign(session.user, getSessionFromCookies())
session.isLoggedIn && (await fetchSessionInfo())
session.isLoggedIn && call('insights.api.telemetry.track_active_site')
session.initialized = true
}

Expand Down
48 changes: 48 additions & 0 deletions frontend/src2/telemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { call } from 'frappe-ui'
import '../../../frappe/frappe/public/js/lib/posthog.js'

const posthog = {
init: (projectToken: string, options: any) => {},
identify: (userId: string) => {},
startSessionRecording: () => {},
capture: (eventName: string, data?: any) => {},
}

declare global {
interface Window {
posthog: typeof posthog
}
}

type PosthogSettings = {
posthog_project_id: string
posthog_host: string
enable_telemetry: boolean
telemetry_site_age: number
record_session: boolean
posthog_identifier: string
}

call('insights.api.telemetry.get_posthog_settings').then((posthogSettings: PosthogSettings) => {
if (!posthogSettings.enable_telemetry || !posthogSettings.posthog_project_id) {
return
}
window.posthog.init(posthogSettings.posthog_project_id, {
api_host: posthogSettings.posthog_host,
person_profiles: 'identified_only',
autocapture: false,
capture_pageview: false,
capture_pageleave: false,
enable_heatmaps: false,
disable_session_recording: true,
loaded: (ph: typeof posthog) => {
ph.identify(posthogSettings.posthog_identifier || window.location.host)
Object.assign(posthog, ph)
if (posthogSettings.record_session) {
ph.startSessionRecording()
}
},
})
})

export { posthog }
1 change: 0 additions & 1 deletion insights/api/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@frappe.whitelist()
def create_alert(alert):
track("create_alert")
alert = frappe._dict(alert)
alert_doc = frappe.new_doc("Insights Alert")
alert_doc.update(alert)
Expand Down
11 changes: 7 additions & 4 deletions insights/api/dashboards.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import frappe

from insights.api.permissions import is_private
from insights.api.telemetry import track
from insights.decorators import check_role
from insights.insights.doctype.insights_team.insights_team import (
get_allowed_resources_for_user,
Expand All @@ -19,7 +18,9 @@ def get_dashboard_list():
)
for dashboard in dashboards:
if dashboard._liked_by:
dashboard["is_favourite"] = frappe.session.user in frappe.as_json(dashboard._liked_by)
dashboard["is_favourite"] = frappe.session.user in frappe.as_json(
dashboard._liked_by
)
dashboard["charts"] = frappe.get_all(
"Insights Dashboard Item",
filters={
Expand All @@ -31,7 +32,10 @@ def get_dashboard_list():
dashboard["charts_count"] = len(dashboard["charts"])
dashboard["view_count"] = frappe.db.count(
"View Log",
filters={"reference_doctype": "Insights Dashboard", "reference_name": dashboard.name},
filters={
"reference_doctype": "Insights Dashboard",
"reference_name": dashboard.name,
},
)

dashboard["is_private"] = is_private("Insights Dashboard", dashboard.name)
Expand All @@ -42,7 +46,6 @@ def get_dashboard_list():
@frappe.whitelist()
@check_role("Insights User")
def create_dashboard(title):
track("create_dashboard")
dashboard = frappe.get_doc({"doctype": "Insights Dashboard", "title": title})
dashboard.insert()
return {
Expand Down
18 changes: 12 additions & 6 deletions insights/api/data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from frappe.utils.caching import redis_cache

from insights import notify
from insights.api.telemetry import track
from insights.decorators import check_role
from insights.insights.doctype.insights_query.utils import infer_type_from_list
from insights.insights.doctype.insights_team.insights_team import (
Expand Down Expand Up @@ -54,7 +53,9 @@ def get_table_columns(data_source, table):
@check_role("Insights User")
def get_table_name(data_source, table):
check_table_permission(data_source, table)
return frappe.get_value("Insights Table", {"data_source": data_source, "table": table}, "name")
return frappe.get_value(
"Insights Table", {"data_source": data_source, "table": table}, "name"
)


@frappe.whitelist()
Expand Down Expand Up @@ -82,7 +83,9 @@ def get_tables(data_source=None, with_query_tables=False):

@frappe.whitelist()
@check_role("Insights User")
def create_table_link(data_source, primary_table, foreign_table, primary_key, foreign_key):
def create_table_link(
data_source, primary_table, foreign_table, primary_key, foreign_key
):
check_table_permission(data_source, primary_table.get("value"))
check_table_permission(data_source, foreign_table.get("value"))

Expand Down Expand Up @@ -187,7 +190,6 @@ def import_csv(table_label, table_name, filename, if_exists, columns, data_sourc
@frappe.whitelist()
@check_role("Insights User")
def delete_data_source(data_source):
track("delete_data_source")
try:
frappe.delete_doc("Insights Data Source", data_source)
notify(
Expand Down Expand Up @@ -230,11 +232,15 @@ def fetch_column_values(data_source, table, column, search_text=None):

@frappe.whitelist()
def get_relation(data_source, table_one, table_two):
table_one_doc = InsightsTable.get_doc({"data_source": data_source, "table": table_one})
table_one_doc = InsightsTable.get_doc(
{"data_source": data_source, "table": table_one}
)
if not table_one_doc:
frappe.throw(f"Table {table_one} not found")

table_two_doc = InsightsTable.get_doc({"data_source": data_source, "table": table_two})
table_two_doc = InsightsTable.get_doc(
{"data_source": data_source, "table": table_two}
)
if not table_two_doc:
frappe.throw(f"Table {table_two} not found")

Expand Down
2 changes: 0 additions & 2 deletions insights/api/queries.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import frappe

from insights.api.telemetry import track
from insights.decorators import check_role
from insights.insights.doctype.insights_team.insights_team import (
get_allowed_resources_for_user,
Expand Down Expand Up @@ -56,7 +55,6 @@ def get_queries():
@frappe.whitelist()
@check_role("Insights User")
def create_query(**query):
track("create_query")
doc = frappe.new_doc("Insights Query")
doc.title = query.get("title")
doc.data_source = query.get("data_source")
Expand Down
6 changes: 0 additions & 6 deletions insights/api/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import frappe

from insights.api.telemetry import track
from insights.setup.demo import DemoDataFactory


Expand All @@ -16,13 +15,11 @@ def setup_complete():

@frappe.whitelist()
def update_erpnext_source_title(title):
track("setup_erpnext_source")
frappe.db.set_value("Insights Data Source", "Site DB", "title", title)


@frappe.whitelist()
def setup_sample_data(dataset):
track("setup_sample_data")
factory = DemoDataFactory()
factory.run()
# import_demo_queries_and_dashboards()
Expand Down Expand Up @@ -58,7 +55,6 @@ def import_demo_queries_and_dashboards():

@frappe.whitelist()
def submit_survey_responses(responses):
track("submit_survey_responses")
responses = frappe.parse_json(responses)

try:
Expand Down Expand Up @@ -113,7 +109,6 @@ def test_database_connection(database):

@frappe.whitelist()
def add_database(database):
track("add_data_source")
data_source = get_new_datasource(database)
data_source.save()
data_source.enqueue_sync_tables()
Expand All @@ -124,4 +119,3 @@ def complete_setup():
settings = frappe.get_single("Insights Settings")
settings.setup_complete = 1
settings.save()
track("setup_complete")
55 changes: 42 additions & 13 deletions insights/api/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,71 @@

import frappe
from frappe.utils.data import date_diff
from frappe.utils.telemetry import POSTHOG_HOST_FIELD, POSTHOG_PROJECT_FIELD
from posthog import Posthog


@frappe.whitelist()
def get_credentials():
def is_enabled():
return bool(
frappe.get_system_settings("enable_telemetry")
and frappe.conf.get("posthog_host")
and frappe.conf.get("posthog_project_id")
)


@frappe.whitelist()
def get_posthog_settings():
can_record_session = False
if start_time := frappe.db.get_default("session_recording_start"):
time_difference = (
frappe.utils.now_datetime() - frappe.utils.get_datetime(start_time)
).total_seconds()
if time_difference < 86400: # 1 day
can_record_session = True

return {
"project_id": frappe.conf.get("posthog_project_id"),
"host": frappe.conf.get("posthog_host"),
"posthog_project_id": frappe.conf.get(POSTHOG_PROJECT_FIELD),
"posthog_host": frappe.conf.get(POSTHOG_HOST_FIELD),
"enable_telemetry": frappe.get_system_settings("enable_telemetry"),
"telemetry_site_age": frappe.utils.telemetry.site_age(),
"record_session": can_record_session,
"posthog_identifier": frappe.local.site,
}


@frappe.whitelist()
def track_active_site():
is_frappe_cloud_site = frappe.conf.get("sk_insights")
def get_credentials():
return {
"posthog_project_id": frappe.conf.get(POSTHOG_PROJECT_FIELD),
"posthog_host": frappe.conf.get(POSTHOG_HOST_FIELD),
}


def track_active_site(is_v3=False):
if (
frappe.conf.developer_mode
or not should_track_active_status()
or not is_frappe_cloud_site
or not frappe.conf.get(POSTHOG_PROJECT_FIELD)
):
return

capture_event("insights_active_site")
capture_event("insights_v3_active_site" if is_v3 else "insights_active_site")
frappe.cache().set_value("last_active_at", frappe.utils.now_datetime())


def capture_event(event_name, properties=None):
if not frappe.conf.get("posthog_project_id") or not frappe.conf.get("posthog_host"):
project_id = frappe.conf.get(POSTHOG_PROJECT_FIELD)
host = frappe.conf.get(POSTHOG_HOST_FIELD)
if not project_id or not host:
return

with suppress(Exception):
ph = Posthog(
frappe.conf.get("posthog_project_id"),
host=frappe.conf.get("posthog_host"),
)
ph = Posthog(project_id, host=host)
ph.capture(
distinct_id=frappe.local.site, event=event_name, properties=properties
distinct_id=frappe.local.site,
event=event_name,
properties=properties,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

import random
from contextlib import suppress
from json import dumps

import frappe
from frappe.model.document import Document

from insights import notify
from insights.api.permissions import is_private
from insights.api.telemetry import track
from insights.cache_utils import make_digest

from .utils import guess_layout_for_chart
Expand All @@ -20,9 +18,6 @@


class InsightsDashboard(Document):
def on_trash(self):
track("delete_dashboard")

@frappe.whitelist()
def is_private(self):
return is_private("Insights Dashboard", self.name)
Expand Down Expand Up @@ -84,7 +79,9 @@ def run_query(self, query_name, additional_filters=None):
"Insights Settings", "query_result_expiry"
)
query_result_expiry_in_seconds = query_result_expiry * 60
frappe.cache().set_value(key, new_results, expires_in_sec=query_result_expiry_in_seconds)
frappe.cache().set_value(
key, new_results, expires_in_sec=query_result_expiry_in_seconds
)
return new_results


Expand Down Expand Up @@ -139,7 +136,9 @@ def get_query_columns(query):


def get_dashboard_public_key(name):
existing_key = frappe.db.get_value("Insights Dashboard", name, "public_key", cache=True)
existing_key = frappe.db.get_value(
"Insights Dashboard", name, "public_key", cache=True
)
if existing_key:
return existing_key

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from frappe.utils.caching import redis_cache, site_cache

from insights import notify
from insights.api.telemetry import track
from insights.insights.doctype.insights_query.insights_query import InsightsQuery
from insights.insights.doctype.insights_team.insights_team import (
check_table_permission,
Expand Down Expand Up @@ -47,8 +46,6 @@ def on_trash(self):
):
frappe.delete_doc(doctype, name)

track("delete_data_source")

def validate(self):
if self.is_site_db or self.name == "Query Store":
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from frappe.utils.caching import site_cache
from ibis import BaseBackend

from insights.api.telemetry import track
from insights.insights.doctype.insights_data_source_v3.data_warehouse import (
WAREHOUSE_DB_NAME,
)
Expand Down Expand Up @@ -92,8 +91,6 @@ def on_trash(self):
):
frappe.delete_doc(doctype, name)

track("delete_data_source")

def validate(self):
if self.is_site_db:
return
Expand Down
Loading

0 comments on commit 7dfd555

Please sign in to comment.