From 039c3b93cba07d4097e3a3b858c590d41513c566 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Wed, 7 Aug 2024 12:17:36 +0300 Subject: [PATCH] #54 simple Fbe.regularly --- Gemfile.lock | 38 +++++++++++++++--------------- lib/fbe/regularly.rb | 46 +++++++++++++++++++++++++++++++++++++ test/fbe/test_regularly.rb | 47 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 19 deletions(-) create mode 100644 lib/fbe/regularly.rb create mode 100644 test/fbe/test_regularly.rb diff --git a/Gemfile.lock b/Gemfile.lock index 4b2a9f4..1e31622 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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 @@ -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) @@ -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) @@ -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) @@ -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 diff --git a/lib/fbe/regularly.rb b/lib/fbe/regularly.rb new file mode 100644 index 0000000..b75d881 --- /dev/null +++ b/lib/fbe/regularly.rb @@ -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 diff --git a/test/fbe/test_regularly.rb b/test/fbe/test_regularly.rb new file mode 100644 index 0000000..519000e --- /dev/null +++ b/test/fbe/test_regularly.rb @@ -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 (yegor256@gmail.com) +# 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