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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// Copyright 2015-2018 Deyan Ginev. See the LICENSE
// file at the top-level directory of this distribution.
//
// Licensed under the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>.
// This file may not be copied, modified, or distributed
// except according to those terms.

//! Helper structures and methods for Task
use rand::{thread_rng, Rng};
use regex::Regex;
use std::fs::File;
use std::io;
use std::path::Path;
use std::str;
use Archive::*;

use diesel::pg::PgConnection;
use diesel::result::Error;

use crate::concerns::CortexInsertable;
use crate::models::{
  LogError, LogFatal, LogInfo, LogInvalid, LogRecord, LogWarning, NewLogError, NewLogFatal,
  NewLogInfo, NewLogInvalid, NewLogWarning, Task,
};

const BUFFER_SIZE: usize = 10_240;

lazy_static! {
  static ref MESSAGE_LINE_REGEX: Regex =
    Regex::new(r"^([^ :]+):([^ :]+):([^ ]+)(\s(.*))?$").unwrap();
  /// "(Loading... file" message regex
  pub static ref LOADING_LINE_REGEX: Regex =
    Regex::new(r"^\(Loading\s(.+/)?([^/]+[^.])\.\.\.(\s|$)").unwrap();
}

#[derive(Clone, PartialEq, Eq, Debug)]
/// An enumeration of the expected task statuses
pub enum TaskStatus {
  /// currently queued for processing
  TODO,
  /// everything went smoothly
  NoProblem,
  /// minor issues
  Warning,
  /// major issues
  Error,
  /// critical/panic issues
  Fatal,
  /// invalid task, fatal + discard from statistics
  Invalid,
  /// currently blocked by dependencies
  Blocked(i32),
  /// currently being processed (marker identifies batch)
  Queued(i32),
}

#[derive(Clone, Debug)]
/// In-progress task, with dispatch metadata
pub struct TaskProgress {
  /// the `Task` struct being tracked
  pub task: Task,
  /// time of entering the job queue / first dispatch
  pub created_at: i64,
  /// number of dispatch retries
  pub retries: i64,
}
impl TaskProgress {
  /// What is the latest admissible time for this task to be completed?
  pub fn expected_at(&self) -> i64 { self.created_at + ((self.retries + 1) * 3600) }
}

#[derive(Clone, Debug)]
/// Completed task, with its processing status and report messages
pub struct TaskReport {
  /// the `Task` we are reporting on
  pub task: Task,
  /// the reported processing status
  pub status: TaskStatus,
  /// a vector of `TaskMessage` log entries
  pub messages: Vec<NewTaskMessage>,
}

#[derive(Clone, Debug)]
/// Enum for all types of reported messages for a given Task, as per the `LaTeXML` convention
/// One of "invalid", "fatal", "error", "warning" or "info"
pub enum TaskMessage {
  /// Debug/low-priroity messages
  Info(LogInfo),
  /// Soft/resumable problem messages
  Warning(LogWarning),
  /// Hard/recoverable problem messages
  Error(LogError),
  /// Critical/unrecoverable problem messages
  Fatal(LogFatal),
  /// Invalid tasks, work can not begin
  Invalid(LogInvalid),
}
impl LogRecord for TaskMessage {
  fn task_id(&self) -> i64 {
    use crate::helpers::TaskMessage::*;
    match *self {
      Info(ref record) => record.task_id(),
      Warning(ref record) => record.task_id(),
      Error(ref record) => record.task_id(),
      Fatal(ref record) => record.task_id(),
      Invalid(ref record) => record.task_id(),
    }
  }
  fn category(&self) -> &str {
    use crate::helpers::TaskMessage::*;
    match *self {
      Info(ref record) => record.category(),
      Warning(ref record) => record.category(),
      Error(ref record) => record.category(),
      Fatal(ref record) => record.category(),
      Invalid(ref record) => record.category(),
    }
  }
  fn what(&self) -> &str {
    use crate::helpers::TaskMessage::*;
    match *self {
      Info(ref record) => record.what(),
      Warning(ref record) => record.what(),
      Error(ref record) => record.what(),
      Fatal(ref record) => record.what(),
      Invalid(ref record) => record.what(),
    }
  }
  fn details(&self) -> &str {
    use crate::helpers::TaskMessage::*;
    match *self {
      Info(ref record) => record.details(),
      Warning(ref record) => record.details(),
      Error(ref record) => record.details(),
      Fatal(ref record) => record.details(),
      Invalid(ref record) => record.details(),
    }
  }
  fn set_details(&mut self, new_details: String) {
    use crate::helpers::TaskMessage::*;
    match *self {
      Info(ref mut record) => record.set_details(new_details),
      Warning(ref mut record) => record.set_details(new_details),
      Error(ref mut record) => record.set_details(new_details),
      Fatal(ref mut record) => record.set_details(new_details),
      Invalid(ref mut record) => record.set_details(new_details),
    }
  }
  fn severity(&self) -> &str {
    use crate::helpers::TaskMessage::*;
    match *self {
      Info(ref record) => record.severity(),
      Warning(ref record) => record.severity(),
      Error(ref record) => record.severity(),
      Fatal(ref record) => record.severity(),
      Invalid(ref record) => record.severity(),
    }
  }
}

impl TaskStatus {
  /// Maps the enumeration into the raw ints for the Task store
  pub fn raw(&self) -> i32 {
    match *self {
      TaskStatus::TODO => 0,
      TaskStatus::NoProblem => -1,
      TaskStatus::Warning => -2,
      TaskStatus::Error => -3,
      TaskStatus::Fatal => -4,
      TaskStatus::Invalid => -5,
      TaskStatus::Blocked(x) | TaskStatus::Queued(x) => x,
    }
  }
  /// Maps the enumeration into the raw severity string for the Task store logs / frontend reports
  pub fn to_key(&self) -> String {
    match *self {
      TaskStatus::NoProblem => "no_problem",
      TaskStatus::Warning => "warning",
      TaskStatus::Error => "error",
      TaskStatus::Fatal => "fatal",
      TaskStatus::TODO => "todo",
      TaskStatus::Invalid => "invalid",
      TaskStatus::Blocked(_) => "blocked",
      TaskStatus::Queued(_) => "queued",
    }
    .to_string()
  }
  /// Maps the enumeration into the Postgresql table name expected to hold messages for this
  /// status
  pub fn to_table(&self) -> String {
    match *self {
      TaskStatus::Warning => "log_warnings",
      TaskStatus::Error => "log_errors",
      TaskStatus::Fatal => "log_fatals",
      TaskStatus::Invalid => "log_invalids",
      _ => "log_infos",
    }
    .to_string()
  }
  /// Maps from the raw Task store value into the enumeration
  pub fn from_raw(num: i32) -> Self {
    match num {
      0 => TaskStatus::TODO,
      -1 => TaskStatus::NoProblem,
      -2 => TaskStatus::Warning,
      -3 => TaskStatus::Error,
      -4 => TaskStatus::Fatal,
      -5 => TaskStatus::Invalid,
      num if num < -5 => TaskStatus::Blocked(num),
      _ => TaskStatus::Queued(num),
    }
  }
  /// Maps from the raw severity log values into the enumeration
  pub fn from_key(key: &str) -> Option<Self> {
    match key.to_lowercase().as_str() {
      "no_problem" => Some(TaskStatus::NoProblem),
      "warning" => Some(TaskStatus::Warning),
      "error" => Some(TaskStatus::Error),
      "todo" => Some(TaskStatus::TODO),
      "in_progress" => Some(TaskStatus::TODO),
      "invalid" => Some(TaskStatus::Invalid),
      "blocked" => Some(TaskStatus::Blocked(-6)),
      "queued" => Some(TaskStatus::Queued(1)),
      "fatal" => Some(TaskStatus::Fatal),
      _ => None,
    }
  }
  /// Returns all raw severity strings as a vector
  pub fn keys() -> Vec<String> {
    [
      "no_problem",
      "warning",
      "error",
      "fatal",
      "invalid",
      "todo",
      "blocked",
      "queued",
    ]
    .iter()
    .map(|&x| x.to_string())
    .collect::<Vec<_>>()
  }
}

#[derive(Clone, Debug)]
/// Enum for all types of reported messages for a given Task, as per the `LaTeXML` convention
/// One of "invalid", "fatal", "error", "warning" or "info"
pub enum NewTaskMessage {
  /// Debug/low-priroity messages
  Info(NewLogInfo),
  /// Soft/resumable problem messages
  Warning(NewLogWarning),
  /// Hard/recoverable problem messages
  Error(NewLogError),
  /// Critical/unrecoverable problem messages
  Fatal(NewLogFatal),
  /// Invalid tasks, work can not begin
  Invalid(NewLogInvalid),
}
impl LogRecord for NewTaskMessage {
  fn task_id(&self) -> i64 {
    use crate::helpers::NewTaskMessage::*;
    match *self {
      Info(ref record) => record.task_id(),
      Warning(ref record) => record.task_id(),
      Error(ref record) => record.task_id(),
      Fatal(ref record) => record.task_id(),
      Invalid(ref record) => record.task_id(),
    }
  }
  fn category(&self) -> &str {
    use crate::helpers::NewTaskMessage::*;
    match *self {
      Info(ref record) => record.category(),
      Warning(ref record) => record.category(),
      Error(ref record) => record.category(),
      Fatal(ref record) => record.category(),
      Invalid(ref record) => record.category(),
    }
  }
  fn what(&self) -> &str {
    use crate::helpers::NewTaskMessage::*;
    match *self {
      Info(ref record) => record.what(),
      Warning(ref record) => record.what(),
      Error(ref record) => record.what(),
      Fatal(ref record) => record.what(),
      Invalid(ref record) => record.what(),
    }
  }
  fn details(&self) -> &str {
    use crate::helpers::NewTaskMessage::*;
    match *self {
      Info(ref record) => record.details(),
      Warning(ref record) => record.details(),
      Error(ref record) => record.details(),
      Fatal(ref record) => record.details(),
      Invalid(ref record) => record.details(),
    }
  }
  fn set_details(&mut self, new_details: String) {
    use crate::helpers::NewTaskMessage::*;
    match *self {
      Info(ref mut record) => record.set_details(new_details),
      Warning(ref mut record) => record.set_details(new_details),
      Error(ref mut record) => record.set_details(new_details),
      Fatal(ref mut record) => record.set_details(new_details),
      Invalid(ref mut record) => record.set_details(new_details),
    }
  }

  fn severity(&self) -> &str {
    use crate::helpers::NewTaskMessage::*;
    match *self {
      Info(ref record) => record.severity(),
      Warning(ref record) => record.severity(),
      Error(ref record) => record.severity(),
      Fatal(ref record) => record.severity(),
      Invalid(ref record) => record.severity(),
    }
  }
}
impl CortexInsertable for NewTaskMessage {
  fn create(&self, connection: &PgConnection) -> Result<usize, Error> {
    use crate::helpers::NewTaskMessage::*;
    match *self {
      Info(ref record) => record.create(connection),
      Warning(ref record) => record.create(connection),
      Error(ref record) => record.create(connection),
      Fatal(ref record) => record.create(connection),
      Invalid(ref record) => record.create(connection),
    }
  }
}

impl NewTaskMessage {
  /// Instantiates an appropriate insertable LogRecord object based on the raw message components
  pub fn new(
    task_id: i64,
    severity: &str,
    category: String,
    what: String,
    details: String,
  ) -> NewTaskMessage
  {
    match severity.to_lowercase().as_str() {
      "warning" => NewTaskMessage::Warning(NewLogWarning {
        task_id,
        category,
        what,
        details,
      }),
      "error" => NewTaskMessage::Error(NewLogError {
        task_id,
        category,
        what,
        details,
      }),
      "fatal" => NewTaskMessage::Fatal(NewLogFatal {
        task_id,
        category,
        what,
        details,
      }),
      "invalid" => NewTaskMessage::Invalid(NewLogInvalid {
        task_id,
        category,
        what,
        details,
      }),
      _ => NewTaskMessage::Info(NewLogInfo {
        task_id,
        category,
        what,
        details,
      }), // unknown severity will be treated as info
    }
  }
}

/// Parses a log string which follows the `LaTeXML` convention
/// (described at [the Manual](http://dlmf.nist.gov/LaTeXML/manual/errorcodes/index.html))
pub fn parse_log(task_id: i64, log: &str) -> Vec<NewTaskMessage> {
  let mut messages: Vec<NewTaskMessage> = Vec::new();
  let mut in_details_mode = false;

  for line in log.lines() {
    // Skip empty lines
    if line.is_empty() {
      continue;
    }
    // If we have found a message header and we're collecting details:
    if in_details_mode {
      // If the line starts with tab, we are indeed reading in details
      if line.starts_with('\t') {
        // Append details line to the last message
        let mut last_message = messages.pop().unwrap_or_else(|| {
          panic!("parse_log tried to parse details without having a log message, invalid log file?")
        });
        let mut truncated_details = last_message.details().to_string() + "\n" + line;
        utf_truncate(&mut truncated_details, 2000);
        last_message.set_details(truncated_details);
        messages.push(last_message);
        continue; // This line has been consumed, next
      } else {
        // Otherwise, no tab at the line beginning means last message has ended
        in_details_mode = false;
        if in_details_mode {} // hacky? disable "unused" warning
      }
    }
    // Since this isn't a details line, check if it's a message line:
    if let Some(cap) = MESSAGE_LINE_REGEX.captures(line) {
      // Indeed a message, so record it
      // We'll need to do some manual truncations, since the POSTGRESQL wrapper prefers
      //   panicking to auto-truncating (would not have been the Perl way, but Rust is Rust)
      let mut truncated_severity = cap
        .get(1)
        .map_or("", |m| m.as_str())
        .to_string()
        .to_lowercase();
      utf_truncate(&mut truncated_severity, 50);
      let mut truncated_category = cap.get(2).map_or("", |m| m.as_str()).to_string();
      utf_truncate(&mut truncated_category, 50);
      let mut truncated_what = cap.get(3).map_or("", |m| m.as_str()).to_string();
      utf_truncate(&mut truncated_what, 50);
      let mut truncated_details = cap.get(5).map_or("", |m| m.as_str()).to_string();
      utf_truncate(&mut truncated_details, 2000);

      if truncated_severity == "fatal" && truncated_category == "invalid" {
        truncated_severity = "invalid".to_string();
        truncated_category = truncated_what;
        truncated_what = "all".to_string();
      }

      let message = NewTaskMessage::new(
        task_id,
        &truncated_severity,
        truncated_category,
        truncated_what,
        truncated_details,
      );
      // Prepare to record follow-up lines with the message details:
      in_details_mode = true;
      // Add to the array of parsed messages
      messages.push(message);
    } else {
      in_details_mode = false; // not a details line.
      if let Some(cap) = LOADING_LINE_REGEX.captures(line) {
        // Special case is a "Loading..." info messages
        let mut filepath = cap.get(1).map_or("", |m| m.as_str()).to_string();
        let mut filename = cap.get(2).map_or("", |m| m.as_str()).to_string();
        utf_truncate(&mut filename, 50);
        filepath += &filename;
        utf_truncate(&mut filepath, 50);
        messages.push(NewTaskMessage::new(
          task_id,
          "info",
          "loaded_file".to_string(),
          filename,
          filepath,
        ));
      } else {
        // Otherwise line is just noise, continue...
      }
    }
  }
  messages
}

/// Generates a `TaskReport`, given the path to a result archive from a `CorTeX` processing job
/// Expects a "cortex.log" file in the archive, following the `LaTeXML` messaging conventions
pub fn generate_report(task: Task, result: &Path) -> TaskReport {
  // println!("Preparing report for {:?}, result at {:?}",self.entry, result);
  let mut messages = Vec::new();
  let mut status = TaskStatus::Fatal; // Fatal by default
  {
    // -- Archive::Reader, trying to localize (to .drop asap)
    // Let's open the archive file and find the cortex.log file:
    let log_name = "cortex.log";
    match Reader::new()
      .unwrap_or_else(|_| panic!("Could not create libarchive Reader struct"))
      .support_filter_all()
      .support_format_all()
      .open_filename(result.to_str().unwrap_or_default(), BUFFER_SIZE)
    {
      Err(e) => {
        println!("Error TODO: Couldn't open archive_reader: {:?}", e);
      },
      Ok(archive_reader) => {
        while let Ok(entry) = archive_reader.next_header() {
          if entry.pathname() != log_name {
            continue;
          }

          // In a "raw" read, we don't know the data size in advance. So we bite the bullet and
          // read the usually manageable log file in memory
          let mut raw_log_data = Vec::new();
          while let Ok(chunk) = archive_reader.read_data(BUFFER_SIZE) {
            raw_log_data.extend(chunk.into_iter());
          }
          let log_string: String = match str::from_utf8(&raw_log_data) {
            Ok(some_utf_string) => some_utf_string.to_string(),
            Err(e) => {
              "Fatal:cortex:unicode_parse_error ".to_string()
                + &e.to_string()
                + "\nStatus:conversion:3"
            },
          };

          // Look for the special status message - Fatal otherwise!
          for message in parse_log(task.id, &log_string).into_iter() {
            // Invalids are a bit of a workaround for now, they're fatal messages in latexml, but
            // we want them separated out in cortex
            let mut skip_message = false;
            match message {
              NewTaskMessage::Invalid(ref _log_invalid) => {
                status = TaskStatus::Invalid;
              },
              NewTaskMessage::Info(ref _log_info) => {
                let message_what = message.what();
                if message.category() == "conversion" && !message_what.is_empty() {
                  // Adapt status to the CorTeX scheme: cortex_status = -(latexml_status+1)
                  let latexml_scheme_status = match message_what.parse::<i32>() {
                    Ok(num) => num,
                    Err(e) => {
                      println!(
                        "Error TODO: Failed to parse conversion status {:?}: {:?}",
                        message_what, e
                      );
                      3 // latexml raw fatal
                    },
                  };
                  let cortex_scheme_status = -(latexml_scheme_status + 1);
                  if status != TaskStatus::Invalid {
                    // Invalid status is final, and derived, all others are set directly from the
                    // log.
                    status = TaskStatus::from_raw(cortex_scheme_status);
                  }
                  skip_message = true; // do not record the status message
                }
              },
              _ => {},
            };
            if !skip_message {
              messages.push(message);
            }
          }
          // We recorded the messages, stop archive traversal
          break;
        }
        drop(archive_reader);
      },
    }
  } // -- END: Archive::Reader, trying to localize (to .drop asap)

  TaskReport {
    task,
    status,
    messages,
  }
}

/// Returns an open file handle to the task's entry
pub fn prepare_input_stream(task: &Task) -> Result<File, io::Error> {
  let entry_path = Path::new(&task.entry);
  File::open(entry_path)
}

/// Utility functions, until they find a better place
pub fn utf_truncate(input: &mut String, maxsize: usize) {
  let mut utf_maxsize = input.len();
  if utf_maxsize >= maxsize {
    {
      let mut char_iter = input.char_indices();
      while utf_maxsize >= maxsize {
        utf_maxsize = match char_iter.next_back() {
          Some((index, _)) => index,
          _ => 0,
        };
      }
    } // Extra {} wrap to limit the immutable borrow of char_indices()
    input.truncate(utf_maxsize);
  }
  // eliminate null characters if any
  *input = input.replace("\x00", "");
}

/// Generate a random integer useful for temporary DB marks
pub fn random_mark() -> i32 {
  let mut rng = thread_rng();
  let mark_rng: u16 = rng.gen();
  i32::from(mark_rng)
}

/// Helper for generating a random i32 in a range, to avoid loading the rng crate + boilerplate
pub fn rand_in_range(from: u16, to: u16) -> u16 {
  let mut rng = thread_rng();
  let mark_rng: u16 = rng.gen_range(from, to);
  mark_rng
}