lib/tasks/dc_cleanup.rake in drg_cms-0.4.39 vs lib/tasks/dc_cleanup.rake in drg_cms-0.4.53
- old
+ new
@@ -19,23 +19,33 @@
# 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.
#++
+########################################################################
+# Task in this file are intend for cleaning up and making archive of statistics collections.
+#
+# rake drgcms:clear_sessions. This is just an example how to cleanup sessions collection.
+#
+# rake clear_visits. Will archive and delete dc_visits documents. You will be prompted to enter end_date.
+#
+# rake drgcms:clear_ad_stats. Will archive and delete dc_ad_stats collection. You will be prompted to enter end_date.
+########################################################################
+
namespace :drg_cms do
desc 'Clears mongodb session collection.'
task :clear_sessions, [:name] => :environment do |t, args|
- p 'This is just an example how to clear sessions collection.'
+ p 'This is just an example how to clear sessions collection. It wont do anything because of next line.'
return if true
ActionDispatch::Session::MongoidStore::Session.where(:updated_at.lt => 1.week.ago).delete
DcSite.collection.database.command(eval: "db.runCommand ( { compact: 'sessions' } )" )
end
###########################################################################
desc 'Removes all statistics from dc_visits up to specified date and save them to visits_date.json.'
task :clear_visits, [:name] => :environment do |t, args|
- date = read_input("Enter from date (yyyymmdd): ")
+ date = read_input("Enter end date (yyyymmdd): ")
return unless date.size == 8
archive_file = "visits_#{date}.json"
return (p "#{archive_file} exists") if File.exist?(archive_file)
@@ -43,43 +53,43 @@
n = 0
save = ''
DcVisit.where(:time.lt => date_to).each do |visit|
save << visit.to_json + "\n"
# visit.delete
- p "Deleted #{n}" if (n+=1)%1000 == 0
+ p "Deleted #{n}" if (n+=1)%10000 == 0
end
DcVisit.where(:time.lt => date_to).delete
File.open(archive_file,'w') {|f| f.write(save)}
DcSite.collection.database.command(eval: "db.runCommand ( { compact: 'dc_visits' } )" )
end
###########################################################################
desc "Removes all statistics of not active ads and save them to ads_#{Time.now.strftime('%Y%d%m')}.json."
task :clear_ad_stats, [:name] => :environment do |t, args|
- input = read_input("Just press Enter to start. If you type in anything process wont start.")
+ input = read_input("Just press Enter to start. If you type in anything process won't start.")
return unless input.to_s.size == 0
today = Time.now.beginning_of_day
n = 0
save_stat, save_ads = '', ''
DcAd.all.each do |ad|
if !ad.active or (ad.valid_to and ad.valid_to < today)
save_ads << ad.to_json + "\n"
DcAdStat.where(:dc_ad_id => ad._id).each do |stat|
save_stat << stat.to_json + "\n"
-# stat.delete
- p "Deleted #{n}" if (n+=1)%1000 == 0
+ p "Deleted #{n}" if (n+=1)%10000 == 0
end
DcAdStat.where(:dc_ad_id => ad._id).delete
end
end
File.open("ads_stat_#{Time.now.strftime('%Y%d%m')}.json",'w') {|f| f.write(save_stat)}
File.open("ads_#{Time.now.strftime('%Y%d%m')}.json",'w') {|f| f.write(save_ads)}
DcSite.collection.database.command(eval: "db.runCommand ( { compact: 'dc_ad_stats' } )" )
end
+=begin
###########################################################################
- desc "Popravi napako kjer se hrani dc_ad_id in ne ad_id"
+ desc "Correct error when ad_id ield is used instead of dc_ad_id."
task :repair_ad_stats, [:name] => :environment do |t, args|
input = read_input("Just press Enter to start. If you type in anything process won't start.")
return unless input.to_s.size == 0
n = 0
p DcAdStat.only(:id).where(:dc_ad_id => nil).to_a.size
@@ -89,6 +99,8 @@
stat.save
p n if (n+=1)%1000 == 0
end
end
+=end
+
end