feat(cli): Enriched wasp telemetry CLI command to show more telemetry info.

This commit is contained in:
Sameer Kolhar 2021-02-26 16:26:33 +05:30 committed by Martin Šošić
parent dfaca5b58c
commit 6844313144
3 changed files with 39 additions and 14 deletions

View File

@ -3,10 +3,11 @@ module Command.Telemetry
, telemetry
) where
import Control.Monad (when)
import Control.Monad (when, unless)
import Control.Monad.Except (catchError, throwError)
import Control.Monad.IO.Class (liftIO)
import Data.Maybe (isJust)
import Data.Foldable (for_)
import qualified System.Environment as ENV
import Command (Command, CommandError (..))
@ -15,16 +16,32 @@ import qualified Command.Call
import Command.Telemetry.Common (ensureTelemetryCacheDirExists)
import qualified Command.Telemetry.Project as TlmProject
import qualified Command.Telemetry.User as TlmUser
import qualified StrongPath as SP
isTelemetryDisabled :: IO Bool
isTelemetryDisabled = isJust <$> ENV.lookupEnv "WASP_TELEMETRY_DISABLE"
-- | Prints basic information about the stauts of telemetry.
telemetry :: Command ()
telemetry = do
telemetryDisabled <- liftIO isTelemetryDisabled
waspSaysC $ "Telemetry is currently: " <> (if telemetryDisabled
then "DISABLED"
else "ENABLED")
else "ENABLED")
unless telemetryDisabled $ do
telemetryCacheDirPath <- liftIO ensureTelemetryCacheDirExists
waspSaysC $ "Telemetry cache directory: " ++ SP.toFilePath telemetryCacheDirPath
maybeProjectHash <- (Just <$> TlmProject.getWaspProjectPathHash) `catchError` const (return Nothing)
for_ maybeProjectHash $ \projectHash -> do
maybeProjectCache <- liftIO $ TlmProject.readProjectTelemetryFile telemetryCacheDirPath projectHash
for_ maybeProjectCache $ \projectCache -> do
let maybeTimeOfLastSending = TlmProject.getTimeOfLastTelemetryDataSent projectCache
for_ maybeTimeOfLastSending $ \timeOfLastSending -> do
waspSaysC $ "Last time telemetry data was sent for this project: " ++ show timeOfLastSending
waspSaysC "Our telemetry is anonymized and very limited in its scope: check https://wasp-lang.dev/docs/telemetry for more details."
-- | Sends telemetry data about the current Wasp project, if conditions are met.
-- If we are not in the Wasp project at the moment, nothing happens.
@ -40,6 +57,5 @@ considerSendingData cmdCall = (`catchError` const (return ())) $ do
userSignature <- liftIO $ TlmUser.readOrCreateUserSignatureFile telemetryCacheDirPath
maybeProjectHash <- (Just <$> TlmProject.getWaspProjectPathHash) `catchError` const (return Nothing)
case maybeProjectHash of
Nothing -> return ()
Just projectHash -> liftIO $ TlmProject.considerSendingData telemetryCacheDirPath userSignature projectHash cmdCall
for_ maybeProjectHash $ \projectHash -> do
liftIO $ TlmProject.considerSendingData telemetryCacheDirPath userSignature projectHash cmdCall

View File

@ -1,6 +1,7 @@
module Command.Telemetry.Common
( TelemetryCacheDir
, ensureTelemetryCacheDirExists
, getTelemetryCacheDirPath
) where
import Path (reldir)

View File

@ -3,6 +3,8 @@
module Command.Telemetry.Project
( getWaspProjectPathHash
, considerSendingData
, readProjectTelemetryFile
, getTimeOfLastTelemetryDataSent
) where
import Command.Common (findWaspProjectRootDirFromCwd)
@ -89,21 +91,29 @@ initialCache = ProjectTelemetryCache { _lastCheckIn = Nothing, _lastCheckInBuild
-- * Project telemetry cache file.
readOrCreateProjectTelemetryFile :: Path Abs (Dir TelemetryCacheDir) -> ProjectHash -> IO ProjectTelemetryCache
readOrCreateProjectTelemetryFile telemetryCacheDirPath projectHash = do
getTimeOfLastTelemetryDataSent :: ProjectTelemetryCache -> Maybe T.UTCTime
getTimeOfLastTelemetryDataSent cache = maximum [_lastCheckIn cache, _lastCheckInBuild cache]
readProjectTelemetryFile :: Path Abs (Dir TelemetryCacheDir) -> ProjectHash -> IO (Maybe ProjectTelemetryCache)
readProjectTelemetryFile telemetryCacheDirPath projectHash = do
fileExists <- SD.doesFileExist filePathFP
maybeCache <- if fileExists then readCacheFile else return Nothing
case maybeCache of
Just cache -> return cache
Nothing -> writeProjectTelemetryFile telemetryCacheDirPath projectHash initialCache >> return initialCache
if fileExists then readCacheFile else return Nothing
where
filePathFP = SP.toFilePath $ getProjectTelemetryFilePath telemetryCacheDirPath projectHash
readCacheFile = Aeson.decode . ByteStringLazyUTF8.fromString <$> readFile filePathFP
readOrCreateProjectTelemetryFile :: Path Abs (Dir TelemetryCacheDir) -> ProjectHash -> IO ProjectTelemetryCache
readOrCreateProjectTelemetryFile telemetryCacheDirPath projectHash = do
maybeProjectTelemetryCache <- readProjectTelemetryFile telemetryCacheDirPath projectHash
case maybeProjectTelemetryCache of
Just cache -> return cache
Nothing -> writeProjectTelemetryFile telemetryCacheDirPath projectHash initialCache >> return initialCache
writeProjectTelemetryFile :: Path Abs (Dir TelemetryCacheDir) -> ProjectHash -> ProjectTelemetryCache -> IO ()
writeProjectTelemetryFile telemetryCacheDirPath projectHash cache = do
let filePathFP = SP.toFilePath $ getProjectTelemetryFilePath telemetryCacheDirPath projectHash
writeFile filePathFP (ByteStringLazyUTF8.toString $ Aeson.encode cache)
where
filePathFP = SP.toFilePath $ getProjectTelemetryFilePath telemetryCacheDirPath projectHash
getProjectTelemetryFilePath :: Path Abs (Dir TelemetryCacheDir) -> ProjectHash -> Path Abs File
getProjectTelemetryFilePath telemetryCacheDir (ProjectHash projectHash) =
@ -149,5 +159,3 @@ sendTelemetryData telemetryData = do
request = HTTP.setRequestBodyJSON reqBodyJson $
HTTP.parseRequest_ "POST https://app.posthog.com/capture"
void $ HTTP.httpNoBody request