Skip to content

Commit 5f87971

Browse files
committed
progress updates in core submit, log R logs
1 parent 09d35dc commit 5f87971

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

tmc-langs-cli/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ fn run() -> Result<()> {
515515
env::var("TMC_CORE_CLI_ROOT_URL").unwrap_or_else(|_| "https://tmc.mooc.fi".to_string());
516516
let mut core = TmcCore::new_in_config(root_url)
517517
.with_context(|| format!("Failed to create TmcCore"))?;
518+
// set progress report to print the updates to stdout as JSON
519+
core.set_progress_report(|update| println!("{}", serde_json::to_string(&update).unwrap()));
518520

519521
let email = matches.value_of("email").unwrap();
520522
// TODO: "Please enter password" and quiet param

tmc-langs-core/src/tmc_core.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use oauth2::{
1111
AuthUrl, ClientId, ClientSecret, ResourceOwnerPassword, ResourceOwnerUsername, TokenUrl,
1212
};
1313
use reqwest::{blocking::Client, Url};
14+
use serde::Serialize;
1415
use std::collections::HashMap;
1516
use std::io::Write;
1617
use std::path::Path;
@@ -21,6 +22,12 @@ use tmc_langs_util::task_executor;
2122
pub type Token =
2223
oauth2::StandardTokenResponse<oauth2::EmptyExtraTokenFields, oauth2::basic::BasicTokenType>;
2324

25+
#[derive(Debug, Serialize)]
26+
pub enum StatusUpdate {
27+
Processing(&'static str, f64),
28+
Finished,
29+
}
30+
2431
/// A struct for interacting with the TestMyCode service, including authentication
2532
pub struct TmcCore {
2633
client: Client,
@@ -29,6 +36,7 @@ pub struct TmcCore {
2936
api_url: Url,
3037
auth_url: String,
3138
token: Option<Token>,
39+
progress_report: Option<Box<dyn Fn(StatusUpdate)>>,
3240
}
3341

3442
// TODO: cache API results?
@@ -64,6 +72,7 @@ impl TmcCore {
6472
api_url,
6573
auth_url,
6674
token: None,
75+
progress_report: None,
6776
})
6877
}
6978

@@ -83,6 +92,25 @@ impl TmcCore {
8392
Self::new(config_dir, root_url)
8493
}
8594

95+
pub fn set_progress_report<F>(&mut self, progress_report: F)
96+
where
97+
F: Fn(StatusUpdate) + 'static,
98+
{
99+
self.progress_report = Some(Box::new(progress_report));
100+
}
101+
102+
pub fn report_progress(&self, msg: &'static str, progress: f64) {
103+
self.progress_report
104+
.as_ref()
105+
.map(|f| f(StatusUpdate::Processing(msg, progress)));
106+
}
107+
108+
pub fn report_complete(&self) {
109+
self.progress_report
110+
.as_ref()
111+
.map(|f| f(StatusUpdate::Finished));
112+
}
113+
86114
/// Attempts to log in with the given credentials, returns an error if an authentication token is already present.
87115
/// Username can be the user's username or email.
88116
///
@@ -329,12 +357,18 @@ impl TmcCore {
329357
locale: Language,
330358
) -> Result<NewSubmission> {
331359
// compress
360+
self.report_progress("Submitting exercise. Compressing submission...", 0.0);
332361
let compressed = task_executor::compress_project(submission_path)?;
362+
self.report_progress("Compressed submission. Creating temporary file...", 0.25);
333363
let mut file = NamedTempFile::new().map_err(CoreError::TempFile)?;
364+
self.report_progress("Created temporary file. Writing compressed data...", 0.5);
334365
file.write_all(&compressed)
335366
.map_err(|e| CoreError::FileWrite(file.path().to_path_buf(), e))?;
367+
self.report_progress("Wrote compressed data. Posting submission...", 0.75);
336368

337-
self.post_submission(submission_url, file.path(), locale)
369+
let result = self.post_submission(submission_url, file.path(), locale);
370+
self.report_complete();
371+
result
338372
}
339373

340374
/// Fetches the course's exercises from the server,

tmc-langs-r/src/plugin.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ impl LanguagePlugin for RPlugin {
8484
.args(args)
8585
.output()
8686
.map_err(|e| RError::Command("Rscript", e))?;
87+
88+
log::trace!("stdout: {}", String::from_utf8_lossy(&out.stdout));
89+
log::debug!("stderr: {}", String::from_utf8_lossy(&out.stderr));
90+
8791
if !out.status.success() {
8892
return Err(RError::CommandStatus("Rscript", out.status).into());
8993
}

0 commit comments

Comments
 (0)