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
use crate::schema::{log_errors, log_fatals, log_infos, log_invalids, log_warnings, tasks};
use diesel::pg::PgConnection;
use diesel::result::Error;
use diesel::*;

use super::RerunOptions;
use crate::concerns::{CortexInsertable, MarkRerun};
use crate::helpers::{random_mark, TaskReport, TaskStatus};
use crate::models::{
  Corpus, HistoricalRun, LogError, LogFatal, LogInfo, LogInvalid, LogRecord, LogWarning,
  NewHistoricalRun, NewTask, Service,
};

pub(crate) fn mark_imported(
  connection: &PgConnection,
  imported_tasks: &[NewTask],
) -> Result<usize, Error>
{
  // Insert, but only if the task is new (allow for extension calls with the same method)
  insert_into(tasks::table)
    .values(imported_tasks)
    .on_conflict_do_nothing()
    .execute(connection)
}

pub(crate) fn mark_done(connection: &PgConnection, reports: &[TaskReport]) -> Result<(), Error> {
  use crate::schema::tasks::{id, status};

  connection.transaction::<(), Error, _>(|| {
    for report in reports.iter() {
      // Update the status
      update(tasks::table)
        .filter(id.eq(report.task.id))
        .set(status.eq(report.status.raw()))
        .execute(connection)?;
      // Next, delete all previous log messages for this task.id
      delete(log_infos::table)
        .filter(log_infos::task_id.eq(report.task.id))
        .execute(connection)?;
      delete(log_warnings::table)
        .filter(log_warnings::task_id.eq(report.task.id))
        .execute(connection)?;
      delete(log_errors::table)
        .filter(log_errors::task_id.eq(report.task.id))
        .execute(connection)?;
      delete(log_fatals::table)
        .filter(log_fatals::task_id.eq(report.task.id))
        .execute(connection)?;
      delete(log_invalids::table)
        .filter(log_invalids::task_id.eq(report.task.id))
        .execute(connection)?;
      // Clean slate, so proceed to add the new messages
      for message in &report.messages {
        if message.severity() != "status" {
          message.create(&connection)?;
        }
      }
      // TODO: Update dependenct services, when integrated in DB
    }
    Ok(())
  })?;
  Ok(())
}

pub(crate) fn mark_rerun<'a>(
  connection: &'a PgConnection,
  options: RerunOptions<'a>,
) -> Result<(), Error>
{
  let RerunOptions {
    corpus,
    service,
    severity_opt,
    category_opt,
    what_opt,
    owner_opt,
    description_opt,
  } = options;
  use crate::schema::tasks::{corpus_id, service_id, status};
  // We are starting a new run, first catalog the current metadata in our historical records.
  let mut description = description_opt.unwrap_or_else(|| String::from("mark for rerun "));
  // auto-generate a report message from the selected filters
  description.push_str("(filters:");
  if severity_opt.is_none() && category_opt.is_none() && what_opt.is_none() {
    description.push_str(" entire corpus");
  } else {
    if let Some(ref severity) = severity_opt {
      description.push_str(" severity=");
      description.push_str(severity);
    }
    if let Some(ref category) = category_opt {
      description.push_str(" category=");
      description.push_str(category);
    }
    if let Some(ref what) = what_opt {
      description.push_str(" what=");
      description.push_str(what);
    }
  }
  description.push(')');

  mark_new_run(
    connection,
    corpus,
    service,
    owner_opt.unwrap_or_else(|| "admin".to_string()),
    description,
  )?;
  // Rerun = set status to TODO for all tasks, deleting old logs
  let mark: i32 = random_mark();

  // First, mark as blocked all of the tasks in the chosen scope, using a special mark
  match severity_opt {
    Some(severity) => match category_opt {
      Some(category) => match what_opt {
        // All tasks in a "what" class
        Some(what) => match severity.to_lowercase().as_str() {
          "warning" => LogWarning::mark_rerun_by_what(
            mark, corpus.id, service.id, &category, &what, connection,
          ),
          "error" => {
            LogError::mark_rerun_by_what(mark, corpus.id, service.id, &category, &what, connection)
          },
          "fatal" => {
            LogFatal::mark_rerun_by_what(mark, corpus.id, service.id, &category, &what, connection)
          },
          "invalid" => LogInvalid::mark_rerun_by_what(
            mark, corpus.id, service.id, &category, &what, connection,
          ),
          _ => {
            LogInfo::mark_rerun_by_what(mark, corpus.id, service.id, &category, &what, connection)
          },
        }?,
        // None: All tasks in a category
        None => match severity.to_lowercase().as_str() {
          "warning" => {
            LogWarning::mark_rerun_by_category(mark, corpus.id, service.id, &category, connection)
          },
          "error" => {
            LogError::mark_rerun_by_category(mark, corpus.id, service.id, &category, connection)
          },
          "fatal" => {
            LogFatal::mark_rerun_by_category(mark, corpus.id, service.id, &category, connection)
          },
          "invalid" => {
            LogInvalid::mark_rerun_by_category(mark, corpus.id, service.id, &category, connection)
          },
          _ => LogInfo::mark_rerun_by_category(mark, corpus.id, service.id, &category, connection),
        }?,
      },
      None => {
        // All tasks in a certain status/severity
        let status_to_rerun: i32 = TaskStatus::from_key(&severity)
          .unwrap_or(TaskStatus::NoProblem)
          .raw();
        update(tasks::table)
          .filter(corpus_id.eq(corpus.id))
          .filter(service_id.eq(service.id))
          .filter(status.eq(status_to_rerun))
          .set(status.eq(mark))
          .execute(connection)?
      },
    },
    None => {
      // Entire corpus
      update(tasks::table)
        .filter(corpus_id.eq(corpus.id))
        .filter(service_id.eq(service.id))
        .filter(status.lt(0))
        .set(status.eq(mark))
        .execute(connection)?
    },
  };

  // Next, delete all logs for the blocked tasks.
  // Note that if we are using a negative blocking status, this query should get sped up via an
  // "Index Scan using log_taskid on logs"
  let affected_tasks = tasks::table
    .filter(corpus_id.eq(corpus.id))
    .filter(service_id.eq(service.id))
    .filter(status.eq(mark));
  let affected_tasks_ids = affected_tasks.select(tasks::id);

  let affected_log_infos = log_infos::table.filter(log_infos::task_id.eq_any(affected_tasks_ids));
  delete(affected_log_infos).execute(connection)?;
  let affected_log_warnings =
    log_warnings::table.filter(log_warnings::task_id.eq_any(affected_tasks_ids));
  delete(affected_log_warnings).execute(connection)?;
  let affected_log_errors =
    log_errors::table.filter(log_errors::task_id.eq_any(affected_tasks_ids));
  delete(affected_log_errors).execute(connection)?;
  let affected_log_fatals =
    log_fatals::table.filter(log_fatals::task_id.eq_any(affected_tasks_ids));
  delete(affected_log_fatals).execute(connection)?;
  let affected_log_invalids =
    log_invalids::table.filter(log_invalids::task_id.eq_any(affected_tasks_ids));
  delete(affected_log_invalids).execute(connection)?;

  // Lastly, switch all blocked tasks to TODO, and complete the rerun mark pass.
  update(affected_tasks)
    .set(status.eq(TaskStatus::TODO.raw()))
    .execute(connection)?;

  Ok(())
}

pub(crate) fn mark_new_run(
  connection: &PgConnection,
  corpus: &Corpus,
  service: &Service,
  owner: String,
  description: String,
) -> Result<(), Error>
{
  // Step 1. Mark any open runs as completed.
  mark_run_completed(connection, corpus, service)?;
  // Step 2. Create this historical run
  let hrun = NewHistoricalRun {
    corpus_id: corpus.id,
    service_id: service.id,
    description,
    owner,
  };
  hrun.create(&connection)?;
  Ok(())
}

fn mark_run_completed(
  connection: &PgConnection,
  corpus: &Corpus,
  service: &Service,
) -> Result<(), Error>
{
  let to_finish: Vec<HistoricalRun> = HistoricalRun::find_by(corpus, service, connection)?
    .into_iter()
    .filter(|run| run.end_time.is_none())
    .collect();
  if !to_finish.is_empty() {
    connection.transaction::<(), Error, _>(move || {
      for run in to_finish.into_iter() {
        run.mark_completed(connection)?;
      }
      Ok(())
    })?;
  }
  Ok(())
}