Module: Mihari::Commands::Alert

Included in:
Mihari::CLI::Alert
Defined in:
lib/mihari/commands/alert.rb

Overview

Alert sub-commands

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object

rubocop:disable Metrics/AbcSize



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
# File 'lib/mihari/commands/alert.rb', line 11

def included(thor)
  thor.class_eval do
    include Concerns::DatabaseConnectable

    no_commands do
      #
      # @param [String] q
      # @param [Integer] page
      # @param [Integer] limit
      #
      # @return [Mihari::Services::ResultValue]
      #
      def _search(q, page: 1, limit: 10)
        filter = Structs::Filters::Search.new(q:, page:, limit:)
        Services::AlertSearcher.result(filter).value!
      end
    end

    desc "create PATH", "Create an alert"
    around :with_db_connection
    #
    # @param [String] path
    #
    def create(path)
      # @type [Mihari::Models::Alert]
      alert = Dry::Monads::Try[StandardError] do
        raise ArgumentError, "#{path} not found" unless Pathname(path).exist?

        params = YAML.safe_load(
          ERB.new(File.read(path)).result,
          permitted_classes: [Date, Symbol]
        )
        Services::AlertCreator.call params
      end.value!
      data = Entities::Alert.represent(alert)
      puts JSON.pretty_generate(data.as_json)
    end

    desc "list QUERY", "List/search alerts"
    around :with_db_connection
    method_option :page, type: :numeric, default: 1
    method_option :limit, type: :numeric, default: 10
    #
    # @param [String] q
    #
    def list(q = "")
      value = _search(q, page: options["page"], limit: options["limit"])
      data = Entities::AlertsWithPagination.represent(
        results: value.results,
        total: value.total,
        current_page: value.filter[:page].to_i,
        page_size: value.filter[:limit].to_i
      )
      puts JSON.pretty_generate(data.as_json)
    end

    desc "list-transform QUERY", "List/search alerts with transformation"
    around :with_db_connection
    method_option :template, type: :string, required: true, aliases: "-t",
      desc: "Jbuilder template stringor a path to a template"
    method_option :page, type: :numeric, default: 1
    method_option :limit, type: :numeric, default: 10
    #
    # @param [String] q
    #
    def list_transform(q = "")
      value = _search(q, page: options["page"], limit: options["limit"])
      puts Services::JbuilderRenderer.call(
        options["template"],
        {
          results: value.results,
          total: value.total,
          current_page: value.filter[:page].to_i,
          page_size: value.filter[:limit].to_i
        }
      )
    end

    desc "get ID", "Get an alert"
    around :with_db_connection
    #
    # @param [Integer] id
    #
    def get(id)
      value = Services::AlertGetter.result(id).value!
      data = Entities::Alert.represent(value)
      puts JSON.pretty_generate(data.as_json)
    end

    desc "delete ID", "Delete an alert"
    around :with_db_connection
    #
    # @param [Integer] id
    #
    def delete(id)
      Services::AlertDestroyer.result(id).value!
    end
  end
end