Skip to content

Commit 57ec016

Browse files
committed
add\remove data-source
1 parent 90cba3d commit 57ec016

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

app/api/data_source.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
from fastapi import APIRouter
66
from pydantic import BaseModel
77
from starlette.background import BackgroundTasks
8+
from starlette.requests import Request
89

910
from data_source.api.base_data_source import ConfigField
1011
from data_source.api.context import DataSourceContext
1112
from db_engine import Session
1213
from schemas import DataSourceType, DataSource
14+
from telemetry import Posthog
1315

1416
router = APIRouter(
1517
prefix='/data-sources',
@@ -64,15 +66,16 @@ class AddDataSource(BaseModel):
6466

6567

6668
@router.delete("/{data_source_id}")
67-
async def delete_data_source(data_source_id: int):
68-
DataSourceContext.delete_data_source(data_source_id=data_source_id)
69+
async def delete_data_source(request: Request, data_source_id: int):
70+
deleted_name = DataSourceContext.delete_data_source(data_source_id=data_source_id)
71+
Posthog.removed_data_source(uuid=request.headers.get('uuid'), name=deleted_name)
6972
return {"success": "Data source deleted successfully"}
7073

7174

7275
@router.post("")
73-
async def add_integration(dto: AddDataSource, background_tasks: BackgroundTasks) -> int:
76+
async def add_integration(request: Request, dto: AddDataSource, background_tasks: BackgroundTasks) -> int:
7477
data_source = DataSourceContext.create_data_source(name=dto.name, config=dto.config)
75-
78+
Posthog.added_data_source(uuid=request.headers.get('uuid'), name=dto.name)
7679
# in main.py we have a background task that runs every 5 minutes and indexes the data source
7780
# but here we want to index the data source immediately
7881
background_tasks.add_task(data_source.index)

app/data_source/api/context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,20 @@ def create_data_source(cls, name: str, config: dict) -> BaseDataSource:
4747
return data_source
4848

4949
@classmethod
50-
def delete_data_source(cls, data_source_id: int):
50+
def delete_data_source(cls, data_source_id: int) -> str:
5151
with Session() as session:
5252
data_source = session.query(DataSource).filter_by(id=data_source_id).first()
5353
if data_source is None:
5454
raise KnownException(message=f"Data source {data_source_id} does not exist")
5555

56+
data_source_name = data_source.type.name
5657
session.delete(data_source)
5758
session.commit()
5859

5960
del cls._data_sources[data_source_id]
6061

62+
return data_source_name
63+
6164
@classmethod
6265
def init(cls):
6366
cls._add_data_sources_to_db()

app/telemetry.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class Posthog:
1818

1919
RUN_EVENT = "run"
2020
BACKEND_SEARCH_EVENT = "backend_search"
21+
BACKEND_ADDED = "backend_added"
22+
BACKEND_REMOVED = "backend_removed"
2123
_should_capture = False
2224
_identified_uuid: Optional[str] = None
2325

@@ -57,15 +59,15 @@ def _identify(cls):
5759
pass
5860

5961
@classmethod
60-
def _capture(cls, event: str, uuid=None):
62+
def _capture(cls, event: str, uuid=None, properties=None):
6163
if cls._identified_uuid is None:
6264
cls._identify()
6365

6466
if not cls._should_capture:
6567
return
6668

6769
try:
68-
posthog.capture(uuid or cls._identified_uuid, event)
70+
posthog.capture(uuid or cls._identified_uuid, event, properties)
6971
except Exception as e:
7072
pass
7173

@@ -80,3 +82,11 @@ def send_startup_telemetry(cls):
8082
@classmethod
8183
def increase_search_count(cls, uuid: str):
8284
cls._capture(cls.BACKEND_SEARCH_EVENT, uuid=uuid)
85+
86+
@classmethod
87+
def added_data_source(cls, uuid: str, name: str):
88+
cls._capture(cls.BACKEND_ADDED, uuid=uuid, properties={"name": name})
89+
90+
@classmethod
91+
def removed_data_source(cls, uuid: str, name: str):
92+
cls._capture(cls.BACKEND_REMOVED, uuid=uuid, properties={"name": name})

ui/src/components/data-source-panel.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,11 @@ export default class DataSourcePanel extends React.Component<DataSourcePanelProp
412412
config: config
413413
}
414414
this.setState({ isAddingLoading: true });
415-
api.post(`/data-sources`, payload).then(response => {
415+
api.post(`/data-sources`, payload, {
416+
headers: {
417+
uuid: localStorage.getItem('uuid')
418+
}
419+
}).then(response => {
416420
toast.success("Data source added successfully, indexing...");
417421

418422
let selectedDataSource = this.state.selectedDataSource;
@@ -446,7 +450,11 @@ export default class DataSourcePanel extends React.Component<DataSourcePanelProp
446450
let connectedDataSource = this.props.connectedDataSources[index];
447451
this.setState({ removeInProgressIndex: index });
448452

449-
api.delete(`/data-sources/${connectedDataSource.id}`).then(response => {
453+
api.delete(`/data-sources/${connectedDataSource.id}`, {
454+
headers: {
455+
uuid: localStorage.getItem('uuid')
456+
}
457+
}).then(response => {
450458
toast.success(`${this.capitilize(connectedDataSource.name)} removed.`);
451459
this.setState({ removeInProgressIndex: -1 });
452460
this.props.onRemoved(connectedDataSource);

0 commit comments

Comments
 (0)