1
//! The `nomisync_form_requests_total` series: one count per request at its entry adapter.
2

            
3
use server::account_state::STALE_ACCOUNT;
4
use server::commodity_state::STALE_COMMODITY;
5
use server::logical::STALE_REVISION;
6
use server::script_state::STALE_SCRIPT;
7
use server::tag_state::STALE_TAG;
8
use server::telemetry::recorder::FORM_REQUESTS;
9
use server::template_state::STALE_TEMPLATE;
10

            
11
use super::Failure;
12
use crate::envelope::ErrorCode;
13
use crate::host_failure::condition_code;
14

            
15
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16
pub enum FormAction {
17
    Load,
18
    Submit,
19
    Query,
20
}
21

            
22
impl FormAction {
23
    #[must_use]
24
118580
    pub const fn as_str(self) -> &'static str {
25
118580
        match self {
26
24607
            Self::Load => "load",
27
59945
            Self::Submit => "submit",
28
34028
            Self::Query => "query",
29
        }
30
118580
    }
31
}
32

            
33
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34
pub enum FormOutcome {
35
    Ok,
36
    Invalid,
37
    UnknownForm,
38
    Malformed,
39
    MalformedId,
40
    Missing,
41
    Refused,
42
    Stale,
43
    Internal,
44
}
45

            
46
impl FormOutcome {
47
    #[must_use]
48
118586
    pub const fn as_str(self) -> &'static str {
49
118586
        match self {
50
95413
            Self::Ok => "ok",
51
14634
            Self::Invalid => "invalid",
52
10
            Self::UnknownForm => "unknown_form",
53
808
            Self::Malformed => "malformed",
54
324
            Self::MalformedId => "malformed_id",
55
2404
            Self::Missing => "missing",
56
2737
            Self::Refused => "refused",
57
1292
            Self::Stale => "stale",
58
964
            Self::Internal => "internal",
59
        }
60
118586
    }
61
}
62

            
63
impl FormOutcome {
64
967
    fn from_condition(code: Option<&str>) -> Self {
65
967
        match code {
66
1
            Some(ErrorCode::NOT_FOUND) => Self::Missing,
67
1
            Some(ErrorCode::ARGS) => Self::Refused,
68
965
            _ => Self::Internal,
69
        }
70
967
    }
71
}
72

            
73
impl From<&Failure> for FormOutcome {
74
22371
    fn from(failure: &Failure) -> Self {
75
22371
        match failure {
76
15930
            Failure::Invalid(errors) if errors.iter().any(is_stale) => Self::Stale,
77
14635
            Failure::Invalid(_) => Self::Invalid,
78
9
            Failure::UnknownForm(_) => Self::UnknownForm,
79
324
            Failure::MalformedId { .. } => Self::MalformedId,
80
2404
            Failure::Missing(_) => Self::Missing,
81
2737
            Failure::Refused(_) => Self::Refused,
82
967
            Failure::Internal(err) => Self::from_condition(condition_code(err)),
83
        }
84
22371
    }
85
}
86

            
87
17573
fn is_stale(error: &forms::FieldError) -> bool {
88
17573
    error.field == "revision"
89
1296
        && [
90
1296
            STALE_REVISION,
91
1296
            STALE_ACCOUNT,
92
1296
            STALE_COMMODITY,
93
1296
            STALE_TAG,
94
1296
            STALE_TEMPLATE,
95
1296
            STALE_SCRIPT,
96
1296
        ]
97
1296
        .contains(&error.message.as_str())
98
17573
}
99

            
100
/// The compiled-in form name, so an arbitrary client string never becomes a label.
101
118577
fn form_label(name: &str) -> &'static str {
102
118577
    forms::by_name(name).map_or("unknown", |form| form.name)
103
118577
}
104

            
105
118577
pub fn record_form_request(form: &str, action: FormAction, outcome: FormOutcome) {
106
118577
    metrics::counter!(
107
118577
        FORM_REQUESTS,
108
118577
        "form" => form_label(form),
109
118577
        "action" => action.as_str(),
110
118577
        "outcome" => outcome.as_str()
111
118577
    )
112
118577
    .increment(1);
113
118577
}
114

            
115
/// Counts `result` as one request and hands it back.
116
88351
pub fn count_result<T>(
117
88351
    form: &str,
118
88351
    action: FormAction,
119
88351
    result: Result<T, Failure>,
120
88351
) -> Result<T, Failure> {
121
88351
    let outcome = result
122
88351
        .as_ref()
123
88351
        .err()
124
88351
        .map_or(FormOutcome::Ok, FormOutcome::from);
125
88351
    record_form_request(form, action, outcome);
126
88351
    result
127
88351
}