-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dec 20 tele fix #49
dec 20 tele fix #49
Changes from 9 commits
304447d
6e6104e
e14517e
e68bcdb
f5b0701
1cef7e9
d0c9967
1aebc50
f176cfe
225b3c9
79ffb95
de27b8a
7b0f50e
1921420
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
use std::collections::HashMap; | ||
use tracing::{error, info}; | ||
use std::sync::Arc; | ||
use std::sync::{Arc, RwLockWriteGuard}; | ||
use std::sync::RwLock as StdRwLock; | ||
use std::path::PathBuf; | ||
use regex::Regex; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::json; | ||
use regex::Regex; | ||
|
||
use tokio::sync::RwLock as ARwLock; | ||
|
||
|
@@ -15,7 +15,51 @@ use crate::telemetry::telemetry_structs; | |
use crate::telemetry::telemetry_structs::{SnippetTracker, TeleRobotHumanAccum}; | ||
|
||
|
||
const ROBOT_HUMAN_FILE_STATS_UPDATE_EVERY: i64 = 15; | ||
|
||
pub fn create_robot_human_record_if_not_exists( | ||
tele_robot_human: &mut Vec<TeleRobotHumanAccum>, | ||
uri: &String, | ||
text: &String | ||
) { | ||
let record_mb = tele_robot_human.iter_mut().find(|stat| stat.uri.eq(uri)); | ||
if record_mb.is_some() { | ||
return; | ||
} | ||
info!("create_robot_human_rec_if_not_exists: new uri {}", uri); | ||
let record = TeleRobotHumanAccum::new( | ||
uri.clone(), | ||
text.clone(), | ||
); | ||
tele_robot_human.push(record); | ||
} | ||
|
||
|
||
fn update_robot_characters_baseline( | ||
rec: &mut TeleRobotHumanAccum, | ||
snip: &SnippetTracker | ||
) { | ||
let re = Regex::new(r"\s+").unwrap(); | ||
let robot_characters = re.replace_all(&snip.grey_text, "").len() as i64; | ||
rec.robot_characters_acc_baseline += robot_characters; | ||
} | ||
|
||
fn basetext_to_text_leap_calculations( | ||
rec: &mut TeleRobotHumanAccum, | ||
baseline_text: String, | ||
text: &String, | ||
) { | ||
let re = Regex::new(r"\s+").unwrap(); | ||
let (added_characters, removed_characters) = utils::get_add_del_from_texts(&baseline_text, text, false); | ||
|
||
let (added_characters, _) = utils::get_add_del_chars_from_texts(&removed_characters, &added_characters); | ||
|
||
// let real_characters_added = re.replace_all(&added_characters, "").len() as i64 - re.replace_all(&removed_characters, "").len() as i64; | ||
let human_characters = re.replace_all(&added_characters, "").len() as i64 - rec.robot_characters_acc_baseline; | ||
info!("human_characters: +{}; robot_characters: +{}", human_characters, rec.robot_characters_acc_baseline); | ||
rec.human_characters += human_characters; | ||
rec.robot_characters += rec.robot_characters_acc_baseline; | ||
rec.robot_characters_acc_baseline = 0; | ||
} | ||
valaises marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
pub fn increase_counters_from_finished_snippet( | ||
|
@@ -24,67 +68,40 @@ pub fn increase_counters_from_finished_snippet( | |
text: &String, | ||
snip: &SnippetTracker, | ||
) { | ||
// Snippet is finished when it stops being valid for correction (user has changed code in a different place) or it timeouts | ||
fn robot_characters(snip: &SnippetTracker) -> i64 { | ||
let re = Regex::new(r"\s+").unwrap(); | ||
let robot_characters = re.replace_all(&snip.grey_text, "").len() as i64; | ||
info!("increase_counters_from_finished_snippet: ID: {}; robot_characters: {}", snip.snippet_telemetry_id, robot_characters); | ||
robot_characters | ||
} | ||
fn human_characters(rec: &TeleRobotHumanAccum, text: &String) -> i64 { | ||
let re = Regex::new(r"\s+").unwrap(); | ||
let (added_characters, _) = utils::get_add_del_from_texts(&rec.baseline_text, text); | ||
let human_characters = re.replace_all(&added_characters, "").len() as i64 - rec.robot_characters_acc_baseline; | ||
human_characters | ||
} | ||
|
||
info!("snip grey_text: {}", snip.grey_text); | ||
let now = chrono::Local::now().timestamp(); | ||
|
||
if let Some(rec) = tele_robot_human.iter_mut().find(|stat| stat.uri.eq(uri)) { | ||
if rec.used_snip_ids.contains(&snip.snippet_telemetry_id) { | ||
return; | ||
} | ||
let robot_characters = robot_characters(snip); | ||
rec.robot_characters_acc_baseline += robot_characters; | ||
rec.used_snip_ids.push(snip.snippet_telemetry_id); | ||
if rec.baseline_updated_ts + ROBOT_HUMAN_FILE_STATS_UPDATE_EVERY < now { | ||
// New baseline, increase counters | ||
rec.baseline_updated_ts = now; | ||
rec.human_characters += human_characters(rec, text); | ||
rec.robot_characters += rec.robot_characters_acc_baseline; | ||
rec.robot_characters_acc_baseline = 0; | ||
rec.baseline_text = text.clone(); | ||
} | ||
info!("increasing for {}, human+{}, robot+{}", snip.snippet_telemetry_id, human_characters(rec, text), robot_characters); | ||
} else { | ||
info!("increase_counters_from_finished_snippet: new uri {}", uri); | ||
let init_file_text_mb = snip.inputs.sources.get(&snip.inputs.cursor.file); | ||
if init_file_text_mb.is_none() { | ||
return; | ||
|
||
if rec.used_snip_ids.is_empty() { | ||
rec.model = snip.model.clone(); | ||
} | ||
let init_file_text = init_file_text_mb.unwrap(); | ||
tele_robot_human.push(TeleRobotHumanAccum::new( | ||
uri.clone(), | ||
snip.model.clone(), | ||
init_file_text.clone(), | ||
robot_characters(snip), | ||
vec![snip.snippet_telemetry_id], | ||
)); | ||
|
||
update_robot_characters_baseline(rec, snip); | ||
basetext_to_text_leap_calculations(rec, rec.baseline_text.clone(), text); | ||
|
||
rec.used_snip_ids.push(snip.snippet_telemetry_id); | ||
rec.baseline_updated_ts = now; | ||
rec.baseline_text = text.clone(); | ||
} | ||
} | ||
|
||
fn compress_robot_human( | ||
data: &mut Vec<TeleRobotHumanAccum> | ||
storage_locked: &mut RwLockWriteGuard<telemetry_structs::Storage> | ||
) -> Vec<TeleRobotHuman> { | ||
let mut unique_combinations: HashMap<(String, String), Vec<&TeleRobotHumanAccum>> = HashMap::new(); | ||
let mut unique_combinations: HashMap<(String, String), Vec<TeleRobotHumanAccum>> = HashMap::new(); | ||
|
||
let tele_robot_human = storage_locked.tele_robot_human.clone(); | ||
|
||
for accum in data { | ||
for accum in tele_robot_human { | ||
let key = (accum.file_extension.clone(), accum.model.clone()); | ||
unique_combinations.entry(key).or_default().push(accum); | ||
} | ||
let mut compressed_vec= vec![]; | ||
for (key, entries) in unique_combinations { | ||
info!("compress_robot_human: compressing {} entries for key {:?}", entries.len(), key); | ||
// info!("compress_robot_human: compressing {} entries for key {:?}", entries.len(), key); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same with debug! |
||
let mut record = TeleRobotHuman::new( | ||
key.0.clone(), | ||
key.1.clone() | ||
|
@@ -114,7 +131,7 @@ pub async fn tele_robot_human_compress_to_file( | |
enduser_client_version = cx_locked.cmdline.enduser_client_version.clone(); | ||
|
||
let mut storage_locked = storage.write().unwrap(); | ||
for rec in compress_robot_human(&mut storage_locked.tele_robot_human) { | ||
for rec in compress_robot_human(&mut storage_locked) { | ||
let json_dict = serde_json::to_value(rec).unwrap(); | ||
records.as_array_mut().unwrap().push(json_dict); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,28 +19,78 @@ pub async fn telemetry_storage_dirs(cache_dir: &PathBuf) -> (PathBuf, PathBuf) { | |
pub fn get_add_del_from_texts( | ||
text_a: &String, | ||
text_b: &String, | ||
only_one_deletion_allowed: bool, | ||
) -> (String, String) { | ||
let diff = TextDiff::from_lines(text_a, text_b); | ||
let mut text_a_lines = text_a.lines().collect::<Vec<&str>>(); | ||
let mut text_b_lines = text_b.lines().collect::<Vec<&str>>(); | ||
|
||
for s in &mut text_a_lines { | ||
*s = s.trim_end().trim_start(); | ||
// info!("text_a: {}; len: {}", s, s.len()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if u not sure, use debug!() macros. I will add extra param for debug lvl https://docs.rs/tracing/latest/tracing/macro.debug.html |
||
} | ||
|
||
for s in &mut text_b_lines { | ||
*s = s.trim_end().trim_start(); | ||
// info!("text_b: {}; len: {}", s, s.len()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
} | ||
|
||
let mut text_a_new = text_a_lines.join("\n"); | ||
let text_b_new = text_b_lines.join("\n"); | ||
|
||
if !text_a_new.ends_with("\n") { | ||
text_a_new += "\n"; | ||
} | ||
|
||
let diff = TextDiff::from_lines(&text_a_new, &text_b_new); | ||
|
||
let mut added = "".to_string(); | ||
let mut removed = "".to_string(); | ||
for change in diff.iter_all_changes() { | ||
match change.tag() { | ||
ChangeTag::Delete => { | ||
removed += change.value(); | ||
// info!("rem: {}; len: {}", change.value(), change.value().len()); | ||
if only_one_deletion_allowed { | ||
removed = change.value().to_string(); | ||
} else { | ||
removed += change.value(); | ||
} | ||
valaises marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
ChangeTag::Insert => { | ||
added += change.value(); | ||
// info!("add: {}; len: {}", change.value(), change.value().len()); | ||
} | ||
ChangeTag::Equal => { | ||
} | ||
} | ||
} | ||
added = added.replace("\r", ""); | ||
removed = added.replace("\r", ""); | ||
|
||
(added, removed) | ||
} | ||
|
||
|
||
pub fn get_add_del_chars_from_texts( | ||
text_a: &String, | ||
text_b: &String, | ||
) -> (String, String) { | ||
let diff = TextDiff::from_chars(text_a, text_b); | ||
let mut added = "".to_string(); | ||
let mut removed = "".to_string(); | ||
for change in diff.iter_all_changes() { | ||
match change.tag() { | ||
ChangeTag::Delete => { | ||
removed += change.value(); | ||
} | ||
ChangeTag::Insert => { | ||
added += change.value(); | ||
} | ||
ChangeTag::Equal => { | ||
} | ||
} | ||
} | ||
|
||
(added, removed) | ||
} | ||
|
||
pub async fn file_save(path: PathBuf, json: serde_json::Value) -> Result<(), String> { | ||
let mut f = tokio::fs::File::create(path).await.map_err(|e| format!("{:?}", e))?; | ||
f.write_all(serde_json::to_string_pretty(&json).unwrap().as_bytes()).await.map_err(|e| format!("{}", e))?; | ||
|
@@ -282,7 +332,7 @@ pub fn unchanged_percentage_approx( | |
} | ||
} | ||
|
||
let (texts_ab_added, _) = get_add_del_from_texts(text_a, text_b); | ||
let (texts_ab_added, _) = get_add_del_from_texts(text_a, text_b, false); | ||
|
||
// info!("unchanged_percentage_approx for snip:\n{grey_text_a}"); | ||
if texts_ab_added.is_empty() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove extra comments