Merge pull request #14084 from ninjadq/support_multiple_artifact_type_in_metric

Support multiple artifact type in metric
This commit is contained in:
Qian Deng 2021-01-28 17:27:16 +08:00 committed by GitHub
commit 6f3774ac8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View File

@ -100,7 +100,9 @@ func (hc *ProjectCollector) Collect(c chan<- prometheus.Metric) {
c <- projectMemberTotal.MustNewConstMetric(p.MemberTotal, p.Name)
c <- projectRepoTotal.MustNewConstMetric(p.RepoTotal, p.Name, getPublicValue(p.Public))
c <- artifactPullTotal.MustNewConstMetric(p.PullTotal, p.Name)
c <- projectArtifactTotal.MustNewConstMetric(p.Artifact.ArtifactTotal, p.Name, getPublicValue(p.Public), p.Artifact.ArtifactType)
for _, a := range p.Artifact {
c <- projectArtifactTotal.MustNewConstMetric(a.ArtifactTotal, p.Name, getPublicValue(p.Public), a.ArtifactType)
}
}
}
@ -123,7 +125,7 @@ type projectInfo struct {
MemberTotal float64 `orm:"column(member_total)"`
RepoTotal float64 `orm:"column(repo_total)"`
PullTotal float64 `orm:"column(pull_total)"`
Artifact artifactInfo
Artifact map[string]artifactInfo
}
type artifactInfo struct {
ProjectID int64 `orm:"column(project_id)"`
@ -179,6 +181,7 @@ func updateProjectBasicInfo(projectMap map[int64]*projectInfo) {
_, err := dao.GetOrmer().Raw(projectBasicSQL).QueryRows(&pList)
checkErr(err, "get project from DB failure")
for _, p := range pList {
p.Artifact = make(map[string]artifactInfo)
projectMap[p.ProjectID] = p
}
}
@ -218,7 +221,7 @@ func updateProjectArtifactInfo(projectMap map[int64]*projectInfo) {
checkErr(err, "get data from DB failure")
for _, a := range aList {
if _, ok := projectMap[a.ProjectID]; ok {
projectMap[a.ProjectID].Artifact = a
projectMap[a.ProjectID].Artifact[a.ArtifactType] = a
} else {
log.Errorf("%v, ID %d", errProjectNotFound, a.ProjectID)
}

View File

@ -130,8 +130,8 @@ func (c *PorjectCollectorTestSuite) TestProjectCollector() {
c.Equalf(pMap[testPro1.ProjectID].Usage, "{\"storage\": 0}", "pMap %v", pMap)
c.Equalf(pMap[testPro1.ProjectID].MemberTotal, float64(2), "pMap %v", pMap)
c.Equalf(pMap[testPro1.ProjectID].PullTotal, float64(0), "pMap %v", pMap)
c.Equalf(pMap[testPro1.ProjectID].Artifact.ArtifactTotal, float64(1), "pMap %v", pMap)
c.Equalf(pMap[testPro1.ProjectID].Artifact.ArtifactType, "IMAGE", "pMap %v", pMap)
c.Equalf(pMap[testPro1.ProjectID].Artifact["IMAGE"].ArtifactTotal, float64(1), "pMap %v", pMap)
c.Equalf(pMap[testPro1.ProjectID].Artifact["IMAGE"].ArtifactType, "IMAGE", "pMap %v", pMap)
c.Equalf(pMap[testPro2.ProjectID].ProjectID, testPro2.ProjectID, "pMap %v", pMap)
c.Equalf(pMap[testPro2.ProjectID].Name, testPro2.Name, "pMap %v", pMap)
@ -140,7 +140,7 @@ func (c *PorjectCollectorTestSuite) TestProjectCollector() {
c.Equalf(pMap[testPro2.ProjectID].Usage, "{\"storage\": 0}", "pMap %v", pMap)
c.Equalf(pMap[testPro2.ProjectID].MemberTotal, float64(3), "pMap %v", pMap)
c.Equalf(pMap[testPro2.ProjectID].PullTotal, float64(0), "pMap %v", pMap)
c.Equalf(pMap[testPro2.ProjectID].Artifact.ArtifactTotal, float64(1), "pMap %v", pMap)
c.Equalf(pMap[testPro2.ProjectID].Artifact["IMAGE"].ArtifactTotal, float64(1), "pMap %v", pMap)
}