Skip to content

Commit

Permalink
[#45] Add sync function for synchronizing all repositories and issues
Browse files Browse the repository at this point in the history
Resolves #45

Fix after review

[#45] Add syncWithGithub function

Resolves #45

Fix after review

[#45] Add syncCache function

Resolves #45

Fix after review
  • Loading branch information
rashadg1030 committed Aug 17, 2019
1 parent f0363e0 commit f780d67
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/IW/Sync/Update.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,35 @@ data and insert it into the database.
-}

module IW.Sync.Update
( syncRepos
( syncCache
) where

import Control.Monad.IO.Unlift (MonadUnliftIO)
import UnliftIO.Async (mapConcurrently_)
import UnliftIO.Async (async, mapConcurrently_, wait)

import IW.App (WithError)
import IW.App (WithError, catchError)
import IW.Core.Repo (Repo (..))
import IW.Db (WithDb, upsertRepos, updateRepoCategories)
import IW.Db (WithDb, upsertRepos, updateRepoCategories, upsertIssues)
import IW.Effects.Cabal (MonadCabal (..), getCabalCategories)
import IW.Sync.Search (searchAllHaskellRepos)
import IW.Sync.Search (searchAllHaskellRepos, searchAllHaskellIssues)
import IW.Time (getToday)


-- | This function fetches all repos from the GitHub API, downloads their @.cabal@ files,
-- and upserts them into the database.
syncRepos
-- | Loop for keeping the database up to date with the latest GitHub data.
syncCache
:: forall env m.
( MonadCabal m
, MonadUnliftIO m
, WithDb env m
, WithLog env m
, WithError m
)
=> Integer
-> m ()
syncCache interval = forever $ syncWithGithub interval `catchError` undefined

-- | This function synchronizes the database with the latest GitHub data.
syncWithGithub
:: forall env m.
( MonadCabal m
, MonadUnliftIO m
Expand All @@ -30,13 +42,22 @@ syncRepos
)
=> Integer -- ^ The starting date interval used in the search function
-> m ()
syncRepos interval = do
syncWithGithub interval = do
log I "Starting synchronization of all repositories and issues..."
today <- liftIO getToday
log I "Searching for all Haskell repositories..."
repos <- searchAllHaskellRepos today interval
log I "Upserting repositories into the database..."
upsertRepos repos
mapConcurrently_ syncCategories repos
populateCategoriesAsync <- async $ mapConcurrently_ syncCategories repos
log I "Searching for all Haskell issues..."
issues <- searchAllHaskellIssues today interval
log I "Upserting issues into the database..."
upsertIssues issues
wait populateCategoriesAsync
log I "All repositories and issues successfully synchronized"

-- | This function takes a @Repo@ and attempts to download its @.cabal@ file.
-- | This function takes a @Repo@ and updates its category field in the database.
syncCategories
:: forall env m.
( MonadCabal m
Expand All @@ -48,5 +69,7 @@ syncCategories
=> Repo
-> m ()
syncCategories Repo{..} = do
log I "Updating repository's categories..."
categories <- getCabalCategories repoOwner repoName
updateRepoCategories repoOwner repoName categories
log I "Categories successfully updated"

0 comments on commit f780d67

Please sign in to comment.