From ee07f1c40dd4180b26291686fb6deda761c6230d Mon Sep 17 00:00:00 2001 From: chiu Date: Tue, 3 Mar 2020 19:30:15 +0800 Subject: [PATCH] fix traffic loading slow problem and close watchdog --- announcement.gemspec | 5 + temp_file/app/helpers/orbit_backend_helper.rb | 255 ++++++++++++++++++ temp_file/app/models/site.rb | 16 ++ 3 files changed, 276 insertions(+) create mode 100644 temp_file/app/helpers/orbit_backend_helper.rb diff --git a/announcement.gemspec b/announcement.gemspec index bb3a79e..edd7bd7 100644 --- a/announcement.gemspec +++ b/announcement.gemspec @@ -148,6 +148,11 @@ if old_gemfile_text != new_gemfile_text a.run #Thread.exit else + dir_name = env_pwd.split('/')[-1] + watch_dog = %x[screen -ls auto_reopen_#{dir_name}] + if watch_dog.scan("auto_reopen_#{dir_name}").count != 0 + Bundler.with_clean_env{%x[screen -ls | grep auto_reopen_#{dir_name} | cut -d. -f1 | awk '{print $1}' | xargs kill]} + end system("cp -rf #{app_path}/temp_file/app #{env_pwd}") end # Describe your gem and declare its dependencies: diff --git a/temp_file/app/helpers/orbit_backend_helper.rb b/temp_file/app/helpers/orbit_backend_helper.rb new file mode 100644 index 0000000..94f2d1d --- /dev/null +++ b/temp_file/app/helpers/orbit_backend_helper.rb @@ -0,0 +1,255 @@ +require "uri" +require "net/http" +module OrbitBackendHelper + + def self.included(base) + ActionView::Helpers::FormBuilder.send(:include, Orbit::FormBuilder) + end + + def thead(field,center=false,enable_sort=true) + sort = field.to_s.include?('.') ? field.to_s.split('.')[1] : field.to_s + active = params[:sort].eql? sort + order = active ? (["asc", "desc"]-[params[:order]]).first : "asc" + arrow = (order.eql? "desc") ? "" : "" + klass = field.eql?(:title) ? "span5" : "span2" + + th_data = (sort=="preview" || !enable_sort) ? t(field.to_sym) : "#{t(field.to_sym)} #{active ? arrow : ""}" + + "#{th_data}".html_safe + end + + def datetime_picker(object_name, method, options = {}) + options[:icon_time] ||= 'icons-clock' + options[:icon_date] ||= 'icons-calendar' + options[:icon_clear] ||= 'icons-cross-3' + options[:input_class] ||= 'input-large' + options[:new_record] = true if options[:new_record].nil? + options[:value] ||= options[:object].send(method) if options[:object] && options[:object][method] + case options[:picker_type] + when 'date' + content_tag :div, :id => options[:id], :class => options[:class] do + date_picker(object_name, method, options) + end + when 'time' + content_tag :div, :id => options[:id], :class => options[:class] do + time_picker(object_name, method, options) + end + when 'separated' + options[:label] ||= I18n.t('datetime_picker.separated.label') + content_tag :div, :id => options[:id], :class => "separated_picker #{options[:class]}" do + concat label_tag options[:label] unless options[:no_label] + concat hidden_field(object_name, method, :value => options[:value]) + concat separated_picker(object_name, method, options) + end + when 'simple' + content_tag :div, :id => options[:id], :class => options[:class] do + simple_picker(object_name, method, options) + end + else + content_tag :div, :id => options[:id], :class => options[:class] do + default_picker(object_name, method, options) + end + end + end + + def default_picker(object_name, method, options) + custom = {} + custom[:format] = options[:format] || 'yyyy/MM/dd hh:mm' + custom[:value] = format_value(options[:value], custom[:format]) if options[:value] && !options[:new_record] + custom[:value] = "" if options[:new_record] + custom[:picker_class] = 'default_picker' + custom[:label] = options[:label] || I18n.t('datetime_picker.default.label') + custom[:placeholder] = options[:placeholder] || I18n.t('datetime_picker.default.placeholder') + picker(object_name, method, options.merge(custom)) + end + + def picker(object_name, method, options) + content_tag :div, :class => "#{options[:picker_class]} input-append", :style => "#{(options[:picker_class].eql?('time_picker') && options[:value].blank? && options[:separated]) ? 'pointer-events:none' : nil}" do + concat label_tag options[:label] unless options[:no_label] + concat text_field object_name, method, :placeholder => options[:placeholder], :class => options[:input_class], 'data-format' => options[:format], :value => options[:value], :data => options[:data] + concat (content_tag :span, :class => 'add-on clearDate' do + content_tag :i, nil, :class => options[:icon_clear] + end) + concat (content_tag :span, :class => 'add-on iconbtn' do + content_tag :i, nil, 'data-time-icon' => options[:icon_time], 'data-date-icon' => options[:icon_date] + end) + end + end + + def format_value(value, format = 'yyyy-MM-dd hh:mm') + value.strftime(format.gsub('yyyy', '%Y').gsub('MM', '%m').gsub('dd', '%d').gsub('hh', '%H').gsub('mm', '%M')) rescue "" + end + + def add_attribute(partial, f, attribute) + new_object = f.object.send(attribute).build rescue nil + fields = f.fields_for(attribute, new_object, :child_index => "new_#{attribute}") do |f| + render :partial => partial, :object => new_object, :locals => {:f => f} + end + end + + def is_filter_active?(field, value) + params[:filters][field].include?(value.to_s) ? "active" : "" rescue "" + end + + + def select_category(f, module_app) + @user_authorized_categories = module_app.categories.enabled.authorized(current_user) if @user_authorized_categories.nil? + render :partial => '/admin/categories/select_form', :locals => {:f=> f, :module_app=>module_app, :categories=> @user_authorized_categories } + end + + def copy_to_all_language_button(language_tab, language_area) + render :partial => '/shared/copy_to_all_language_button', :locals => {:language_area => language_area, :language_tab => language_tab} + end + + def select_tags(f, module_app) + render :partial => '/admin/tags/tag_form', :locals => {:f=> f, :module_app=>module_app, :tags=>module_app.tags } + end + + def render_filter(fields, search_dom_id=nil, quick_new=false) + render :partial => "shared/filter", :locals =>{:fields => fields, :search_dom_id=>search_dom_id, :quick_new=>quick_new} + end + + def display_visitors(options={}) + Impression.where(options).count + end + + def display_visitors_today + display_visitors(created_at: {'$gte' => Time.now.beginning_of_day}) + end + + def display_visitors_this_week + display_visitors(created_at: {'$gte' => Time.now.beginning_of_week}) + end + + def display_visitors_this_month + visitors_this_month = Rails.cache.fetch("visitors_this_month", expires_in: 1.day) do + display_visitors(created_at: {'$gte' => Time.now.beginning_of_month}) + end + visitors_this_month + end + + def display_visitors_this_year + visitors_this_year = Rails.cache.fetch("visitors_this_year", expires_in: 1.day) do + display_visitors(created_at: {'$gte' => Time.now.beginning_of_year}) + end + visitors_this_year + end + + def get_month_traffic + #site = Site.first + #if site.month_traffic_cache.blank? or (site.month_traffic_cache['updated_at'] < (Time.now-1.day) rescue true) + #site.month_traffic_cache = {} + #site.month_traffic_cache['result'] = [] + result = Impression.group_by(:created_at,30) + trafic_result = result.map do |v| + date = v['_id'].values + [Date.new(date[0],date[1],date[2]).to_time,v['count']] + end + #site.month_traffic_cache['result'] = trafic_result + #site.month_traffic_cache['updated_at'] = Time.now + #site.save + #end + + #[:name=> t(:visitors_count),:data=>site.month_traffic_cache['result']] + [:name=> t(:visitors_count),:data=>trafic_result] + end + + def can_edit_or_delete?(obj) + create_user = obj.create_user_id.to_s rescue nil + if @user_authenticated_categories.first == "all" + return true + elsif obj.class == Page && current_user.is_manager?(ModuleApp.where(:key=>'page_content').first) + return true + elsif @current_user_is_sub_manager && !create_user.nil? + create_user == current_user.id.to_s + else + @user_authenticated_categories.include?obj.category_id rescue (current_user.is_manager?(@module_app) rescue false) + end + end + + def user_authenticated_categories + @user_authenticated_categories + end + + def is_user_sub_manager? + @current_user_is_sub_manager + end + + def has_access? #@user_has_privileges comes from orbit_member_controller.. used just in members + @user_has_privileges + end + + def check_store_permissions #checks with the store if it has proper access and rights to access store + store_permission = {} + store_token = current_site.store_token rescue nil + if !store_token.nil? + params_to_send = {'store_token' => current_site.store_token} + uri = URI.parse(OrbitStore::URL) + http = Net::HTTP.new(uri.host,uri.port) + http.read_timeout = 25 #seconds + request = Net::HTTP::Get.new("/site/permissions") + request.body = params_to_send.to_query + response = http.request(request) rescue nil + if response.nil? + data = {} + data["message"] = "Could not connect to the store." + data["error"] = "CONNECTION_REFUSED" + data["success"] = false + else + data = JSON.parse(response.body) rescue nil + end + if data.nil? + data = {} + data["message"] = "Could not connect to the store." + data["error"] = "JSON_ERROR" + data["success"] = false + end + if !data["success"] + case data["error"] + when "INVALID_SITE_TOKEN" + current_site.store_token = nil + current_site.save + end + store_permission["permission_granted"] = false + store_permission["error"] = data["error"] + store_permission["message"] = data["message"] + else + store_permission["permission_granted"] = true + end + else + store_permission["permission_granted"] = false + store_permission["error"] = "SITE_NOT_REGISTERED" + store_permission["message"] = "Site not registered." + end + store_permission + end + + def render_401 + render "errors/401" + end + + def render_403 + render "errors/403" + end + + def need_access_right + render_401 if !has_access? + end + + def get_referer_url + referer_path = Rails.application.routes.recognize_path(request.referer) + if referer_path[:controller]!="pages" or (referer_path[:controller]==params[:controller] and referer_path[:action]!="index") + referer_url = '/'+params[:controller] + else + referer_url = request.referer + end + referer_url + end + +end + +module Orbit::FormBuilder + def datetime_picker(method, options = {}) + @template.datetime_picker(@object_name, method, objectify_options(options)) + end +end diff --git a/temp_file/app/models/site.rb b/temp_file/app/models/site.rb index a842953..7bf7d57 100644 --- a/temp_file/app/models/site.rb +++ b/temp_file/app/models/site.rb @@ -71,6 +71,22 @@ class Site mount_uploader :site_logo_1, ImageUploader mount_uploader :favicon, ImageUploader mount_uploader :mobile_icon, ImageUploader + Impression.class_eval{ + def self.group_by(field,day_limit,start_day=Date.today,format = 'day') + limit_ele = Impression.desc(:id).where({ created_at: { "$lt" => start_day-(day_limit-2).days }}).first + key_op = [['year','$year'],['month', '$month'], ['day', '$dayOfMonth']] + key_op = key_op.take(1 + key_op.find_index { |key, op| format == key }) + project_date_fields = Hash[*key_op.collect { |key, op| [key, {op => "$#{field}"}] }.flatten] + group_id_fields = Hash[*key_op.collect { |key, op| [key, "$#{key}"] }.flatten] + pipeline = [ + {"$match"=> {"_id" => {"$gte" => limit_ele['_id']}}}, + {"$project" => {field => 1}.merge(project_date_fields)}, + {"$group" => {"_id" => group_id_fields,"count" => {"$sum" => 1}}}, + {"$sort" => {"_id"=>-1}} + ] + collection.aggregate(pipeline) + end + } after_initialize do if !self.new_record? if self.is_hidden_orbit_bar.nil?