td_client/
model.rs

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
use chrono::*;
use rustc_serialize::*;
use std::str::FromStr;

use error::*;

#[derive(PartialEq, Eq, Debug, RustcEncodable)]
pub struct TimeStamp(DateTime<Utc>);

impl FromStr for TimeStamp {
    type Err = ParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let datetime = s
            .parse::<DateTime<Utc>>()
            .or(Utc.datetime_from_str(s, "%Y-%m-%d %H:%M:%S UTC"))?;
        Ok(TimeStamp(datetime))
    }
}

impl ToString for TimeStamp {
    fn to_string(&self) -> String {
        let TimeStamp(datetime) = *self;
        datetime.format("%Y-%m-%d %H:%M:%S UTC").to_string()
    }
}

impl Decodable for TimeStamp {
    fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
        let field = d.read_str()?;
        match field.parse() {
            Ok(result) => Ok(result),
            Err(_) => Err(d.error(&*format!("Could not parse '{}' as a TimeStamp.", field))),
        }
    }
}

#[derive(Debug, RustcDecodable, RustcEncodable)]
pub struct Table {
    pub name: String,
    pub schema: String,
    pub count: u64,
    pub created_at: TimeStamp,
    pub updated_at: TimeStamp,
    pub estimated_storage_size: u64,
    pub last_import: Option<TimeStamp>,
    pub last_log_timestamp: Option<TimeStamp>,
    pub expire_days: Option<u32>,
}

#[derive(Debug, RustcDecodable, RustcEncodable)]
pub struct Tables {
    pub database: String,
    pub tables: Vec<Table>,
}

#[derive(Debug, RustcDecodable, RustcEncodable)]
pub struct Database {
    pub name: String,
    pub count: u64,
    pub created_at: TimeStamp,
    pub updated_at: TimeStamp,
    pub permission: String,
}

#[derive(Debug, RustcDecodable, RustcEncodable)]
pub struct Databases {
    pub databases: Vec<Database>,
}

#[derive(Debug)]
pub enum JobStatus {
    Queued,
    Running,
    Success,
    Killed,
    Error,
}

impl FromStr for JobStatus {
    type Err = json::DecoderError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "queued" => Ok(JobStatus::Queued),
            "running" => Ok(JobStatus::Running),
            "success" => Ok(JobStatus::Success),
            "killed" => Ok(JobStatus::Killed),
            "error" => Ok(JobStatus::Error),
            _ => Err(json::DecoderError::ExpectedError(
                "(queued|running|success|error|killed)".to_string(),
                s.to_string(),
            )),
        }
    }
}

#[derive(Debug, RustcEncodable)]
pub enum JobQuery {
    Query(String),
    Config(json::Json),
}

// We can't use RustcDecodable because `query` can have either string or object...
#[derive(Debug)]
pub struct Job {
    pub job_id: u64,
    pub job_type: String,
    pub query: JobQuery,
    pub status: String,
    pub url: String,
    pub created_at: TimeStamp,
    pub start_at: Option<TimeStamp>,
    pub end_at: Option<TimeStamp>,
    pub cpu_time: Option<String>,
    pub result_size: Option<u64>,
    pub hive_result_schema: Option<Vec<Vec<String>>>,
    pub priority: u64,
    pub retry_limit: u64,
    pub duration: Option<u64>,
}

#[derive(Debug)]
pub struct Jobs {
    pub count: u64,
    pub from: Option<u64>,
    pub to: Option<u64>,
    pub jobs: Vec<Job>,
}

#[derive(Debug, Clone, RustcDecodable, RustcEncodable)]
pub enum QueryType {
    Hive,
    Presto,
    Pig,
}

impl ToString for QueryType {
    fn to_string(&self) -> String {
        match self {
            &QueryType::Hive => "hive".to_string(),
            &QueryType::Presto => "presto".to_string(),
            &QueryType::Pig => "pig".to_string(),
        }
    }
}

impl FromStr for QueryType {
    type Err = InvalidArgument;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "hive" => Ok(QueryType::Hive),
            "presto" => Ok(QueryType::Presto),
            "pig" => Ok(QueryType::Pig),
            _ => Err(InvalidArgument {
                key: "query_type".to_string(),
                value: s.to_string(),
            }),
        }
    }
}

#[derive(Debug, RustcDecodable, RustcEncodable)]
pub enum SchemaType {
    Int,
    Long,
    Float,
    Double,
    String,
    Array(Box<SchemaType>),
}

impl ToString for SchemaType {
    fn to_string(&self) -> String {
        match self {
            &SchemaType::Int => "int".to_string(),
            &SchemaType::Long => "long".to_string(),
            &SchemaType::Float => "float".to_string(),
            &SchemaType::Double => "double".to_string(),
            &SchemaType::String => "string".to_string(),
            &SchemaType::Array(ref inner_type) => {
                ["array<", inner_type.to_string().as_str(), ">"].concat()
            }
        }
    }
}