Sunday, February 28, 2016

用 curl 測試 Reserve Proxy 是否正確運作

架好 reverse proxy 後要測試可以用 curl 的 --resolve 的功能來確認。

# curl -v --resolve i.kfs.io:443:68.232.45.191 https://i.kfs.io/article5/global/364,324,6v1/original.png > /dev/null

其中 --resolve 的第三個參數一定要用 IP address,你可以看到他的運作原理:

* Added i.kfs.io:443:68.232.45.191 to DNS cache

Reference:

https://blog.gslin.org/archives/2016/02/28/6375/%E7%94%A8-curl-%E6%B8%AC%E8%A9%A6-reserve-proxy-%E6%98%AF%E5%90%A6%E6%AD%A3%E7%A2%BA%E9%81%8B%E4%BD%9C/

Friday, February 26, 2016

jquery : focus to div is not working

Focus doesn't work on divs by default. But, according to this, you can make it work:

The focus event is sent to an element when it gains focus. This event is implicitly applicable to a limited set of elements, such as form elements (<input>, <select>, etc.) and links (<a href>). In recent browser versions, the event can be extended to include all element types by explicitly setting the element's tabindex property. An element can gain focus via keyboard commands, such as the Tab key, or by mouse clicks on the element.

http://api.jquery.com/focus/

To focus on Div element using jQuery its required to DIV element has tabindex property value -1. so set:

<div id="mydiv" tabindex="-1"></div>

Reference:

http://stackoverflow.com/questions/5965924/jquery-focus-to-div-is-not-working

Thursday, February 25, 2016

mysqldump Warning: mysqldump: ignoring option '--databases' due to invalid value 'simply'

mysqldump Warning: mysqldump: ignoring option '--databases' due to invalid value 'simply'

Reason: it was because I have the "database" line in my.cnf file. But, mysqldump doesn't like that parameter.

# cat ~/.my.cnf

[client]
user=root
host=127.0.0.1
password="root"
database=simply

Reference:

http://serverfault.com/questions/670648/mysqldump-complains-about-chosen-databases-regardless-of-command-line

Tuesday, February 23, 2016

InnoDB memory usage buffer pool status

InnoDB memory usage buffer pool status

Making sense of INNODB buffer pool stats

After having read this page in the mysql documentation, I tried to make sense of our current InnoDB usage. Currently, we allocate 6GB of RAM for the buffer pool. Our database size is about the same. Here's the output from show engine innodb status\G (we're running v5.5)

----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 6593445888; in additional pool allocated 0
Dictionary memory allocated 1758417
Buffer pool size   393215
Free buffers       853
Database pages     360515
Old database pages 133060
Modified db pages  300
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 7365790, not young 23099457
0.00 youngs/s, 0.00 non-youngs/s
Pages read 1094342, created 185628, written 543182148
0.00 reads/s, 0.00 creates/s, 37.32 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 360515, unzip_LRU len: 0
I/O sum[2571]:cur[0], unzip sum[0]:cur[0]

I wanted to know how well we're utilizing the buffer cache. After initially glancing at the output, it appeared that we are indeed using it, based off of the Pages made young and not young have numbers in them and Buffer pool hit rate is 1000 / 10000 (which I saw elsewhere on the web that this means it's being used pretty heavily. True?)

What's throwing me through a loop is why the young-making rate and not are both at 0/1000 and the young/s and non-young/s accesses are both at 0. Those would all indicate that it's not being used at all, right?

Can anyone help make sense of this?

This is in pages not bytes:

The Buffer pool size   393215

To see the Buffer Pool size in GB run this:

SELECT FORMAT(BufferPoolPages*PageSize/POWER(1024,3),2) BufferPoolDataGB FROM
(SELECT variable_value BufferPoolPages FROM information_schema.global_status
WHERE variable_name = 'Innodb_buffer_pool_pages_total') A,
(SELECT variable_value PageSize FROM information_schema.global_status
WHERE variable_name = 'Innodb_page_size') B;

This is the number of pages with data inside the Buffer Pool:

Database pages     360515

To see the amount of data in the Buffer Pool size in GB run this:

SELECT FORMAT(BufferPoolPages*PageSize/POWER(1024,3),2) BufferPoolDataGB FROM
(SELECT variable_value BufferPoolPages FROM information_schema.global_status
WHERE variable_name = 'Innodb_buffer_pool_pages_data') A,
(SELECT variable_value PageSize FROM information_schema.global_status
WHERE variable_name = 'Innodb_page_size') B;

To see the percentage of the Buffer Pool in use, run this:

SELECT CONCAT(FORMAT(DataPages*100.0/TotalPages,2),' %') BufferPoolDataPercentage FROM
(SELECT variable_value DataPages FROM information_schema.global_status
WHERE variable_name = 'Innodb_buffer_pool_pages_data') A,
(SELECT variable_value TotalPages FROM information_schema.global_status
WHERE variable_name = 'Innodb_buffer_pool_pages_total') B;

This is the number of pages in the Buffer Pool that have to be written back to the database. They are also referred to as dirty pages:

Modified db pages  300

To see the Space Taken Up by Dirty Pages, run this:

SELECT FORMAT(DirtyPages*PageSize/POWER(1024,3),2) BufferPoolDirtyGB FROM
(SELECT variable_value DirtyPages FROM information_schema.global_status
WHERE variable_name = 'Innodb_buffer_pool_pages_dirty') A,
(SELECT variable_value PageSize FROM information_schema.global_status
WHERE variable_name = 'Innodb_page_size') B;

To see the Percentage of Dirty Pages, run this:

SELECT CONCAT(FORMAT(DirtyPages*100.0/TotalPages,2),' %') BufferPoolDirtyPercentage FROM
(SELECT variable_value DirtyPages FROM information_schema.global_status
WHERE variable_name = 'Innodb_buffer_pool_pages_dirty') A,
(SELECT variable_value TotalPages FROM information_schema.global_status
WHERE variable_name = 'Innodb_buffer_pool_pages_total') B;

As for the other things in the display, run this:

SHOW GLOBAL STATUS LIKE 'Innodb_buffer_pool%';

You'll see all the status variables for the Buffer Pool. ou can apply the same queries against whatever you need to examine.

The buffer pool is divided into two part, a young list and a not-young list. The making rate shows how many pages in the buffer pools are being shuffled between the two lists.

Pages made young are not-young pages being made (i.e. being read out of the cache. Pages made not-young are pages moved from the young list because either they are too old, or because the young list is full.

The rate at pages are moved between the two depends upon how much of the buffer pool is currently being used vs the size of the young pool. Set at zero means your active set (the pages you are using) is smaller than the young pool.

Reference:

http://dba.stackexchange.com/questions/56494/making-sense-of-innodb-buffer-pool-stats

http://dev.mysql.com/doc/refman/5.7/en/innodb-buffer-pool.html

Thursday, February 18, 2016

jQuery selector escape square brackets to select element

jQuery selector escape square brackets to select element

If you feel more comfortable without escaping you also use the attributes selector and search for the element with that id like this:

$('[id="meta[152][value]"]')

From the jQuery documentation:

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\.

Reference:

http://stackoverflow.com/questions/8404037/jquery-escape-square-brackets-to-select-element

http://stackoverflow.com/questions/18573178/what-is-the-difference-between-these-different-ways-to-escape-square-brackets-in

Tuesday, February 16, 2016

To disable MySQL query cache when testing

To disable MySQL query cache when testing:

SELECT SQL_NO_CACHE id, name FROM customer;

Or

RESET QUERY CACHE

Or

SET SESSION query_cache_type = OFF

Note: Besides the query cache, there is also the cache in buffer pool you need to consider. The only way to purge the buffer pool is to restart the MySQL instance.

Reference:

http://stackoverflow.com/questions/16043943/how-to-disable-mysql-query-caching

http://dev.mysql.com/doc/refman/5.7/en/innodb-buffer-pool.html

Fast counting of rows on InnoDB

Fast counting of rows on InnoDB

See, MyISAM always stores the number of rows on the table header. So, whenever we ask "how many rows are there?", it can just grab the count and return it. Not InnoDB. This makes InnoDB very slow in count queries without a where clause.

So if you have query like SELECT COUNT(*) FROM USER It will be much faster for MyISAM (MEMORY and some others) tables because they would simply read number of rows in the table from stored value. Innodb will however need to perform full table scan or full index scan because it does not have such counter, it also can’t be solved by simple singe counter for Innodb tables as different transactions may see different number of rows in the table.

If you have query like SELECT COUNT(*) FROM IMAGE WHERE USER_ID=5 this query will be executed same way both for MyISAM and Innodb tables by performing index rage scan. This can be faster or slower both for MyISAM and Innodb depending on various conditions.

The trick is to hint it at a specific index. So, if you’re getting poor response times from InnoDB count, it means you’ve got lots of rows that have to be counted one at a time. And since you’ve got a lot of rows, you have at least one index. Just pick an index that will never contain a NULL value, and tell MySQL to USE INDEX (index_name).

Note: COUNT doesn't count NULL values, so if you are counting values by a field that has NULL values, those rows won't be counted by COUNT.

SELECT COUNT(*) FROM messages USE INDEX (index_messages_on_remote_created_at);

+----------+
| count(*) |
+----------+
|  1276831 |
+----------+

1 row in set (3.19 sec)

Reference:

http://kingori.co/minutae/2013/05/mysql-count-innodb/

Fast MySQL InnoDB count. Really fast

MySQL Performance Blog | COUNT(*) for Innodb Tables

MySQL 5.1 Reference Manual | Counting Rows

Wednesday, February 10, 2016

How to generate a new GUID unique ID?

How to generate a new GUID unique ID?

According to Is there any difference between a GUID and a UUID?

GUID is Microsoft's implementation of the UUID standard.

So, here's a link to libraries, that let's you create UUIDs of the following types:

version 1 (time-based)
version 3 (name-based and hashed with MD5)
version 4 (random)
version 5 (name-based and hashed with SHA1)

https://github.com/search?p=1&q=uuid+php&ref=cmdform&type=Repositories

I don't know exactly, which one C# is using, but that's at least something you can use if you're writing some piece of software and want to have universal unique identifiers.

My perfered choice was https://github.com/fredriklindberg/class.uuid.php because it is just a simple PHP file and the most rated one (https://github.com/ramsey/uuid) had to much dependencies on other libraries, but his may change soon (see https://github.com/ramsey/uuid/issues/20).

But if you really need a GUID (according to the Microsoft standard), they have a different generation process than these 4122. Wikipedia claims that

GUIDs and RFC 4122 UUIDs should be identical when displayed textually

http://en.wikipedia.org/wiki/Globally_Unique_Identifier#Binary_encoding

In most cases, you should be fine by going for one of the PHP libs for UUIDs. I don't think you're meddling with Microsoft Component Object Model (COM), don't you?

<?php
// Usage
// Named-based UUID.

$v3uuid = UUID::v3('1546058f-5a25-4334-85ae-e68f2a44bbaf', 'SomeRandomString');
$v5uuid = UUID::v5('1546058f-5a25-4334-85ae-e68f2a44bbaf', 'SomeRandomString');

// Pseudo-random UUID

$v4uuid = UUID::v4();

class UUID {
  public static function v3($namespace, $name) {
    if(!self::is_valid($namespace)) return false;

    // Get hexadecimal components of namespace
    $nhex = str_replace(array('-','{','}'), '', $namespace);

    // Binary Value
    $nstr = '';

    // Convert Namespace UUID to bits
    for($i = 0; $i < strlen($nhex); $i+=2) {
      $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
    }

    // Calculate hash value
    $hash = md5($nstr . $name);

    return sprintf('%08s-%04s-%04x-%04x-%12s',

      // 32 bits for "time_low"
      substr($hash, 0, 8),

      // 16 bits for "time_mid"
      substr($hash, 8, 4),

      // 16 bits for "time_hi_and_version",
      // four most significant bits holds version number 3
      (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,

      // 16 bits, 8 bits for "clk_seq_hi_res",
      // 8 bits for "clk_seq_low",
      // two most significant bits holds zero and one for variant DCE1.1
      (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,

      // 48 bits for "node"
      substr($hash, 20, 12)
    );
  }

  public static function v4() {
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

      // 32 bits for "time_low"
      mt_rand(0, 0xffff), mt_rand(0, 0xffff),

      // 16 bits for "time_mid"
      mt_rand(0, 0xffff),

      // 16 bits for "time_hi_and_version",
      // four most significant bits holds version number 4
      mt_rand(0, 0x0fff) | 0x4000,

      // 16 bits, 8 bits for "clk_seq_hi_res",
      // 8 bits for "clk_seq_low",
      // two most significant bits holds zero and one for variant DCE1.1
      mt_rand(0, 0x3fff) | 0x8000,

      // 48 bits for "node"
      mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
    );
  }

  public static function v5($namespace, $name) {
    if(!self::is_valid($namespace)) return false;

    // Get hexadecimal components of namespace
    $nhex = str_replace(array('-','{','}'), '', $namespace);

    // Binary Value
    $nstr = '';

    // Convert Namespace UUID to bits
    for($i = 0; $i < strlen($nhex); $i+=2) {
      $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
    }

    // Calculate hash value
    $hash = sha1($nstr . $name);

    return sprintf('%08s-%04s-%04x-%04x-%12s',

      // 32 bits for "time_low"
      substr($hash, 0, 8),

      // 16 bits for "time_mid"
      substr($hash, 8, 4),

      // 16 bits for "time_hi_and_version",
      // four most significant bits holds version number 5
      (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,

      // 16 bits, 8 bits for "clk_seq_hi_res",
      // 8 bits for "clk_seq_low",
      // two most significant bits holds zero and one for variant DCE1.1
      (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,

      // 48 bits for "node"
      substr($hash, 20, 12)
    );
  }

  public static function is_valid($uuid) {
    return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
                      '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
  }
}
?>

Reference:

http://stackoverflow.com/questions/21671179/how-to-generate-a-new-guid

http://php.net/manual/en/function.uniqid.php

http://php.net/manual/en/function.com-create-guid.php

Sunday, February 7, 2016

一個年薪9000萬的銷售女神經驗總結,太精闢了!

一個年薪9000萬的銷售女神經驗總結,太精闢了!

1、銷售最大的信念

一切成交都是為了愛!

2、銷售要掌握的兩大能力

①銷售攻心能力;

②整合資源的能力。

3、銷售三境界

①圍人:能將顧客圍住,並死纏爛打,初步具備接近顧客、推介購買的能力;

②維人:建立長期穩定的關係,不是簡單的買賣,而是朋友、夥伴關係;

③為人:不只是把產品賣出去,同時把自己也銷售出去。

4、銷售不出業績4主要原因

①拖延,不斷的拖延;

②無意義的拜訪;

③一問三不知;

④生理的疲憊。

5、業績猛增的5類銷售

①導師型:靠智慧吃飯的帶隊者;

②鬥士型:喜歡交談、擅長外交,喜歡與銷售並肩作戰;

③警官型:有極高的忠誠度;

④自信型:沒有“不可能”;

⑤事必躬親型:有強烈的責任感。

6、頂級銷售的6個人格特質

①主動積極,永不放棄,提高成功機率;

②同理心,察覺客戶沒說出口的需求;

③正向思考,挫折復原力強,修正再出發;

④守紀律,做好簡單的小事,累積成卓越;

⑤聽多於說,先聽後說,提出對的問題;

⑥說真話,重承諾,不說謊,不誇張。

7、銷售頂尖人員的7個小習慣

①不說尖酸刻薄的話;

②牢記顧客的名字,養成翻看客戶檔案的習慣;

③嘗試著跟你討厭的人交往;

④一定要尊重顧客的隱私;

⑤很多人在一起的時候,當你與其中某個人交談,請不要無視其他人存在;

⑥勇於認錯,誠信待人;

⑦以謙卑的姿態面對身邊的每一個人。

8、銷售的八個更重要

①找到顧客重要,找准顧客更重要;

②瞭解產品重要,瞭解需求更重要;

③搞清價格重要,搞清價值更重要;

④融入團隊重要,融入顧客更重要;

⑤口勤腿勤重要,心勤腦勤更重要;

⑥獲得認可重要,獲得信任更重要;

⑦達成合作重要,持續合作更重要;

⑧卓越銷售重要,不需銷售更重要。

9、頂尖銷售員的九大秘訣

①錢是給內行人賺的——世界上沒有賣不出的貨,只有賣不出的貨的人;

②想幹的人永遠在找方法,不想幹的人永遠在找理由;世界上沒有走不通的路,只有想不通的人;

③銷售者不要與顧客爭論價格,要與顧客討論價值;

④帶著目標出去,帶著結果回來,成功不是因為快,而是因為有方法;

⑤沒有不對的客戶,只有不夠的服務;

⑥營銷人的職業信念:要把接受別人拒絕作為一種職業生活方式;

⑦客戶會走到我們店裡來,我們要走進客戶心裡去;老客戶要坦誠,新客戶要熱情,急客戶要速度,大客戶要品味,小客戶要利益;

⑧客戶需要的不是產品,而是一套解決方案;賣什麼不重要,重要的是怎麼賣;

⑨客戶不會關心你賣什麼,而只會關心自己要什麼;沒有最好的產品,只有最合適的產品。

Reference:

http://tw.anyelse.com/article/155710.html

Friday, February 5, 2016

Use CSS's transform property to move the element to a position

Use CSS's transform property to move the element to a position

<html>
  <head>
    <style>
    .sourceDiv {
      background: #000;
      color: #fff;
      width: 100px;
      height: 100px;
      position: absolute;
    }

    .destinationDiv {
      top: 300px;
      left: 700px;
      background: #c2c3c4;
      color: #fff;
      width: 100px;
      height: 100px;
      position: absolute;
    }

    .moveTo600x400 {
      transform: translate(600px, 400px);
      transition-duration: 5s;
    }
    </style>
    <script src="/javascript/jquery/jquery-1.10.2.min.js"></script>
    <script>
      $(document).ready(function() {
        $('.sourceDiv').on('click', function(e){
          //$(this).addClass('moveTo600x400');

          var pos = $('.destinationDiv').offset();
          console.log(pos);

          $(this).css({
            transform: 'translate(' + pos.left + 'px, ' + pos.top + 'px)',
            'transition-duration': '3s',
          });
        });
      });
    </script>
  </head>
  <body>
    <div class="sourceDiv">test</div>
    <div class="destinationDiv">test</div>
  </body>
</html>