Module: CPEE::Logging

Defined in:
lib/cpee-logging-xes-yaml/tools.rb,
lib/cpee-logging-xes-yaml/logging.rb

Defined Under Namespace

Classes: Handler

Constant Summary collapse

SERVER =
File.expand_path(File.join(__dir__,'logging.xml'))

Class Method Summary collapse

Class Method Details

.doc(topic, event_name, log_dir, template, payload) ⇒ Object



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
# File 'lib/cpee-logging-xes-yaml/tools.rb', line 83

def self::doc(topic,event_name,log_dir,template,payload)
  notification = JSON.parse(payload)
  instance = notification['instance-uuid']
  return unless instance

  instancenr = notification['instance']
  content = notification['content']
  activity = content['activity']
  parameters = content['parameters']
  receiving = content['received']

  if content['dslx']
    CPEE::Logging::extract_probes(File.join(log_dir,instance),content['dslx'])
  end

  if topic == 'dataelements' && event_name == 'change'
    if content['changed']&.any?
      CPEE::Logging::persist_values(File.join(log_dir,instance + '.data.json'),content['values'])
    end
  end

  log = YAML::load(File.read(template))
  log["log"]["trace"]["concept:name"] ||= instancenr
  log["log"]["trace"]["cpee:name"] ||= notification['instance-name'] if notification['instance-name']
  log["log"]["trace"]["cpee:instance"] ||= instance
  File.open(File.join(log_dir,instance+'.xes.yaml'),'w'){|f| f.puts log.to_yaml} unless File.exists? File.join(log_dir,instance+'.xes.yaml')
  event = {}
  event["concept:instance"] = instancenr
  event["concept:name"] = content["label"] if content["label"]
  if content["endpoint"]
    event["concept:endpoint"] = content["endpoint"]
  end
  event["id:id"] = (activity.nil? || activity == "") ? 'external' : activity
  event["cpee:activity"] = event["id:id"]
  event["cpee:activity_uuid"] = content['activity-uuid'] if content['activity-uuid']
  event["cpee:instance"] = instance
  case event_name
    when 'receiving', 'change', 'instantiation'
      event["lifecycle:transition"] = "unknown"
    when 'done'
      event["lifecycle:transition"] = "complete"
    else
      event["lifecycle:transition"] = "start"
  end
  event["cpee:lifecycle:transition"] = "#{topic}/#{event_name}"
  event["cpee:state"] = content['state'] if content['state']
  event["cpee:description"] = content['dslx'] if content['dslx']
  unless parameters["arguments"]&.nil?
    event["data"] = parameters["arguments"]
  end if parameters
  if content['changed']&.any?
    event["data"] = content['values']
  end
  if receiving && !receiving.empty?
    fname = File.join(log_dir,instance + '_' + event["id:id"] + '.probe')
    dname = File.join(log_dir,instance + '.data.json')

    if File.exists?(fname)
      te = event.dup
      te['stream:sensorstream'] = []

      rs = WEEL::ReadStructure.new(File.exists?(dname) ? JSON::load(File::open(dname)) : {},{},{})
      XML::Smart::open_unprotected(fname) do |doc|
        doc.register_namespace 'd', 'http://cpee.org/ns/description/1.0'
        doc.find('//d:probe').each do |p|
          tid = p.find('string(d:id)')
          tso = p.find('string(d:source)')
          if p.find('d:extractor_type[.="extrinsic"]')
            val = CPEE::Logging::extract_sensor(rs,p.find('string(d:extractor_code)'),receiving) rescue nil
            if val.is_a? Array
              val.each do |e|
                if e.is_a? StreamPoint
                  e.source ||= tso
                  te['stream:sensorstream'] << e.to_h(tid)
                end
              end
            else
              tp = StreamPoint.new
              tp.source =  tso
              tp.value = val
              te['stream:sensorstream'] << tp.to_h(tid)
            end
          end
        end
      end
      te["cpee:lifecycle:transition"] = "sensor/stream"
      File.open(File.join(log_dir,instance+'.xes.yaml'),'a') do |f|
        f << {'event' => te}.to_yaml
      end
    end

    event["raw"] = receiving
  end
  event["time:timestamp"]= event['cpee:timestamp'] || Time.now.strftime("%Y-%m-%dT%H:%M:%S.%L%:z")
  File.open(File.join(log_dir,instance+'.xes.yaml'),'a') do |f|
    f << {'event' => event}.to_yaml
  end
end

.extract_probes(where, xml) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/cpee-logging-xes-yaml/tools.rb', line 27

def self::extract_probes(where,xml)
  XML::Smart::string(xml) do |doc|
    doc.register_namespace 'd', 'http://cpee.org/ns/description/1.0'
    doc.find('//d:call').each do |c|
      c.find('d:annotations/d:_context_data_analysis/d:probes').each do |p|
        File.write(where + '_' + c.attributes['id'] + '.probe', p.dump)
      end
    end
  end
end

.extract_sensor(rs, code, result) ⇒ Object



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
# File 'lib/cpee-logging-xes-yaml/tools.rb', line 38

def self::extract_sensor(rs,code,result)
  result.map! do |res|
    if res['mimetype'].nil?
      res['value']
    elsif res['mimetype'] == 'application/json'
      JSON::parse(res['data'])
    elsif res['mimetype'] == 'application/xml' || res['mimetype'] == 'text/xml'
      XML::Smart::string(res['data']) rescue nil
    elsif res.mimetype == 'text/yaml'
      YAML::load(res['data']) rescue nil
    elsif result[0].mimetype == 'text/plain'
      t = res['data']
      if t.start_with?("<?xml version=")
        t = XML::Smart::string(t)
      else
        t = t.to_f if t == t.to_f.to_s
        t = t.to_i if t == t.to_i.to_s
      end
      t
    elsif res.mimetype == 'text/html'
      t = res['data']
      t = t.to_f if t == t.to_f.to_s
      t = t.to_i if t == t.to_i.to_s
      t
    else
      res['data']
    end
  end
  result = result[0] if result.length == 1
  rs.instance_eval(code)
end

.implementation(opts) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/cpee-logging-xes-yaml/logging.rb', line 45

def self::implementation(opts)
  opts[:log_dir] ||= File.join(__dir__,'logs')
  opts[:template] ||= File.join(__dir__,'template.xes_yaml')

  Proc.new do
    interface 'events' do
      run Handler, opts[:log_dir], opts[:template] if post 'event'
    end
  end
end

.persist_values(where, values) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cpee-logging-xes-yaml/tools.rb', line 70

def self::persist_values(where,values)
  unless File.exists?(where)
    File.write(where,'{}')
  end
  f = File.open(where,'r+')
  f.flock(File::LOCK_EX)
  json = JSON::load(f).merge(values)
  f.rewind
  f.truncate(0)
  f.write(JSON.generate(json))
  f.close
end