prey/prey-node-client

View on GitHub
lib/agent/actions/triggers/index.js

Summary

Maintainability
F
6 days
Test Coverage

File index.js has 657 lines of code (exceeds 250 allowed). Consider refactoring.
Open

'use strict';

var schedule = require('node-schedule');
var EventEmitter = require('events').EventEmitter;
var logger = require('./../../common').logger.prefix('triggers');
Severity: Major
Found in lib/agent/actions/triggers/index.js - About 1 day to fix

    Function activate has a Cognitive Complexity of 46 (exceeds 5 allowed). Consider refactoring.
    Open

    exports.activate = (trigger) => {
      try {
        var index = trigger.automation_events.findIndex(
            (obj) => obj.type === 'exact_time' || obj.type === 'repeat_time'
          ),
    Severity: Minor
    Found in lib/agent/actions/triggers/index.js - About 7 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function sync has 120 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    exports.sync = (id, err, triggers, stored, cb) => {
      var watching = [];
    
      if (err) {
        logger.error('error starting async: ' + err);
    Severity: Major
    Found in lib/agent/actions/triggers/index.js - About 4 hrs to fix

      Function activate has 101 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

      exports.activate = (trigger) => {
        try {
          var index = trigger.automation_events.findIndex(
              (obj) => obj.type === 'exact_time' || obj.type === 'repeat_time'
            ),
      Severity: Major
      Found in lib/agent/actions/triggers/index.js - About 4 hrs to fix

        Function set_up_hooks has 74 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

        function set_up_hooks() {
          if (Object.keys(event_triggers).length == 0) return;
        
          Object.keys(event_triggers).forEach((event) => {
            hooks.on(event.split('-')[0], (info) => {
        Severity: Major
        Found in lib/agent/actions/triggers/index.js - About 2 hrs to fix

          Function activate_event has 68 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

          exports.activate_event = (trigger) => {
            try {
              var event_index = trigger.automation_events.findIndex(
                (obj) => events_list.indexOf(obj.type) > -1
              );
          Severity: Major
          Found in lib/agent/actions/triggers/index.js - About 2 hrs to fix

            Function to_unix has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
            Open

            function to_unix(date) {
              var new_date;
              try {
                if (/^\d+$/.test(date)) {
                  var year = parseInt(date.slice(0, 4)),
            Severity: Minor
            Found in lib/agent/actions/triggers/index.js - About 1 hr to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Function delete_obsolete_triggers has 28 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

            function delete_obsolete_triggers(stored, triggers) {
              logger.debug('deleting outdated triggers');
              logger.debug(
                'iterates over: ' +
                  triggers.map(function (item) {
            Severity: Minor
            Found in lib/agent/actions/triggers/index.js - About 1 hr to fix

              Function check_rules has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
              Open

              function check_rules(rule) {
                if (rule.second && (rule.second < 0 || rule.second > 60)) return false;
                if (rule.minute && (rule.minute < 0 || rule.minute > 60)) return false;
                if (rule.hour && (rule.hour < 0 || rule.hour > 24)) return false;
                if (rule.dayOfWeek.some((elem) => elem > 6 || elem < 0)) return false;
              Severity: Minor
              Found in lib/agent/actions/triggers/index.js - About 1 hr to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Function to_unix has 26 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

              function to_unix(date) {
                var new_date;
                try {
                  if (/^\d+$/.test(date)) {
                    var year = parseInt(date.slice(0, 4)),
              Severity: Minor
              Found in lib/agent/actions/triggers/index.js - About 1 hr to fix

                Consider simplifying this complex logical expression.
                Open

                  } else if (info.days_of_week && info.hour) {
                    // At least the days and hour
                
                    try {
                      let rule = new schedule.RecurrenceRule();
                Severity: Major
                Found in lib/agent/actions/triggers/index.js - About 40 mins to fix

                  Avoid too many return statements within this function.
                  Open

                        return 2;
                  Severity: Major
                  Found in lib/agent/actions/triggers/index.js - About 30 mins to fix

                    Avoid too many return statements within this function.
                    Open

                        if (!check_range_format(date_from, date_until)) return 4;
                    Severity: Major
                    Found in lib/agent/actions/triggers/index.js - About 30 mins to fix

                      Avoid too many return statements within this function.
                      Open

                              if (current_date > end_date) return 3;
                      Severity: Major
                      Found in lib/agent/actions/triggers/index.js - About 30 mins to fix

                        Avoid too many return statements within this function.
                        Open

                            if (!check_repeat_format(days, hour_from, hour_until, until)) return 4;
                        Severity: Major
                        Found in lib/agent/actions/triggers/index.js - About 30 mins to fix

                          Avoid too many return statements within this function.
                          Open

                            return true;
                          Severity: Major
                          Found in lib/agent/actions/triggers/index.js - About 30 mins to fix

                            Avoid too many return statements within this function.
                            Open

                                      if (current_triggers[trigger_index].last_exec) return;
                            Severity: Major
                            Found in lib/agent/actions/triggers/index.js - About 30 mins to fix

                              Avoid too many return statements within this function.
                              Open

                                  return 1;
                              Severity: Major
                              Found in lib/agent/actions/triggers/index.js - About 30 mins to fix

                                Avoid too many return statements within this function.
                                Open

                                      if (!check_rules(rule)) return 2;
                                Severity: Major
                                Found in lib/agent/actions/triggers/index.js - About 30 mins to fix

                                  Avoid too many return statements within this function.
                                  Open

                                      return 2;
                                  Severity: Major
                                  Found in lib/agent/actions/triggers/index.js - About 30 mins to fix

                                    Avoid too many return statements within this function.
                                    Open

                                                return;
                                    Severity: Major
                                    Found in lib/agent/actions/triggers/index.js - About 30 mins to fix

                                      Avoid too many return statements within this function.
                                      Open

                                        } else return 2;
                                      Severity: Major
                                      Found in lib/agent/actions/triggers/index.js - About 30 mins to fix

                                        Avoid too many return statements within this function.
                                        Open

                                          return 0;
                                        Severity: Major
                                        Found in lib/agent/actions/triggers/index.js - About 30 mins to fix

                                          Similar blocks of code found in 2 locations. Consider refactoring.
                                          Open

                                            const intersected_triggers = triggers.filter((new_trigger) => {
                                              return !stored.some((old_trigger) => {
                                                return old_trigger.id.toString() === new_trigger.id.toString();
                                              });
                                            });
                                          Severity: Major
                                          Found in lib/agent/actions/triggers/index.js and 1 other location - About 1 hr to fix
                                          lib/agent/actions/triggers/index.js on lines 708..712

                                          Duplicated Code

                                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                          Tuning

                                          This issue has a mass of 69.

                                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                          Refactorings

                                          Further Reading

                                          Similar blocks of code found in 2 locations. Consider refactoring.
                                          Open

                                            const intersected_triggers = stored.filter((old_trigger) => {
                                              return !triggers.some((new_trigger) => {
                                                return old_trigger.id.toString() === new_trigger.id.toString();
                                              });
                                            });
                                          Severity: Major
                                          Found in lib/agent/actions/triggers/index.js and 1 other location - About 1 hr to fix
                                          lib/agent/actions/triggers/index.js on lines 667..671

                                          Duplicated Code

                                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                          Tuning

                                          This issue has a mass of 69.

                                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                          Refactorings

                                          Further Reading

                                          Identical blocks of code found in 2 locations. Consider refactoring.
                                          Open

                                                    if (
                                                      Object.keys(dates).length == 0 ||
                                                      date.getTime() < dates.from ||
                                                      date.getTime() > dates.until
                                                    )
                                          Severity: Major
                                          Found in lib/agent/actions/triggers/index.js and 1 other location - About 1 hr to fix
                                          lib/agent/actions/triggers/index.js on lines 483..488

                                          Duplicated Code

                                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                          Tuning

                                          This issue has a mass of 59.

                                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                          Refactorings

                                          Further Reading

                                          Identical blocks of code found in 2 locations. Consider refactoring.
                                          Open

                                                if (
                                                  Object.keys(dates).length == 0 ||
                                                  date.getTime() < dates.from ||
                                                  date.getTime() > dates.until
                                                )
                                          Severity: Major
                                          Found in lib/agent/actions/triggers/index.js and 1 other location - About 1 hr to fix
                                          lib/agent/actions/triggers/index.js on lines 220..225

                                          Duplicated Code

                                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                          Tuning

                                          This issue has a mass of 59.

                                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                          Refactorings

                                          Further Reading

                                          Identical blocks of code found in 2 locations. Consider refactoring.
                                          Open

                                                if (isNaN(date_from) || isNaN(date_until) || date.getTime() < date_from || date.getTime() > date_until)
                                                  return;
                                          Severity: Minor
                                          Found in lib/agent/actions/triggers/index.js and 1 other location - About 35 mins to fix
                                          lib/agent/actions/triggers/index.js on lines 266..272

                                          Duplicated Code

                                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                          Tuning

                                          This issue has a mass of 47.

                                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                          Refactorings

                                          Further Reading

                                          Identical blocks of code found in 2 locations. Consider refactoring.
                                          Open

                                                    if (
                                                      isNaN(date_from) ||
                                                      isNaN(date_until) ||
                                                      date.getTime() < date_from ||
                                                      date.getTime() > date_until
                                          Severity: Minor
                                          Found in lib/agent/actions/triggers/index.js and 1 other location - About 35 mins to fix
                                          lib/agent/actions/triggers/index.js on lines 495..496

                                          Duplicated Code

                                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                          Tuning

                                          This issue has a mass of 47.

                                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                          Refactorings

                                          Further Reading

                                          There are no issues that match your filters.

                                          Category
                                          Status