feat: add select kind param. Supported editors could log line select … (#299)

* feat: add select kind param. Supported editors could log line select or block select

* fix lint
sweep/improve-logging-information
Meng Zhang 2023-07-16 16:02:40 +08:00 committed by GitHub
parent 3457599db5
commit 95bd53ac9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -33,6 +33,12 @@ pub struct Choice<'a> {
pub text: &'a str, pub text: &'a str,
} }
#[derive(Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SelectKind {
Line,
}
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
pub enum Event<'a> { pub enum Event<'a> {
@ -43,6 +49,8 @@ pub enum Event<'a> {
Select { Select {
completion_id: &'a str, completion_id: &'a str,
choice_index: u32, choice_index: u32,
#[serde(skip_serializing_if = "Option::is_none")]
kind: Option<SelectKind>,
}, },
Completion { Completion {
completion_id: &'a str, completion_id: &'a str,

View File

@ -1,7 +1,9 @@
use axum::Json; use std::collections::HashMap;
use axum::{extract::Query, Json};
use hyper::StatusCode; use hyper::StatusCode;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tabby_common::events; use tabby_common::events::{self, SelectKind};
use utoipa::ToSchema; use utoipa::ToSchema;
#[derive(Serialize, Deserialize, ToSchema, Clone, Debug)] #[derive(Serialize, Deserialize, ToSchema, Clone, Debug)]
@ -27,7 +29,10 @@ pub struct LogEventRequest {
(status = 400, description = "Bad Request") (status = 400, description = "Bad Request")
) )
)] )]
pub async fn log_event(Json(request): Json<LogEventRequest>) -> StatusCode { pub async fn log_event(
Query(params): Query<HashMap<String, String>>,
Json(request): Json<LogEventRequest>,
) -> StatusCode {
if request.event_type == "view" { if request.event_type == "view" {
events::Event::View { events::Event::View {
completion_id: &request.completion_id, completion_id: &request.completion_id,
@ -36,9 +41,18 @@ pub async fn log_event(Json(request): Json<LogEventRequest>) -> StatusCode {
.log(); .log();
StatusCode::OK StatusCode::OK
} else if request.event_type == "select" { } else if request.event_type == "select" {
let is_line = params
.get("select_kind")
.map(|x| x == "line")
.unwrap_or(false);
events::Event::Select { events::Event::Select {
completion_id: &request.completion_id, completion_id: &request.completion_id,
choice_index: request.choice_index, choice_index: request.choice_index,
kind: if is_line {
Some(SelectKind::Line)
} else {
None
},
} }
.log(); .log();
StatusCode::OK StatusCode::OK