Skip to content

Commit

Permalink
feat: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sclinede committed Sep 20, 2015
0 parents commit dcca110
Show file tree
Hide file tree
Showing 26 changed files with 1,158 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: ruby
rvm:
- 2.2.1
before_install: gem install bundler -v 1.10.6
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in ruby-reports.gemspec
gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Sergey D.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Ruby::Reports

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ruby/reports`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'ruby-reports'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install ruby-reports

## Usage

TODO: Write usage instructions here

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby-reports.


## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
11 changes: 11 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "ruby/reports"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
require "pry"
Pry.start
7 changes: 7 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

bundle install

# Do any other automated setup that you need to do here
19 changes: 19 additions & 0 deletions lib/ruby/reports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'forwardable'
require 'attr_extras'

require 'ruby/reports/services'

require 'ruby/reports/cache_file'
require 'ruby/reports/base_report'
require 'ruby/reports/csv_report'
require 'ruby/reports/cache_file'
require 'ruby/reports/config'

require 'ruby/reports/version'

module Ruby
module Reports
CP1251 = 'cp1251'.freeze
UTF8 = 'utf-8'.freeze
end
end
172 changes: 172 additions & 0 deletions lib/ruby/reports/base_report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# coding: utf-8
# Resque namespace
module Ruby
# Resque::Reports namespace
module Reports
# Class describes base report class for inheritance.
# BaseReport successor must implement "write(io, force)" method
# and may specify file extension with "extension" method call
# example:
#
# class CustomTypeReport < Resque::Reports::BaseReport
# extension :type # specify that report file must ends
# # with '.type', e.g. 'abc.type'
#
# # Method specifies how to output report data
# def write(io, force)
# io << 'Hello World!'
# end
# end
#
# BaseReport provides following DSL, example:
#
# class CustomReport < CustomTypeReport
# # include Resque::Reports::Common::BatchedReport
# # overrides data retrieving to achieve batching
# # if included 'source :select_data' becomes needless
#
# queue :custom_reports # Resque queue name
# source :select_data # method called to retrieve report data
# encoding UTF8 # file encoding
# expire_in 86_400 # cache time of the file, default: 86_400
#
# # Specify in which directory to keep this type files
# directory File.join(Dir.tmpdir, 'resque-reports')
#
# # Describe table using 'column' method
# table do |element|
# column 'Column 1 Header', :decorate_one
# column 'Column 2 Header', decorate_two(element[1])
# column 'Column 3 Header', 'Column 3 Cell'
# column 'Column 4 Header', :formatted_four, formatter: :just_cute
# end
#
# # Class initialize if needed
# # NOTE: must be used instead of define 'initialize' method
# # Default behaviour is to receive in *args Hash with report attributes
# # like: CustomReport.new(main_param: 'value') => calls send(:main_param=, 'value')
# create do |param|
# @main_param = param
# end
#
# def self.just_cute_formatter(column_value)
# "I'm so cute #{column_value}"
# end
#
# # decorate method, called by symbol-name
# def decorate_one(element)
# "decorate_one: #{element[0]}"
# end
#
# # decorate method, called directly when filling cell
# def decorate_two(text)
# "decorate_two: #{text}"
# end
#
# # method returns report data Enumerable
# def select_data
# [[0, 'text0'], [1, 'text1']]
# end
# end
class BaseReport
extend Forwardable

class << self
attr_reader :config_hash, :table_block, :progress_handle_block, :error_handle_block

def config(hash)
@config_hash = hash
end

def table(&block)
@table_block = block
end

def build(options = {})
force = options.delete(:force)

report = new(options)
report.build(force)

report
end
end

attr_reader :args, :job_id, :events_handler
def_delegators :cache_file, :filename, :exists?, :ready?

#--
# Public instance methods
#++

def initialize(*args)
@args = args
end

# Builds report synchronously
def build(force = false)
@table = nil if force
@events_handler = Services::EventsHandler.new(@progress_handle_block, @error_handle_block)

cache_file.open(force) { |file| write(file, force) }
end

def progress_handler(&block)
@progress_handle_block = block
end

def error_handler(&block)
@error_handle_block = block
end

def formatter
nil
end

private

def query
# descendant of QueryBuilder or SqlQuery with #take_batch(limit, offset) method defined
# @query ||= Query.new(self)
fail NotImplementedError
end

def iterator
@iterator ||= Services::DataIterator.new(query, config)
end

def config
@config ||= Config.new(self.class.config_hash)
end

def table
@table ||= Services::TableBuilder.new(self, self.class.table_block, config)
end

def cache_file
@cache_file ||= CacheFile.new(config.directory,
Services::FilenameGenerator.generate(args, config.extension),
expire_in: config.expire_in, coding: config.encoding)
end

# Method specifies how to output report data
# @param [IO] io stream for output
# @param [true, false] force write to output or skip due its existance
def write(io, force)
# You must use ancestor methods to work with report data:
# 1) iterator.data_size => returns source data size (calls #count on data
# retrieved from 'source')
# 2) iterator.data_each => yields given block for each source data element
# 3) table.build_header => returns Array of report column names
# 4) table.build_row(object) => returns Array of report cell
# values (same order as header)
# 5) events_handler.progress(progress, total) => call to iterate job progress
# 6) events_handler.error(error) => call to handle error in job
#
# HINT: You may override data_size and data_each, to retrieve them
# effectively
fail NotImplementedError
end
end # class BaseReport
end # module Report
end # module Resque
84 changes: 84 additions & 0 deletions lib/ruby/reports/cache_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require 'tempfile'
# coding: utf-8
module Ruby
module Reports
# Class describes how to storage and access cache file
# NOTE: Every time any cache file is opening,
# cache is cleared from old files.
class CacheFile
DEFAULT_EXPIRE_TIME = 86_400
DEFAULT_CODING = 'utf-8'.freeze

attr_reader :dir, :ext, :coding, :expiration_time
def initialize(dir, filename, options = {})
@dir = dir
@filename = File.join(dir, filename)
@ext = File.extname(@filename)

# options
@coding = options[:coding] || DEFAULT_CODING
@expiration_time = options[:expire_in] || DEFAULT_EXPIRE_TIME
end

def exists?
!expired?(@filename)
end
alias_method :ready?, :exists?

def filename
fail 'File doesn\'t exist, check exists? before' unless exists?
@filename
end

def open(force = false)
prepare_cache_dir

(force ? clear : return) if File.exists?(@filename)

with_tempfile do |tempfile|
yield tempfile

tempfile.close
FileUtils.cp(tempfile.path, @filename)
FileUtils.chmod(0644, @filename)
end
end

def clear
FileUtils.rm_f(@filename)
end

protected

def with_tempfile
yield(tempfile = Tempfile.new(Digest::MD5.hexdigest(@filename), :encoding => coding))
ensure
return unless tempfile
tempfile.close unless tempfile.closed?
tempfile.unlink
end

def prepare_cache_dir
FileUtils.mkdir_p dir # create folder if not exists
clear_expired_files
end

def clear_expired_files
# TODO: avoid races when worker building
# his report longer than @expiration_time
FileUtils.rm_f cache_files_array.select { |fname| expired?(fname) }
end

def expired?(fname)
return true unless File.file?(fname)
File.mtime(fname) + expiration_time < Time.now
end

def cache_files_array
Dir.new(dir)
.map { |fname| File.join(dir, fname) if File.extname(fname) == ext }
.compact
end
end
end
end
Loading

0 comments on commit dcca110

Please sign in to comment.