Skip to content

Commit

Permalink
時刻がしっかり入るようにする
Browse files Browse the repository at this point in the history
  • Loading branch information
voluntas committed Jun 16, 2024
1 parent c1b8923 commit e652b0b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
10 changes: 7 additions & 3 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ SET allow_experimental_object_type = 1;

DROP TABLE IF EXISTS sora_rtc_stats;
CREATE TABLE IF NOT EXISTS sora_rtc_stats (
timestamp DateTime64(3, 'UTC'),
-- マイクロ秒
timestamp DateTime64(6, 'UTC'),

version String,
label String,
Expand All @@ -19,12 +20,15 @@ CREATE TABLE IF NOT EXISTS sora_rtc_stats (
client_id String,
connection_id String,

rtc_stats_timestamp Float64,
-- マイクロ秒
rtc_stats_timestamp DateTime64(6, 'UTC'),
rtc_stats_type String,
rtc_stats_id String,

rtc_stats_data JSON,

created_at DateTime64(3, 'UTC') DEFAULT now()
-- TTL に使うので DateTime にしている
created_at DateTime('UTC') DEFAULT now()
) ENGINE = MergeTree()
PRIMARY KEY (connection_id, timestamp)
TTL created_at + INTERVAL 14 DAY DELETE
19 changes: 13 additions & 6 deletions rtc_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

"github.com/labstack/echo/v4"
zlog "github.com/rs/zerolog/log"
)

type RTCStats struct {
Expand Down Expand Up @@ -45,10 +46,16 @@ func (s *Server) rtcStats(c echo.Context, stats soraConnectionStats) error {
}
// TODO: ここで timestamp と id と type が空の場合はエラーにする

// zlog.Debug().
// Str("channel_id", stats.ChannelID).
// Str("connection_id", stats.ConnectionID).
// Str("type", rtcStats.Type).Send()
zlog.Debug().
Time("timestamp", stats.Timestamp).
Str("channel_id", stats.ChannelID).
Str("connection_id", stats.ConnectionID).
Float64("rtc_stats_timestamp", rtcStats.Timestamp).
Str("rtc_stats_type", rtcStats.Type).Send()

// マイクロ秒まで入れたいので CLickHouse DateTime64 にあわせて {秒}.{マイクロ秒} 形式に変換する
timestamp := float64(stats.Timestamp.UTC().UnixMicro()) / 1e6
rtcStatsTimestamp := (rtcStats.Timestamp / 1000)

// INSERT する
if err := s.conn.Exec(c.Request().Context(), `
Expand All @@ -69,12 +76,12 @@ func (s *Server) rtcStats(c echo.Context, stats soraConnectionStats) error {
?, ?, ?,
?
)`,
stats.Timestamp,
timestamp,
stats.Version, stats.Label, stats.NodeName,
stats.Multistream, stats.Simulcast, stats.Spotlight,
stats.Role,
stats.ChannelID, stats.SessionID, stats.ClientID, stats.ConnectionID,
rtcStats.Timestamp, rtcStats.Type, rtcStats.ID,
rtcStatsTimestamp, rtcStats.Type, rtcStats.ID,
// stats は json.RawMessage なので string() で囲むだけ
string(v),
); err != nil {
Expand Down

0 comments on commit e652b0b

Please sign in to comment.