Class: Frequency

Inherits:
ActiveRecord::Base show all
Includes:
Generic_Table
Defined in:
app/models/frequency.rb

Constant Summary collapse

@@TargetUpdates =
0.9
@@a =
@@TargetUpdates
@@b =
(1.0-@@TargetUpdates)

Constants included from ColumnGroup::ClassMethods

ColumnGroup::ClassMethods::History_columns

Constants included from GenericGrep::ClassMethods

GenericGrep::ClassMethods::ASSOCIATION_MACRO_LETTERS, GenericGrep::ClassMethods::ASSOCIATION_MACRO_PATTERN

Constants included from GenericTableAssociation::Examples

GenericTableAssociation::Examples::First_stream_method

Instance Method Summary collapse

Methods included from Generic_Table

activeRecordTableNotCreatedYet?, classDefiniton, classReference, class_of_name, eval_constant, is_ActiveRecord_table?, is_generic_table?, is_generic_table_name?, is_table?, rails_MVC_class?, rubyClassName, table_exists?

Methods included from Generic_Table::ClassMethods

#is_active_record_method?, #model_file_name, #nesting, #one_pass_statistics, #sample, #sample_burst

Methods included from ColumnGroup::ClassMethods

#attribute_ddl, #attribute_rails_type, #attribute_ruby_type, #candidate_logical_keys_from_indexes, #categorical?, #column_symbols, #default_logical_primary_key, #defaulted_primary_logical_key?, #history_type?, #is_logical_primary_key?, #logical_attributes, #logical_primary_key_recursive, #numerical?, #probably_categorical?, #probably_numerical?, #sequential_id?

Methods included from GenericGrep::ClassMethods

#all_associations, #association_grep, #association_grep_pattern, #association_macro_type, #association_type, #belongs_to_association?, #grep_all_associations_command, #grep_command, #has_many_association?, #has_one_association?, #model_grep, #model_grep_command

Methods included from GenericTableHtml::ClassMethods

#column_order, #header_html, #table_html

Methods included from GenericTableAssociation

#associated_foreign_key_records, #associated_to_s, #association_class, #association_has_data, #association_state, #foreign_key_points_to_me?, #foreign_key_to_association, #foreign_key_value, #logical_primary_key_recursive_value, #logical_primary_key_value, #name_to_association

Methods included from GenericTableAssociation::Assertions::ClassMethods

#assert_foreign_keys_not_nil, #assert_invariant, #assert_post_conditions, #assert_pre_conditions

Methods included from GenericTableAssociation::Assertions

#assert_associated_foreign_key, #assert_associated_foreign_key_name, #assert_association, #assert_association_methods, #assert_association_to_many, #assert_association_to_one, #assert_foreign_key_association_names, #assert_foreign_key_name, #assert_foreign_key_not_nil, #assert_foreign_keys_not_nil, #assert_invariant, #assert_post_conditions, #assert_pre_conditions

Methods included from GenericTableHtml

#column_html, #rails_route, #row_html

Methods included from GenericTableAssociation::ClassMethods

#associated_foreign_key_name, #association_arity, #association_class, #association_default_class_name?, #association_method_plurality, #association_method_symbol, #association_methods, #association_names, #association_names_to_many, #association_names_to_one, #association_patterns, #foreign_key_association_names, #foreign_key_names, #foreign_key_to_association_name, #is_association?, #is_association_patterns?, #is_association_to_many?, #is_association_to_one?, #is_foreign_key_name?, #is_matching_association?, #is_polymorphic_association?, #match_association_patterns?, #name_symbol

Constructor Details

#initialize(params = nil) ⇒ Frequency

def


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/frequency.rb', line 16

def initialize(params=nil)
	@frozen=false
	@thawLength=0
	@freezeLength=0
	@previousThaw=0
	@previousFreeze=0
 
	super(params)	

	if self[:shortest_update].nil? then
		self[:shortest_update]=100*365*24*3600
	end
	if self[:longest_no_update].nil? then
		self[:longest_no_update]=0
	end

end

Instance Method Details

#acquisitionNotUpdatedObject

def


107
108
109
110
111
112
113
114
115
116
# File 'app/models/frequency.rb', line 107

def acquisitionNotUpdated
	noUpdateInterval=Time.now-self[previous_update]
	longestWait=[self[:max_seconds],self[:longest_no_update]].min
	self[:longest_no_update]=[noUpdateInterval,self[:longest_no_update]].max		
		self[:interval]=@@b*self[:shortest_update]+@@a*self[:longest_no_update]
		assert_operator(self[:interval],:<,365*24*3600)
		assert_not_nil(@freezeLength)
		assert_operator(self[:shortest_update],:>,0.0)
		@@log.debug(sleepRange)
end

#acquisitionUpdatedObject

def


96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/frequency.rb', line 96

def acquisitionUpdated
		if firstUpdateAfterInterruption? then
			self[:longest_no_update]=[self[:interval],self[:longest_no_update]].max
			self[:interval]=[self[:shortest_update],self[:min_seconds]].min
		else
			self[:interval]=@@a*self[:shortest_update]+@@b*self[:longest_no_update]
			self[:shortest_update]=[Time.now-self[previous_update],self[:shortest_update]].min
			self[:longest_no_update]=[Time.now-self[previous_update],self[:longest_no_update]].max
		end #if
		self[previous_update]=Time.now
end

#assert_model_countersObject


33
34
35
36
37
38
39
# File 'app/models/frequency.rb', line 33

def assert_model_counters
	assert_not_nil(@frozen)
	assert_not_nil(@thawLength)
	assert_not_nil(@freezeLength)
	assert_not_nil(@previousThaw)
	assert_not_nil(@previousFreeze)
end

#checkFreeze(acquisitionsUpdated = acquisitionsUpdated?) ) ⇒ Object

def


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/frequency.rb', line 50

def checkFreeze(acquisitionsUpdated=acquisitionsUpdated?)
	if acquisitionsUpdated then
		if @frozen then #begin thaw
			@frozen=false
			@previousThaw=@thawLength	
			@thawLength=1
		else 
			@thawLength+=1
		end
	else
		if @frozen then
			@freezeLength+=1
		else
			@frozen=true
			@previousFreeze=@freezeLength
			@freezeLength=1
		end
	end
	return @freezeLength
	
end

#firstUpdateAfterInterruption?Boolean

Returns:

  • (Boolean)

89
90
91
92
93
94
95
# File 'app/models/frequency.rb', line 89

def firstUpdateAfterInterruption?
	if @thawLength==1 then
		return true
	else
		return false
	end #if
end

#freezeMetricObject


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/frequency.rb', line 71

def freezeMetric
	if @freezeLength > @previousFreeze then
		if @thawLength > @previousThaw
			return @freezeLength/(@freezeLength+@thawLength)
		else
			return @freezeLength/(@freezeLength+@previousThaw)
		end
	else
		if @thawLength > @previousThaw
			return @previousFreeze/(@previousFreeze+@thawLength)
		else
			return @previousFreeze/(@previousFreeze+@previousThaw)
		end
	end
end

#intervalString(seconds) ⇒ Object

def


7
8
9
10
11
12
13
14
15
# File 'app/models/frequency.rb', line 7

def intervalString(seconds)
#	assert_operator(seconds,:>,0.0)
#	assert_operator(seconds,:<,365*24*3600)
	days=(seconds/(24*3600))
	dayString=("%3i"%days)
	timeString=Time.at(seconds).getutc.strftime("%H:%M:%S")
	fractionalString="%04i"%((10000*seconds)%10000)
	dayString+'d '+timeString+"."+fractionalString
end

#logical_primary_keyObject


4
5
6
# File 'app/models/frequency.rb', line 4

def logical_primary_key
	return :frequency_name
end

#sleepRangeObject

def


40
41
42
43
44
45
46
47
48
49
# File 'app/models/frequency.rb', line 40

def sleepRange
	ret="#{intervalString(self[:min_seconds])}   "
	assert_operator(self[:shortest_update],:>,0.0)
	ret+= "#{intervalString(self[:shortest_update])} "
	ret+= "#{intervalString(self[:interval])}   "
	ret+= "#{intervalString(self[:longest_no_update])}  "
	ret+= "#{intervalString(self[:max_seconds])}  "
	ret+= "sd=#{@@scheduling_delay}"
	return ret
end

#updateSleepInterval(acquisitionsUpdated = acquisitionsUpdated?) ) ⇒ Object

def


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/models/frequency.rb', line 117

def updateSleepInterval(acquisitionsUpdated=acquisitionsUpdated?)
	assert_model_counters
	checkFreeze(acquisitionsUpdated)
	assert_model_counters
	if acquisitionsUpdated then # might be too long; decrease interval, too_long, and too_short
		assert_not_nil(@thawLength)
		assert_operator(@thawLength,:>,0)
		if firstUpdateAfterInterruption? then
			self[:longest_no_update]=[self[:interval],self[:longest_no_update]].max
			self[:interval]=[self[:shortest_update],self[:min_seconds]].min
		else
			self[:interval]=@@a*self[:shortest_update]+@@b*self[:longest_no_update]
		end #if
		assert_operator(self[:longest_no_update],:<,365*24*3600)
		assert_operator(self[:interval],:>,0.0)
		assert_operator(self[:interval],:<,365*24*3600)
		assert_operator(self[:shortest_update],:>,0.0)
		assert_operator(self[:max_seconds],:>,0.0)
		self[:shortest_update]=[self[:interval],self[:shortest_update]].min
	else 
		self[:interval]=@@b*self[:shortest_update]+@@a*self[:longest_no_update]
		assert_operator(self[:interval],:<,365*24*3600)
		assert_not_nil(@freezeLength)
		self[:longest_no_update]=@@a*self[:longest_no_update]+@@b*self[:max_seconds]*@freezeLength
		assert_operator(self[:shortest_update],:>,0.0)
		@@log.debug(sleepRange)
	end
	assert_operator(self[:interval],:>,0.0)
	assert_operator(self[:interval],:<,365*24*3600)
	assert_operator(self[:shortest_update],:>,0.0)
	blockUnderflow # postgresql doesn't like float underflow
	return self[:interval] # result of most interest, other results are instance variables
end