diff --git a/Gemfile b/Gemfile index b8aa689be..4fe2bcb56 100644 --- a/Gemfile +++ b/Gemfile @@ -139,3 +139,5 @@ gem 'protected_attributes' gem 'message_format' gem 'openlab_ruby' + +gem 'has_secure_token' diff --git a/Gemfile.lock b/Gemfile.lock index 5b49a40db..62a082fab 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -164,6 +164,8 @@ GEM activerecord (>= 4.0.0) globalid (0.3.6) activesupport (>= 4.1.0) + has_secure_token (1.0.0) + activerecord (>= 3.0) hashdiff (0.3.0) hashie (3.4.2) highline (1.7.1) @@ -452,6 +454,7 @@ DEPENDENCIES foreman forgery friendly_id (~> 5.1.0) + has_secure_token jbuilder (~> 2.0) jquery-rails kaminari diff --git a/app/models/open_api.rb b/app/models/open_api.rb new file mode 100644 index 000000000..1e18b02f2 --- /dev/null +++ b/app/models/open_api.rb @@ -0,0 +1,5 @@ +module OpenAPI + def self.table_name_prefix + 'open_api_' + end +end diff --git a/app/models/open_api/calls_count_tracing.rb b/app/models/open_api/calls_count_tracing.rb new file mode 100644 index 000000000..c460e06f4 --- /dev/null +++ b/app/models/open_api/calls_count_tracing.rb @@ -0,0 +1,4 @@ +class OpenAPI::CallsCountTracing < ActiveRecord::Base + belongs_to :client, foreign_key: :open_api_client_id + validates :client, :at, presence: true +end diff --git a/app/models/open_api/client.rb b/app/models/open_api/client.rb new file mode 100644 index 000000000..05d248335 --- /dev/null +++ b/app/models/open_api/client.rb @@ -0,0 +1,9 @@ +class OpenAPI::Client < ActiveRecord::Base + has_many :calls_count_tracings, foreign_key: :open_api_client_id, dependent: :destroy + has_secure_token + validates :name, presence: true + + def increment_calls_count + update_column(:calls_count, calls_count+1) + end +end diff --git a/app/models/open_api/parameter_error.rb b/app/models/open_api/parameter_error.rb new file mode 100644 index 000000000..fd758af24 --- /dev/null +++ b/app/models/open_api/parameter_error.rb @@ -0,0 +1 @@ +class OpenAPI::ParameterError < StandardError; end diff --git a/app/workers/open_api_trace_calls_count_worker.rb b/app/workers/open_api_trace_calls_count_worker.rb new file mode 100644 index 000000000..1120ddb8b --- /dev/null +++ b/app/workers/open_api_trace_calls_count_worker.rb @@ -0,0 +1,10 @@ +class OpenAPITraceCallsCountWorker < ActiveJob::Base + include Sidekiq::Worker + sidekiq_options queue: 'default', retry: true + + def perform + OpenAPI::Client.find_each do |client| + OpenAPI::CallsCountTracing.create!(client: client, calls_count: client.calls_count, at: DateTime.now) + end + end +end diff --git a/config/schedule.yml b/config/schedule.yml index 09e819987..32ecc8fcb 100644 --- a/config/schedule.yml +++ b/config/schedule.yml @@ -14,3 +14,7 @@ generate_statistic: cron: "0 1 * * *" class: "StatisticWorker" queue: default + +open_api_trace_calls_count: + cron: "0 4 * * 0" # every sunday at 4am + class: "OpenAPITraceCallsCountWorker" diff --git a/db/migrate/20160504085703_create_open_api_clients.rb b/db/migrate/20160504085703_create_open_api_clients.rb new file mode 100644 index 000000000..fd070547d --- /dev/null +++ b/db/migrate/20160504085703_create_open_api_clients.rb @@ -0,0 +1,10 @@ +class CreateOpenAPIClients < ActiveRecord::Migration + def change + create_table :open_api_clients do |t| + t.string :name + t.integer :calls_count, default: 0 + t.string :token + t.timestamps null: false + end + end +end diff --git a/db/migrate/20160504085905_create_open_api_calls_count_tracings.rb b/db/migrate/20160504085905_create_open_api_calls_count_tracings.rb new file mode 100644 index 000000000..35e213930 --- /dev/null +++ b/db/migrate/20160504085905_create_open_api_calls_count_tracings.rb @@ -0,0 +1,10 @@ +class CreateOpenAPICallsCountTracings < ActiveRecord::Migration + def change + create_table :open_api_calls_count_tracings do |t| + t.belongs_to :open_api_client, foreign_key: true, index: true + t.integer :calls_count, null: false + t.datetime :at, null: false + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index f346c4f84..67ee62ce5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160119131623) do +ActiveRecord::Schema.define(version: 20160504085905) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -267,6 +267,24 @@ ActiveRecord::Schema.define(version: 20160119131623) do add_index "offer_days", ["subscription_id"], name: "index_offer_days_on_subscription_id", using: :btree + create_table "open_api_calls_count_tracings", force: :cascade do |t| + t.integer "open_api_client_id" + t.integer "calls_count", null: false + t.datetime "at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "open_api_calls_count_tracings", ["open_api_client_id"], name: "index_open_api_calls_count_tracings_on_open_api_client_id", using: :btree + + create_table "open_api_clients", force: :cascade do |t| + t.string "name" + t.integer "calls_count", default: 0 + t.string "token" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "plans", force: :cascade do |t| t.string "name" t.integer "amount" @@ -643,6 +661,7 @@ ActiveRecord::Schema.define(version: 20160119131623) do add_foreign_key "availability_tags", "availabilities" add_foreign_key "availability_tags", "tags" add_foreign_key "o_auth2_mappings", "o_auth2_providers" + add_foreign_key "open_api_calls_count_tracings", "open_api_clients" add_foreign_key "prices", "groups" add_foreign_key "prices", "plans" add_foreign_key "user_tags", "tags"