Class: FluidFeatures::AppUserTransaction

Inherits:
Object
  • Object
show all
Defined in:
lib/fluidfeatures/app/transaction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, url) ⇒ AppUserTransaction

Returns a new instance of AppUserTransaction.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fluidfeatures/app/transaction.rb', line 9

def initialize(user, url)

  @user = user
  @url  = url

  # take a snap-shot of the features enabled state at
  # the beginning of the transaction
  @features = user.features

  @features_hit = {}
  @goals_hit = {}
  @unknown_features = {}
  @start_time = Time.now
  @ended = false

end

Instance Attribute Details

#endedObject

Returns the value of attribute ended.



7
8
9
# File 'lib/fluidfeatures/app/transaction.rb', line 7

def ended
  @ended
end

#featuresObject

Returns the value of attribute features.



7
8
9
# File 'lib/fluidfeatures/app/transaction.rb', line 7

def features
  @features
end

#features_hitObject

Returns the value of attribute features_hit.



7
8
9
# File 'lib/fluidfeatures/app/transaction.rb', line 7

def features_hit
  @features_hit
end

#goals_hitObject

Returns the value of attribute goals_hit.



7
8
9
# File 'lib/fluidfeatures/app/transaction.rb', line 7

def goals_hit
  @goals_hit
end

#start_timeObject

Returns the value of attribute start_time.



7
8
9
# File 'lib/fluidfeatures/app/transaction.rb', line 7

def start_time
  @start_time
end

#unknown_featuresObject

Returns the value of attribute unknown_features.



7
8
9
# File 'lib/fluidfeatures/app/transaction.rb', line 7

def unknown_features
  @unknown_features
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/fluidfeatures/app/transaction.rb', line 7

def url
  @url
end

#userObject

Returns the value of attribute user.



7
8
9
# File 'lib/fluidfeatures/app/transaction.rb', line 7

def user
  @user
end

Instance Method Details

#durationObject



85
86
87
88
89
90
91
# File 'lib/fluidfeatures/app/transaction.rb', line 85

def duration
  if ended
    @duration
  else
    Time.now - start_time
  end
end

#end_transactionObject

This reports back to FluidFeatures which features we encountered during this request, the request duration, and statistics on time spent talking to the FluidFeatures service. Any new features encountered will also be reported back with the default_enabled status (see unknown_feature_hit) so that FluidFeatures can auto-populate the dashboard.



101
102
103
104
105
106
# File 'lib/fluidfeatures/app/transaction.rb', line 101

def end_transaction
  raise "transaction ended" if ended
  @duration = duration #Time.now - start_time
  user.app.reporter.report_transaction(self)
  @ended = true
end

#feature_enabled?(feature_name, version_name = nil, default_enabled = nil) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fluidfeatures/app/transaction.rb', line 26

def feature_enabled?(feature_name, version_name=nil, default_enabled=nil)
  raise "transaction ended" if ended
  raise "feature_name invalid : #{feature_name}" unless feature_name.is_a? String
  version_name ||= ::FluidFeatures::DEFAULT_VERSION_NAME

  if features.has_key? feature_name
    feature = features[feature_name]
    if feature.is_a? Hash
      if feature.has_key? version_name
        enabled = feature[version_name]
      end
    end
  end

  if enabled === nil
    enabled = default_enabled
    
    # Tell FluidFeatures about this amazing new feature...
    unknown_feature_hit(feature_name, version_name, default_enabled)
  end

  if enabled
    @features_hit[feature_name] ||= {}
    @features_hit[feature_name][version_name.to_s] = {}
  end

  enabled
end

#goal_hit(goal_name, goal_version_name = nil) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/fluidfeatures/app/transaction.rb', line 76

def goal_hit(goal_name, goal_version_name=nil)
  raise "transaction ended" if ended
  raise "goal_name invalid : #{goal_name}" unless goal_name.is_a? String
  goal_version_name ||= ::FluidFeatures::DEFAULT_VERSION_NAME
  raise "goal_version_name invalid : #{goal_version_name}" unless goal_version_name.is_a? String
  @goals_hit[goal_name.to_s] ||= {}
  @goals_hit[goal_name.to_s][goal_version_name.to_s] = {}
end

#unknown_feature_hit(feature_name, version_name, default_enabled) ⇒ Object

This is called when we encounter a feature_name that FluidFeatures has no record of for your application. This will be reported back to the FluidFeatures service so that it can populate your dashboard with this feature. The parameter “default_enabled” is a boolean that says whether this feature should be enabled to all users or no users. Usually, this is “true” for existing features that you are planning to phase out and “false” for new feature that you intend to phase in.



66
67
68
69
70
71
72
73
74
# File 'lib/fluidfeatures/app/transaction.rb', line 66

def unknown_feature_hit(feature_name, version_name, default_enabled)
  raise "transaction ended" if ended
  unless @unknown_features.has_key? feature_name
    @unknown_features[feature_name] = {}
  end
  unless @unknown_features[feature_name].has_key? version_name
    @unknown_features[feature_name][version_name] = default_enabled
  end
end