Browse Source

Refactor snapshot diffs

- do not leak the HashMap out
- use existing types (Version, PackageName)
jsonDiff
Konstantin Zudov 6 years ago
parent
commit
1e1e875bd0
  1. 21
      Handler/Feed.hs
  2. 1
      Handler/StackageHome.hs
  3. 18
      Stackage/Snapshot/Diff.hs
  4. 8
      templates/stackage-diff.hamlet

21
Handler/Feed.hs

@ -5,9 +5,8 @@ module Handler.Feed
import Import
import Stackage.Database
import Data.These
import Data.These
import Stackage.Snapshot.Diff
import qualified Data.HashMap.Strict as HashMap
import Text.Blaze (text)
getFeedR :: Handler TypedContent
@ -67,17 +66,17 @@ getContent sid2 snap = do
<th align=right>Old
<th align=left>New
<tbody>
$forall (name, VersionChange verChange) <- sortOn (toCaseFold . fst) $ HashMap.toList snapDiff
$forall (PackageName name, VersionChange change) <- toDiffList snapDiff
<tr>
<th align=right>#{name}
$case verChange
$of This oldVersion
<td align=right>#{oldVersion}
$case change
$of This (Version old)
<td align=right>#{old}
<td>
$of That newVersion
$of That (Version new)
<td align=right>
<td>#{newVersion}
$of These oldVersion newVersion
<td align=right>#{oldVersion}
<td>#{newVersion}
<td>#{new}
$of These (Version old) (Version new)
<td align=right>#{old}
<td>#{new}
|]

1
Handler/StackageHome.hs

@ -7,7 +7,6 @@ module Handler.StackageHome
) where
import Import
import qualified Data.HashMap.Strict as HashMap
import Data.These
import Data.Time (FormatTime)
import Stackage.Database

18
Stackage/Snapshot/Diff.hs

@ -2,7 +2,8 @@
module Stackage.Snapshot.Diff
( getSnapshotDiff
, snapshotDiff
, SnapshotDiff
, SnapshotDiff()
, toDiffList
, VersionChange(..)
) where
@ -13,10 +14,13 @@ import ClassyPrelude
import Data.These
import Stackage.Database (SnapshotId, PackageListingInfo(..),
GetStackageDatabase, getPackages)
type PackageName = Text
type Version = Text
import Types
type SnapshotDiff = HashMap PackageName VersionChange
newtype SnapshotDiff
= SnapshotDiff { unSnapshotDiff :: HashMap PackageName VersionChange }
toDiffList :: SnapshotDiff -> [(PackageName, VersionChange)]
toDiffList = sortOn (toCaseFold . unPackageName . fst) . HashMap.toList . unSnapshotDiff
-- | Versions of a package as it occurs in the listings provided to `snapshotDiff`.
--
@ -32,6 +36,8 @@ getSnapshotDiff :: GetStackageDatabase m => SnapshotId -> SnapshotId -> m Snapsh
getSnapshotDiff a b = snapshotDiff <$> getPackages a <*> getPackages b
snapshotDiff :: [PackageListingInfo] -> [PackageListingInfo] -> SnapshotDiff
snapshotDiff as bs = HashMap.filter changed $ alignWith VersionChange (toMap as) (toMap bs)
snapshotDiff as bs =
SnapshotDiff $ HashMap.filter changed
$ alignWith VersionChange (toMap as) (toMap bs)
where
toMap = HashMap.fromList . map (pliName &&& pliVersion)
toMap = HashMap.fromList . map (PackageName . pliName &&& Version . pliVersion)

8
templates/stackage-diff.hamlet

@ -33,15 +33,15 @@
$else
<option value=@{StackageDiffR name1 name2'}>#{toPathPiece name2'}
<tbody>
$forall (name, VersionChange verChange) <- sortOn (toCaseFold . fst) $ HashMap.toList snapDiff
$forall (PackageName name, VersionChange verChange) <- toDiffList snapDiff
<tr>
$case verChange
$of This oldVersion
$of This (Version oldVersion)
<td>#{name}-#{oldVersion}
<td>
$of That newVersion
$of That (Version newVersion)
<td>
<td>#{name}-#{newVersion}
$of These oldVersion newVersion
$of These (Version oldVersion) (Version newVersion)
<td>#{name}-#{oldVersion}
<td>#{name}-#{newVersion}

Loading…
Cancel
Save