text:
module Metric::CiMixin::Capture
  def perf_capture_object
    self.class.parent::MetricsCapture.new(self)
  end

  delegate :perf_collect_metrics, :to => :perf_capture_object

  def ems_for_capture_target
    if self.kind_of?(ExtManagementSystem)
      self
    elsif respond_to?(:ext_management_system) && ext_management_system.present?
      ext_management_system
    elsif respond_to?(:old_ext_management_system) && old_ext_management_system.present?
      old_ext_management_system
    end
  end

  def split_capture_intervals(interval_name, start_time, end_time, threshold = 1.day)
    raise _("Start time must be earlier than End time") if start_time > end_time
    # Create an array of ordered pairs from start_time and end_time so that each ordered pair is contained
    # within the threshold.  Then, reverse it so the newest ordered pair is first:
    # start_time = 2017/01/01 12:00:00, end_time = 2017/01/04 12:00:00
    # [[interval_name, 2017-01-03 12:00:00 UTC, 2017-01-04 12:00:00 UTC],
    #  [interval_name, 2017-01-02 12:00:00 UTC, 2017-01-03 12:00:00 UTC],
    #  [interval_name, 2017-01-01 12:00:00 UTC, 2017-01-02 12:00:00 UTC]]
    (start_time.utc..end_time.utc).step_value(threshold).each_cons(2).collect do |s_time, e_time|
      [interval_name, s_time, e_time]
    end.reverse
  end
  private :split_capture_intervals

  def perf_capture_queue(interval_name, options = {})
    start_time = options[:start_time]
    end_time   = options[:end_time]
    priority   = options[:priority] || Metric::Capture.const_get("#{interval_name.upcase}_PRIORITY")
    task_id    = options[:task_id]
    zone       = options[:zone] || my_zone
    zone = zone.name if zone.respond_to?(:name)
    ems        = ems_for_capture_target

    raise ArgumentError, "invalid interval_name '#{interval_name}'" unless Metric::Capture::VALID_CAPTURE_INTERVALS.include?(interval_name)
    raise ArgumentError, "end_time cannot be specified if start_time is nil" if start_time.nil? && !end_time.nil?
    raise ArgumentError, "target does not have an ExtManagementSystem" if ems.nil?

    start_time = start_time.utc unless start_time.nil?
    end_time = end_time.utc unless end_time.nil?

    # Determine the items to queue up
    # cb is the task used to group cluster realtime metrics
    cb = nil
    if interval_name == 'historical'
      start_time = Metric::Capture.historical_start_time if start_time.nil?
      end_time ||= 1.day.from_now.utc.beginning_of_day # Ensure no more than one historical collection is queue up in the same day
      items = split_capture_intervals(interval_name, start_time, end_time)
    else
      # if last_perf_capture_on is earlier than 4.hour.ago.beginning_of_day,
      # then create *one* realtime capture for start_time = 4.hours.ago.beginning_of_day (no end_time)
      # and create historical captures for each day from last_perf_capture_on until 4.hours.ago.beginning_of_day
      realtime_cut_off = 4.hours.ago.utc.beginning_of_day
      items =
        if last_perf_capture_on.nil?
          [[interval_name, realtime_cut_off]]
        elsif last_perf_capture_on < realtime_cut_off
          [[interval_name, realtime_cut_off]] +
            split_capture_intervals("historical", last_perf_capture_on, realtime_cut_off)
        else
          [interval_name]
        end

      cb = {:class_name => self.class.name, :instance_id => id, :method_name => :perf_capture_callback, :args => [[task_id]]} if task_id
    end

    # Queue up the actual items
    queue_item = {
      :class_name  => self.class.name,
      :instance_id => id,
      :role        => 'ems_metrics_collector',
      :queue_name  => ems.metrics_collector_queue_name,
      :zone        => zone,
      :state       => ['ready', 'dequeue'],
    }

    messages = MiqQueue.where.not(:method_name => 'perf_capture_realtime').where(queue_item).index_by(&:args)
    items.each do |item_interval, *start_and_end_time|
      # Should both interval name and args (dates) be part of uniqueness query?
      queue_item_options = queue_item.merge(:method_name => "perf_capture_#{item_interval}")
      queue_item_options[:args] = start_and_end_time if start_and_end_time.present?
      next if item_interval != 'realtime' && messages[start_and_end_time].try(:priority) == priority
      MiqQueue.put_or_update(queue_item_options) do |msg, qi|
        if msg.nil?
          qi[:priority] = priority
          qi.delete(:state)
          if cb && item_interval == "realtime"
            qi[:miq_callback] = cb
          end
          qi
        elsif msg.state == "ready" && (task_id || MiqQueue.higher_priority?(priority, msg.priority))
          qi[:priority] = priority
          # rerun the job (either with new task or higher priority)
          qi.delete(:state)
          if task_id
            existing_tasks = (((msg.miq_callback || {})[:args] || []).first) || []
            qi[:miq_callback] = cb.merge(:args => [existing_tasks + [task_id]]) if item_interval == "realtime"
          end
          qi
        else
          interval = qi[:method_name].sub("perf_capture_", "")
          _log.debug "Skipping capture of #{log_target} - Performance capture for interval #{interval} is still running"
          # NOTE: do not update the message queue
          nil
        end
      end
    end
  end

  def perf_capture_realtime(*args)
    perf_capture('realtime', *args)
  end

  def perf_capture_hourly(*args)
    perf_capture('hourly', *args)
  end

  def perf_capture_historical(*args)
    perf_capture('historical', *args)
  end

  def perf_capture(interval_name, start_time = nil, end_time = nil)
    unless Metric::Capture::VALID_CAPTURE_INTERVALS.include?(interval_name)
      raise ArgumentError, _("invalid interval_name '%{name}'") % {:name => interval_name}
    end
    raise ArgumentError, _("end_time cannot be specified if start_time is nil") if start_time.nil? && !end_time.nil?

    start_time = start_time.utc unless start_time.nil?
    end_time = end_time.utc unless end_time.nil?

    log_header = "[#{interval_name}]"
    log_time = ''
    log_time << ", start_time: [#{start_time}]" unless start_time.nil?
    log_time << ", end_time: [#{end_time}]" unless end_time.nil?

    # Determine the start_time for capturing if not provided
    if interval_name == 'historical'
      start_time = Metric::Capture.historical_start_time if start_time.nil?

      interval_name_for_capture = 'hourly'
    else
      start_time = last_perf_capture_on if start_time.nil?
      if start_time.nil? && interval_name == 'hourly'
        # For hourly on the first capture, we don't want to get all of the
        #   historical data, so we shorten the query
        start_time = 4.hours.ago.utc
      end

      interval_name_for_capture = interval_name
    end

    # Determine the expected start time, so we can detect gaps or missing data
    expected_start_range = start_time
    # If we've changed power state within the last hour, the returned data
    #   may not include all the data we'd expect
    expected_start_range = nil if self.respond_to?(:state_changed_on) && state_changed_on && state_changed_on > Time.now.utc - 1.hour

    unless expected_start_range.nil?
      # Shift the expected time for first item, since you may not get back an
      #   item for the first timestamp.
      case interval_name
      when 'realtime' then expected_start_range += (1.minute / Metric::Capture::REALTIME_METRICS_PER_MINUTE)
      when 'hourly'   then expected_start_range += 1.hour
      end
      expected_start_range = expected_start_range.iso8601
    end

    _log.info "#{log_header} Capture for #{log_target}#{log_time}..."

    start_range = end_range = counters = counter_values = nil
    _, t = Benchmark.realtime_block(:total_time) do
      Benchmark.realtime_block(:capture_state) { perf_capture_state }

      counters_by_mor, counter_values_by_mor_and_ts = perf_collect_metrics(interval_name_for_capture, start_time, end_time)

      counters       = counters_by_mor[ems_ref] || {}
      counter_values = counter_values_by_mor_and_ts[ems_ref] || {}

      ts = counter_values.keys.sort
      start_range = ts.first
      end_range   = ts.last
    end

    _log.info "#{log_header} Capture for #{log_target}#{log_time}...Complete - Timings: #{t.inspect}"

    if start_range.nil?
      _log.info "#{log_header} Skipping processing for #{log_target}#{log_time} as no metrics were captured."
      # Set the last capture on to end_time to prevent forever queueing up the same collection range
      update_attributes(:last_perf_capture_on => end_time || Time.now.utc) if interval_name == 'realtime'
    else
      if expected_start_range && start_range > expected_start_range
        _log.warn "#{log_header} For #{log_target}#{log_time}, expected to get data as of [#{expected_start_range}], but got data as of [#{start_range}]."

        # Raise ems_performance_gap_detected alert event to enable notification.
        MiqEvent.raise_evm_alert_event_queue(ext_management_system, "ems_performance_gap_detected",
                                             :resource_class       => self.class.name,
                                             :resource_id          => id,
                                             :expected_start_range => expected_start_range,
                                             :start_range          => start_range
                                            )
      end
      perf_process(interval_name, start_range, end_range, counters, counter_values)
    end
  end

  def perf_capture_callback(task_ids, _status, _message, _result)
    tasks = MiqTask.where(:id => task_ids)
    tasks.each do |t|
      t.lock do |task|
        tkey = "#{self.class.name}:#{id}"
        task.context_data[:complete] << tkey
        task.pct_complete = (task.context_data[:complete].length.to_f / task.context_data[:targets].length.to_f) * 100

        if (task.context_data[:targets] - task.context_data[:complete]).empty?
          # Task is done, call the rollup on the parent
          task.state, task.status, task.message = [MiqTask::STATE_FINISHED, MiqTask::STATUS_OK, "Performance collection complete, #{task.context_data[:complete].length} out of #{task.context_data[:targets].length} collections completed"]

          # Splitting e.g. "ManageIQ::Providers::Openstack::InfraManager::EmsCluster:8" to class and id
          pclass, _, pid = task.context_data[:parent].rpartition(":")
          parent = pclass.constantize.find(pid)
          msg = "Queueing [#{task.context_data[:interval]}] rollup to #{parent.class.name} id: [#{parent.id}] for time range: [#{task.context_data[:start]} - #{task.context_data[:end]}]"
          _log.info "#{msg}..."
          parent.perf_rollup_range_queue(task.context_data[:start], task.context_data[:end], task.context_data[:interval])
          _log.info "#{msg}...Complete"
        else
          task.state, task.status, task.message = [MiqTask::STATE_ACTIVE, MiqTask::STATUS_OK, task.message = "Performance collection active, #{task.context_data[:complete].length} out of #{task.context_data[:targets].length} collections completed"]
        end
        _log.info("Updating task id: [#{task.id}] #{task.message}")
        task.save!
      end
    end
  end

  def perf_capture_state
    VimPerformanceState.capture(self)
  end

  def perf_capture_realtime_now
    # For UI to enable refresh of realtime charts on demand
    _log.info "Realtime capture requested for #{log_target}"

    perf_capture_queue('realtime', :priority => MiqQueue::HIGH_PRIORITY)
  end
end


text:
from skrf.vi.lifetimeProbeTester import LifeTimeProbeTester
from skrf import Network
import pdb
import multiprocessing
from time import sleep
import numpy as npy

class Private():
    def __init__(self):
        self.lpt = None
        self.logger = multiprocessing.get_logger()
        self.counter= 0
private = Private()

def connect(**kwargs):
    try:
        private.lpt = LifeTimeProbeTester()
        private.logger.info('prober connected.')
    except:
        private.logger.error('prober failed to connect')
def close(**kwargs):
    private.lpt.close()

def contact(**kwargs):
    private.lpt.contact()

def byebye(**kwargs):
    private.move_apart_fast(100)
    private.lpt.close()

def enable_settings(text_contact_force,text_step_increment,\
        text_position_upper_limit, text_stage_delay,**kwargs):
    private.lpt.contact_force = float(text_contact_force.value)
    private.lpt.step_increment = float(text_step_increment.value)*1e-3
    private.lpt.position_upper_limit = float(text_position_upper_limit.value)
    private.lpt.stage.delay = float(text_stage_delay.value)

def update_plot (chart, **kwargs):
    private.counter +=1
    lpt= private.lpt
    force  = lpt.read_loadcell()
    chart.append(npy.array([private.counter, force]))

def stop(timer, **kwargs):
    timer.stop()
def monitor( timer, chart, **kwargs):
    chart.span = 100
    if not timer.running:
        timer.start(interval = .3)
    else:
        timer.stop()

redpajama_set_name: RedPajamaGithub


text:
<div id="bx-std-widgets-__page__" class="bx-std-widgets-wrp" <bx_if:page_hidden>style="display:none"</bx_if:page_hidden>>

__content__


text:
package com.intellij.openapi.vcs.vfs;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vcs.VcsBundle;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.history.ShortVcsRevisionNumber;
import com.intellij.openapi.vcs.history.VcsFileRevision;
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.openapi.vfs.VirtualFileSystem;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 * author: lesya
 */
public class VcsVirtualFile extends AbstractVcsVirtualFile {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.vcs.vfs.VcsVirtualFile");

  private byte[] myContent;
  private final VcsFileRevision myFileRevision;
  private boolean myContentLoadFailed = false;
  private Charset myCharset;

  public VcsVirtualFile(@NotNull String path,
                        @Nullable VcsFileRevision revision,
                        @NotNull VirtualFileSystem fileSystem) {
    super(path, fileSystem);
    myFileRevision = revision;
  }

  public VcsVirtualFile(@NotNull String path,
                        @NotNull byte[] content,
                        @Nullable String revision,
                        @NotNull VirtualFileSystem fileSystem) {
    this(path, null, fileSystem);
    myContent = content;
    setRevision(revision);
  }

  @NotNull
  public byte[] contentsToByteArray() throws IOException {
    if (myContentLoadFailed || myProcessingBeforeContentsChange) {
      return ArrayUtil.EMPTY_BYTE_ARRAY;
    }
    if (myContent == null) {
      loadContent();
    }
    return myContent;
  }

  private void loadContent() throws IOException {
    if (myContent != null) return;
    assert myFileRevision != null;

    final VcsFileSystem vcsFileSystem = ((VcsFileSystem)getFileSystem());

    try {
      myFileRevision.loadContent();
      fireBeforeContentsChange();

      myModificationStamp++;
      final VcsRevisionNumber revisionNumber = myFileRevision.getRevisionNumber();
      if (revisionNumber instanceof ShortVcsRevisionNumber) {
        setRevision(((ShortVcsRevisionNumber) revisionNumber).toShortString());
      }
      else {
        setRevision(revisionNumber.asString());
      }
      myContent = myFileRevision.getContent();
      myCharset = new CharsetToolkit(myContent).guessEncoding(myContent.length);
      ApplicationManager.getApplication().runWriteAction(new Runnable() {
        public void run() {
          vcsFileSystem.fireContentsChanged(this, VcsVirtualFile.this, 0);
        }
      });

    }
    catch (VcsException e) {
      myContentLoadFailed = true;
      ApplicationManager.getApplication().runWriteAction(new Runnable() {
        public void run() {
          vcsFileSystem.fireBeforeFileDeletion(this, VcsVirtualFile.this);
        }
      });
      myContent = ArrayUtil.EMPTY_BYTE_ARRAY;
      setRevision("0");

      Messages.showMessageDialog(
        VcsBundle.message("message.text.could.not.load.virtual.file.content", getPresentableUrl(), e.getLocalizedMessage()),
                                 VcsBundle.message("message.title.could.not.load.content"),
                                 Messages.getInformationIcon());

      ApplicationManager.getApplication().runWriteAction(new Runnable() {
        public void run() {
          vcsFileSystem.fireFileDeleted(this, VcsVirtualFile.this, getName(), getParent());
        }
      });

    }
    catch (ProcessCanceledException ex) {
      myContent = null;
    }

  }

  @Nullable
  public VcsFileRevision getFileRevision() {
    return myFileRevision;
  }

  @NotNull
  @Override
  public Charset getCharset() {
    if (myCharset != null) return myCharset;
    return super.getCharset();
  }

  public boolean isDirectory() {
    return false;
  }

  public String getRevision() {
    if (myRevision == null) {
      try {
        loadContent();
      }
      catch (IOException e) {
        LOG.info(e);
      }
    }
    return myRevision;
  }
}


text:
using Microsoft.Owin;
using Owin;

[assembly: OwinStartupAttribute(typeof(IdentityAndEmail.Startup))]
namespace IdentityAndEmail
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}


text:
package com.graphhopper.routing.util.tour;

import java.util.Random;

/**
 * Generate a single point tour with a heading
 *
 * @author ratrun
 */
public class SinglePointHeadingTour extends SinglePointTour
{
    private double heading;

    public SinglePointHeadingTour( Random random, double overallDistance, double heading )
    {
        super(random, overallDistance);
        this.heading = heading;
    }

    @Override
    public double getHeadingForIteration( int iteration )
    {
        return heading + random.nextInt(30);
    }
}


text:
<!DOCTYPE html>

    <title>VU Anh Tuan - Select Keyframe Method</title>

        <video id="video" width="640" height="360" controls autobuffer onloadeddata="initialize();">
            <source src="ct640.ogg">
        </video>

            Chose video file:

            <select id="vfile" autocomplete="off" onchange="changeVideo();">
                <option value="ct640.ogg">ct640.ogg</option>
                <option value="bmw.mp4">bmw.mp4</option>
                <option value="ct320.ogg">ct320.ogg</option>
            </select>

        <button id="selFrame" onclick="captureFrame();">SELECT FRAME</button>

            <canvas id="nav" width="640" height="360"></canvas>


text:
var access_enum = {
    NONE: 0,
    READ: 1,
    WRITE: 2,
    OWNER: 3,
    PROJECT_OWNER: 4
};

var access_text = [
  'Cannot access',
  'Can read',
  'Can write',
  'Owner',
  'Project Owner'
];

var createThumbnail = function(username, firstname, lastname, email, access, share) {
  var $thumbnail, data, select, options, access_text_current;

  data = {
      username: username,
      firstname: firstname,
      lastname: lastname,
      email: email,
      access: {
          read: false,
          write: false
      },
      currentaccess: {
          read: false,
          write: false
      }
  };

  access_text_current = access_text[access];

  if (access !== access_enum.NONE && access !== access_enum.OWNER) {
      data.access.read = true;
      data.currentaccess.read = true;
  }

  if (access === access_enum.WRITE) {
      data.access.write = true;
      data.currentaccess.write = true;
  }

  select = '';

  if (share) {
      select = '<select class="sharing-thumbnail-access" style="display: none;" disabled>';

      options = '';
      options += '<option value="' + access_enum.NONE + '"' + (access === access_enum.NONE ? "selected" : "") + ' style="display: none;">No Permissions</option>';
      options += '<option value="' + access_enum.READ + '"' + (access === access_enum.READ ? "selected" : "") + '>Can Read</option>';
      options += '<option value="' + access_enum.WRITE + '"' + (access === access_enum.WRITE ? "selected" : "") + '>Can Write</option>';

      select += options;
      select += '</select>';
   }

   $thumbnail = $('
 \

 \
                        <button type="button" class="sharing-thumbnail-unshare close" aria-label="Close">×</button> \

 \

' + username + '
\

 \

 \
                            \

 \

 \

' + firstname + ' ' + lastname + '
 \

' + email + '
 \
                              ' + select + ' \
                              ' + access_text_current + ' \

 \

');

   $thumbnail.find('.baseimage').show();
   $thumbnail.data(data);

   return $thumbnail;
};

var changeShareState = function($target) {
    var data;
    data = $target.data();
    $target.addClass('sharing-to-update');
    if ($target.hasClass('share-box-users-item')) {
        $target.find('.sharing-thumbnail-access').val('1').prop("disabled", false).show();
        $target.find('.sharing-thumbnail-access-text').text(access_text[access_enum.READ]).hide();
        data.currentaccess.read = true;
        $target.data(data);
        $target.find('.sharing-thumbnail-unshare').show();
        $target.detach().prependTo('#share-box-share').show();
    }
    else if ($target.hasClass('share-box-share-item')) {
        $target.find('.sharing-thumbnail-access').val('0').prop("disabled", true).hide();
        $target.find('.sharing-thumbnail-access-text').text(access_text[access_enum.NONE]).show();
        data.currentaccess.read = false;
        data.currentaccess.write = false;
        $target.data(data);
        $target.find('.sharing-thumbnail-unshare').hide();
        $target.detach().appendTo('#share-box-users');
        $('#share-box-filter').trigger('keydown');
        $(".order-results-selector").trigger('change');
    }
    $target.toggleClass('share-box-users-item share-box-share-item');
};

var usernameComparator = function(a, b) {
   var username_a, username_b;
   username_a = $(a).data().username.toLowerCase();
   username_b = $(b).data().username.toLowerCase();

   if (username_a < username_b) {
       return -1;
   } else if (username_a > username_b) {
       return 1;
   } else {
       return 0;
   }
};

var firstLastComparator = function(a, b) {
   var firstname_a, firstname_b, lastname_a, lastname_b;
   firstname_a = $(a).data().firstname.toLowerCase();
   firstname_b = $(b).data().firstname.toLowerCase();
   lastname_a = $(a).data().lastname.toLowerCase();
   lastname_b = $(b).data().lastname.toLowerCase();

   if (firstname_a < firstname_b) {
       return -1;
   } else if (firstname_a > firstname_b) {
       return 1;
   } else {
       if (lastname_a < lastname_b) {
           return -1;
       } else if (lastname_a > lastname_b) {
           return 1;
       } else {
           return 0;
       }
   }
};

var lastFirstComparator = function(a, b) {
  var firstname_a, firstname_b, lastname_a, lastname_b;
  firstname_a = $(a).data().firstname.toLowerCase();
  firstname_b = $(b).data().firstname.toLowerCase();
  lastname_a = $(a).data().lastname.toLowerCase();
  lastname_b = $(b).data().lastname.toLowerCase();

   if (lastname_a < lastname_b) {
       return -1;
   } else if (lastname_a > lastname_b) {
       return 1;
   } else {
       if (firstname_a < firstname_b) {
           return -1;
       } else if (firstname_a > firstname_b) {
           return 1;
       } else {
           return 0;
       }
   }
};

var emailComparator = function(a, b) {
   var email_a, email_b;
   email_a = $(a).data().email.toLowerCase();
   email_b = $(b).data().email.toLowerCase();

   if (email_a < email_b) {
       return -1;
   } else if (email_a > email_b) {
       return 1;
   } else {
       return 0;
   }
};

var userFilter = function(users, pattern) {
   re = new RegExp(pattern, 'i');
   $(users).each(function(index, element) {
       var data;
       data = $(element).data();
       if (re.test(data.username.toLowerCase()) ||
           re.test(data.firstname.toLowerCase()) ||
           re.test(data.lastname.toLowerCase()) ||
           re.test(data.email.toLowerCase())
       ) {
           $(element).show();
       } else {
           $(element).hide();
       }
   });
};


text:
namespace Org.Strausshome.Yapbt.YapbtEditor
{
    partial class MainWindow
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.AirportGroup = new System.Windows.Forms.GroupBox();
            this.OpenAirport = new System.Windows.Forms.Button();
            this.openBglXmlFileDialog = new System.Windows.Forms.OpenFileDialog();
            this.setBglTool = new System.Windows.Forms.OpenFileDialog();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
            this.CurrentStatusLabel = new System.Windows.Forms.ToolStripStatusLabel();
            this.YapbtBrowser = new System.Windows.Forms.WebBrowser();
            this.AirportGroup.SuspendLayout();
            this.statusStrip1.SuspendLayout();
            this.SuspendLayout();
            //
            // AirportGroup
            //
            this.AirportGroup.Controls.Add(this.OpenAirport);
            this.AirportGroup.Location = new System.Drawing.Point(12, 12);
            this.AirportGroup.Name = "AirportGroup";
            this.AirportGroup.Size = new System.Drawing.Size(89, 83);
            this.AirportGroup.TabIndex = 1;
            this.AirportGroup.TabStop = false;
            this.AirportGroup.Text = "Airport";
            //
            // OpenAirport
            //
            this.OpenAirport.Image = global::Org.Strausshome.Yapbt.YapbtEditor.Properties.Resources.airliner;
            this.OpenAirport.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
            this.OpenAirport.Location = new System.Drawing.Point(6, 19);
            this.OpenAirport.Name = "OpenAirport";
            this.OpenAirport.Size = new System.Drawing.Size(75, 57);
            this.OpenAirport.TabIndex = 0;
            this.OpenAirport.Text = "Open airport";
            this.OpenAirport.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
            this.OpenAirport.UseVisualStyleBackColor = true;
            this.OpenAirport.Click += new System.EventHandler(this.OpenAirport_Click);
            //
            // openBglXmlFileDialog
            //
            this.openBglXmlFileDialog.Filter = "XML files|*.xml|BGL files|*.bgl";
            this.openBglXmlFileDialog.Title = "Open BGL file";
            //
            // setBglTool
            //
            this.setBglTool.Filter = "BGL Converter|*.exe";
            this.setBglTool.Title = "Set BGL converter tool";
            //
            // statusStrip1
            //
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripStatusLabel1,
            this.CurrentStatusLabel});
            this.statusStrip1.Location = new System.Drawing.Point(0, 581);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(950, 22);
            this.statusStrip1.TabIndex = 2;
            this.statusStrip1.Text = "statusStrip1";
            //
            // toolStripStatusLabel1
            //
            this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
            this.toolStripStatusLabel1.Size = new System.Drawing.Size(45, 17);
            this.toolStripStatusLabel1.Text = "Status: ";
            //
            // CurrentStatusLabel
            //
            this.CurrentStatusLabel.Name = "CurrentStatusLabel";
            this.CurrentStatusLabel.Size = new System.Drawing.Size(17, 17);
            this.CurrentStatusLabel.Text = "--";
            //
            // YapbtBrowser
            //
            this.YapbtBrowser.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.YapbtBrowser.IsWebBrowserContextMenuEnabled = false;
            this.YapbtBrowser.Location = new System.Drawing.Point(12, 101);
            this.YapbtBrowser.MinimumSize = new System.Drawing.Size(20, 20);
            this.YapbtBrowser.Name = "YapbtBrowser";
            this.YapbtBrowser.ScriptErrorsSuppressed = true;
            this.YapbtBrowser.Size = new System.Drawing.Size(926, 477);
            this.YapbtBrowser.TabIndex = 3;
            //
            // MainWindow
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(950, 603);
            this.Controls.Add(this.YapbtBrowser);
            this.Controls.Add(this.statusStrip1);
            this.Controls.Add(this.AirportGroup);
            this.Name = "MainWindow";
            this.Text = "YAPBT- Editor";
            this.Shown += new System.EventHandler(this.MainWindow_Shown);
            this.AirportGroup.ResumeLayout(false);
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.GroupBox AirportGroup;
        private System.Windows.Forms.Button OpenAirport;
        private System.Windows.Forms.OpenFileDialog openBglXmlFileDialog;
        private System.Windows.Forms.OpenFileDialog setBglTool;
        private System.Windows.Forms.StatusStrip statusStrip1;
        private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
        private System.Windows.Forms.ToolStripStatusLabel CurrentStatusLabel;
        private System.Windows.Forms.WebBrowser YapbtBrowser;
    }
}


text:
<?php

namespace Bolt\Controller\Async;

use Bolt\Exception\EmbedResolverException;
use Embed\Exceptions\InvalidUrlException;
use GuzzleHttp\Psr7;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Async controller for embed routes.
 *
 * @internal
 *
 * @author Gawain Lynch <gawain.lynch@gmail.com>
 */
class Embed extends AsyncBase
{
    /**
     * {@inheritdoc}
     */
    protected function addRoutes(ControllerCollection $c)
    {
        $c->post('/embed', 'embed')
            ->bind('embedRequestEndpoint')
            ->before(function (Request $request) {
                $tokenValue = $request->request->get('_token');
                // Only accept valid tokens from the ContentType edit form, or the default "bolt" token ID
                if ($tokenValue === null || !($this->isCsrfTokenValid($tokenValue, 'content_edit') || $this->isCsrfTokenValid($tokenValue))) {
                    return new JsonResponse(['error' => ['message' => 'Invalid CSRF token']], Response::HTTP_FORBIDDEN);
                }
                if ($request->request->has('provider')) {
                    return null;
                }
                if ($request->request->has('url')) {
                    return null;
                }

                return new JsonResponse(['error' => ['message' => 'Invalid POST parameters']], Response::HTTP_FORBIDDEN);
            })
        ;
    }

    /**
     * @param Request $request
     *
     * @throws InvalidUrlException
     *
     * @return JsonResponse
     */
    public function embed(Request $request)
    {
        $provider = $request->request->get('provider');
        $url = $request->request->get('url');
        $url = new Psr7\Uri($url);
        /** @var \Bolt\Embed\Resolver $resolver */
        $resolver = $this->app['embed'];

        try {
            $data = $resolver->embed($url, $provider);
        } catch (EmbedResolverException $e) {
            $response = ['error' => ['message' => $e->getMessage()]];

            return new JsonResponse($response, Response::HTTP_SERVICE_UNAVAILABLE);
        }

        return new JsonResponse($data);
    }
}


text:
<?php

namespace LGM\AdministrationBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use LGM\AdministrationBundle\Entity\Article;
use LGM\AdministrationBundle\Form\ArticleType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\File\File;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class ArticleController extends Controller
{

    public function indexAction()
    {
        $article =  new Article();
        $em = $this->getDoctrine()->getManager();
        $article = $em->getRepository('LGMAdministrationBundle:Article')->findAll();

        return $this->render('LGMAdministrationBundle:Article:index.html.twig', array('articles'=>$article));
    }

     public function newAction(Request $request)
    {

         $article = new Article;

         $form = $this->createForm( new ArticleType(),$article);
         if($request->getMethod()=='POST')
         {
             $form->handleRequest($request);
              if ($form->isSubmitted() && $form->isValid()) {
             {

                 $em = $this->getDoctrine()->getManager();

                 $em->persist($article);

                 $em->flush();

                 return $this->redirect($this->generateUrl('index_article'));

             }
         }
         }
        return $this->render('LGMAdministrationBundle:Article:new.html.twig',array('form'=>$form->createView()));

    }
     public function editAction(Request $request, $token)
    {
          $em = $this->getDoctrine()->getManager();
         $article = new Article;
         $article = $em->getRepository('LGMAdministrationBundle:Article')->findOneByToken($token);
         $form = $this->createForm( new ArticleType(),$article);
         if($request->getMethod()=='POST')
         {
             $form->handleRequest($request);
             if ($form->isValid())
             {

                 $em->persist($article);
                 $em->flush();
                 return $this->redirect($this->generateUrl('index_article'));

             }
         }
        return $this->render('LGMAdministrationBundle:Article:edit.html.twig', array('form'=>$form->createView()));
    }

   public function deleteAction(Article $article, Request $request)
    {
        $form = $this->createFormBuilder()->getForm();

        if($request->getMethod()=='POST')
         {
          $form->handleRequest($request);
             if ($form->isValid())
             {
         $em = $this->getDoctrine()->getManager();
         $em->remove($article);
         $em->flush();
         return $this->redirect($this->generateUrl('index_article'));
             }
         }
        return $this->render('LGMAdministrationBundle:Article:delete.html.twig'
                , array('article'=>$article, 'form'=>$form->createView())
                );
    }

}


text:
import gdal
import scipy
import numpy as np
from sklearn import tree
from sklearn import ensemble
from sklearn import linear_model
from sklearn import svm
from sklearn import metrics
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
import pickle

# some functions for plotting later
def func_linear(x, m, b):
    return m * x + b

def func_fit(x, y, function):
    opt, cov = scipy.optimize.curve_fit(function, x, y)
    y_fit = function(np.array(x), *opt)
    rsq = metrics.r2_score(y, y_fit)
    rms = np.sqrt(metrics.mean_squared_error(y, y_fit))
    return [y_fit, rsq, rms]

# set base directory for files
base = '/home/cba/Downloads/tree-cover/'

# predictor data
#pred_file = base + 'coto_brus_predictors_masked2.tif'
pred_file = base + 'coto_brus_despeckle.tif'
pred_ref = gdal.Open(pred_file)
pred_prj = pred_ref.GetProjection()
pred_geo = pred_ref.GetGeoTransform()
nx = pred_ref.RasterXSize
ny = pred_ref.RasterYSize
nb = pred_ref.RasterCount
#pred_names = ["Palsar HH", "Palsar HV", "% Soil", "% Veg", "% Urban", "NIRv"]
pred_names = ["Palsar HH", "Palsar HV", "NIRv", "% Soil", "% Veg", "% Urban"]

# training data
train_file = base + 'coto_brus_tree_cover.tif'
train_ref = gdal.Open(train_file)
train_nd = train_ref.GetRasterBand(1).GetNoDataValue()

# name of the output mode
model_file = base + 'coto_brus_tree_cover_model'

# read the data into memory and reduce dimensions
train_arr = train_ref.ReadAsArray()
gd = np.where(train_arr != train_nd)
train = train_arr[gd[0], gd[1]]
train_arr = None

pred_arr = pred_ref.ReadAsArray()
pred = pred_arr[:, gd[0], gd[1]]
pred_arr = None
pred = pred.transpose()

# transform the predictor data to 0-1 range
scaler = preprocessing.MinMaxScaler()
pred = scaler.fit_transform(pred)

# save the scaler to use when applying later
pickle.dump(scaler, open(base + 'tree_cover_scaler.sav', 'wb'))

# create the train/test split
x_train, x_test, y_train, y_test = train_test_split(
    pred, train, test_size = 0.8)

# create a more advanced train/test split based on even
#  sampling across tree cover bins (here, 10%)
binsize = 0.1
bins = np.arange(0, 1.00001, binsize)
#bins = np.array([0, 0.1, 1.0001])

# use 90% of the least common bin size as the number of per-bin samples
train_hist = np.histogram(train, bins = bins)
nrnd = int(np.floor(min(train_hist[0]) * 0.9))

# to get good representation of low tree cover, add a bunch more
#  samples from that class
lt10 = 10
nrnd_lt10 = lt10 * nrnd

# create arrays to store the train and test data
n_train = int(np.floor(nrnd * len(train_hist[0]) + (nrnd_lt10 - nrnd)))
n_test = train.shape[0] - n_train

x_train = np.zeros((n_train, nb))
y_train = np.zeros(n_train)
x_test = np.zeros((n_test, nb))
y_test = np.zeros(n_test)

# create a counter to store the indices for where the test data go in the test array
train_start = 0
test_start = 0

for i in range(len(bins)-1):
    # find the indices for this bin, then randomize them
    ll = np.where(train >= bins[i])
    if i != len(bins)-2:
        ul = np.where(train < bins[i+1])
    else:
        ul = np.where(train <= bins[i+1])
    intersect = np.intersect1d(ul, ll)
    np.random.shuffle(intersect)

    # separate the indices for each bin into train and test sets
    if i == 0:
        int_train = intersect[:nrnd_lt10]
        int_test = intersect[nrnd_lt10:]
    else:
        int_train = intersect[:nrnd]
        int_test = intersect[nrnd:]

    # extract these data from the response and predictor data
    n_test = len(int_test)
    if i == 0:
        n_train = nrnd_lt10
    else:
        n_train = nrnd

    x_train[train_start:train_start+n_train, :] = pred[int_train, :]
    y_train[train_start:train_start+n_train] = train[int_train]
    train_start += n_train

    x_test[test_start:test_start+n_test] = pred[int_test, :]
    y_test[test_start:test_start+n_test] = train[int_test]
    test_start += n_test

# create the regression models
gb = ensemble.GradientBoostingRegressor(learning_rate=0.1, min_samples_split=2,
    n_estimators=1000, verbose=2, loss='lad')
rf = ensemble.RandomForestRegressor(n_jobs = 7, verbose = 2, n_estimators=15,
    criterion='mae')
et = ensemble.ExtraTreesRegressor(n_jobs = 7, verbose = 1)
br = ensemble.BaggingRegressor(n_jobs = 7, verbose = 1)
ab = ensemble.AdaBoostRegressor()
ol = linear_model.LinearRegression(n_jobs = 7)

models = [rf, gb, et, br, ab, ol]
model_names = ['GradientBoosting', 'RandomForest', 'ExtraTrees',
    'Bagging', 'AdaBoost', 'LinearModel']

output_pred = {}
output_test = {}

for i in range(len(models)):

    # report starting
    print("-----")
    print("Processing {} model".format(model_names[i]))

    # train the model
    models[i].fit(x_train, y_train)

    model_file_name = "{}_{}.sav".format(model_file, model_names[i])

    # save the model
    pickle.dump(models[i], open(model_file_name, 'wb'))

    # test the model
    y_pred = models[i].predict(x_test)
    output_test[model_names[i]] = y_pred

    # output the full model to a dictionary
    opred = models[i].predict(pred)
    output_pred[model_names[i]] = opred

    # get metrics
    r_squared = metrics.r2_score(y_test, y_pred)
    explained_var = metrics.explained_variance_score(y_test, y_pred)
    rmse = np.sqrt(metrics.mean_squared_error(y_test, y_pred))
    mae = metrics.mean_absolute_error(y_test, y_pred)

    # print model results
    print("r-squared    : {:0.3f}".format(r_squared))
    print("explained var: {:0.3f}".format(explained_var))
    print("rmse         : {:0.3f}".format(rmse))
    print("mean abs err : {:0.3f}".format(mae))
    #print("var. importance")
    #for j in range(len(pred_names)):
    #    print("{} : {:0.3f}".format(pred_names[j], rf_def.feature_importances_[j]))

    # write to an output raster
    output_file_name = "{}_{}.tif".format(model_file, model_names[i])
    oref = gdal.GetDriverByName("GTiff").Create(output_file_name,
        nx, ny, 1, gdal.GDT_Float32)
    oref.SetProjection(pred_prj)
    oref.SetGeoTransform(pred_geo)
    ob = oref.GetRasterBand(1)
    outarr = np.zeros((ny, nx))
    outarr[gd[0], gd[1]] = opred
    ob.WriteArray(outarr)
    outarr = None
    ob = None
    oref = None

    # plot the output
    #plt.figure(1)
    #lin_fit = func_fit(y_test, y_pred, func_linear)
    #plt.hexbin(y_pred, y_test, cmap = 'plasma', alpha = 0.95, bins = 'log',
    #    label = "rmse: {:0.3f}".format(rmse), gridsize = 20)
    #plt.plot(lin_fit[0], y_test, c = "black", label = "r-sqared: {:0.3f}".format(lin_fit[1]))
    #plt.xlabel("Predicted")
    #plt.ylabel("Actual")
    #plt.title("{} Tree Cover".format(model_names[i]))
    #plt.legend()
    #plt.show()
    #plt.tight_layout()
    #outfile = "{}_{}.png".format(model_file, model_names[i])
    #plt.savefig(outfile)
    #plt.close()


text:
layout: post
theme: UbuntuMono
series:
  url: try-cloudfoundry
  title: Try Cloud Foundry
title: Autoscaler
excerpt: "Autoscaler"
modified: 2017-09-05T17:00:00-00:00
categories: articles
tags: [Scalability, Cloud Foundry]
image:
  vendor: twitter
  feature: /media/DMCGHYyXkAI5vDb.jpg:large
  credit: Nat Geo Photography
  creditlink: https://twitter.com/NatGeoPhotos
comments: true
share: true
references:
  - title: "Scaling an Application Using App Autoscaler"
    url: "https://docs.run.pivotal.io/appsman-services/autoscaler/using-autoscaler.html"
---

* TOC
{:toc}

Pivotal Web Services (PWS) 的 Marketplace 里的 App Autoscaler 是用来根据设定的规则自动伸缩应用程序的一项服务。

## Set up App Autoscaler
在 PWS 界面里创建或者使用 CLI 创建。

用命令行创建 AutoScaler 服务实例 `cf cs app-autoscaler standard try-cf-autoscaler`。

绑定到 Application `cf bind-service try-cf-spring-boot try-cf-autoscaler`。

然后重启应用程序 `cf restage try-cf-spring-boot`

## Configure Autoscaling for an App

打开 AutoScaler 实例的管理界面

![full-autoscaler.png](https://docs.run.pivotal.io/appsman-services/autoscaler/images/full-autoscaler.png)

首先 AutoScaler 可以设置最小最大实例数限制。

其次 Autoscaler Scale 应用程序是通过事件机制实现的。目前事件有两种,基于 Rule 的事件,和 Scheduled 的事件。

### Rules

基于 rule 的事件有三种指标:

| Metric	| Description |
|-------- |:----------- |
|CPU Utilization|	Average CPU percentage for all instances of the app|
|HTTP Latency|	Average Latency of applications response to HTTP requests. This does not include Gorouter processing time or other network latency. Average is calculated on the middle 99% or middle 95% of all HTTP requests.|
|HTTP Throughput|	Total HTTP Requests per second (divided by total app instances).|

当规则中指标达到设定的上下限时,会产生相应的事件,Autoscaler 根据事件增加或减少应用程序实例数量。

### Scheduled Limit Changes

可以设定时间去改变应用程序实例数的范围数值,以达到定时增加或减少应用程序实例的目的。

## Event History

Event History 是一个简单记录事件的页面。


text:
class CreateItsfFundReportsTradesOrders < ActiveRecord::Migration
  def change
    create_table :itsf_fund_reports_trades_orders do |t|
      t.references :account
      t.references :action
      t.references :currency
      t.references :asset_category
      t.decimal :fx_rate_to_base, :precision => 12, :scale => 4
      t.references :symbol
      t.string :description
      t.string :con_identifier
      t.string :security_indentifier
      t.string :security_id_type
      t.string :cusip
      t.references :security
      t.string :underlying_con_identifier
      t.references :underlying_symbol
      t.string :issuer
      t.string :trade_identifier
      t.date :report_date
      t.timestamp :trade_time
      t.date :settle_date_target
      t.references :transaction_type
      t.references :exchange
      t.integer :quantity
      t.decimal :trade_price, :precision => 12, :scale => 4
      t.integer :multiplier
      t.decimal :trade_money, :precision => 12, :scale => 4
      t.decimal :proceeds, :precision => 12, :scale => 4
      t.decimal :taxes, :precision => 12, :scale => 4
      t.decimal :ib_commission, :precision => 12, :scale => 4
      t.references :ib_commission_currency
      t.decimal :close_price, :precision => 12, :scale => 4
      t.string :open_close_indicator
      t.string :notes
      t.decimal :cost, :precision => 12, :scale => 4
      t.decimal :fifo_pnl_realized, :precision => 12, :scale => 4
      t.decimal :mtm_pnl, :precision => 12, :scale => 4
      t.decimal :orig_trade_price, :precision => 12, :scale => 4
      t.timestamp :orig_trade_date
      t.string :orig_trade_identifier
      t.string :orig_order_identifier
      t.string :strike
      t.date :expiry
      t.string :put_call
      t.string :ib_order_identifier
      t.string :ib_exec_identifier
      t.string :brokerage_order_ifentifier
      t.string :order_reference
      t.string :volatility_order_link
      t.timestamp :order_placement_time
      t.string :clearing_firm_identifier
      t.string :exch_order_identifier
      t.string :ext_exec_identifier
      t.timestamp :order_time
      t.timestamp :open_date_time
      t.timestamp :holding_period_date_time
      t.timestamp :when_realized
      t.timestamp :when_reopened
      t.string :level_of_detail
      t.decimal :change_in_price, :precision => 12, :scale => 4
      t.integer :change_in_quantity
      t.decimal :net_cash, :precision => 12, :scale => 4
      t.references :order_type
      t.references :flex_query_run

      t.timestamps
    end
    add_index :itsf_fund_reports_trades_orders, :account_id
    add_index :itsf_fund_reports_trades_orders, :action_id
    add_index :itsf_fund_reports_trades_orders, :currency_id
    add_index :itsf_fund_reports_trades_orders, :asset_category_id
    add_index :itsf_fund_reports_trades_orders, :symbol_id
    add_index :itsf_fund_reports_trades_orders, :security_id
    add_index :itsf_fund_reports_trades_orders, :underlying_symbol_id
    add_index :itsf_fund_reports_trades_orders, :transaction_type_id
    add_index :itsf_fund_reports_trades_orders, :exchange_id
    add_index :itsf_fund_reports_trades_orders, :ib_commission_currency_id, :name => 'index_itsf_fund_reports_trades_orders_on_ib_comm_currency_id'
    add_index :itsf_fund_reports_trades_orders, :order_type_id
    add_index :itsf_fund_reports_trades_orders, :flex_query_run_id
  end
end


text:
<?php //-->

namespace Eden\Cookie;

/**
 * Exception Class
 *
 * @vendor   Eden
 * @package  Cookie
 * @author   Christian Blanquera <cblanquera@openovate.com>
 * @standard PSR-2
 */
class Exception extends \Eden\Core\Exception
{
}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using Collectively.Api.Framework;
using Collectively.Api.Queries;
using Collectively.Common.Extensions;
using Collectively.Common.Locations;
using Collectively.Common.Types;
using Collectively.Services.Storage.Models.Remarks;

namespace Collectively.Api.Filters
{
    public class BrowseSimilarRemarksPagedFilter : IPagedFilter<Remark, BrowseSimilarRemarks>
    {
        public PagedResult<Remark> Filter(IEnumerable<Remark> values, BrowseSimilarRemarks query)
        {
            if (!query.IsLocationProvided())
            {
                return PagedResult<Remark>.Empty;
            }
            if (query.Category.Empty())
            {
                return PagedResult<Remark>.Empty;
            }
            if (query.Page <= 0)
            {
                query.Page = 1;
            }
            if (query.Results <= 0)
            {
                query.Results = 10;
            }
            if (query.Results > 1000)
            {
                query.Results = 1000;
            }
            if (query.Radius <= 0)
            {
                query.Radius = 10;
            }
            if (query.Radius > 1000)
            {
                query.Radius = 1000;
            }
            values = values.Where(x => x.Category.Name == query.Category);
            values.SetRemarksDistance(query.Latitude, query.Longitude);

            return values.OrderBy(x => x.Distance).Paginate(query);
        }
    }
}