Skip to content

Commit a36abf9

Browse files
committed
changed the algorithm to match cargo
1 parent 137d09d commit a36abf9

File tree

2 files changed

+75
-52
lines changed

2 files changed

+75
-52
lines changed

src/Distribution/Server/Features/PackageList.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import Distribution.Package
3434
import Distribution.PackageDescription
3535
import Distribution.PackageDescription.Configuration
3636
import Distribution.Utils.ShortText (fromShortText)
37+
import Distribution.Simple.Utils (safeLast)
3738

3839
import Control.Concurrent
3940
import Data.Maybe (mapMaybe)
@@ -269,8 +270,8 @@ listFeature CoreFeature{..}
269270
votes <- pkgNumScore pkgname
270271
deprs <- queryGetDeprecatedFor pkgname
271272
maintainers <- queryUserGroup (maintainersGroup pkgname)
272-
packageR <- rankPackage versions (cmFind pkgname downs)
273-
(UserIdSet.size maintainers) documentation tar env pkgs
273+
packageR <- rankPackage versions (cmFind pkgname downs)
274+
(UserIdSet.size maintainers) documentation tar env pkgs (safeLast pkgs)
274275

275276
return $ (,) pkgname $ (updateDescriptionItem (pkgDesc pkg) $ emptyPackageItem pkgname) {
276277
itemTags = tags

src/Distribution/Server/Features/PackageRank.hs

Lines changed: 72 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import Data.List ( maximumBy
3232
import Data.Maybe ( isNothing )
3333
import Data.Ord ( comparing )
3434
import qualified Data.Time.Clock as CL
35+
import Distribution.Server.Packages.Readme
3536
import GHC.Float ( int2Float )
3637
import System.FilePath ( isExtensionOf )
3738

@@ -60,6 +61,9 @@ boolScor k False = Scorer k 0
6061
total :: Scorer -> Float
6162
total (Scorer a b) = b / a
6263

64+
scale :: Float -> Scorer -> Scorer
65+
scale mx sc = fracScor mx (total sc)
66+
6367
major :: Num a => [a] -> a
6468
major (x : _) = x
6569
major _ = 0
@@ -96,38 +100,46 @@ freshness (x : xs) lastUpd app =
96100
age = flip numDays (Just lastUpd) . Just <$> CL.getCurrentTime
97101
decayDays = expectedUpdateInterval / 2 + (if app then 300 else 200)
98102

99-
-- lookupPackageId
100-
-- queryHasDocumentation
103+
cabalScore :: PackageDescription -> IO Bool -> IO Scorer
104+
cabalScore p docum =
105+
(<>) (tests <> benchs <> desc <> homeP <> sourceRp <> cats)
106+
<$> (boolScor 30 <$> docum)
107+
where
108+
tests = boolScor 50 (hasTests p)
109+
benchs = boolScor 10 (hasBenchmarks p)
110+
desc = scorer 30 (min 1 (int2Float (S.length $ description p) / 300))
111+
-- documentation = boolScor 30 ()
112+
homeP = boolScor 30 (not $ S.null $ homepage p)
113+
sourceRp = boolScor 8 (not $ null $ sourceRepos p)
114+
cats = boolScor 5 (not $ S.null $ category p)
115+
116+
readmeScore _ = Scorer 0 0
101117

102-
-- TODO CoreFeature can be substituted by CoreResource
103-
rankIO
118+
-- queryHasDocumentation
119+
baseScore
104120
:: VersionsFeature
105121
-> Int
106-
-> Int
107122
-> DocumentationFeature
108123
-> ServerEnv
109124
-> TarIndexCacheFeature
110-
-> [PkgInfo]
111-
-> Maybe PkgInfo
125+
-> [Version]
126+
-> [CL.UTCTime]
127+
-> PkgInfo
112128
-> IO Scorer
113129

114-
rankIO _ _ _ _ _ _ _ Nothing = return (Scorer (118 + 16 + 4 + 1) 0)
115-
rankIO vers recentDownloads maintainers docs env tarCache pkgs (Just pkgI) = do
116-
temp <- temporalScore pkg lastUploads versionList recentDownloads
117-
versS <- versionScore versionList vers lastUploads pkg
118-
codeS <- codeScore documSize srcLines
119-
return $ temp <> versS <> codeS <> authorScore maintainers pkg
120-
130+
baseScore vers maintainers docs env tarCache versionList lastUploads pkgI = do
131+
versS <- versionScore versionList vers lastUploads pkg
132+
codeS <- codeScore documSize srcLines
133+
cabalS <- cabalScore pkg documHas
134+
return
135+
$ scale 5 versS
136+
<> scale 2 codeS
137+
<> scale 3 (authorScore maintainers pkg)
138+
<> scale 2 cabalS
139+
<> scale 5 (readmeScore readme)
121140
where
122-
pkg = packageDescription $ pkgDesc pkgI
123-
pkgId = package pkg
124-
lastUploads =
125-
sortBy (flip compare)
126-
$ (fst . pkgOriginalUploadInfo <$> pkgs)
127-
++ (fst . pkgLatestUploadInfo <$> pkgs)
128-
versionList :: [Version]
129-
versionList = sortBy (flip compare)
130-
$ map (pkgVersion . package . packageDescription) (pkgDesc <$> pkgs)
141+
pkg = packageDescription $ pkgDesc pkgI
142+
pkgId = package pkg
131143
srcLines = do
132144
Right (path, _, _) <- packageTarball tarCache pkgI
133145
filterLines (isExtensionOf ".hs") countLines
@@ -141,6 +153,8 @@ rankIO vers recentDownloads maintainers docs env tarCache pkgs (Just pkgI) = do
141153
filterLines (isExtensionOf ".html") countSize
142154
. Tar.read
143155
<$> BSL.readFile pth
156+
readme = findToplevelFile tarCache pkgI isReadmeFile
157+
>>= either (\_ -> return Nothing) (return . Just)
144158

145159
filterLines f g = Tar.foldEntries (g f) 0 (const 0)
146160
countLines :: (FilePath -> Bool) -> Tar.Entry -> Float -> Float
@@ -161,6 +175,7 @@ rankIO vers recentDownloads maintainers docs env tarCache pkgs (Just pkgI) = do
161175
documentPath = do
162176
blob <- documentBlob
163177
return $ BlobStorage.filepath (serverBlobStore env) <$> blob
178+
documHas = queryHasDocumentation docs pkgId
164179

165180
authorScore :: Int -> PackageDescription -> Scorer
166181
authorScore maintainers desc =
@@ -169,14 +184,14 @@ authorScore maintainers desc =
169184
maintScore = boolScor 3 (maintainers > 1) <> scorer 5 (int2Float maintainers)
170185

171186
codeScore :: IO Float -> IO Float -> IO Scorer
172-
codeScore documentL haskellL = do
173-
docum <- documentL
187+
codeScore documentS haskellL = do
188+
docum <- documentS
174189
haskell <- haskellL
175190
return
176191
$ boolScor 1 (haskell > 700)
177192
<> boolScor 1 (haskell < 80000)
178193
<> fracScor 2 (min 1 (haskell / 5000))
179-
<> fracScor 2 (min 1 (10 * docum) / (3000 + haskell))
194+
<> fracScor 2 (min 1 docum / ((3000 + haskell) * 200))
180195

181196
versionScore
182197
:: [Version]
@@ -241,20 +256,6 @@ temporalScore p lastUploads versionList recentDownloads = do
241256
fresh <- packageFreshness
242257
return $ boolScor 1 (fresh * int2Float recentDownloads > 1000)
243258

244-
rankPackagePage :: Maybe PackageDescription -> Scorer
245-
rankPackagePage Nothing = Scorer 233 0
246-
rankPackagePage (Just p) = tests <> benchs <> desc <> homeP <> sourceRp <> cats
247-
where
248-
tests = boolScor 50 (hasTests p)
249-
benchs = boolScor 10 (hasBenchmarks p)
250-
desc = scorer 30 (min 1 (int2Float (S.length $ description p) / 300))
251-
-- documentation = boolScor 30 ()
252-
homeP = boolScor 30 (not $ S.null $ homepage p)
253-
sourceRp = boolScor 8 (not $ null $ sourceRepos p)
254-
cats = boolScor 5 (not $ S.null $ category p)
255-
256-
-- TODO fix the function Signature replace PackageDescription to PackageName/Identifier
257-
258259
rankPackage
259260
:: VersionsFeature
260261
-> Int
@@ -263,14 +264,35 @@ rankPackage
263264
-> TarIndexCacheFeature
264265
-> ServerEnv
265266
-> [PkgInfo]
267+
-> Maybe PkgInfo
266268
-> IO Float
267-
rankPackage versions recentDownloads maintainers docs tarCache env pkgs =
268-
total . (<>) (rankPackagePage pkgD) <$> rankIO versions
269-
recentDownloads
270-
maintainers
271-
docs
272-
env
273-
tarCache
274-
pkgs
275-
(safeLast pkgs)
276-
where pkgD = packageDescription . pkgDesc <$> safeLast pkgs
269+
rankPackage _ _ _ _ _ _ _ Nothing = return 0
270+
rankPackage versions recentDownloads maintainers docs tarCache env pkgs (Just pkgUsed)
271+
= do
272+
t <- temporalScore pkgD uploads versionList recentDownloads
273+
274+
b <- baseScore versions
275+
maintainers
276+
docs
277+
env
278+
tarCache
279+
versionList
280+
uploads
281+
pkgUsed
282+
depr <- deprP
283+
return $ sAverage t b * case depr of
284+
Nothing -> 1
285+
_ -> 0.2
286+
where
287+
pkgname = pkgName . package $ pkgD
288+
pkgD = packageDescription . pkgDesc $ pkgUsed
289+
deprP = queryGetDeprecatedFor versions pkgname
290+
sAverage x y = (total x + total y) * 0.5
291+
292+
versionList :: [Version]
293+
versionList = sortBy (flip compare)
294+
$ map (pkgVersion . package . packageDescription) (pkgDesc <$> pkgs)
295+
uploads =
296+
sortBy (flip compare)
297+
$ (fst . pkgOriginalUploadInfo <$> pkgs)
298+
++ (fst . pkgLatestUploadInfo <$> pkgs)

0 commit comments

Comments
 (0)