Comments

异常平台 Exceptional

visit: 官网

一个不错的统计项目的出错请求,而且支持多种语言,但是不是免费($9/month)

Rails 使用方式

  1. 在Gemfile配置

     gem exceptional
    
  2. 打包

     bundle install
    
  3. 生成exceptional的配置文件

     #YOUR-API-KEY 你申请的key
     bundle exec exceptional install YOUR-API-KEY
    
Comments

Ngrok反向代理工具

ngrok是个款不错的工具,在我们开发中经常要用到。

visit: 官网

使用方式

  1. 简单的运行

     // 代理你电脑3001端口, 运行后通过http://127.0.0.1:4040查看请求状态    
     ngrok 3001
    
  2. 验证运行

     // authtoken你在官方注册,就会生成一个, authtoken只要在电脑上运行一次就可以了会
     // 记录的, 如果你验证了的话ngrok 生成的前缀域名不会被占用和变化
     ngrok -authtoken U54UzZiDLFQxyZ-Ow*** 3001
    
  3. 需要别人验证才可以浏览

     // helmet用户名, 12345密码
     ngrok -httpauth="helmet:12345" 3001
    
  4. 设置前缀域名

     ngrok -subdomain=example 3001
    
  5. tcp代理

     ngrok -proto=tcp 22
    
  6. 指定其它服务器代理

     //为192.168.1.16服务器的3001端口代理
     ngrok 192.168.1.16:3001
    
  7. 运行多个类型服务

     // ngrok.yml配置类型如下
    
     tunnels:
       client:
         proto:
           https: "3000"
         auth: user:password
       c99bba1.ngrok.com:
         proto:
           http: "3000"
       ssh:
         proto:
           tcp: "22"
    
    
     // config: 配置文件路径
     // start: 运行哪几种类型服务(client, ssh c99bba1.ngrok.com)
     ngrok -log=stdout -authtoken=U54UzZiDLFQxyZ-Ow*** -config=ngrok.yml start client ssh c99bba1.ngrok.com
    
git
Comments

Git 错误 Object Is Empy / Corrupt

问题

早上上班开始工作是准备git push到远程服务器时,出现下面错误!我估计是之前的merge导致的

1
2
  error: object file .git/objects/xx/xxx is empty
  fatal: loose object xxx (stored in .git/objects/xx/xxx is corrupt

解决方案

参考链接, 其实我就是删除空的commit就可以了

find .git/objects/ -size 0 -exec rm -f {} \; 
Comments

Elasticsearch 动态加载属性的mapping

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  curl -XPUT 'localhost:9200/products' -d '{
    "settings": {
      "analysis": {
        "analyzer": {
          "pinyin_analyzer": {
            "tokenizer": "my_pinyin",
            "filter": ["standard", "nGram"]
          }
        },
        "tokenizer": {
          "my_pinyin": {
            "type": "pinyin",
            "first_letter": "prefix",
            "padding_char": ""
          }
        }
      }
    },
    "mappings": {
      "product": {
        "dynamic_templates": [
          {
            "property_template": {
              "path_match": "properties.*",
              "mapping": {
                "type": "multi_field",
                "fields": {
                  "{name}": {
                    "type": "string",
                    "analyzer" : "pinyin_analyzer",
                    "boost": 10,
                    "store": "no",
                    "term_vector": "with_positions_offsets"
                  },
                  "{name}_untouched": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
              }
            }
          }
        ]
      }
    }
  }'

  curl -XPOST 'localhost:9200/products/product' -d '{name: "加多宝", properties: {ping: 1} }'
  curl -XPOST 'localhost:9200/products/product' -d '{name: "水杯", properties: {ge: 1} }'

  curl -XGET 'localhost:9200/products/product/_mapping?pretty'
Comments

Elasticsearch脚本排序

由于自己要自定义score, 所以才会使用script来写复杂的sort

javascript script 排序

1
bin/plugin -install elasticsearch/elasticsearch-lang-javascript/1.4.0
  • 写js脚本

在你的配置目录下新建文件scripts/products/sort.js, 我的配置目录是/etc/elasticsearch,简单实例:

1
2
3
(function(){
  doc._score.value + doc.likes.value
})()
  • 查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
curl -XGET 'http://localhost:9200/products/_search?pretty' -d '{
  "query": {
    "custom_score": {
      "script": "global_product",
      "query": {
        "filtered": {
          "filter": {
            "and": [
              {
                "term": {
                  "_type": "product"
                }
              }
            ]
          }
        }
      }
    }
  },
  "sort": [
    {
      "_score": "desc"
    }
  ]
}'

products_sort: products是目录sort脚本文件名

native script 排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
curl -X GET 'http://localhost:9200/products/_search?pretty' -d '{
  "query": {
    "custom_score": {
      "script": "productSort",
      "lang": "native",
      "query": {
        "filtered": {
          "filter": {
            "and": [
              {
                "term": {
                  "_type": "product"
                }
              }
            ]
          }
        }
      }
    }
  },
  "sort": [
    {
      "_score": "desc"
    }
  ]
}'

单机做测试,js与native排序性能方面有差距同样的5W数据,比较一下查询速度: js:70ms, native: 20ms

Comments

Elasticsearch 多个索引查询,根据不同类型条件判定

CURL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
curl -XGET 'http://localhost:9200/indexA,indexB/_search?pretty=1' -d '{
"query" : {
    "bool": {
      "should": [{
        "filtered": {
          "filter":{
            "and": [
              {
                "range":{
                  "start_time":{
                    "lte": "2013-09-24T00:00:00+08:00"
                  }
                }
              },{
                "range": {
                  "end_time": {
                    "gt": "2013-09-24T00:00:00+08:00"
                  }
                }
              },
              {
                "term": {
                  "_type": "TypeA"
                }
              }]
          }
        }
      },{
        "filtered": {
          "filter": {
            "term": {
              "_type": "TypeB"
            }
          }
        }
      }]
    }
  },
  "sort": [
    {
      "_script": {
        "script": "doc[\u0027score\u0027].value/((time()-doc[\u0027start_time_ms\u0027].value) / 3600)",
        "type": "number",
        "order": "desc"
      },
      "_score": {

      }
    }
  ]
}'

indexA 与 indexB 索引的 TypeA与TypeB

ruby gem Tire

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Tire.search ['indexA', 'indexB'] do
  from _from
  size _size

  query do
    boolean do
      should do
        filtered do
          filter :range, :end_time => {gt: toDay}
          filter :range, :start_time => {lte: toDay}
          filter :term, :_type => "TypeA"
          filter :term, :status => 1
          filter :terms, {"category.id" => category_ids} if q[:catalog_id].present?
        end
      end
      should do
        filtered do
          filter :term, {:_type => "TypeB"}
        end
      end

    end
  end

  sort("_script" => {
    :script => "doc['score'].value/((time()-doc['start_time_ms'].value) / 3600)",
    :type   => "number",
    :order  => "desc"
  }, "_score" => {})

end
Comments

Gem Requirejs-rails Assets Precompile 简介

安装requirejs-rails, 然后新建配置文件config/requirejs.yml

requirejs.yml

1
2
3
4
5
6
7
8
9
10
#配置单个模块,会编译一个单独的模块
modules:
  - name: 'mytoplevel'

#路径
paths
  jquery: "lib/jquery.js"

#设置依赖关系
shim:

其实与requirejs配置差不多,只是rails会根据requirejs.yml编译

Comments

Ubuntu Rabbitmq 服务器安装过程

rabbitmq是用于消息列队处理的,准备研究一下它来做消息处理 rabbitmq是用Erlang开发的,所有安装之前要部署Erlang环境

Erlang部署

wget https://elearning.erlang-solutions.com/binaries/sources/otp_src_R16B.tar.gz
tar xvzf otp_src_R16B.tar.gz
cd otp_src_R16B
./configure
make && make install

Rabbitmq部署

官方安装文档

  1. 添加公共key

    wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc

    sudo apt-key add rabbitmq-signing-key-public.asc

  2. 安装

    sudo apt-get install rabbitmq-server

官方配置文档配置文件路径在/etc/rabbitmq/

启动rabbitmq_management管理

cd /usr/lib/rabbitmq/lib/rabbitmq_server-2.7.1/sbin
sudo ./rabbitmq-plugins enable rabbitmq_management

然后浏览地址http://server-name:55672/, 官方说3.0以下版本端口是55672

Comments

postMessage 跨域通讯

不相同的域可以相互通讯, 例如iframe与页面进行通讯, postMessage是个封装不错的包

简单实例解释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  //在iframe 绑定频道
  pm.bind("message1", function(data) {
    $(document.body).append(JSON.stringify(data));
  });

  //发送消息
  pm({
    //给那个iframe
    target: window.frames["example1"],
    //频道
    type:"message1",
    //内容
    data:{hello:"world"}
  });
Copyright © 2014 kaka