|
| 1 | +module RailsPerformance |
| 2 | + module Events |
| 3 | + class Record |
| 4 | + attr_reader :name, :datetimei, :options |
| 5 | + |
| 6 | + DEFAULT_COLOR = "#FF00FF" |
| 7 | + DEFAULT_LABEL_COLOR = "#FF00FF" |
| 8 | + DEFAULT_LABEL_ORIENTATION = "horizontal" |
| 9 | + |
| 10 | + class << self |
| 11 | + def create(name:, datetimei: Time.now.to_i, options: {}) |
| 12 | + instance = new(name: name, datetimei: datetimei, options: options) |
| 13 | + instance.save |
| 14 | + instance |
| 15 | + end |
| 16 | + |
| 17 | + def all |
| 18 | + _, values = RailsPerformance::Utils.fetch_from_redis("rails_performance:records:events:*") |
| 19 | + Array(values).map do |value| |
| 20 | + json = JSON.parse(value) |
| 21 | + new(name: json["name"], datetimei: json["datetimei"], options: Hash(json["options"])) |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + def initialize(name:, datetimei:, options: {}) |
| 27 | + @name = name |
| 28 | + @datetimei = datetimei |
| 29 | + @options = options |
| 30 | + end |
| 31 | + |
| 32 | + def save |
| 33 | + RailsPerformance::Utils.save_to_redis(rails_performance_key, value) |
| 34 | + end |
| 35 | + |
| 36 | + def rails_performance_key |
| 37 | + "rails_performance:records:events:#{datetimei}|#{RailsPerformance::EVENTS_SCHEMA}" |
| 38 | + end |
| 39 | + |
| 40 | + def value |
| 41 | + { |
| 42 | + name: name, |
| 43 | + datetime: RailsPerformance::Utils.from_datetimei(datetimei.to_i), |
| 44 | + datetimei: datetimei, |
| 45 | + options: options |
| 46 | + } |
| 47 | + end |
| 48 | + |
| 49 | + def to_annotation |
| 50 | + { |
| 51 | + x: datetimei * 1000, |
| 52 | + borderColor: options.dig("borderColor") || DEFAULT_COLOR, |
| 53 | + label: { |
| 54 | + borderColor: options.dig("label", "borderColor") || DEFAULT_LABEL_COLOR, |
| 55 | + orientation: options.dig("label", "orientation") || DEFAULT_LABEL_ORIENTATION, |
| 56 | + text: options.dig("label", "text") || name |
| 57 | + } |
| 58 | + } |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | +end |
0 commit comments