Skip to content

Commit

Permalink
#54 simple Fbe.regularly
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 7, 2024
1 parent abb1740 commit 039c3b9
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 19 deletions.
38 changes: 19 additions & 19 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,28 @@ GEM
crass (1.0.6)
decoor (0.0.1)
diff-lcs (1.5.1)
docile (1.4.0)
docile (1.4.1)
drb (2.2.1)
erubi (1.13.0)
ethon (0.16.0)
ffi (>= 1.15.0)
factbase (0.1.1)
backtrace (~> 0.3)
decoor (~> 0.0)
factbase (0.2.1)
backtrace (> 0)
decoor (> 0)
json (~> 2.7)
loog (~> 0.2)
loog (> 0)
nokogiri (~> 1.10)
others (~> 0.0)
tago (~> 0.0)
others (> 0)
tago (> 0)
yaml (~> 0.3)
faraday (2.10.0)
faraday (2.10.1)
faraday-net_http (>= 2.0, < 3.2)
logger
faraday-http-cache (2.5.1)
faraday (>= 0.8)
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (3.1.0)
faraday-net_http (3.1.1)
net-http
faraday-retry (2.2.1)
faraday (~> 2.0)
Expand All @@ -91,7 +91,7 @@ GEM
ffi (1.17.0-x86_64-darwin)
ffi (1.17.0-x86_64-linux-gnu)
gli (2.21.5)
hashdiff (1.1.0)
hashdiff (1.1.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
Expand All @@ -100,7 +100,7 @@ GEM
reline (>= 0.4.2)
iri (0.8.0)
json (2.7.2)
judges (0.18.1)
judges (0.22.1)
backtrace (~> 0)
concurrent-ruby (~> 1.2)
factbase (~> 0)
Expand All @@ -118,7 +118,7 @@ GEM
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
loog (0.5.2)
loog (0.6.0)
minitest (5.24.1)
minitest-reporters (1.7.1)
ansi
Expand All @@ -130,13 +130,13 @@ GEM
mutex_m (0.2.0)
net-http (0.4.1)
uri
nokogiri (1.16.6-arm64-darwin)
nokogiri (1.16.7-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.6-x64-mingw-ucrt)
nokogiri (1.16.7-x64-mingw-ucrt)
racc (~> 1.4)
nokogiri (1.16.6-x86_64-darwin)
nokogiri (1.16.7-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.6-x86_64-linux)
nokogiri (1.16.7-x86_64-linux)
racc (~> 1.4)
obk (0.3.1)
octokit (9.1.0)
Expand All @@ -149,7 +149,7 @@ GEM
racc
psych (5.1.2)
stringio
public_suffix (6.0.0)
public_suffix (6.0.1)
racc (1.8.1)
rack (3.1.7)
rack-session (2.0.0)
Expand Down Expand Up @@ -212,7 +212,7 @@ GEM
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
rubocop-ast (1.32.0)
parser (>= 3.3.1.0)
rubocop-performance (1.21.1)
rubocop (>= 1.48.1, < 2.0)
Expand Down Expand Up @@ -252,7 +252,7 @@ GEM
webrick (1.8.1)
yaml (0.3.0)
yard (0.9.36)
zeitwerk (2.6.16)
zeitwerk (2.6.17)

PLATFORMS
arm64-darwin-22
Expand Down
46 changes: 46 additions & 0 deletions lib/fbe/regularly.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

# MIT License
#
# Copyright (c) 2024 Zerocracy
#
# 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.

require_relative '../fbe'
require_relative 'fb'

def Fbe.regularly(area, days, interval, fb: Fbe.fb, judge: $judge, loog: $loog, &)
pmp = fb.query("(and (eq what 'pmp') (eq area '#{area}') (exists #{days}))").each.to_a.first
days = pmp.nil? ? 28 : pmp[days].first
since = Time.now - (days * 24 * 60 * 60)
interval = pmp.nil? ? 7 : pmp[interval].first
unless fb.query(
"(and
(eq what '#{judge}')
(gt when (minus (to_time (env 'TODAY' '#{Time.now.utc.iso8601}')) '#{interval} days')))"
).each.to_a.empty?
loog.debug("#{$judge} statistics have recently been collected, skipping now")
return
end
f = fb.insert
f.what = judge
f.when = Time.now
f.since = since
yield f
end
47 changes: 47 additions & 0 deletions test/fbe/test_regularly.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

# MIT License
#
# Copyright (c) 2024 Zerocracy
#
# 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.

require 'minitest/autorun'
require 'loog'
require 'factbase'
require_relative '../test__helper'
require_relative '../../lib/fbe/regularly'

# Test.
# Author:: Yegor Bugayenko ([email protected])
# Copyright:: Copyright (c) 2024 Zerocracy
# License:: MIT
class TestRegularly < Minitest::Test
def test_simple
fb = Factbase.new
loog = Loog::NULL
judge = 'test'
2.times do
Fbe.regularly('pmp', 'days', 'interval', fb:, loog:, judge:) do |f|
f.foo = 42
end
end
assert_equal(1, fb.size)
end
end

0 comments on commit 039c3b9

Please sign in to comment.