Class: Cctv::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/cctv/tracker.rb

Instance Method Summary collapse

Constructor Details

#initialize(table_name, aws_config = {}) ⇒ Tracker

Returns a new instance of Tracker.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cctv/tracker.rb', line 5

def initialize(table_name, aws_config = {})
  @table_name = table_name.to_s

  access_key_id     = aws_config.fetch(:access_key_id) { env.fetch('AWS_ACCESS_KEY_ID') }
  secret_access_key = aws_config.fetch(:secret_access_key) { env.fetch('AWS_SECRET_ACCESS_KEY') }
  region            = aws_config.fetch(:region) { env.fetch('AWS_REGION') }

  @client = Aws::DynamoDB::Client.new(
    access_key_id:     access_key_id,
    secret_access_key: secret_access_key,
    region:            region
  )
end

Instance Method Details

#record_activity(target, actor, additional_data = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cctv/tracker.rb', line 19

def record_activity(target, actor, additional_data = {})
  client.put_item(
    table_name: table_name,
    item: {
      timestamp: Time.now.utc.to_i.to_s,
      actor:     actor.to_s,
      target:    target.to_s
    }.merge(additional_data),
    return_values: 'NONE',
    return_consumed_capacity: 'TOTAL',
    return_item_collection_metrics: 'NONE',
  )
end

#view_activity(target, time_period_range, params = {}) ⇒ Object



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
# File 'lib/cctv/tracker.rb', line 33

def view_activity(target, time_period_range, params = {})
  limit              = params.fetch(:limit, 25)
  consistent_read    = params.fetch(:consistent_read, false)
  scan_index_forward = params.fetch(:scan_index_forward, true)
  attributes_to_get  = params.fetch(:attributes_to_get, nil)

  select = (attributes_to_get && attributes_to_get == :all) ? 'SPECIFIC_ATTRIBUTES' : 'ALL_ATTRIBUTES'

  client.query(
    table_name: table_name,
    select: select,
    attributes_to_get: attributes_to_get,
    limit: limit,
    consistent_read: consistent_read,
    key_conditions: {
      'target' => {
        attribute_value_list: [
          target.to_s,
        ],
        comparison_operator: 'EQ',
      },
      'timestamp' => {
        attribute_value_list: [
          (time_period_range.min.to_i).to_s,
          (time_period_range.max.to_i).to_s
        ],
        comparison_operator: 'BETWEEN'
      }
    },
    scan_index_forward: true,
    return_consumed_capacity: 'TOTAL',
  ).items
end