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
mod corpora_aggregate;
mod mark;
mod reports;
mod services_aggregate;
mod tasks_aggregate;
pub(crate) use reports::progress_report;
pub use reports::TaskReportOptions;
use diesel::pg::PgConnection;
use diesel::result::Error;
use diesel::*;
use dotenv::dotenv;
use std::collections::HashMap;
use crate::concerns::{CortexDeletable, CortexInsertable};
use crate::helpers::{TaskReport, TaskStatus};
use crate::models::{Corpus, NewTask, Service, Task};
pub const DEFAULT_DB_ADDRESS: &str = dotenv!("DATABASE_URL");
pub const TEST_DB_ADDRESS: &str = dotenv!("TEST_DATABASE_URL");
pub struct Backend {
pub connection: PgConnection,
}
impl Default for Backend {
fn default() -> Self {
dotenv().ok();
let connection = connection_at(DEFAULT_DB_ADDRESS);
Backend { connection }
}
}
pub fn connection_at(address: &str) -> PgConnection {
PgConnection::establish(address).unwrap_or_else(|_| panic!("Error connecting to {}", address))
}
pub fn testdb() -> Backend {
dotenv().ok();
Backend {
connection: connection_at(TEST_DB_ADDRESS),
}
}
pub fn from_address(address: &str) -> Backend {
Backend {
connection: connection_at(address),
}
}
pub struct RerunOptions<'a> {
pub corpus: &'a Corpus,
pub service: &'a Service,
pub severity_opt: Option<String>,
pub category_opt: Option<String>,
pub what_opt: Option<String>,
pub owner_opt: Option<String>,
pub description_opt: Option<String>,
}
impl Backend {
pub fn mark_imported(&self, imported_tasks: &[NewTask]) -> Result<usize, Error> {
mark::mark_imported(&self.connection, imported_tasks)
}
pub fn mark_done(&self, reports: &[TaskReport]) -> Result<(), Error> {
mark::mark_done(&self.connection, reports)
}
pub fn mark_rerun(&self, options: RerunOptions) -> Result<(), Error> {
mark::mark_rerun(&self.connection, options)
}
pub fn mark_new_run(
&self,
corpus: &Corpus,
service: &Service,
owner: String,
description: String,
) -> Result<(), Error>
{
mark::mark_new_run(
&self.connection,
corpus,
service,
owner,
description,
)
}
pub fn delete<Model: CortexDeletable>(&self, object: &Model) -> Result<usize, Error> {
object.delete_by(&self.connection, "id")
}
pub fn delete_by<Model: CortexDeletable>(
&self,
object: &Model,
field: &str,
) -> Result<usize, Error>
{
object.delete_by(&self.connection, field)
}
pub fn add<Model: CortexInsertable>(&self, object: &Model) -> Result<usize, Error> {
object.create(&self.connection)
}
pub fn fetch_tasks(&self, service: &Service, limit: usize) -> Result<Vec<Task>, Error> {
tasks_aggregate::fetch_tasks(&self.connection, service, limit)
}
pub fn clear_limbo_tasks(&self) -> Result<usize, Error> {
tasks_aggregate::clear_limbo_tasks(&self.connection)
}
pub fn register_service(&self, service: &Service, corpus_path: &str) -> Result<(), Error> {
services_aggregate::register_service(&self.connection, service, corpus_path)
}
pub fn extend_service(&self, service: &Service, corpus_path: &str) -> Result<(), Error> {
services_aggregate::extend_service(&self.connection, service, corpus_path)
}
pub fn delete_service_by_name(&self, name: &str) -> Result<usize, Error> {
services_aggregate::delete_service_by_name(&self.connection, name)
}
pub fn corpora(&self) -> Vec<Corpus> { corpora_aggregate::list_corpora(&self.connection) }
pub fn tasks(&self, corpus: &Corpus, service: &Service, task_status: &TaskStatus) -> Vec<Task> {
reports::list_tasks(&self.connection, corpus, service, task_status)
}
pub fn entries(
&self,
corpus: &Corpus,
service: &Service,
task_status: &TaskStatus,
) -> Vec<String>
{
reports::list_entries(&self.connection, corpus, service, task_status)
}
pub fn task_report(&self, options: TaskReportOptions) -> Vec<HashMap<String, String>> {
reports::task_report(&self.connection, options)
}
pub fn progress_report(&self, corpus: &Corpus, service: &Service) -> HashMap<String, f64> {
reports::progress_report(&self.connection, corpus.id, service.id)
}
}