Code Monkey home page Code Monkey logo

Comments (1)

ochaochaocha3 avatar ochaochaocha3 commented on September 23, 2024

現在実装されている1日分のページも含め、「前」や「次」が存在することがどういうことか決めておく必要がある。また、隣がメッセージの存在しない年・月・日だったときにそれを表示するかしないかの2つの方法が考えられる。

MessageDateモデル

ある日のログが存在するかは、現在の実装ではMessageDateのレコードが存在するかと等しい。MessageDateの1件分の内容はチャンネルと日付の組合せで、例えば「チャンネル #cre の2017-04-18のログが存在する」ことを表す。

create_table "message_dates", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC" do |t|
t.integer "channel_id", limit: 4
t.date "date"
t.datetime "created_at"
t.datetime "updated_at"
end

MessageDateの例

id channel_id date
1 13 2017-04-18
2 1 2017-04-18
3 13 2017-04-19
... ... ...

(この例では channel_id == 1 → #cre、channel_id == 13 → #もの書き)

MessageDateのレコードの追加

MessageDateは、IRCボットを使ったメッセージの記録の際に、MessageDate.find_or_create_by! によって1日に1回だけ追加される。

前後の年が存在するか

年月(yyyy-mm)の選択画面。Channels::MonthsController#index が担当する。

def index
@channel = Channel.find_by(identifier: params[:identifier])
@year = params[:year].to_i
start_date = Date.new(@year, 1, 1)
@month_count = MessageDate.
uniq.
where(channel: @channel,
date: start_date...(start_date.next_year)).
group('MONTH(date)').
order(:date).
count
end

チャンネルcy年の前の年が存在するとは、チャンネルcかつy年1月1日より前のMessageDateが存在すること。調べるには以下のようにする。

MessageDate.
  where(channel: c).
  where('date < ?', Time.zone.local(y, 1, 1)).
  exists?

同様に、チャンネルcy年の後の年が存在するとは、チャンネルcかつyの翌年1月1日以降のMessageDateが存在すること。調べるには以下のようにする。

MessageDate.
  where(channel: c).
  where('date >= ?', Time.zone.local(y, 1, 1).next_year).
  exists?

前後の月が存在するか

年月日(yyyy-mm-dd)の選択画面。Channels::DaysController#index が担当する。

def index
@channel = Channel.find_by(identifier: params[:identifier])
@year = params[:year].to_i
@month = params[:month].to_i
start_date = Date.new(@year, @month, 1)
@day_count = Message.
uniq.
where(channel: @channel,
type: %w(Privmsg Notice),
timestamp: start_date...(start_date.next_month)).
group('DATE(timestamp)').
order(:timestamp).
count
end

チャンネルcym月の前の月が存在するとは、チャンネルcかつym月1日より前のMessageDateが存在すること。調べるには以下のようにする。

MessageDate.
  where(channel: c).
  where('date < ?', Time.zone.local(y, m, 1)).
  exists?

同様に、チャンネルcym月の後の月が存在するとは、チャンネルcかつym月1日の翌月以降のMessageDateが存在すること。調べるには以下のようにする。

MessageDate.
  where(channel: c).
  where('date >= ?', Time.zone.local(y, m, 1).next_month).
  exists?

前後の日が存在するか

1日分のログの画面。Channels#DaysController#show が担当する。

def show
@channel = Channel.find_by(identifier: params[:identifier])
@year = params[:year].to_i
@month = params[:month].to_i
@day = params[:day].to_i
@date = Date.new(@year, @month, @day)
@messages = Message.
where(channel: @channel,
timestamp: @date...(@date.next_day)).
order(:timestamp)
end

チャンネルcに日付dateの前の日が存在するとは、チャンネルcかつ日付dateより前のMessageDateが存在すること。調べるには以下のようにする。

MessageDate.
  where(channel: c).
  where('date < ?', date).
  exists?

同様に、チャンネルcに日付dateの後の日が存在するとは、チャンネルcかつ日付dateより後のMessageDateが存在すること。調べるには以下のようにする。

MessageDate.
  where(channel: c).
  where('date > ?', date.
  exists?

from log-archiver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.