首页 > 解决方案 > Artifactory getStats 总是返回空对象

问题描述

我正在尝试在服务器上的 groovy 插件中获取我们在自己的工件服务器上托管的 docker 映像的下载统计信息。到目前为止,该manifest.json文件似乎包含我正在寻找的统计信息。

repositories.getStats(RepoPath)应该返回一个StatsInfo具有getDownloadCount().

但是,无论我传递给它什么,repositories.getStats()它总是返回一个null对象。

我试过了:

但仍然repositories.getStats()返回一个空对象。

我希望以下工作:

def stats = repositories.getStats(RepoPathFactory.create('docker-local','ai/latest/manifest.json'))
def dlCount = stats.getDownloadCount()

但它不断返回错误:

"Cannot invoke method getDownloadCount() on null object"

?stats通过REST API调用manifest.json有效,它返回一个带有下载统计信息的 json 格式的字符串。前任:

my-artifactory-server.io/api/storage/docker-local/ai/latest/manifest.json?stats

  "uri" : "my-artifactory-server.io/api/storage/docker-local/ai/latest/manifest.json",

  "downloadCount" : 10,

  "lastDownloaded" : 1593160404880,

  "lastDownloadedBy" : "Peter Griffin",

  "remoteDownloadCount" : 0,

  "remoteLastDownloaded" : 0

我究竟做错了什么?

标签: dockerartifactoryrest

解决方案


你试过的看起来不错,可能是你需要强制stats对象必须是StatsInfo
下面是我测试和工作的代码,它打印下载计数manifest.json

import org.artifactory.repo.RepoPathFactory
import org.artifactory.fs.StatsInfo

executions {
    getDockerStats() {
        def statsInfo = (StatsInfo) repositories.getStats(RepoPathFactory.create("docker-local","hello-world/latest/manifest.json"))
        log.info("stats download count : ${statsInfo.getDownloadCount()}")
    }
}

并在日志中
[getDockerStats:12 ] [http-nio-8081-exec-5] - stats download count : 2


推荐阅读