Skip to content

Commit

Permalink
[improvement] Support fetching logs for app-ids too
Browse files Browse the repository at this point in the history
using the `AppIdentOpts` instead of `Identifier`, the `logs`
subcommand can now fetch get a `DeployApp` instance from an ID, or a
name or an alias.

misc: this commit also cleans up the formatting a little bit.
  • Loading branch information
ayys authored and theduke committed May 16, 2024
1 parent 930abdf commit 6b9afb9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
3 changes: 2 additions & 1 deletion lib/backend-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ mod queries {
pub token: String,
}

#[derive(cynic::Enum, Clone, Copy, Debug)]
#[derive(cynic::Enum, Clone, Copy, Debug, PartialEq)]
pub enum LogStream {
Stdout,
Stderr,
Expand Down Expand Up @@ -871,6 +871,7 @@ mod queries {
pub message: String,
/// When the message was recorded, in nanoseconds since the Unix epoch.
pub timestamp: f64,
pub stream: Option<LogStream>,
}

#[derive(cynic::QueryVariables, Debug)]
Expand Down
71 changes: 37 additions & 34 deletions lib/cli/src/commands/app/logs.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
//! Show logs for an Edge app.
use comfy_table::Table;
use colored::Colorize;
use comfy_table::{Cell, Table};
use edge_schema::pretty_duration::parse_timestamp_or_relative_time;
use futures::StreamExt;
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use wasmer_api::types::{Log, LogStream};

use crate::{
opts::{ApiOpts, ListFormatOpts},
utils::{render::CliRender, Identifier},
utils::render::CliRender,
};

use super::util::AppIdentOpts;

#[derive(Debug, PartialEq, Eq, Clone, Copy, clap::ValueEnum)]
pub enum LogStreamArg {
Stdout,
Expand Down Expand Up @@ -58,17 +61,13 @@ pub struct CmdAppLogs {
#[clap(long, default_value = "false")]
watch: bool,

/// The name of the app.
///
/// Eg:
/// - name (assumes current user)
/// - namespace/name
/// - namespace/name@version
ident: Identifier,

/// Streams of logs to display
#[clap(long, value_delimiter = ',', value_enum)]
streams: Option<Vec<LogStreamArg>>,

#[clap(flatten)]
#[allow(missing_docs)]
pub ident: AppIdentOpts,
}

#[async_trait::async_trait]
Expand All @@ -78,28 +77,16 @@ impl crate::commands::AsyncCliCommand for CmdAppLogs {
async fn run_async(self) -> Result<(), anyhow::Error> {
let client = self.api.client()?;

let Identifier {
name,
owner,
version,
} = &self.ident;

let owner = match owner {
Some(owner) => owner.to_string(),
None => {
let user = wasmer_api::query::current_user_with_namespaces(&client, None).await?;
user.username
}
};
let (_ident, app) = self.ident.load_app(&client).await?;

let from = self
.from
.unwrap_or_else(|| OffsetDateTime::now_utc() - time::Duration::minutes(10));

tracing::info!(
package.name=%self.ident.name,
package.owner=%owner,
package.version=self.ident.version.as_deref(),
app.name=%app.name,
app.owner=%app.owner.global_name,
app.version=app.active_version.version,
range.start=%from,
range.end=self.until.map(|ts| ts.to_string()),
"Fetching logs",
Expand Down Expand Up @@ -131,9 +118,9 @@ impl crate::commands::AsyncCliCommand for CmdAppLogs {

let logs_stream = wasmer_api::query::get_app_logs_paginated(
&client,
name.clone(),
owner.to_string(),
version.clone(),
app.name.clone(),
app.owner.global_name.to_string(),
None, // keep version None since we want logs from all versions atm
from,
self.until,
self.watch,
Expand Down Expand Up @@ -171,8 +158,10 @@ impl crate::commands::AsyncCliCommand for CmdAppLogs {
impl CliRender for Log {
fn render_item_table(&self) -> String {
let mut table = Table::new();

let Log { message, timestamp } = self;
// remove all borders from the table
let Log {
message, timestamp, ..
}: &Log = self;

table.add_rows([
vec![
Expand All @@ -186,12 +175,26 @@ impl CliRender for Log {

fn render_list_table(items: &[Self]) -> String {
let mut table = Table::new();
table.set_header(vec!["Timestamp".to_string(), "Message".to_string()]);
// table.set_header(vec!["Timestamp".to_string(), "Message".to_string()]);
table.load_preset(comfy_table::presets::NOTHING);
table.set_content_arrangement(comfy_table::ContentArrangement::Dynamic);

for item in items {
let mut message = item.message.clone().bold();
if let Some(stream) = item.stream {
message = match stream {
LogStream::Stdout => message,
LogStream::Stderr => message.yellow(),
LogStream::Runtime => message.cyan(),
};
}
table.add_row([
datetime_from_unix(item.timestamp).format(&Rfc3339).unwrap(),
item.message.clone(),
Cell::new(format!(
"[{}]",
datetime_from_unix(item.timestamp).format(&Rfc3339).unwrap()
))
.set_alignment(comfy_table::CellAlignment::Right),
Cell::new(message),
]);
}
table.to_string()
Expand Down

0 comments on commit 6b9afb9

Please sign in to comment.