below_dump/
disk.rs

1// Copyright (c) Facebook, Inc. and its affiliates.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use model::SingleDiskModelFieldId;
16
17use super::*;
18
19pub struct Disk {
20    opts: GeneralOpt,
21    select: Option<SingleDiskModelFieldId>,
22    fields: Vec<DiskField>,
23}
24
25impl Disk {
26    pub fn new(
27        opts: &GeneralOpt,
28        select: Option<SingleDiskModelFieldId>,
29        fields: Vec<DiskField>,
30    ) -> Self {
31        Self {
32            opts: opts.to_owned(),
33            select,
34            fields,
35        }
36    }
37}
38
39impl Dumper for Disk {
40    fn dump_model(
41        &self,
42        ctx: &CommonFieldContext,
43        model: &model::Model,
44        output: &mut dyn Write,
45        round: &mut usize,
46        comma_flag: bool,
47    ) -> Result<IterExecResult> {
48        let mut disks: Vec<_> = model
49            .system
50            .disks
51            .iter()
52            .filter_map(
53                |(_, model)| match (self.select.as_ref(), self.opts.filter.as_ref()) {
54                    (Some(field_id), Some(filter))
55                        if !filter.is_match(
56                            &model
57                                .query(field_id)
58                                .map_or("?".to_owned(), |v| v.to_string()),
59                        ) =>
60                    {
61                        None
62                    }
63                    _ => Some(model),
64                },
65            )
66            .collect();
67
68        if let Some(field_id) = &self.select {
69            if self.opts.sort {
70                model::sort_queriables(&mut disks, field_id, false);
71            }
72
73            if self.opts.rsort {
74                model::sort_queriables(&mut disks, field_id, true);
75            }
76
77            if (self.opts.sort || self.opts.rsort) && self.opts.top != 0 {
78                disks.truncate(self.opts.top as usize);
79            }
80        }
81        let mut json_output = json!([]);
82
83        disks
84            .into_iter()
85            .map(|model| {
86                match self.opts.output_format {
87                    Some(OutputFormat::Raw) | None => write!(
88                        output,
89                        "{}",
90                        print::dump_raw_indented(
91                            &self.fields,
92                            ctx,
93                            model,
94                            *round,
95                            self.opts.repeat_title,
96                            self.opts.disable_title,
97                            self.opts.raw
98                        )
99                    )?,
100                    Some(OutputFormat::Csv) => write!(
101                        output,
102                        "{}",
103                        print::dump_csv(
104                            &self.fields,
105                            ctx,
106                            model,
107                            *round,
108                            self.opts.disable_title,
109                            self.opts.raw
110                        )
111                    )?,
112                    Some(OutputFormat::Tsv) => write!(
113                        output,
114                        "{}",
115                        print::dump_tsv(
116                            &self.fields,
117                            ctx,
118                            model,
119                            *round,
120                            self.opts.disable_title,
121                            self.opts.raw
122                        )
123                    )?,
124                    Some(OutputFormat::KeyVal) => write!(
125                        output,
126                        "{}",
127                        print::dump_kv(&self.fields, ctx, model, self.opts.raw)
128                    )?,
129                    Some(OutputFormat::Json) => {
130                        let par = print::dump_json(&self.fields, ctx, model, self.opts.raw);
131                        json_output.as_array_mut().unwrap().push(par);
132                    }
133                    Some(OutputFormat::OpenMetrics) => write!(
134                        output,
135                        "{}",
136                        print::dump_openmetrics(&self.fields, ctx, model)
137                    )?,
138                }
139                *round += 1;
140                Ok(())
141            })
142            .collect::<Result<Vec<_>>>()?;
143
144        match (self.opts.output_format, comma_flag) {
145            (Some(OutputFormat::Json), true) => write!(output, ",{}", json_output)?,
146            (Some(OutputFormat::Json), false) => write!(output, "{}", json_output)?,
147            (Some(OutputFormat::OpenMetrics), _) => (),
148            _ => writeln!(output)?,
149        };
150
151        Ok(IterExecResult::Success)
152    }
153}