From 8103b14e154dd6225e0b970880ba1ac65b200642 Mon Sep 17 00:00:00 2001 From: santaux Date: Fri, 14 Oct 2011 10:35:25 +0400 Subject: [PATCH] [refs #1861] Add downloads statistics --- .gitignore | 1 + Gemfile | 1 + Gemfile.lock | 5 +++ app/controllers/downloads_controller.rb | 5 +++ app/helpers/download_helper.rb | 2 + app/models/download.rb | 38 +++++++++++++++++++ app/views/downloads/index.html.haml | 28 ++++++++++++++ app/views/layouts/application.html.haml | 4 +- config/locales/ru.yml | 15 +++++++- config/routes.rb | 6 ++- config/schedule.rb | 7 ++++ db/migrate/20111012065448_create_downloads.rb | 17 +++++++++ db/schema.rb | 12 +++++- spec/controllers/download_controller_spec.rb | 5 +++ spec/helpers/download_helper_spec.rb | 15 ++++++++ spec/models/download_spec.rb | 5 +++ 16 files changed, 160 insertions(+), 6 deletions(-) create mode 100644 app/controllers/downloads_controller.rb create mode 100644 app/helpers/download_helper.rb create mode 100644 app/models/download.rb create mode 100644 app/views/downloads/index.html.haml create mode 100644 config/schedule.rb create mode 100644 db/migrate/20111012065448_create_downloads.rb create mode 100644 spec/controllers/download_controller_spec.rb create mode 100644 spec/helpers/download_helper_spec.rb create mode 100644 spec/models/download_spec.rb diff --git a/.gitignore b/.gitignore index dd86dc098..47d69c731 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ public/stylesheets/compiled/* public/assets/* config/initializers/local.rb public/system/* +*.swp diff --git a/Gemfile b/Gemfile index c4e9780d0..35b69d762 100644 --- a/Gemfile +++ b/Gemfile @@ -52,5 +52,6 @@ gem "grit" gem 'unicorn' gem 'delayed_job' gem 'paperclip', "~> 2.3" +gem 'whenever', :require => false gem 'jammit' diff --git a/Gemfile.lock b/Gemfile.lock index 5582f021e..0f971baac 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -42,6 +42,7 @@ GEM net-ssh-gateway (>= 1.0.0) capistrano-ext (1.2.1) capistrano (>= 1.0.0) + chronic (0.6.4) closure-compiler (1.1.1) compass (0.10.6) haml (>= 3.0.4) @@ -139,6 +140,9 @@ GEM warden (1.0.3) rack (>= 1.0.0) web-app-theme (0.6.3) + whenever (0.7.0) + activesupport (>= 2.3.4) + chronic (~> 0.6.3) will_paginate (3.0.pre2) yui-compressor (0.9.5) @@ -166,4 +170,5 @@ DEPENDENCIES russian unicorn web-app-theme (>= 0.6.2) + whenever will_paginate (~> 3.0.pre2) diff --git a/app/controllers/downloads_controller.rb b/app/controllers/downloads_controller.rb new file mode 100644 index 000000000..763aec82b --- /dev/null +++ b/app/controllers/downloads_controller.rb @@ -0,0 +1,5 @@ +class DownloadsController < ApplicationController + def index + @downloads = Download.paginate :page => params[:page], :per_page => 30 + end +end diff --git a/app/helpers/download_helper.rb b/app/helpers/download_helper.rb new file mode 100644 index 000000000..9e232c4ef --- /dev/null +++ b/app/helpers/download_helper.rb @@ -0,0 +1,2 @@ +module DownloadHelper +end diff --git a/app/models/download.rb b/app/models/download.rb new file mode 100644 index 000000000..3aa8c5a47 --- /dev/null +++ b/app/models/download.rb @@ -0,0 +1,38 @@ +class Download < ActiveRecord::Base + PREV_LOG_FILE = "#{ APP_CONFIG['nginx_log'] }.0" + + default_scope order(:name) + + class << self + def rotate_nginx_log + system("mv #{ APP_CONFIG['nginx_log'] } #{ PREV_LOG_FILE }") + system("sudo kill -USR1 `cat #{ APP_CONFIG['nginx_pid'] }`") + end + + def parse_nginx_log + File.open(PREV_LOG_FILE) do |log| + while (line = log.gets) + if package = line.match( /GET \/.+\/([\w\d]+)-([\d.]+)-((\d+mdv[\d.]+)|([\d\w]+-mdv[\d.]+))\.([\w\d]+)\.rpm/ ) + increase( + :name => package[1], + :version => package[2], + :distro => package[3].sub(/-/, ''), + :platform => package[6] + ) + end + end + end + end + + def parse_and_remove_nginx_log + parse_nginx_log + system("rm -f #{PREV_LOG_FILE}") + end + + def increase(opts={}) + download = find_or_initialize_by_name_and_version_and_platform_and_distro(opts) + download.counter += 1 + download.save + end + end +end diff --git a/app/views/downloads/index.html.haml b/app/views/downloads/index.html.haml new file mode 100644 index 000000000..115b114b2 --- /dev/null +++ b/app/views/downloads/index.html.haml @@ -0,0 +1,28 @@ +.block + .content + %h2.title + = t("layout.downloads.title") + .inner + %table.table + %tr + %th.first= t("activerecord.attributes.download.name") + %th= t("activerecord.attributes.download.version") + %th= t("activerecord.attributes.download.distro") + %th= t("activerecord.attributes.download.platform") + %th.last= t("activerecord.attributes.download.counter") + - @downloads.each do |download| + %tr{:class => cycle("odd", "even")} + %td + = download.name + %td + = download.version + %td + = download.distro + %td + = download.platform + %td.last + = download.counter + .actions-bar.wat-cf + .actions + = will_paginate @downloads + diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 5f3ed52f2..f62b571e3 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -20,13 +20,15 @@ %a{:href => users_path}= t("layout.menu.users") %li{:class => controller.controller_path == 'platforms' ? 'active' : '' } %a{:href => platforms_path}= t("layout.menu.platforms") + %li{:class => controller.controller_path == 'downloads' ? 'active' : '' } + %a{:href => downloads_path}= t("layout.menu.downloads") #wrapper.wat-cf = render :partial => "layouts/flashes" #main = yield #footer .block - + #sidebar = yield :sidebar diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 69248a18c..ea4364388 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -21,6 +21,9 @@ ru: false_: Нет publish: Опубликовать + downloads: + title: Статистика закачек пакетов + weekdays: Monday: Понедельник Tuesday: Вторник @@ -33,6 +36,7 @@ ru: menu: users: Пользователи platforms: Платформы + downloads: Статистика sessions: sign_in_header: Вход в систему @@ -145,7 +149,7 @@ ru: no_items_data: Данных нет show: Просмотр confirm_publish: Вы уверены, что хотите опубликовать контейнер? - + items: statuses: build_error: ошибка сборки @@ -228,6 +232,7 @@ ru: product: Продукт build_list: Сборочный лист build_list_item: Элемент сборочного листа + download: Статистика attributes: repository: @@ -244,7 +249,7 @@ ru: build_status: Статус последней сборки build_path: Путь к iso created_at: Создан - updated_at: Обновлен + updated_at: Обновлен ks: Содержимое .ks.template counter: Содержимое .counter build: Содержимое build @@ -322,3 +327,9 @@ ru: level: Уровень status: Статус build_list: Сборочный лист + download: + name: Название + version: Версия + distro: Дистрибутив + platform: Архитектура + counter: Закачки diff --git a/config/routes.rb b/config/routes.rb index 151faf1b1..4e005bbbf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,8 @@ Rosa::Application.routes.draw do devise_for :users + resources :downloads, :only => :index + resources :platforms do member do get 'freeze' @@ -25,7 +27,7 @@ Rosa::Application.routes.draw do end member do post :publish - end + end end member do @@ -38,7 +40,7 @@ Rosa::Application.routes.draw do end resources :users - + match 'build_lists/status_build', :to => "build_lists#status_build" match 'build_lists/post_build', :to => "build_lists#post_build" match 'build_lists/pre_build', :to => "build_lists#pre_build" diff --git a/config/schedule.rb b/config/schedule.rb new file mode 100644 index 000000000..d672b35e2 --- /dev/null +++ b/config/schedule.rb @@ -0,0 +1,7 @@ +every 1.day, :at => '0:05 am' do + runner "Download.rotate_nginx_log" +end + +every 1.day, :at => '0:10 am' do + runner "Download.parse_and_remove_nginx_log" +end diff --git a/db/migrate/20111012065448_create_downloads.rb b/db/migrate/20111012065448_create_downloads.rb new file mode 100644 index 000000000..8eeb4f022 --- /dev/null +++ b/db/migrate/20111012065448_create_downloads.rb @@ -0,0 +1,17 @@ +class CreateDownloads < ActiveRecord::Migration + def self.up + create_table :downloads do |t| + t.string :name, :null => false + t.string :version + t.string :distro + t.string :platform + t.integer :counter, :default => 0 + + t.timestamps + end + end + + def self.down + drop_table :downloads + end +end diff --git a/db/schema.rb b/db/schema.rb index 727a3cb43..8448e50a2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110428140753) do +ActiveRecord::Schema.define(:version => 20111012065448) do create_table "arches", :force => true do |t| t.string "name", :null => false @@ -73,6 +73,16 @@ ActiveRecord::Schema.define(:version => 20110428140753) do add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority" + create_table "downloads", :force => true do |t| + t.string "name", :null => false + t.string "version" + t.string "distro" + t.string "platform" + t.integer "counter", :default => 0 + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "platforms", :force => true do |t| t.string "name" t.string "unixname" diff --git a/spec/controllers/download_controller_spec.rb b/spec/controllers/download_controller_spec.rb new file mode 100644 index 000000000..32c5dd4b8 --- /dev/null +++ b/spec/controllers/download_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe DownloadController do + +end diff --git a/spec/helpers/download_helper_spec.rb b/spec/helpers/download_helper_spec.rb new file mode 100644 index 000000000..e375fe17c --- /dev/null +++ b/spec/helpers/download_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the DownloadHelper. For example: +# +# describe DownloadHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe DownloadHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/download_spec.rb b/spec/models/download_spec.rb new file mode 100644 index 000000000..5077ce6a0 --- /dev/null +++ b/spec/models/download_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Download do + pending "add some examples to (or delete) #{__FILE__}" +end