rb (Listing 10-3). These functions are not publicly available
as URLs. The first function creates temporary file names to save your Gruff graphs:
CHAPTER 10 n CALCULATING COSTS BY ANALYZING APACHE WEB LOGS 209
protected # Protected functions are for internal use only and
# don't correspond to a URL.
# This function is used by the two graph functions to create a
# temporary filename.
def get_tempfile_name(prefix)
File.join(RAILS_ROOT, "tmp/#{prefix}_#{request.env['REMOTE_ADDR']
}_#{Time.now.to_f
}_#{rand(10000)}.jpg")
end
Note that this solution does not absolutely guarantee unique file names, but it
should work fine under most conditions. Time.now.to_f returns the unique timestamp
in fractional seconds. It also appends a random number between one and 10,000. This
should be more than sufficient to remove the possibility of collisions for your average
intranet application. You would need to have the same IP accessing the same graph in
the same millisecond or so to have a problem.
nNote You could check if the file name has been used before using it, but this has race conditions.
Pages:
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302