@@ -32,6 +32,7 @@ import Data.List ( maximumBy
32
32
import Data.Maybe ( isNothing )
33
33
import Data.Ord ( comparing )
34
34
import qualified Data.Time.Clock as CL
35
+ import Distribution.Server.Packages.Readme
35
36
import GHC.Float ( int2Float )
36
37
import System.FilePath ( isExtensionOf )
37
38
@@ -60,6 +61,9 @@ boolScor k False = Scorer k 0
60
61
total :: Scorer -> Float
61
62
total (Scorer a b) = b / a
62
63
64
+ scale :: Float -> Scorer -> Scorer
65
+ scale mx sc = fracScor mx (total sc)
66
+
63
67
major :: Num a => [a ] -> a
64
68
major (x : _) = x
65
69
major _ = 0
@@ -96,38 +100,46 @@ freshness (x : xs) lastUpd app =
96
100
age = flip numDays (Just lastUpd) . Just <$> CL. getCurrentTime
97
101
decayDays = expectedUpdateInterval / 2 + (if app then 300 else 200 )
98
102
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
101
117
102
- -- TODO CoreFeature can be substituted by CoreResource
103
- rankIO
118
+ -- queryHasDocumentation
119
+ baseScore
104
120
:: VersionsFeature
105
121
-> Int
106
- -> Int
107
122
-> DocumentationFeature
108
123
-> ServerEnv
109
124
-> TarIndexCacheFeature
110
- -> [PkgInfo ]
111
- -> Maybe PkgInfo
125
+ -> [Version ]
126
+ -> [CL. UTCTime ]
127
+ -> PkgInfo
112
128
-> IO Scorer
113
129
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)
121
140
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
131
143
srcLines = do
132
144
Right (path, _, _) <- packageTarball tarCache pkgI
133
145
filterLines (isExtensionOf " .hs" ) countLines
@@ -141,6 +153,8 @@ rankIO vers recentDownloads maintainers docs env tarCache pkgs (Just pkgI) = do
141
153
filterLines (isExtensionOf " .html" ) countSize
142
154
. Tar. read
143
155
<$> BSL. readFile pth
156
+ readme = findToplevelFile tarCache pkgI isReadmeFile
157
+ >>= either (\ _ -> return Nothing ) (return . Just )
144
158
145
159
filterLines f g = Tar. foldEntries (g f) 0 (const 0 )
146
160
countLines :: (FilePath -> Bool ) -> Tar. Entry -> Float -> Float
@@ -161,6 +175,7 @@ rankIO vers recentDownloads maintainers docs env tarCache pkgs (Just pkgI) = do
161
175
documentPath = do
162
176
blob <- documentBlob
163
177
return $ BlobStorage. filepath (serverBlobStore env) <$> blob
178
+ documHas = queryHasDocumentation docs pkgId
164
179
165
180
authorScore :: Int -> PackageDescription -> Scorer
166
181
authorScore maintainers desc =
@@ -169,14 +184,14 @@ authorScore maintainers desc =
169
184
maintScore = boolScor 3 (maintainers > 1 ) <> scorer 5 (int2Float maintainers)
170
185
171
186
codeScore :: IO Float -> IO Float -> IO Scorer
172
- codeScore documentL haskellL = do
173
- docum <- documentL
187
+ codeScore documentS haskellL = do
188
+ docum <- documentS
174
189
haskell <- haskellL
175
190
return
176
191
$ boolScor 1 (haskell > 700 )
177
192
<> boolScor 1 (haskell < 80000 )
178
193
<> fracScor 2 (min 1 (haskell / 5000 ))
179
- <> fracScor 2 (min 1 ( 10 * docum) / (3000 + haskell))
194
+ <> fracScor 2 (min 1 docum / (( 3000 + haskell) * 200 ))
180
195
181
196
versionScore
182
197
:: [Version ]
@@ -241,20 +256,6 @@ temporalScore p lastUploads versionList recentDownloads = do
241
256
fresh <- packageFreshness
242
257
return $ boolScor 1 (fresh * int2Float recentDownloads > 1000 )
243
258
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
-
258
259
rankPackage
259
260
:: VersionsFeature
260
261
-> Int
@@ -263,14 +264,35 @@ rankPackage
263
264
-> TarIndexCacheFeature
264
265
-> ServerEnv
265
266
-> [PkgInfo ]
267
+ -> Maybe PkgInfo
266
268
-> 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