twc-rs 4.0.3

Fast single-binary CLI and interactive TUI dashboard for Timeweb Cloud
Documentation
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// SPDX-FileCopyrightText: 2026 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT

//! Per-resource summary view-models and the full dashboard data snapshot.

/// Severity of an event-log entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)]
pub enum LogLevel {
    /// Neutral informational event.
    Info,
    /// A successful outcome.
    Success,
    /// A non-fatal warning.
    Warn,
    /// A failure.
    Error
}

/// A single entry in the dashboard event log.
#[derive(Debug, Clone)]
pub struct LogEntry {
    /// Severity of the entry.
    pub level: LogLevel,
    /// Human-readable message.
    pub text:  String
}

/// Account information from the API.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct AccountInfo {
    pub login:      String,
    pub account_id: i64,
    pub balance:    String,
    pub status:     String
}

/// Summary of a single server.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[allow(dead_code)]
pub struct ServerSummary {
    pub id:       i32,
    pub name:     String,
    pub status:   String,
    pub cpu:      i32,
    pub ram_mb:   i32,
    pub disk_gb:  i32,
    pub ip:       String,
    pub location: String
}

/// Summary of a single database cluster, carrying every field the list
/// endpoint exposes (the password is deliberately not kept: summaries are
/// persisted to the on-disk snapshot).
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct DatabaseSummary {
    pub id:           i32,
    pub name:         String,
    pub status:       String,
    pub engine:       String,
    pub size_mb:      i64,
    #[serde(default)]
    pub disk_used_mb: i64,
    #[serde(default)]
    pub created_at:   String,
    #[serde(default)]
    pub location:     String,
    #[serde(default)]
    pub port:         i32,
    #[serde(default)]
    pub public_ip:    String,
    #[serde(default)]
    pub local_ip:     String,
    #[serde(default)]
    pub preset_id:    i32,
    #[serde(default)]
    pub hash_type:    String,
    #[serde(default)]
    pub local_only:   bool,
    #[serde(default)]
    pub config:       Vec<(String, String)>
}

/// Summary of a single S3 storage.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct S3Summary {
    pub id:           i32,
    pub name:         String,
    pub region:       String,
    pub size_kb:      i64,
    pub object_count: i64
}

/// Summary of a single Kubernetes cluster.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct K8sSummary {
    pub id:      i32,
    pub name:    String,
    pub status:  String,
    pub version: String,
    pub cpu:     i32,
    pub ram_mb:  i32,
    pub disk_gb: i32
}

/// Summary of a single project with per-type resource counts.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct ProjectSummary {
    pub id:              i32,
    pub name:            String,
    pub server_count:    i32,
    pub database_count:  i32,
    pub bucket_count:    i32,
    pub cluster_count:   i32,
    pub balancer_count:  i32,
    pub dedicated_count: i32,
    pub app_count:       i32
}

impl ProjectSummary {
    /// Returns the total number of resources across all types.
    #[must_use]
    pub const fn resource_count(&self) -> i32 {
        self.server_count
            + self.database_count
            + self.bucket_count
            + self.cluster_count
            + self.balancer_count
            + self.dedicated_count
            + self.app_count
    }
}

/// Summary of a single load balancer.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct BalancerSummary {
    pub id:       i32,
    pub name:     String,
    pub status:   String,
    pub ip:       String,
    pub location: String
}

/// Summary of a single container registry.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct RegistrySummary {
    pub id:        i32,
    pub name:      String,
    pub disk_used: i64,
    pub disk_size: i64
}

/// Summary of a single domain.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct DomainSummary {
    pub id:           i32,
    pub name:         String,
    pub status:       String,
    pub auto_prolong: bool
}

/// Summary of a single firewall.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct FirewallSummary {
    pub id:     String,
    pub name:   String,
    pub policy: String
}

/// Summary of a single floating IP.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct FloatingIpSummary {
    pub id:          String,
    pub ip:          String,
    pub status:      String,
    pub server_name: String
}

/// Summary of a single image.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct ImageSummary {
    pub id:      String,
    pub name:    String,
    pub size_mb: i64,
    pub status:  String
}

/// Summary of a single network drive.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct NetworkDriveSummary {
    pub id:      String,
    pub name:    String,
    pub size_gb: i64,
    pub status:  String
}

/// Summary of a single VPC.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct VpcSummary {
    pub id:       String,
    pub name:     String,
    pub subnet:   String,
    pub location: String
}

/// Summary of a single dedicated server.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct DedicatedServerSummary {
    pub id:     i32,
    pub name:   String,
    pub status: String,
    pub cpu:    String,
    pub ram:    String,
    pub disk:   String,
    pub ip:     String
}

/// Summary of a single mail service.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct MailSummary {
    pub name:    String,
    pub owner:   String,
    pub comment: String
}

/// Summary of a single application, carrying every field the list endpoint
/// exposes.
///
/// Environment variable values are deliberately not kept (they are secrets
/// and summaries hit the on-disk snapshot) — only their count is.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct AppSummary {
    pub id:            i32,
    pub name:          String,
    pub status:        String,
    pub ip:            String,
    pub location:      String,
    pub app_type:      String,
    pub framework:     String,
    pub language:      String,
    pub branch:        String,
    pub commit:        String,
    pub auto_deploy:   bool,
    pub comment:       String,
    pub domains:       Vec<String>,
    #[serde(default)]
    pub repository:    String,
    #[serde(default)]
    pub repo_url:      String,
    #[serde(default)]
    pub repo_private:  bool,
    #[serde(default)]
    pub provider:      String,
    #[serde(default)]
    pub env_version:   String,
    #[serde(default)]
    pub env_count:     usize,
    #[serde(default)]
    pub preset_id:     i64,
    #[serde(default)]
    pub index_dir:     String,
    #[serde(default)]
    pub build_cmd:     String,
    #[serde(default)]
    pub run_cmd:       String,
    #[serde(default)]
    pub cfg_cpu:       i64,
    #[serde(default)]
    pub cfg_ram_mb:    i64,
    #[serde(default)]
    pub cfg_bandwidth: i64,
    #[serde(default)]
    pub cfg_freq:      String,
    #[serde(default)]
    pub cfg_disk_type: String,
    #[serde(default)]
    pub disk_used_mb:  i64,
    #[serde(default)]
    pub disk_size_mb:  i64,
    #[serde(default)]
    pub started_at:    String
}

/// Summary of a single AI agent.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct AiAgentSummary {
    pub id:           i32,
    pub name:         String,
    pub status:       String,
    pub tokens_used:  i64,
    pub tokens_total: i64
}

/// Summary of a single knowledge base.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct KnowledgeBaseSummary {
    pub id:             i32,
    pub name:           String,
    pub document_count: i32,
    pub status:         String
}

/// Summary of a single SSH key, carrying every field the list endpoint
/// exposes (the key body is public material, so persisting it in the
/// on-disk snapshot is safe).
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct SshKeySummary {
    pub id:         i64,
    pub name:       String,
    pub body:       String,
    pub created_at: String,
    pub used_by:    Vec<String>,
    pub is_default: bool
}

/// Account-level billing summary from the finances endpoint.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct FinancesSummary {
    pub balance:           f64,
    pub currency:          String,
    pub discount_percent:  f64,
    pub discount_end_date: String,
    pub hourly_cost:       f64,
    pub hourly_fee:        f64,
    pub monthly_cost:      f64,
    pub monthly_fee:       f64,
    pub total_paid:        f64,
    pub hours_left:        Option<f64>,
    pub autopay_card:      String
}

impl FinancesSummary {
    /// Number of metric cards [`Self::card_entries`] yields.
    pub const CARD_COUNT: usize = 6;

    /// Formats an amount with the account currency, e.g. `123.45 RUB`.
    #[must_use]
    pub fn money(&self, amount: f64) -> String {
        format!("{amount:.2} {}", self.currency)
    }

    /// The metric cards shown on the Finances tab: a localized label and the
    /// humanized amount, balance first.
    #[must_use]
    pub fn card_entries(&self) -> Vec<(String, String)> {
        vec![
            (
                rust_i18n::t!("finances.balance").into_owned(),
                self.money(self.balance)
            ),
            (
                rust_i18n::t!("finances.monthly_cost").into_owned(),
                self.money(self.monthly_cost)
            ),
            (
                rust_i18n::t!("finances.hourly_cost").into_owned(),
                self.money(self.hourly_cost)
            ),
            (
                rust_i18n::t!("finances.monthly_fee").into_owned(),
                self.money(self.monthly_fee)
            ),
            (
                rust_i18n::t!("finances.hourly_fee").into_owned(),
                self.money(self.hourly_fee)
            ),
            (
                rust_i18n::t!("finances.total_paid").into_owned(),
                self.money(self.total_paid)
            ),
        ]
    }
}

/// An owned snapshot of all dashboard data, applied in one shot via
/// `App::apply_data` and persisted between runs for instant startup.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DashboardData {
    pub account:           AccountInfo,
    pub servers:           Vec<ServerSummary>,
    pub databases:         Vec<DatabaseSummary>,
    pub s3_storages:       Vec<S3Summary>,
    pub k8s_clusters:      Vec<K8sSummary>,
    pub projects:          Vec<ProjectSummary>,
    pub balancers:         Vec<BalancerSummary>,
    pub registries:        Vec<RegistrySummary>,
    pub domains:           Vec<DomainSummary>,
    pub firewalls:         Vec<FirewallSummary>,
    pub floating_ips:      Vec<FloatingIpSummary>,
    pub images:            Vec<ImageSummary>,
    pub network_drives:    Vec<NetworkDriveSummary>,
    pub vpcs:              Vec<VpcSummary>,
    pub dedicated_servers: Vec<DedicatedServerSummary>,
    pub mails:             Vec<MailSummary>,
    pub apps:              Vec<AppSummary>,
    pub ai_agents:         Vec<AiAgentSummary>,
    pub knowledge_bases:   Vec<KnowledgeBaseSummary>,
    pub ssh_keys:          Vec<SshKeySummary>,
    pub finances:          Option<FinancesSummary>,
    pub error_message:     Option<String>,
    pub status_message:    Option<String>,
    pub load_errors:       Vec<String>
}

impl DashboardData {
    /// Clones the resource data out of a populated `App`, for persisting the
    /// startup snapshot.
    #[must_use]
    pub fn from_app(app: &super::App) -> Self {
        Self {
            account:           app.account.clone(),
            servers:           app.servers.clone(),
            databases:         app.databases.clone(),
            s3_storages:       app.s3_storages.clone(),
            k8s_clusters:      app.k8s_clusters.clone(),
            projects:          app.projects.clone(),
            balancers:         app.balancers.clone(),
            registries:        app.registries.clone(),
            domains:           app.domains.clone(),
            firewalls:         app.firewalls.clone(),
            floating_ips:      app.floating_ips.clone(),
            images:            app.images.clone(),
            network_drives:    app.network_drives.clone(),
            vpcs:              app.vpcs.clone(),
            dedicated_servers: app.dedicated_servers.clone(),
            mails:             app.mails.clone(),
            apps:              app.apps.clone(),
            ai_agents:         app.ai_agents.clone(),
            knowledge_bases:   app.knowledge_bases.clone(),
            ssh_keys:          app.ssh_keys.clone(),
            finances:          app.finances.clone(),
            error_message:     None,
            status_message:    None,
            load_errors:       Vec::new()
        }
    }
}