vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | |
2 | --[[ |
||
3 | |||
4 | Code for manipulating a data set. |
||
5 | |||
6 | ]] |
||
7 | |||
8 | -- table setup |
||
9 | local mod = thismod |
||
10 | local me = { } |
||
11 | mod.dataset = me |
||
12 | |||
13 | |||
14 | --[[ |
||
15 | add <value> to <dataset>.current |
||
16 | ]] |
||
17 | me.adddatapoint = function(dataset, value) |
||
18 | |||
19 | dataset.current = dataset.current + value |
||
20 | |||
21 | me.updatedataset(dataset) |
||
22 | end |
||
23 | |||
24 | --[[ |
||
25 | me.updatedataset for tree with nested data sets. |
||
26 | This method is currently unused. |
||
27 | ]] |
||
28 | me.updatedatatree = function(tree) |
||
29 | |||
30 | if tree.current then |
||
31 | -- this is an ordinary dataset |
||
32 | me.updatedataset(tree) |
||
33 | |||
34 | else |
||
35 | |||
36 | local value |
||
37 | |||
38 | for _, value in tree do |
||
39 | if type(value) == "table" then |
||
40 | me.updatedatatree(value) |
||
41 | end |
||
42 | end |
||
43 | end |
||
44 | |||
45 | end |
||
46 | |||
47 | --[[ |
||
48 | Manage ticks / history, etc |
||
49 | ]] |
||
50 | me.updatedataset = function(data) |
||
51 | |||
52 | local timenow = GetTime() |
||
53 | local sum |
||
54 | |||
55 | if timenow < data.lasttick + data.ticklength then |
||
56 | -- not time for a new tick |
||
57 | return |
||
58 | end |
||
59 | |||
60 | -- do a new tick |
||
61 | data.totalticks = data.totalticks + 1 |
||
62 | data.lasttick = timenow |
||
63 | |||
64 | -- add recent value to history |
||
65 | local x |
||
66 | sum = 0 |
||
67 | |||
68 | for x = data.historylength, 2, -1 do |
||
69 | data.history[x] = data.history[x - 1] |
||
70 | sum = sum + data.history[x] |
||
71 | end |
||
72 | |||
73 | data.history[1] = data.current |
||
74 | sum = sum + data.current |
||
75 | data.currenthistorysum = sum |
||
76 | |||
77 | if sum > data.maxhistory then |
||
78 | data.maxhistory = sum |
||
79 | end |
||
80 | |||
81 | -- increment total |
||
82 | data.total = data.total + data.current |
||
83 | data.current = 0 |
||
84 | |||
85 | return true |
||
86 | end |
||
87 | |||
88 | --[[ |
||
89 | mod.dataset.createnewdataset(ticklength, historylength) |
||
90 | Returns a newly created dataset. |
||
91 | <ticklength> is a fractional value, in seconds, how long each update period is. |
||
92 | <historylength> is an integer, the number of recent values that are kept in record. |
||
93 | The history is used to get an average usage over a decently long period of time. |
||
94 | ]] |
||
95 | me.createnewdataset = function(ticklength, historylength) |
||
96 | |||
97 | local value = |
||
98 | { |
||
99 | ["total"] = 0, |
||
100 | ["ticklength"] = ticklength, |
||
101 | ["totalticks"] = 0, |
||
102 | ["lasttick"] = GetTime(), |
||
103 | ["starttime"] = GetTime(), |
||
104 | ["history"] = { }, |
||
105 | ["historylength"] = historylength, |
||
106 | ["current"] = 0, |
||
107 | ["maxhistory"] = 0, |
||
108 | ["currenthistorysum"] = 0, |
||
109 | } |
||
110 | |||
111 | local x |
||
112 | |||
113 | for x = 1, value.historylength do |
||
114 | table.insert(value.history, 0) |
||
115 | end |
||
116 | |||
117 | return value |
||
118 | end |
||
119 | |||
120 | --[[ |
||
121 | mod.dataset.resetdataset(data) |
||
122 | <data> is the dataset to reset. |
||
123 | This will return it to factory settings. |
||
124 | ]] |
||
125 | me.resetdataset = function(data) |
||
126 | |||
127 | data.total = 0 |
||
128 | data.totalticks = 0 |
||
129 | data.lasttick = GetTime() |
||
130 | data.starttime = GetTime() |
||
131 | me.clearhistory(data) |
||
132 | |||
133 | end |
||
134 | |||
135 | --[[ |
||
136 | mod.dataset.clearhistory(data) |
||
137 | <data> is the dataset to clear. |
||
138 | This will just remove the history. |
||
139 | ]] |
||
140 | me.clearhistory = function(data) |
||
141 | |||
142 | local x |
||
143 | |||
144 | for x = 1, data.historylength do |
||
145 | data.history[x] = 0 |
||
146 | end |
||
147 | |||
148 | for x = 1, data.deephistorylength do |
||
149 | data.deephistory[x] = 0 |
||
150 | end |
||
151 | |||
152 | data.maxhistory = 0 |
||
153 | data.maxdeephistory = 0 |
||
154 | end |
||
155 | |||
156 | --[[ |
||
157 | print a specific data set. |
||
158 | This method is currently unused. |
||
159 | ]]-- |
||
160 | me.printdataset = function(dataset, description) |
||
161 | |||
162 | local totaltime = dataset.lasttick - dataset.starttime |
||
163 | local tickmultiplier = dataset.totalticks / (totaltime / dataset.ticklength) |
||
164 | local historytime = math.min(totaltime, dataset.ticklength * dataset.historylength) * tickmultiplier |
||
165 | local historyaverage = 0 |
||
166 | |||
167 | local x |
||
168 | |||
169 | for x = 1, dataset.historylength do |
||
170 | historyaverage = historyaverage + dataset.history[x] |
||
171 | end |
||
172 | |||
173 | historyaverage = historyaverage / historytime |
||
174 | |||
175 | |||
176 | mod.out.print(string.format("|cffffff00%s:|r Total = |cff00ffff%s|r. Average = |cff00ffff%s|r over |cff00ff00%s|r secs, |cff00ffff%s|r over |cff00ff00%s|r secs.", |
||
177 | description, me.formatdecimal(dataset.total), me.formatdecimal(dataset.total / totaltime), me.formatdecimal(totaltime), me.formatdecimal(historyaverage), me.formatdecimal(historytime))) |
||
178 | |||
179 | end |
||
180 | |||
181 | --[[ |
||
182 | me.formatdecimal(value) |
||
183 | Returns a string representation of <value>, including up the the first place after the decimal, if it exists. |
||
184 | ]] |
||
185 | me.formatdecimal = function(value) |
||
186 | |||
187 | if floor(value) == value then |
||
188 | return string.format("%d", value) |
||
189 | |||
190 | else |
||
191 | local base = string.format("%f", value) |
||
192 | local dotpoint = string.find(base, "%.") |
||
193 | return string.sub(base, 1, dotpoint + 1) |
||
194 | end |
||
195 | end |