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
use crate::collection::prow::download_artifacts;
use crate::system::create_file_index;
use std::io::BufRead;
use std::path::Path;

pub struct Nouns {
    pub nouns: Vec<String>,
}

impl Nouns {
    pub fn new() -> Self {
        Nouns {
            nouns: vec![
                "error".to_string(),
                "failure".to_string(),
                "mistake".to_string(),
                "problem".to_string(),
                "timeout".to_string(),
                "warning".to_string(),
            ],
        }
    }

    pub fn add(&mut self, noun: String) {
        self.nouns.push(noun);
    }

    pub fn list(&self) -> Vec<String> {
        self.nouns.clone()
    }
}

pub struct Verbs {
    pub verbs: Vec<String>,
}

impl Verbs {
    pub fn new() -> Self {
        Verbs {
            verbs: vec![
                "abort".to_string(),
                "add".to_string(),
                "analyze".to_string(),
                "build".to_string(),
                "cancel".to_string(),
                "check".to_string(),
                "collect".to_string(),
                "compile".to_string(),
                "connect".to_string(),
                "create".to_string(),
                "delete".to_string(),
                "disconnect".to_string(),
                "download".to_string(),
                "edit".to_string(),
                "exceed".to_string(),
                "expire".to_string(),
                "execute".to_string(),
                "fail".to_string(),
                "find".to_string(),
                "get".to_string(),
                "install".to_string(),
                "list".to_string(),
                "load".to_string(),
                "log".to_string(),
                "modify".to_string(),
                "move".to_string(),
                "open".to_string(),
                "parse".to_string(),
                "print".to_string(),
                "pull".to_string(),
                "push".to_string(),
                "read".to_string(),
                "remove".to_string(),
                "run".to_string(),
                "save".to_string(),
                "search".to_string(),
                "show".to_string(),
                "start".to_string(),
                "stop".to_string(),
                "test".to_string(),
                "time out".to_string(),
                "upload".to_string(),
                "use".to_string(),
                "verify".to_string(),
                "watch".to_string(),
                "write".to_string(),
            ],
        }
    }

    pub fn add(&mut self, verb: String) {
        self.verbs.push(verb);
    }

    pub fn list(&self) -> Vec<String> {
        self.verbs.clone()
    }
}

pub struct Adjectives {
    pub adjectives: Vec<String>,
}

impl Adjectives {
    pub fn new() -> Self {
        Adjectives {
            adjectives: vec![
                "unreachable".to_string(),
                "unresponsive".to_string(),
                "unsigned".to_string(),
                "unstable".to_string(),
                "unsuccessful".to_string(),
            ],
        }
    }
}

#[derive(Debug)]
/// Individual log events including their build ID, metadata, and content
pub struct Event {
    pub build_id: String,
    pub metadata: Option<Vec<String>>,
    pub content: String,
}

/// Collects all log events for a given build ID
///
/// # Arguments
///
/// * `build_id` - The build ID for the requested job
/// * `path` - The root data directory
///
/// # Returns
///
/// A vector of log events containing elements from the failure-relevant corpora
pub async fn collect_events(build_id: String, path: String) -> Vec<String> {
    let artifact_path = format!("{}/prow/artifacts/{}", path, build_id);
    if !Path::new(&artifact_path).exists() {
        download_artifacts(&build_id, &path).await;
    }
    let file_index = create_file_index(artifact_path.clone()).await;
    let mut events: Vec<String> = Vec::new();

    for file in file_index {
        let file_open = std::fs::File::open(&file);
        if file_open.is_ok() {
            let file_contents = file_open.unwrap();
            let reader = std::io::BufReader::new(file_contents);
            for line in reader.lines() {
                if line.is_ok() {
                    let line_contents = line.unwrap();
                    if line_contents.contains(Nouns::new().nouns[0].as_str()) {
                        let event = line_contents;
                        events.push(event);
                    }
                }
            }
            //}
        }
    }
    return events;
}

#[cfg(test)]
mod tests {}