Logs wireup (#116)

* wireup

* Added live logs

* Finish up wireup

* Run linter

* finish most

* Fix most issues

* Finish page

* run lint

---------

Co-authored-by: Jai A <jaiagr+gpg@pm.me>
Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
Adrian O.V
2023-05-17 16:05:20 -04:00
committed by GitHub
parent c6e2133e15
commit 0801d7a145
10 changed files with 336 additions and 192 deletions

View File

@@ -5,44 +5,64 @@ use tokio::fs::read_to_string;
#[derive(Serialize, Deserialize, Debug)]
pub struct Logs {
pub datetime_string: String,
pub stdout: String,
pub stderr: String,
pub stdout: Option<String>,
pub stderr: Option<String>,
}
impl Logs {
async fn build(
profile_uuid: uuid::Uuid,
datetime_string: String,
clear_contents: Option<bool>,
) -> crate::Result<Self> {
Ok(Self {
stdout: get_stdout_by_datetime(profile_uuid, &datetime_string)
.await?,
stderr: get_stderr_by_datetime(profile_uuid, &datetime_string)
.await?,
stdout: if clear_contents.unwrap_or(false) {
None
} else {
Some(
get_stdout_by_datetime(profile_uuid, &datetime_string)
.await?,
)
},
stderr: if clear_contents.unwrap_or(false) {
None
} else {
Some(
get_stderr_by_datetime(profile_uuid, &datetime_string)
.await?,
)
},
datetime_string,
})
}
}
#[tracing::instrument]
pub async fn get_logs(profile_uuid: uuid::Uuid) -> crate::Result<Vec<Logs>> {
pub async fn get_logs(
profile_uuid: uuid::Uuid,
clear_contents: Option<bool>,
) -> crate::Result<Vec<Logs>> {
let state = State::get().await?;
let logs_folder = state.directories.profile_logs_dir(profile_uuid);
let mut logs = Vec::new();
for entry in std::fs::read_dir(logs_folder)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
if let Some(datetime_string) = path.file_name() {
logs.push(
Logs::build(
profile_uuid,
datetime_string.to_string_lossy().to_string(),
)
.await,
);
if logs_folder.exists() {
for entry in std::fs::read_dir(logs_folder)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
if let Some(datetime_string) = path.file_name() {
logs.push(
Logs::build(
profile_uuid,
datetime_string.to_string_lossy().to_string(),
clear_contents,
)
.await,
);
}
}
}
}
let mut logs = logs.into_iter().collect::<crate::Result<Vec<Logs>>>()?;
logs.sort_by_key(|x| x.datetime_string.clone());
Ok(logs)
@@ -54,8 +74,12 @@ pub async fn get_logs_by_datetime(
datetime_string: String,
) -> crate::Result<Logs> {
Ok(Logs {
stdout: get_stdout_by_datetime(profile_uuid, &datetime_string).await?,
stderr: get_stderr_by_datetime(profile_uuid, &datetime_string).await?,
stdout: Some(
get_stdout_by_datetime(profile_uuid, &datetime_string).await?,
),
stderr: Some(
get_stderr_by_datetime(profile_uuid, &datetime_string).await?,
),
datetime_string,
})
}