#!/usr/bin/ruby # # This file is part of CPEE-LOGGING-XES-YAML. # # CPEE-LOGGING-XES-YAML is free software: you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by the Free # Software Foundation, either version 3 of the License, or (at your option) any # later version. # # CPEE-LOGGING-XES-YAML is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for # more details. # # You should have received a copy of the GNU Lesser General Public License along with # CPEE-LOGGING-XES-YAML (file LICENSE in the main directory). If not, see # . curpath = __dir__ require 'rubygems' require 'optparse' require 'fileutils' require 'xml/smart' require 'yaml' require 'typhoeus' require 'stringio' def wrap(s, width=78, indent=18, extra_indent=4) lines = [] line, s = s[0..indent-2], s[indent..-1] s.split(/\n/).each do |ss| ss.split(/[ \t]+/).each do |word| if line.size + word.size >= width lines << line line = (" " * (indent + extra_indent)) + word else line << " " << word end end lines << line if line line = (" " * (indent-1)) end return lines.join "\n" end def follow(fname,io,copy,deep=0) tname = if fname =~ /\.xes\.shift\.yaml/ File.basename(fname,'.xes.shift.yaml') elsif fname =~ /\.xes\.yaml/ File.basename(fname,'.xes.yaml') end if copy File.write(File.basename(fname),io.read) io.rewind end YAML.load_stream(io) do |e| if name = e.dig('log','trace','cpee:name') puts " " * deep + name + " (#{tname}) - #{e.dig('log','trace','concept:name')}" end if e.dig('event','cpee:lifecycle:transition') == 'task/instantiation' base = e.dig('event','raw') val = base.dig('CPEE-INSTANCE') rescue nil if val.nil? val = File.basename(base) end uuid = base.dig('CPEE-INSTANCE-UUID') rescue nil if uuid react File.dirname(fname) + "/#{uuid}.xes.yaml",copy,deep + 2 end end end end def react(name,copy=false,deep=0) if name.nil? help elsif name =~ /^https?:\/\// res = Typhoeus.get(name) if res.success? file = Tempfile.new('sic') file.write(res.body) file.rewind follow name, file, copy, deep file.close file.unlink end elsif File.exist? name follow name, File.open(name), copy, deep else help end end exname = File.basename($0) ARGV.options { |opt| opt.summary_indent = ' ' * 2 opt.summary_width = 15 opt.banner = "Usage:\n#{opt.summary_indent}#{exname} new [DIR] | view [URI] | copy [URI]\n" opt.on("Options:") opt.on("--help", "-h", "This text") { puts opt; exit } opt.on("") opt.on(wrap("new [DIR] scaffolds a sample logging service. Add a handler to a cpee instance to experience the pleasure.",78,18,0)) opt.on("") opt.on(wrap("view [DIR] view the dependencies between processes and subprocesses.\nWorks for local and remote logs. Examples:\n#{exname} view https://cpee.org/log/123.xes.yaml\n#{exname} view https://cpee.org/log/a.xes.yaml > index.txt\n#{exname} view ~/log/logs/456.xes.yaml")) opt.on("") opt.on(wrap("copy [DIR] copy dependent processes and subprocesses to the current\ndirectory. Works for local and remote logs. Examples:\n#{exname} copy https://cpee.org/log/123.xes.yaml\n#{exname} copy ~/log/logs/456.xes.yaml")) opt.parse! } if (ARGV.length != 2) puts ARGV.options exit else command = ARGV[0] dir = ARGV[1] end if command == 'new' if !File.exist?(dir) FileUtils.cp_r(File.join(curpath,'..','server'),dir) FileUtils.mkdir(File.join(dir,'logs')) rescue nil else puts 'Directory already exists.' end elsif command == 'view' react dir, false elsif command == 'copy' react dir, true else puts ARGV.options end