Merge pull request #1384 from zed-industries/expose-collaboration-metrics
Expose collaboration metrics
This commit is contained in:
@@ -8,4 +8,3 @@
|
|||||||
/crates/collab/static/styles.css
|
/crates/collab/static/styles.css
|
||||||
/vendor/bin
|
/vendor/bin
|
||||||
/assets/themes/*.json
|
/assets/themes/*.json
|
||||||
dump.rdb
|
|
||||||
|
|||||||
@@ -1,3 +1,2 @@
|
|||||||
web: cd ../zed.dev && PORT=3000 npx next dev
|
web: cd ../zed.dev && PORT=3000 npx next dev
|
||||||
collab: cd crates/collab && cargo run
|
collab: cd crates/collab && cargo run
|
||||||
redis: redis-server
|
|
||||||
|
|||||||
@@ -23,12 +23,6 @@ script/sqlx migrate run
|
|||||||
script/seed-db
|
script/seed-db
|
||||||
```
|
```
|
||||||
|
|
||||||
Install Redis:
|
|
||||||
|
|
||||||
```
|
|
||||||
brew install redis
|
|
||||||
```
|
|
||||||
|
|
||||||
Run the web frontend and the collaboration server.
|
Run the web frontend and the collaboration server.
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -304,6 +304,8 @@ struct ActiveUserCountParams {
|
|||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
period: TimePeriodParams,
|
period: TimePeriodParams,
|
||||||
durations_in_minutes: String,
|
durations_in_minutes: String,
|
||||||
|
#[serde(default)]
|
||||||
|
only_collaborative: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@@ -329,6 +331,7 @@ async fn get_active_user_counts(
|
|||||||
.get_active_user_count(
|
.get_active_user_count(
|
||||||
params.period.start..params.period.end,
|
params.period.start..params.period.end,
|
||||||
Duration::from_secs(duration * 60),
|
Duration::from_secs(duration * 60),
|
||||||
|
params.only_collaborative,
|
||||||
)
|
)
|
||||||
.await?,
|
.await?,
|
||||||
})
|
})
|
||||||
|
|||||||
+139
-26
@@ -75,6 +75,7 @@ pub trait Db: Send + Sync {
|
|||||||
&self,
|
&self,
|
||||||
time_period: Range<OffsetDateTime>,
|
time_period: Range<OffsetDateTime>,
|
||||||
min_duration: Duration,
|
min_duration: Duration,
|
||||||
|
only_collaborative: bool,
|
||||||
) -> Result<usize>;
|
) -> Result<usize>;
|
||||||
|
|
||||||
/// Get the users that have been most active during the given time period,
|
/// Get the users that have been most active during the given time period,
|
||||||
@@ -605,15 +606,48 @@ impl Db for PostgresDb {
|
|||||||
&self,
|
&self,
|
||||||
time_period: Range<OffsetDateTime>,
|
time_period: Range<OffsetDateTime>,
|
||||||
min_duration: Duration,
|
min_duration: Duration,
|
||||||
|
only_collaborative: bool,
|
||||||
) -> Result<usize> {
|
) -> Result<usize> {
|
||||||
let query = "
|
let mut with_clause = String::new();
|
||||||
WITH
|
with_clause.push_str("WITH\n");
|
||||||
project_durations AS (
|
with_clause.push_str(
|
||||||
SELECT user_id, project_id, SUM(duration_millis) AS project_duration
|
"
|
||||||
FROM project_activity_periods
|
project_durations AS (
|
||||||
WHERE $1 < ended_at AND ended_at <= $2
|
SELECT user_id, project_id, SUM(duration_millis) AS project_duration
|
||||||
GROUP BY user_id, project_id
|
FROM project_activity_periods
|
||||||
),
|
WHERE $1 < ended_at AND ended_at <= $2
|
||||||
|
GROUP BY user_id, project_id
|
||||||
|
),
|
||||||
|
",
|
||||||
|
);
|
||||||
|
with_clause.push_str(
|
||||||
|
"
|
||||||
|
project_collaborators as (
|
||||||
|
SELECT project_id, COUNT(DISTINCT user_id) as max_collaborators
|
||||||
|
FROM project_durations
|
||||||
|
GROUP BY project_id
|
||||||
|
),
|
||||||
|
",
|
||||||
|
);
|
||||||
|
|
||||||
|
if only_collaborative {
|
||||||
|
with_clause.push_str(
|
||||||
|
"
|
||||||
|
user_durations AS (
|
||||||
|
SELECT user_id, SUM(project_duration) as total_duration
|
||||||
|
FROM project_durations, project_collaborators
|
||||||
|
WHERE
|
||||||
|
project_durations.project_id = project_collaborators.project_id AND
|
||||||
|
max_collaborators > 1
|
||||||
|
GROUP BY user_id
|
||||||
|
ORDER BY total_duration DESC
|
||||||
|
LIMIT $3
|
||||||
|
)
|
||||||
|
",
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
with_clause.push_str(
|
||||||
|
"
|
||||||
user_durations AS (
|
user_durations AS (
|
||||||
SELECT user_id, SUM(project_duration) as total_duration
|
SELECT user_id, SUM(project_duration) as total_duration
|
||||||
FROM project_durations
|
FROM project_durations
|
||||||
@@ -621,12 +655,20 @@ impl Db for PostgresDb {
|
|||||||
ORDER BY total_duration DESC
|
ORDER BY total_duration DESC
|
||||||
LIMIT $3
|
LIMIT $3
|
||||||
)
|
)
|
||||||
|
",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let query = format!(
|
||||||
|
"
|
||||||
|
{with_clause}
|
||||||
SELECT count(user_durations.user_id)
|
SELECT count(user_durations.user_id)
|
||||||
FROM user_durations
|
FROM user_durations
|
||||||
WHERE user_durations.total_duration >= $3
|
WHERE user_durations.total_duration >= $3
|
||||||
";
|
"
|
||||||
|
);
|
||||||
|
|
||||||
let count: i64 = sqlx::query_scalar(query)
|
let count: i64 = sqlx::query_scalar(&query)
|
||||||
.bind(time_period.start)
|
.bind(time_period.start)
|
||||||
.bind(time_period.end)
|
.bind(time_period.end)
|
||||||
.bind(min_duration.as_millis() as i64)
|
.bind(min_duration.as_millis() as i64)
|
||||||
@@ -654,16 +696,22 @@ impl Db for PostgresDb {
|
|||||||
GROUP BY user_id
|
GROUP BY user_id
|
||||||
ORDER BY total_duration DESC
|
ORDER BY total_duration DESC
|
||||||
LIMIT $3
|
LIMIT $3
|
||||||
|
),
|
||||||
|
project_collaborators as (
|
||||||
|
SELECT project_id, COUNT(DISTINCT user_id) as max_collaborators
|
||||||
|
FROM project_durations
|
||||||
|
GROUP BY project_id
|
||||||
)
|
)
|
||||||
SELECT user_durations.user_id, users.github_login, project_id, project_duration
|
SELECT user_durations.user_id, users.github_login, project_durations.project_id, project_duration, max_collaborators
|
||||||
FROM user_durations, project_durations, users
|
FROM user_durations, project_durations, project_collaborators, users
|
||||||
WHERE
|
WHERE
|
||||||
user_durations.user_id = project_durations.user_id AND
|
user_durations.user_id = project_durations.user_id AND
|
||||||
user_durations.user_id = users.id
|
user_durations.user_id = users.id AND
|
||||||
|
project_durations.project_id = project_collaborators.project_id
|
||||||
ORDER BY total_duration DESC, user_id ASC
|
ORDER BY total_duration DESC, user_id ASC
|
||||||
";
|
";
|
||||||
|
|
||||||
let mut rows = sqlx::query_as::<_, (UserId, String, ProjectId, i64)>(query)
|
let mut rows = sqlx::query_as::<_, (UserId, String, ProjectId, i64, i64)>(query)
|
||||||
.bind(time_period.start)
|
.bind(time_period.start)
|
||||||
.bind(time_period.end)
|
.bind(time_period.end)
|
||||||
.bind(max_user_count as i32)
|
.bind(max_user_count as i32)
|
||||||
@@ -671,18 +719,23 @@ impl Db for PostgresDb {
|
|||||||
|
|
||||||
let mut result = Vec::<UserActivitySummary>::new();
|
let mut result = Vec::<UserActivitySummary>::new();
|
||||||
while let Some(row) = rows.next().await {
|
while let Some(row) = rows.next().await {
|
||||||
let (user_id, github_login, project_id, duration_millis) = row?;
|
let (user_id, github_login, project_id, duration_millis, project_collaborators) = row?;
|
||||||
let project_id = project_id;
|
let project_id = project_id;
|
||||||
let duration = Duration::from_millis(duration_millis as u64);
|
let duration = Duration::from_millis(duration_millis as u64);
|
||||||
|
let project_activity = ProjectActivitySummary {
|
||||||
|
id: project_id,
|
||||||
|
duration,
|
||||||
|
max_collaborators: project_collaborators as usize,
|
||||||
|
};
|
||||||
if let Some(last_summary) = result.last_mut() {
|
if let Some(last_summary) = result.last_mut() {
|
||||||
if last_summary.id == user_id {
|
if last_summary.id == user_id {
|
||||||
last_summary.project_activity.push((project_id, duration));
|
last_summary.project_activity.push(project_activity);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.push(UserActivitySummary {
|
result.push(UserActivitySummary {
|
||||||
id: user_id,
|
id: user_id,
|
||||||
project_activity: vec![(project_id, duration)],
|
project_activity: vec![project_activity],
|
||||||
github_login,
|
github_login,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -1314,7 +1367,14 @@ pub struct Project {
|
|||||||
pub struct UserActivitySummary {
|
pub struct UserActivitySummary {
|
||||||
pub id: UserId,
|
pub id: UserId,
|
||||||
pub github_login: String,
|
pub github_login: String,
|
||||||
pub project_activity: Vec<(ProjectId, Duration)>,
|
pub project_activity: Vec<ProjectActivitySummary>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Serialize)]
|
||||||
|
pub struct ProjectActivitySummary {
|
||||||
|
id: ProjectId,
|
||||||
|
duration: Duration,
|
||||||
|
max_collaborators: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize)]
|
#[derive(Clone, Debug, PartialEq, Serialize)]
|
||||||
@@ -1667,47 +1727,99 @@ pub mod tests {
|
|||||||
id: user_1,
|
id: user_1,
|
||||||
github_login: "user_1".to_string(),
|
github_login: "user_1".to_string(),
|
||||||
project_activity: vec![
|
project_activity: vec![
|
||||||
(project_1, Duration::from_secs(25)),
|
ProjectActivitySummary {
|
||||||
(project_2, Duration::from_secs(30)),
|
id: project_1,
|
||||||
|
duration: Duration::from_secs(25),
|
||||||
|
max_collaborators: 2
|
||||||
|
},
|
||||||
|
ProjectActivitySummary {
|
||||||
|
id: project_2,
|
||||||
|
duration: Duration::from_secs(30),
|
||||||
|
max_collaborators: 2
|
||||||
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
UserActivitySummary {
|
UserActivitySummary {
|
||||||
id: user_2,
|
id: user_2,
|
||||||
github_login: "user_2".to_string(),
|
github_login: "user_2".to_string(),
|
||||||
project_activity: vec![(project_2, Duration::from_secs(50))]
|
project_activity: vec![ProjectActivitySummary {
|
||||||
|
id: project_2,
|
||||||
|
duration: Duration::from_secs(50),
|
||||||
|
max_collaborators: 2
|
||||||
|
}]
|
||||||
},
|
},
|
||||||
UserActivitySummary {
|
UserActivitySummary {
|
||||||
id: user_3,
|
id: user_3,
|
||||||
github_login: "user_3".to_string(),
|
github_login: "user_3".to_string(),
|
||||||
project_activity: vec![(project_1, Duration::from_secs(15))]
|
project_activity: vec![ProjectActivitySummary {
|
||||||
|
id: project_1,
|
||||||
|
duration: Duration::from_secs(15),
|
||||||
|
max_collaborators: 2
|
||||||
|
}]
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.get_active_user_count(t0..t6, Duration::from_secs(56))
|
db.get_active_user_count(t0..t6, Duration::from_secs(56), false)
|
||||||
.await
|
.await
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.get_active_user_count(t0..t6, Duration::from_secs(54))
|
db.get_active_user_count(t0..t6, Duration::from_secs(56), true)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
0
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
db.get_active_user_count(t0..t6, Duration::from_secs(54), false)
|
||||||
.await
|
.await
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
1
|
1
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.get_active_user_count(t0..t6, Duration::from_secs(30))
|
db.get_active_user_count(t0..t6, Duration::from_secs(54), true)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
1
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
db.get_active_user_count(t0..t6, Duration::from_secs(30), false)
|
||||||
.await
|
.await
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
2
|
2
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.get_active_user_count(t0..t6, Duration::from_secs(10))
|
db.get_active_user_count(t0..t6, Duration::from_secs(30), true)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
2
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
db.get_active_user_count(t0..t6, Duration::from_secs(10), false)
|
||||||
.await
|
.await
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
3
|
3
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
db.get_active_user_count(t0..t6, Duration::from_secs(10), true)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
3
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
db.get_active_user_count(t0..t1, Duration::from_secs(5), false)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
1
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
db.get_active_user_count(t0..t1, Duration::from_secs(5), true)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
0
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.get_user_activity_timeline(t3..t6, user_1).await.unwrap(),
|
db.get_user_activity_timeline(t3..t6, user_1).await.unwrap(),
|
||||||
@@ -2549,6 +2661,7 @@ pub mod tests {
|
|||||||
&self,
|
&self,
|
||||||
_time_period: Range<OffsetDateTime>,
|
_time_period: Range<OffsetDateTime>,
|
||||||
_min_duration: Duration,
|
_min_duration: Duration,
|
||||||
|
_only_collaborative: bool,
|
||||||
) -> Result<usize> {
|
) -> Result<usize> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user