<commit_before>{-# LANGUAGE RecordWildCards #-}

-- |
-- Module: BDCS.Builds
-- Copyright: (c) 2016-2017 Red Hat, Inc.
-- License: LGPL
--
-- Maintainer: https://github.com/weldr
-- Stability: alpha
-- Portability: portable
--
-- Manage 'Builds' records in the database.  This record keeps track of a single
-- build of a single software source (tarball, etc.).  A single source can be
-- built multiple times, each of which will require a separate 'Builds' record.

module BDCS.Builds(associateBuildWithPackage,
                   findBuild,
                   getBuild,
                   insertBuild,
                   insertBuildKeyValue)
 where

import           Control.Monad.IO.Class(MonadIO)
import qualified Data.Text as T
import           Database.Esqueleto

import BDCS.DB
import BDCS.KeyType
import BDCS.KeyValue(findKeyValue, insertKeyValue)

{-# ANN findBuild ("HLint: ignore Use ." :: String) #-}

-- | Find a single build of a software package in the database, returning the database key
-- for that build if it exists.  All arguments are required and must be matched for this
-- function to return anything.  Note that conceptually, a build is of some software source
-- which is why a key to a 'Sources' record is required.
findBuild :: MonadIO m =>
             Int                                    -- ^ Epoch (usually, 0)
          -> T.Text                                 -- ^ Release
          -> T.Text                                 -- ^ Hardware architecture
          -> Key Sources                            -- ^ Reference to a 'Sources' record
          -> SqlPersistT m (Maybe (Key Builds))
findBuild epoch release arch sourceId = firstKeyResult $
    -- FIXME: Is (source_id, epoch, release, arch) unique in Builds?
    select $ from $ \build -> do
    where_ $ build ^. BuildsSource_id ==. val sourceId &&.
             build ^. BuildsEpoch ==. val epoch &&.
             build ^. BuildsRelease ==. val release &&.
             build ^. BuildsArch ==. val arch
    limit 1
    return $ build ^. BuildsId

-- | Given a key to a 'Builds' record in the database, return that record.  This function is
-- suitable for using on the result of 'findBuild'.
getBuild :: MonadIO m => Key Builds -> SqlPersistT m (Maybe Builds)
getBuild key = firstEntityResult $
    select $ from $ \build -> do
    where_ $ build ^. BuildsId ==. val key
    limit 1
    return build<commit_msg>Add a new findBuilds function.<commit_after>{-# LANGUAGE RecordWildCards #-}

-- |
-- Module: BDCS.Builds
-- Copyright: (c) 2016-2017 Red Hat, Inc.
-- License: LGPL
--
-- Maintainer: https://github.com/weldr
-- Stability: alpha
-- Portability: portable
--
-- Manage 'Builds' records in the database.  This record keeps track of a single
-- build of a single software source (tarball, etc.).  A single source can be
-- built multiple times, each of which will require a separate 'Builds' record.

module BDCS.Builds(associateBuildWithPackage,
                   findBuild,
                   findBuilds,
                   getBuild,
                   insertBuild,
                   insertBuildKeyValue)
 where

import           Control.Monad.IO.Class(MonadIO)
import qualified Data.Text as T
import           Database.Esqueleto

import BDCS.DB
import BDCS.KeyType
import BDCS.KeyValue(findKeyValue, insertKeyValue)

{-# ANN findBuild ("HLint: ignore Use ." :: String) #-}

-- | Find a single build of a software package in the database, returning the database key
-- for that build if it exists.  All arguments are required and must be matched for this
-- function to return anything.  Note that conceptually, a build is of some software source
-- which is why a key to a 'Sources' record is required.
findBuild :: MonadIO m =>
             Int                                    -- ^ Epoch (usually, 0)
          -> T.Text                                 -- ^ Release
          -> T.Text                                 -- ^ Hardware architecture
          -> Key Sources                            -- ^ Reference to a 'Sources' record
          -> SqlPersistT m (Maybe (Key Builds))
findBuild epoch release arch sourceId = firstKeyResult $
    -- FIXME: Is (source_id, epoch, release, arch) unique in Builds?
    select $ from $ \build -> do
    where_ $ build ^. BuildsSource_id ==. val sourceId &&.
             build ^. BuildsEpoch ==. val epoch &&.
             build ^. BuildsRelease ==. val release &&.
             build ^. BuildsArch ==. val arch
    limit 1
    return $ build ^. BuildsId

-- | Given the key to a 'Sources' record, find all builds for that source in the database,
-- across all versions, arches, etc.  The key for each result is returned.
findBuilds :: MonadIO m => Key Sources -> SqlPersistT m [Key Builds]
findBuilds sourceId = do
    vals <- select $ from $ \build -> do
            where_ $ build ^. BuildsSource_id ==. val sourceId
            return $ build ^. BuildsId
    return $ map unValue vals

-- | Given a key to a 'Builds' record in the database, return that record.  This function is
-- suitable for using on the result of 'findBuild'.
getBuild :: MonadIO m => Key Builds -> SqlPersistT m (Maybe Builds)
getBuild key = firstEntityResult $
    select $ from $ \build -> do
    where_ $ build ^. BuildsId ==. val key
    limit 1
    return build
{'fraction_non_alphanumeric': 0.09463543634302984, 'fraction_numerical': 0.00415564790328674, 'mean_word_length': 2.0660104226983207, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 24, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1024939', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1431, 'n_words': 573}