Commit 85c04a8a authored by Douwe Maan's avatar Douwe Maan

Track historical active user count.

parent fbb011be
class HistoricalData < ActiveRecord::Base
validate :date, presence: true
# HistoricalData.during((Date.today - 1.year)..Date.today).average(:active_user_count)
scope :during, ->(range) { where(date: range) }
class << self
def track!
create!(
date: Date.today,
active_user_count: User.active.count
)
end
# HistoricalData.at(Date.new(2014, 1, 1)).active_user_count
def at(date)
find_by(date: date)
end
end
end
class HistoricalDataWorker
include Sidekiq::Worker
include Sidetiq::Schedulable
recurrence { daily.hour_of_day(12) }
def perform
HistoricalData.track!
end
end
......@@ -2,9 +2,7 @@ class LdapSyncWorker
include Sidekiq::Worker
include Sidetiq::Schedulable
# We check if we are in a Sidekiq server process because of a bug in Sidetiq
# 0.6.1 which was giving Unicorn trouble (throwing a Redis::InheritedError).
if Gitlab.config.ldap.enabled && Sidekiq.server?
if Gitlab.config.ldap.enabled
HOUR = Gitlab.config.ldap.schedule_sync_hour
MINUTE = Gitlab.config.ldap.schedule_sync_minute
......
class CreateHistoricalData < ActiveRecord::Migration
def change
create_table :historical_data do |t|
t.date :date, null: false
t.integer :active_user_count
t.timestamps
end
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150502064022) do
ActiveRecord::Schema.define(version: 20150507194350) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -132,6 +132,13 @@ ActiveRecord::Schema.define(version: 20150502064022) do
t.boolean "is_sample", default: false
end
create_table "historical_data", force: true do |t|
t.date "date", null: false
t.integer "active_user_count"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "identities", force: true do |t|
t.string "extern_uid"
t.string "provider"
......
require 'spec_helper'
describe HistoricalData do
before do
(1..12).each do |i|
HistoricalData.create!(date: Date.new(2014, i, 1), active_user_count: i * 100)
end
end
describe ".during" do
it "returns the historical data during the given period" do
expect(HistoricalData.during(Date.new(2014, 1, 1)..Date.new(2014, 12, 31)).average(:active_user_count)).to eq(650)
end
end
describe ".at" do
it "returns the historical data at the given date" do
expect(HistoricalData.at(Date.new(2014, 8, 1)).active_user_count).to eq(800)
end
end
describe ".track!" do
before do
allow(User).to receive(:active).and_return([1,2,3,4,5])
end
it "creates a new historical data record" do
HistoricalData.track!
data = HistoricalData.last
expect(data.date).to eq(Date.today)
expect(data.active_user_count).to eq(5)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment