LoginSignup
5
4

More than 5 years have passed since last update.

Milkcocoa新機能のHistory APIで期間を指定してデータ取得

Last updated at Posted at 2015-11-15

MilkcocoaのJavaScript SDKにHistory機能が追加されました。

JavaScript SDKの導入について

ブラウザの場合は以下をHTMLファイルに貼り付けてください。

<script src="http://cdn.mlkcca.com/v0.6.0/milkcocoa.js"></script>

npmのmilkcocoaモジュールではv0.3.x以降で使用可能です。

npm install milkcocoa

使い方

対象のデータストアはmessageデータストアだとします。

個数を指定して取得する場合

limitを使います。

var history = milkcocoa.dataStore('message').history();
history.sort('desc');
history.size(10);
history.limit(2000);
// 上記のように書くと'data'イベントが200回発行されます。
history.on('data', function(data) {
    // 200回呼ばれる
    console.log(data);
});
history.run();

期間を指定して取得する場合

spanを使い、タイムスタンプを二つ指定します。二つのタイムスタンプの範囲内のデータが取得できます。タイムスタンプはデータのpush時に自動で付与されます。

var history = milkcocoa.dataStore('message').history();
history.sort('desc');
history.size(10);
// 2015/10/30-2015/11/3のpush()で保存したデータを取得
history.span(new Date(2015,9,30).getTime(), new Date(2015,10,3).getTime());
history.on('data', function(data) {
    console.log(data);
});
history.run();

まとめ

History APIは過去データを取得したい場合に便利です。データの集計、解析やチャットの過去ログなどにご利用いただけると思います。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4