{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -fno-warn-unrecognised-pragmas #-}

module Simplex.FileTransfer.Server.Prometheus
  ( FileServerMetrics (..),
    rtsOptionsEnv,
    xftpPrometheusMetrics,
  ) where

import Data.Int (Int64)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time.Clock (UTCTime (..), diffUTCTime)
import Data.Time.Clock.System (systemEpochDay)
import Data.Time.Format.ISO8601 (iso8601Show)
import Simplex.FileTransfer.Server.Stats
import Simplex.Messaging.Server.Stats (PeriodStatCounts (..))
import Simplex.Messaging.Transport (simplexMQVersion)
import Simplex.Messaging.Util (tshow)

data FileServerMetrics = FileServerMetrics
  { FileServerMetrics -> FileServerStatsData
statsData :: FileServerStatsData,
    FileServerMetrics -> PeriodStatCounts
filesDownloadedPeriods :: PeriodStatCounts,
    FileServerMetrics -> Text
rtsOptions :: Text
  }

rtsOptionsEnv :: Text
rtsOptionsEnv :: Text
rtsOptionsEnv = Text
"XFTP_RTS_OPTIONS"

{-# FOURMOLU_DISABLE\n#-}
xftpPrometheusMetrics :: FileServerMetrics -> UTCTime -> Text
xftpPrometheusMetrics :: FileServerMetrics -> UTCTime -> Text
xftpPrometheusMetrics FileServerMetrics
sm UTCTime
ts =
  Text
time Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
files Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
info
  where
    FileServerMetrics {FileServerStatsData
$sel:statsData:FileServerMetrics :: FileServerMetrics -> FileServerStatsData
statsData :: FileServerStatsData
statsData, PeriodStatCounts
$sel:filesDownloadedPeriods:FileServerMetrics :: FileServerMetrics -> PeriodStatCounts
filesDownloadedPeriods :: PeriodStatCounts
filesDownloadedPeriods, Text
$sel:rtsOptions:FileServerMetrics :: FileServerMetrics -> Text
rtsOptions :: Text
rtsOptions} = FileServerMetrics
sm
    FileServerStatsData
      { UTCTime
_fromTime :: UTCTime
$sel:_fromTime:FileServerStatsData :: FileServerStatsData -> UTCTime
_fromTime,
        Int
_filesCreated :: Int
$sel:_filesCreated:FileServerStatsData :: FileServerStatsData -> Int
_filesCreated,
        Int
_fileRecipients :: Int
$sel:_fileRecipients:FileServerStatsData :: FileServerStatsData -> Int
_fileRecipients,
        Int
_filesUploaded :: Int
$sel:_filesUploaded:FileServerStatsData :: FileServerStatsData -> Int
_filesUploaded,
        Int
_filesExpired :: Int
$sel:_filesExpired:FileServerStatsData :: FileServerStatsData -> Int
_filesExpired,
        Int
_filesDeleted :: Int
$sel:_filesDeleted:FileServerStatsData :: FileServerStatsData -> Int
_filesDeleted,
        Int
_filesBlocked :: Int
$sel:_filesBlocked:FileServerStatsData :: FileServerStatsData -> Int
_filesBlocked,
        Int
_fileDownloads :: Int
$sel:_fileDownloads:FileServerStatsData :: FileServerStatsData -> Int
_fileDownloads,
        Int
_fileDownloadAcks :: Int
$sel:_fileDownloadAcks:FileServerStatsData :: FileServerStatsData -> Int
_fileDownloadAcks,
        Int
_filesCount :: Int
$sel:_filesCount:FileServerStatsData :: FileServerStatsData -> Int
_filesCount,
        Int64
_filesSize :: Int64
$sel:_filesSize:FileServerStatsData :: FileServerStatsData -> Int64
_filesSize
      } = FileServerStatsData
statsData
    time :: Text
time =
      Text
"# Recorded at: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (UTCTime -> String
forall t. ISO8601 t => t -> String
iso8601Show UTCTime
ts) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \# Stats from: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (UTCTime -> String
forall t. ISO8601 t => t -> String
iso8601Show UTCTime
_fromTime) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \\n"
    files :: Text
files =
      Text
"# Files\n\
      \# -----\n\
      \\n\
      \# HELP simplex_xftp_files_created Created files\n\
      \# TYPE simplex_xftp_files_created counter\n\
      \simplex_xftp_files_created " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
mshow Int
_filesCreated Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \# filesCreated\n\
      \\n\
      \# HELP simplex_xftp_files_recipients Files recipients\n\
      \# TYPE simplex_xftp_files_recipients counter\n\
      \simplex_xftp_files_recipients " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
mshow Int
_fileRecipients Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \# fileRecipients\n\
      \\n\
      \# HELP simplex_xftp_files_uploaded Uploaded files\n\
      \# TYPE simplex_xftp_files_uploaded counter\n\
      \simplex_xftp_files_uploaded " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
mshow Int
_filesUploaded Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \# filesUploaded\n\
      \\n\
      \# HELP simplex_xftp_files_expired Expired files\n\
      \# TYPE simplex_xftp_files_expired counter\n\
      \simplex_xftp_files_expired " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
mshow Int
_filesExpired Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \# filesExpired\n\
      \\n\
      \# HELP simplex_xftp_files_deleted Deleted files\n\
      \# TYPE simplex_xftp_files_deleted counter\n\
      \simplex_xftp_files_deleted " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
mshow Int
_filesDeleted Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \# filesDeleted\n\
      \\n\
      \# HELP simplex_xftp_files_blocked Blocked files\n\
      \# TYPE simplex_xftp_files_blocked counter\n\
      \simplex_xftp_files_blocked " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
mshow Int
_filesBlocked Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \# filesBlocked\n\
      \\n\
      \# HELP simplex_xftp_file_downloads File downloads\n\
      \# TYPE simplex_xftp_file_downloads counter\n\
      \simplex_xftp_file_downloads " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
mshow Int
_fileDownloads Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \# fileDownloads\n\
      \\n\
      \# HELP simplex_xftp_file_download_acks File download ACKs\n\
      \# TYPE simplex_xftp_file_download_acks counter\n\
      \simplex_xftp_file_download_acks " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
mshow Int
_fileDownloadAcks Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \# fileDownloadAcks\n\
      \\n\
      \# HELP simplex_xftp_files_count_total Total files count \n\
      \# TYPE simplex_xftp_files_count_total gauge\n\
      \simplex_xftp_files_count_total " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
mshow Int
_filesCount Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \# filesCount\n\
      \\n\
      \# HELP simplex_xftp_files_size Size of files \n\
      \# TYPE simplex_xftp_files_size gauge\n\
      \simplex_xftp_files_size " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int64 -> Text
forall a. Show a => a -> Text
mshow Int64
_filesSize Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \# filesSize \n\
      \\n\
      \# HELP simplex_xftp_files_count_daily Daily files count\n\
      \# TYPE simplex_xftp_files_count_daily gauge\n\
      \simplex_xftp_files_count_daily " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
mstr (PeriodStatCounts -> Text
dayCount PeriodStatCounts
filesDownloadedPeriods) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \# filesDownloaded.dayCount\n\
      \\n\
      \# HELP simplex_xftp_files_count_weekly Weekly files count\n\
      \# TYPE simplex_xftp_files_count_weekly gauge\n\
      \simplex_xftp_files_count_weekly " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
mstr (PeriodStatCounts -> Text
weekCount PeriodStatCounts
filesDownloadedPeriods) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \# filesDownloaded.weekCount\n\
      \\n\
      \# HELP simplex_xftp_files_count_monthly Monthly files count\n\
      \# TYPE simplex_xftp_files_count_monthly gauge\n\
      \simplex_xftp_files_count_monthly " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
mstr (PeriodStatCounts -> Text
monthCount PeriodStatCounts
filesDownloadedPeriods) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\
      \# filesDownloaded.monthCount\n\
      \\n"
    info :: Text
info =
      Text
"# Info\n\
      \# ----\n\
      \\n\
      \# HELP simplex_xftp_info Server information. RTS options have to be passed via " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
rtsOptionsEnv Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" env var\n\
      \# TYPE simplex_xftp_info gauge\n\
      \simplex_xftp_info{version=\"" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack String
simplexMQVersion Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\",rts_options=\"" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
rtsOptions Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\"} 1\n\
      \\n"
    mstr :: Text -> Text
mstr Text
a = Text
a Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
tsEpoch
    mshow :: Show a => a -> Text
    mshow :: forall a. Show a => a -> Text
mshow = Text -> Text
mstr (Text -> Text) -> (a -> Text) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Text
forall a. Show a => a -> Text
tshow
    tsEpoch :: Text
tsEpoch = forall a. Show a => a -> Text
tshow @Int64 (Int64 -> Text) -> Int64 -> Text
forall a b. (a -> b) -> a -> b
$ forall a b. (RealFrac a, Integral b) => a -> b
floor @Double (Double -> Int64) -> Double -> Int64
forall a b. (a -> b) -> a -> b
$ NominalDiffTime -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac (UTCTime
ts UTCTime -> UTCTime -> NominalDiffTime
`diffUTCTime` UTCTime
epoch) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1000
    epoch :: UTCTime
epoch = Day -> DiffTime -> UTCTime
UTCTime Day
systemEpochDay DiffTime
0
{-# FOURMOLU_ENABLE\n#-}