前提・実現したいこと
勤怠管理サービスを開発しています。
具体的には、スマホで都度位置情報と業務内容と時刻を示したデータを一定間隔でRails側に送信し、集計するというようなものです。
1 位置情報 接客 14:00 2 位置情報 移動 14:15 3 位置情報 移動 14:30 4 位置情報 移動 14:40 5 位置情報 移動 14:50 6 位置情報 接客 15:00 7 位置情報 移動 16:00 8 位置情報 事務作業 16:45 9 位置情報 完了 17:30
このようなデータが取得できているのですが、ここから、業務内容ごとに集計をしてグラフとして出したいと考えております。
接客 合計:75分 移動 合計:90分 事務作業 合計:45分 総業務時間 3時間30分 (210分)
と、人間の目には普通に計算できるのですが、rails(ruby)で、はどのようにコードとしてかけますでしょうか。
私がイメージしている実装は、こんな感じなのかと思うのですが、何かもっと良い方法がありますか?それと、次の要素はどのように指定できるのでしょうか。
work_datas.each do |work_data| if work_data.type == "接客" 配列に入れておく? 次の要素.current_at - work_data.current_at else . . . end end
モデル
LocationLog 位置および業務内容のモデル # == Schema Information # # Table name: location_logs # # id :integer not null, primary key # user_id :integer not null # work_category_id :integer not null # item_id :integer # latitude :float not null # longitude :float not null # current_at :datetime # created_at :datetime not null # updated_at :datetime not null
# Table name: work_categories # # id :integer not null, primary key # name :string not null # created_at :datetime not null # updated_at :datetime not null # Seedで作成している # WorkCategory.create(id: 100, name:"その他") # WorkCategory.create(id: 1, name:"移動") # WorkCategory.create(id: 2, name:"休憩") # WorkCategory.create(id: 3, name:"接客") # WorkCategory.create(id: 4, name:"待機") # WorkCategory.create(id: 5, name:"完了")
以下は今回作ったモデルです。上記の情報を収集して格納したいと考えているものです。
集計結果を入れる予定のモデル(daily_reports) # Table name: daily_reports # # id :integer not null, primary key # item_count :string # total_distance :string # total_business_time :string # total_moving_time :string # total_breaking_time :string # total_working_time :string # total_waiting_time :string # total_other_time :string # created_at :datetime not null # updated_at :datetime not null
他にもモデルはたくさんありますが、関係するモデルを記しました。また、とりあえず作ったのでstringなど最終的にintegerにすべき部分もありそうですが取り急ぎ。
最終的には、sidekiq& wheneverを使って、翌日にBatch処理して、データを保存できたらと思っています。(そのためのWorkerはすでに出来ています。)
データベースは、postgresqlを利用しています。
補足情報(FW/ツールのバージョンなど)
rails5
jQuery
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/16 14:14
2018/03/16 14:15
2018/03/16 14:16