Skip to content

Commit

Permalink
Fix languages, implement Other query, and fix analytics bug
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Apr 9, 2024
1 parent 1afa613 commit 201bec5
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ derive_builder = "0.12.0"
futures = "0.3.28"
async-stream = "0.3.5"
regex = "1.10.0"
strum = { version = "0.24", features = ["derive"] }
thiserror = "1.0.49"
utoipa = "3.3"
axum = "0.6"
Expand Down
2 changes: 1 addition & 1 deletion crates/tabby/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ serde_json = { workspace = true }
tower-http = { version = "0.4.0", features = ["cors", "timeout"] }
clap = { version = "4.3.0", features = ["derive"] }
lazy_static = { workspace = true }
strum = { version = "0.24", features = ["derive"] }
strum = { workspace = true }
strfmt = "0.2.4"
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
Expand Down
17 changes: 14 additions & 3 deletions ee/tabby-db/src/user_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,23 @@ impl DbConn {
end: DateTime<Utc>,
users: Vec<i64>,
languages: Vec<String>,
not_languages: Vec<String>,
) -> Result<Vec<UserCompletionDailyStatsDAO>> {
let users = users
.iter()
.map(|u| u.to_string())
.collect::<Vec<_>>()
.join(",");
let languages = languages.join(",");
let languages = languages
.into_iter()
.map(|l| format!("{:?}", l.to_string()))
.collect::<Vec<_>>()
.join(",");
let not_languages = not_languages
.into_iter()
.map(|l| format!("{:?}", l.to_string()))
.collect::<Vec<_>>()
.join(",");
let res = sqlx::query_as!(
UserCompletionDailyStatsDAO,
r#"
Expand All @@ -117,14 +127,15 @@ impl DbConn {
FROM user_completions
WHERE created_at >= ?1 AND created_at < ?2
AND (?3 = '' OR user_id IN (?3))
AND (?4 = '' OR language IN (?4))
AND ((?4 = '' OR '"' + language + '"' IN (?4)) AND (?5 = '' OR '"' + language + '"' NOT IN (?5)))
GROUP BY 1
ORDER BY 1 ASC
"#,
start,
end,
users,
languages
languages,
not_languages
)
.fetch_all(&self.pool)
.await?;
Expand Down
1 change: 1 addition & 0 deletions ee/tabby-webserver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ reqwest = { workspace = true, features = ["json"] }
rust-embed = "8.0.0"
serde.workspace = true
serde_json.workspace = true
strum.workspace = true
tabby-common = { path = "../../crates/tabby-common" }
tabby-db = { path = "../../ee/tabby-db" }
tarpc = { version = "0.33.0", features = ["serde-transport"] }
Expand Down
45 changes: 26 additions & 19 deletions ee/tabby-webserver/src/schema/analytic.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::fmt::Display;

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use juniper::{GraphQLEnum, GraphQLObject, ID};
use strum::{EnumIter, IntoEnumIterator};

use crate::schema::Result;

Expand All @@ -15,38 +14,46 @@ pub struct CompletionStats {
pub selects: i32,
}

#[derive(GraphQLEnum, Clone, Debug)]
#[derive(GraphQLEnum, Clone, Debug, Eq, PartialEq, EnumIter)]

Check warning on line 17 in ee/tabby-webserver/src/schema/analytic.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/analytic.rs#L17

Added line #L17 was not covered by tests
pub enum Language {
Rust,
Python,
Java,
Kotlin,
JavascriptTypescript,
Javascript,
Typescript,
Go,
Ruby,
CSharp,
C,
CPlusPlus,
CPP,

Check warning on line 29 in ee/tabby-webserver/src/schema/analytic.rs

View workflow job for this annotation

GitHub Actions / autofix

name `CPP` contains a capitalized acronym
Solidity,
Other,
}

impl Display for Language {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl Language {
pub fn all_known() -> impl Iterator<Item = Language> {
Language::iter().filter(|l| l != &Language::Other)
}

Check warning on line 37 in ee/tabby-webserver/src/schema/analytic.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/analytic.rs#L35-L37

Added lines #L35 - L37 were not covered by tests

pub fn to_strings(&self) -> impl IntoIterator<Item = String> {

Check warning on line 39 in ee/tabby-webserver/src/schema/analytic.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/analytic.rs#L39

Added line #L39 was not covered by tests
match self {
Language::Rust => write!(f, "rust"),
Language::Python => write!(f, "python"),
Language::Java => write!(f, "java"),
Language::Kotlin => write!(f, "kotlin"),
Language::JavascriptTypescript => write!(f, "javascript-typescript"),
Language::Go => write!(f, "go"),
Language::Ruby => write!(f, "ruby"),
Language::CSharp => write!(f, "csharp"),
Language::C => write!(f, "c"),
Language::CPlusPlus => write!(f, "cpp"),
Language::Solidity => write!(f, "solidity"),
Language::Other => write!(f, "other"),
Language::Rust => vec!["rust"],
Language::Python => vec!["python"],
Language::Java => vec!["java"],
Language::Kotlin => vec!["kotlin"],
Language::Javascript => vec!["javascript", "javascriptreact"],
Language::Typescript => vec!["typescript", "typescriptreact"],
Language::Go => vec!["go"],
Language::Ruby => vec!["ruby"],
Language::CSharp => vec!["csharp"],
Language::C => vec!["c"],
Language::CPP => vec!["cpp"],
Language::Solidity => vec!["solidity"],
Language::Other => vec!["other"],

Check warning on line 53 in ee/tabby-webserver/src/schema/analytic.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/analytic.rs#L41-L53

Added lines #L41 - L53 were not covered by tests
}
.into_iter()
.map(|s| s.to_string())

Check warning on line 56 in ee/tabby-webserver/src/schema/analytic.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/analytic.rs#L55-L56

Added lines #L55 - L56 were not covered by tests
}
}

Expand Down
16 changes: 13 additions & 3 deletions ee/tabby-webserver/src/service/analytic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,23 @@ impl AnalyticService for AnalyticServiceImpl {
start: DateTime<Utc>,
end: DateTime<Utc>,
users: Vec<ID>,
languages: Vec<Language>,
mut languages: Vec<Language>,
) -> Result<Vec<CompletionStats>> {
let users = convert_ids(users);
let languages = languages.into_iter().map(|l| l.to_string()).collect();

let not_languages = languages
.iter()
.position(|l| l == &Language::Other)
.map(|i| {
languages.remove(i);
Language::all_known().flat_map(|l| l.to_strings()).collect()

Check warning on line 51 in ee/tabby-webserver/src/service/analytic.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/analytic.rs#L50-L51

Added lines #L50 - L51 were not covered by tests
})
.unwrap_or_default();

let languages = languages.into_iter().flat_map(|l| l.to_strings()).collect();
let stats = self
.db
.compute_daily_stats(start, end, users, languages)
.compute_daily_stats(start, end, users, languages, not_languages)
.await?;
let stats = stats
.into_iter()
Expand Down

0 comments on commit 201bec5

Please sign in to comment.