1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * AquaVM Workflow Engine
 *
 * Copyright (C) 2024 Fluence DAO
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use serde::Deserialize;
use std::collections::HashMap;

#[derive(Deserialize)]
pub(crate) struct LogRecord {
    pub(crate) target: String,
    pub(crate) span: Span,

    #[serde(flatten)]
    pub(crate) value: LogValue,
}

#[derive(Debug, Deserialize)]
pub(crate) struct Span {
    pub(crate) name: String,
    #[serde(flatten)]
    pub(crate) args: HashMap<String, serde_json::Value>,
}

#[derive(Hash, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct LogKey {
    pub(crate) target: String,
    pub(crate) span_name: String,
}

#[derive(Deserialize)]
pub(crate) struct LogValue {
    pub(crate) timestamp: String,
    pub(crate) fields: Message,
    pub(crate) level: String,
    pub(crate) spans: Vec<Span>,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "message", rename_all = "lowercase")]
pub(crate) enum Message {
    New,
    Enter,
    Close(CloseMessage),
}

#[derive(Debug, Deserialize)]
pub(crate) struct CloseMessage {
    #[serde(rename = "time.busy")]
    pub(crate) time_busy: String,
    #[serde(rename = "time.idle")]
    pub(crate) time_idle: String,
}

impl LogRecord {
    pub(crate) fn get_key(&self) -> LogKey {
        LogKey {
            target: self.target.clone(),
            span_name: self.span.name.clone(),
        }
    }
}

impl std::fmt::Display for Message {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Message::New => write!(f, "new"),
            Message::Enter => write!(f, "enter"),
            Message::Close(c) => write!(f, "close {c}"),
        }
    }
}

impl std::fmt::Display for CloseMessage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "idle={}, busy={}", self.time_idle, self.time_busy)
    }
}

fn format_argument(val: &serde_json::Value) -> String {
    match val {
        serde_json::Value::String(s) => s.to_owned(),
        _ => val.to_string(),
    }
}

impl std::fmt::Display for Span {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use itertools::Itertools as _;

        self.name.fmt(f)?;
        if !self.args.is_empty() {
            "{".fmt(f)?;
            write!(
                f,
                "{}",
                self.args
                    .iter()
                    .map(|(k, v)| format!("{}={}", k, format_argument(v)))
                    .format(", ")
            )?;
            "}".fmt(f)?;
        }
        Ok(())
    }
}