<commit_before>#
# Copyright (c) 2013-2015 SKYARCH NETWORKS INC.
#
# This software is released under the MIT License.
#
# http://opensource.org/licenses/mit-license.php
#

class Operation_worker

  def perform
    # ws = WSConnector.new('notifications', User.find(user_id).ws_key)
    now = Time.new.in_time_zone
    operation = OperationDuration.all
    operation.each do |item|
      # Check if resource is still available.
      if Resource.exists?(item.resource_id)
        resource = Resource.find(item.resource_id)
        if now >= item.start_date && now <= item.end_date
          start_time = item.recurring_date.end_time.strftime( "%H%M%S%N" ).to_i
          end_time = item.recurring_date.start_time.strftime( "%H%M%S%N" ).to_i
          case item.recurring_date.repeats
            when "everyday"
              evaluate_evr(start_time, end_time, now, resource)
            when "weekdays"
              evaluate_weekdays(start_time, end_time, now, resource)
            when "weekends"
              evaluate_weekends(start_time, end_time, now, resource)
            when "other"
              evaluate_other(start_time, end_time, now, resource, item.recurring_date.dates)
          end
        else
          stop(resource)
        end
      else
        OperationDuration.where(resource_id: item.resource_id).destroy_all
      end

    end
  end

  def evaluate_evr(start_time, end_time, now, resource)
    now_time = now.strftime( "%H%M%S%N" ).to_i
    if start_time <= now_time && end_time >= now_time
      start(resource)
    else
      stop(resource)
    end
  end

  def evaluate_weekdays(start_time, end_time, now, resource)
    now_time = now.strftime( "%H%M%S%N" ).to_i
    if now.wday != 0 && now.wday != 6
      if start_time <= now_time && end_time >= now_time
        start(resource)
      else
        stop(resource)
      end
    else
      stop(resource)
    end
  end

  def evaluate_weekends(start_time, end_time, now, resource)
    now_time = now.strftime( "%H%M%S%N" ).to_i
    if now.wday == 0 && now.wday == 6
      if start_time <= now_time && end_time >= now_time
        start(resource)
      else
        stop(resource)
      end
    else
      stop(resource)
    end
  end

  def evaluate_other(start_time, end_time, now, resource, dates)
    now_time = now.strftime( "%H%M%S%N" ).to_i
    dow =  Array.new
    dates.each do |item|
      if item[1]["checked"] == "true"
        dow.push(item[1]["value"].to_i)
      end
    end

    if dow.include? now.wday
      if start_time <= now_time && end_time >= now_time
        start(resource)
      else
        stop(resource)
      end
    else
      stop(instance)
    end

  end

  def start(resource)
    instance = resource.infrastructure.instance(resource.physical_id)
    if instance.status == :stopped
      instance.start
      notify_ec2_status(resource, "Started: #{instance.physical_id} ")
      Rails.logger.debug "Started: #{instance.physical_id} "
    end

  end

  def stop(resource)
    instance = resource.infrastructure.instance(resource.physical_id)
    if instance.status == :running
      instance.stop
      notify_ec2_status(resource, "Started: #{instance.physical_id} ")
      Rails.logger.debug "Stopped: #{instance.physical_id}"
    end
  end

  def notify_ec2_status(resource, details)
    Thread.new_with_db do
      ws = WSConnector.new('ec2_status', resource.physical_id)
      begin<commit_msg>fixed interchanged end and start date<commit_after>#
# Copyright (c) 2013-2015 SKYARCH NETWORKS INC.
#
# This software is released under the MIT License.
#
# http://opensource.org/licenses/mit-license.php
#

class Operation_worker

  def perform
    # ws = WSConnector.new('notifications', User.find(user_id).ws_key)
    now = Time.new.in_time_zone
    operation = OperationDuration.all
    operation.each do |item|
      # Check if resource is still available.
      if Resource.exists?(item.resource_id)
        resource = Resource.find(item.resource_id)
        if now >= item.start_date && now <= item.end_date
          start_time = item.recurring_date.start_time.strftime( "%H%M%S%N" ).to_i
          end_time = item.recurring_date.end_time.strftime( "%H%M%S%N" ).to_i
          case item.recurring_date.repeats
            when "everyday"
              evaluate_evr(start_time, end_time, now, resource)
            when "weekdays"
              evaluate_weekdays(start_time, end_time, now, resource)
            when "weekends"
              evaluate_weekends(start_time, end_time, now, resource)
            when "other"
              evaluate_other(start_time, end_time, now, resource, item.recurring_date.dates)
          end
        else
          stop(resource)
        end
      else
        OperationDuration.where(resource_id: item.resource_id).destroy_all
      end

    end
  end

  def evaluate_evr(start_time, end_time, now, resource)
    now_time = now.strftime( "%H%M%S%N" ).to_i
    if start_time <= now_time && end_time >= now_time
      start(resource)
    else
      stop(resource)
    end
  end

  def evaluate_weekdays(start_time, end_time, now, resource)
    now_time = now.strftime( "%H%M%S%N" ).to_i
    if now.wday != 0 && now.wday != 6
      if start_time <= now_time && end_time >= now_time
        start(resource)
      else
        stop(resource)
      end
    else
      stop(resource)
    end
  end

  def evaluate_weekends(start_time, end_time, now, resource)
    now_time = now.strftime( "%H%M%S%N" ).to_i
    if now.wday == 0 && now.wday == 6
      if start_time <= now_time && end_time >= now_time
        start(resource)
      else
        stop(resource)
      end
    else
      stop(resource)
    end
  end

  def evaluate_other(start_time, end_time, now, resource, dates)
    now_time = now.strftime( "%H%M%S%N" ).to_i
    dow =  Array.new
    dates.each do |item|
      if item[1]["checked"] == "true"
        dow.push(item[1]["value"].to_i)
      end
    end

    if dow.include? now.wday
      if start_time <= now_time && end_time >= now_time
        start(resource)
      else
        stop(resource)
      end
    else
      stop(resource)
    end

  end

  def start(resource)
    instance = resource.infrastructure.instance(resource.physical_id)
    if instance.status == :stopped
      instance.start
      notify_ec2_status(resource, "Started: #{instance.physical_id} ")
      Rails.logger.debug "Started: #{instance.physical_id} "
    end

  end

  def stop(resource)
    instance = resource.infrastructure.instance(resource.physical_id)
    if instance.status == :running
      instance.stop
      notify_ec2_status(resource, "Started: #{instance.physical_id} ")
      Rails.logger.debug "Stopped: #{instance.physical_id}"
    end
  end

  def notify_ec2_status(resource, details)
    Thread.new_with_db do
      ws = WSConnector.new('ec2_status', resource.physical_id)
      begin
{'fraction_non_alphanumeric': 0.1054014598540146, 'fraction_numerical': 0.005255474452554744, 'mean_word_length': 2.5831589958158996, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2200914', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2230, 'n_words': 486}