1
- {-# LANGUAGE BangPatterns #-}
2
-
3
- -- TODO change the module name probably Distribution.Server.Features.PackageList.PackageRank
4
-
1
+ {-# LANGUAGE BangPatterns, ScopedTypeVariables #-}
5
2
module Distribution.Server.Features.PackageList.PackageRank
6
3
( rankPackage
7
4
) where
8
5
9
- import Distribution.Server.Features.PackageList.MStats
10
-
11
- import Data.TarIndex ( TarEntryOffset )
12
6
import Distribution.Package
13
7
import Distribution.PackageDescription
14
8
import Distribution.Server.Features.Documentation
15
9
( DocumentationFeature (.. ) )
10
+ import Distribution.Server.Features.PackageList.MStats
16
11
import Distribution.Server.Features.PreferredVersions
17
12
import Distribution.Server.Features.PreferredVersions.State
18
13
import Distribution.Server.Features.TarIndexCache
19
14
import qualified Distribution.Server.Framework.BlobStorage
20
15
as BlobStorage
21
- import Distribution.Server.Framework.CacheControl
22
16
import Distribution.Server.Framework.ServerEnv
23
17
( ServerEnv (.. ) )
24
18
import Distribution.Server.Packages.Types
@@ -33,6 +27,9 @@ import Distribution.Types.Version
33
27
import qualified Distribution.Utils.ShortText as S
34
28
35
29
import qualified Codec.Archive.Tar as Tar
30
+ import Control.Exception ( SomeException (.. )
31
+ , handle
32
+ )
36
33
import qualified Data.ByteString.Lazy as BSL
37
34
import Data.List ( maximumBy
38
35
, sortBy
@@ -44,6 +41,9 @@ import Distribution.Server.Packages.Readme
44
41
import GHC.Float ( int2Float )
45
42
import System.FilePath ( isExtensionOf )
46
43
44
+ handleConst :: a -> IO a -> IO a
45
+ handleConst c = handle (\ (_ :: SomeException ) -> return c)
46
+
47
47
data Scorer = Scorer
48
48
{ maximumS :: ! Float
49
49
, score :: ! Float
@@ -117,17 +117,16 @@ cabalScore p docum =
117
117
sourceRp = boolScor 8 (not $ null $ sourceRepos p)
118
118
cats = boolScor 5 (not $ S. null $ category p)
119
119
120
- readmeScore
121
- :: Maybe (FilePath , ETag , Data.TarIndex. TarEntryOffset , FilePath )
122
- -> Bool
123
- -> IO Scorer
124
- readmeScore Nothing _ = return $ Scorer 1 0 -- readmeScore is scaled so it does not need correct max
125
- readmeScore (Just (tarfile, _, offset, name)) app = do
126
- entr <- loadTarEntry tarfile offset
120
+ readmeScore :: TarIndexCacheFeature -> PkgInfo -> Bool -> IO Scorer
121
+ readmeScore tarCache pkgI app = do
122
+ Just (tarfile, _, offset, name) <- readme
123
+ entr <- loadTarEntry tarfile offset
127
124
case entr of
128
125
(Right (size, str)) -> return $ calcScore str size name
129
126
_ -> return $ Scorer 1 0
130
127
where
128
+ readme = findToplevelFile tarCache pkgI isReadmeFile
129
+ >>= either (\ _ -> return Nothing ) (return . Just )
131
130
calcScore str size filename =
132
131
scorer 75 (min 1 (fromInteger (toInteger size) / 3000 ))
133
132
<> if supposedToBeMarkdown filename
@@ -162,13 +161,13 @@ baseScore
162
161
163
162
baseScore vers maintainers docs env tarCache versionList lastUploads pkgI = do
164
163
165
- readM <- readme
166
- hasDocum <- documHas
167
- documS <- documSize
168
- srcL <- srcLines
164
+ hasDocum <- handleConst False documHas -- Probably redundant
165
+ documS <- handleConst 0 documSize
166
+ srcL <- handleConst 0 srcLines
169
167
170
- versS <- versionScore versionList vers lastUploads pkg
171
- readmeS <- readmeScore readM isApp
168
+ versS <- handleConst (Scorer 1 0 )
169
+ (versionScore versionList vers lastUploads pkg)
170
+ readmeS <- handleConst (Scorer 1 0 ) (readmeScore tarCache pkgI isApp)
172
171
return
173
172
$ scale 5 versS
174
173
<> scale 2 (codeScore documS srcL)
@@ -192,9 +191,6 @@ baseScore vers maintainers docs env tarCache versionList lastUploads pkgI = do
192
191
filterLines (isExtensionOf " .html" ) countSize
193
192
. Tar. read
194
193
<$> BSL. readFile pth
195
- readme = findToplevelFile tarCache pkgI isReadmeFile
196
- >>= either (\ _ -> return Nothing ) (return . Just )
197
-
198
194
filterLines f g = Tar. foldEntries (g f) 0 (const 0 )
199
195
countLines :: (FilePath -> Bool ) -> Tar. Entry -> Float -> Float
200
196
countLines f entry l = if not . f . Tar. entryPath $ entry then l else lns
@@ -279,15 +275,16 @@ temporalScore p lastUploads versionList recentDownloads = do
279
275
where
280
276
isApp = (isNothing . library) p && (not . null . executables) p
281
277
downloadScore = calcDownScore recentDownloads
282
- calcDownScore i = fracScor 5
278
+ calcDownScore i = fracScor
279
+ 5
283
280
( (logBase 2 (int2Float $ max 0 (i - 32 ) + 32 ) - 5 )
284
281
/ (if isApp then 6 else 8 )
285
282
)
286
283
packageFreshness = case safeHead lastUploads of
287
284
Nothing -> return 0
288
- (Just l) -> freshness versionList l isApp
285
+ (Just l) -> freshness versionList l isApp -- Getting time hopefully does not throw Exc.
289
286
freshnessScore = fracScor 10 <$> packageFreshness
290
- -- Missing dependencyFreshnessScore for reasonable effectivity needs caching
287
+ -- Missing dependencyFreshnessScore for reasonable effectivity needs caching
291
288
tractionScore = do
292
289
fresh <- packageFreshness
293
290
return $ boolScor 1 (fresh * int2Float recentDownloads > 200 )
@@ -315,7 +312,7 @@ rankPackage versions recentDownloads maintainers docs tarCache env pkgs (Just pk
315
312
versionList
316
313
uploads
317
314
pkgUsed
318
- depr <- deprP
315
+ depr <- handleConst Nothing deprP
319
316
return $ sAverage t b * case depr of
320
317
Nothing -> 1
321
318
_ -> 0.2
0 commit comments