/ cloudformation-templates / node_modules / sane / src / utils / recrawl-warning-dedupe.js
recrawl-warning-dedupe.js
 1  'use strict';
 2  
 3  class RecrawlWarning {
 4    constructor(root, count) {
 5      this.root = root;
 6      this.count = count;
 7    }
 8  
 9    static findByRoot(root) {
10      for (let i = 0; i < this.RECRAWL_WARNINGS.length; i++) {
11        let warning = this.RECRAWL_WARNINGS[i];
12        if (warning.root === root) {
13          return warning;
14        }
15      }
16    }
17  
18    static isRecrawlWarningDupe(warningMessage) {
19      if (typeof warningMessage !== 'string') {
20        return false;
21      }
22      let match = warningMessage.match(this.REGEXP);
23      if (!match) {
24        return false;
25      }
26      let count = Number(match[1]);
27      let root = match[2];
28  
29      let warning = this.findByRoot(root);
30  
31      if (warning) {
32        // only keep the highest count, assume count to either stay the same or
33        // increase.
34        if (warning.count >= count) {
35          return true;
36        } else {
37          // update the existing warning to the latest (highest) count
38          warning.count = count;
39          return false;
40        }
41      } else {
42        this.RECRAWL_WARNINGS.push(new RecrawlWarning(root, count));
43        return false;
44      }
45    }
46  }
47  
48  RecrawlWarning.RECRAWL_WARNINGS = [];
49  RecrawlWarning.REGEXP = /Recrawled this watch (\d+) times, most recently because:\n([^:]+)/;
50  
51  module.exports = RecrawlWarning;