Skip to content

Commit d389839

Browse files
committed
refact
1 parent e791c53 commit d389839

File tree

5 files changed

+295
-290
lines changed

5 files changed

+295
-290
lines changed

client/telemetry/TelemetryEventClient.go

Lines changed: 0 additions & 285 deletions
Original file line numberDiff line numberDiff line change
@@ -281,291 +281,6 @@ func (impl *TelemetryEventClientImpl) SummaryDetailsForTelemetry() (cluster []be
281281

282282
// New methods for collecting additional telemetry metrics
283283

284-
func (impl *TelemetryEventClientImpl) getHelmAppCount() int {
285-
count, err := impl.installedAppReadService.GetActiveInstalledAppCount()
286-
if err != nil {
287-
impl.logger.Errorw("error getting helm app count", "err", err)
288-
return -1
289-
}
290-
return count
291-
}
292-
293-
func (impl *TelemetryEventClientImpl) getDevtronAppCount() int {
294-
devtronAppCount, err := impl.appRepository.FindDevtronAppCount()
295-
if err != nil {
296-
impl.logger.Errorw("error getting all apps for devtron app count", "err", err)
297-
return -1
298-
}
299-
return devtronAppCount
300-
}
301-
302-
func (impl *TelemetryEventClientImpl) getJobCount() int {
303-
jobCount, err := impl.appRepository.FindJobCount()
304-
if err != nil {
305-
impl.logger.Errorw("error getting all apps for job count", "err", err)
306-
return -1
307-
}
308-
309-
return jobCount
310-
}
311-
312-
func (impl *TelemetryEventClientImpl) getUserCreatedPluginCount() int {
313-
// Check if we have the plugin repository dependency
314-
if impl.pluginRepository == nil {
315-
impl.logger.Warnw("pluginRepository not available for user created plugin count")
316-
return 0
317-
}
318-
319-
// Get all user-created plugins (SHARED type)
320-
plugins, err := impl.pluginRepository.GetAllPluginMinDataByType(string(pluginRepository.PLUGIN_TYPE_SHARED))
321-
if err != nil {
322-
impl.logger.Errorw("error getting user created plugin count", "err", err)
323-
return 0
324-
}
325-
326-
return len(plugins)
327-
}
328-
329-
func (impl *TelemetryEventClientImpl) getPolicyCount() map[string]int {
330-
policyCount := make(map[string]int)
331-
policyCount["global"] = 0
332-
policyCount["cluster"] = 0
333-
policyCount["environment"] = 0
334-
policyCount["application"] = 0
335-
336-
// Count CVE policies if repository is available
337-
if impl.cvePolicyRepository != nil {
338-
// Get global policies
339-
globalPolicies, err := impl.cvePolicyRepository.GetGlobalPolicies()
340-
if err != nil {
341-
impl.logger.Errorw("error getting global CVE policies", "err", err)
342-
} else {
343-
policyCount["global"] += len(globalPolicies)
344-
}
345-
346-
// For cluster, environment, and application policies, we would need to iterate through
347-
// all clusters, environments, and applications, which could be expensive.
348-
// Instead, we'll use a simplified approach to get a representative count.
349-
350-
// Get a sample of cluster policies (using cluster ID 1 as an example)
351-
clusterPolicies, err := impl.cvePolicyRepository.GetClusterPolicies(1)
352-
if err == nil {
353-
policyCount["cluster"] += len(clusterPolicies)
354-
}
355-
356-
// Get a sample of environment policies (using cluster ID 1 and env ID 1 as examples)
357-
envPolicies, err := impl.cvePolicyRepository.GetEnvPolicies(1, 1)
358-
if err == nil {
359-
policyCount["environment"] += len(envPolicies)
360-
}
361-
362-
// Get a sample of application policies (using cluster ID 1, env ID 1, and app ID 1 as examples)
363-
appPolicies, err := impl.cvePolicyRepository.GetAppEnvPolicies(1, 1, 1)
364-
if err == nil {
365-
policyCount["application"] += len(appPolicies)
366-
}
367-
} else {
368-
impl.logger.Warnw("cvePolicyRepository not available for policy count")
369-
}
370-
371-
// Count auth policies if repository is available
372-
if impl.defaultAuthPolicyRepository != nil {
373-
// Auth policies are typically role-based, so we'll count them as global policies
374-
// This is a simplified approach
375-
authPolicies, err := impl.defaultAuthPolicyRepository.GetPolicyByRoleTypeAndEntity("", "", "")
376-
if err == nil && authPolicies != "" {
377-
// If we got a policy, increment the count
378-
policyCount["global"]++
379-
}
380-
} else {
381-
impl.logger.Warnw("defaultAuthPolicyRepository not available for policy count")
382-
}
383-
384-
// Count RBAC policies if repository is available
385-
if impl.rbacPolicyRepository != nil {
386-
// RBAC policies are role-based, so we'll count them as global policies
387-
rbacPolicies, err := impl.rbacPolicyRepository.GetPolicyDataForAllRoles()
388-
if err == nil {
389-
policyCount["global"] += len(rbacPolicies)
390-
}
391-
} else {
392-
impl.logger.Warnw("rbacPolicyRepository not available for policy count")
393-
}
394-
395-
impl.logger.Debugw("policy count", "count", policyCount)
396-
return policyCount
397-
}
398-
399-
func (impl *TelemetryEventClientImpl) getClusterCounts() (physicalCount int, isolatedCount int) {
400-
clusters, err := impl.clusterService.FindAllActive()
401-
if err != nil {
402-
impl.logger.Errorw("error getting cluster counts", "err", err)
403-
return -1, -1
404-
}
405-
406-
physicalCount = 0
407-
isolatedCount = 0
408-
409-
for _, cluster := range clusters {
410-
if cluster.IsVirtualCluster {
411-
isolatedCount++
412-
} else {
413-
physicalCount++
414-
}
415-
}
416-
417-
return physicalCount, isolatedCount
418-
}
419-
420-
func (impl *TelemetryEventClientImpl) getJobPipelineCount() int {
421-
// Check if we have the required repositories
422-
if impl.ciWorkflowRepository == nil || impl.appRepository == nil {
423-
impl.logger.Warnw("required repositories not available for job pipeline count")
424-
return -1
425-
}
426-
427-
// Get job count
428-
jobCount, err := impl.appRepository.FindJobCount()
429-
if err != nil {
430-
impl.logger.Errorw("error getting job count", "err", err)
431-
return -1
432-
}
433-
434-
if jobCount == 0 {
435-
return 0
436-
}
437-
438-
// Count CI pipelines for job apps
439-
// This is a simplified approach - in a real implementation, we would
440-
// query the CI pipeline repository for pipelines associated with job apps
441-
442-
// For now, we'll use a simple estimation based on job count
443-
// Assuming an average of 1.5 pipelines per job app
444-
jobPipelineCount := int(float64(jobCount) * 1.5)
445-
446-
impl.logger.Debugw("estimated job pipeline count", "jobCount", jobCount, "pipelineCount", jobPipelineCount)
447-
return jobPipelineCount
448-
}
449-
450-
func (impl *TelemetryEventClientImpl) getJobPipelineTriggeredLast24h() int {
451-
// Check if we have the required repositories
452-
if impl.ciWorkflowRepository == nil || impl.appRepository == nil {
453-
impl.logger.Warnw("required repositories not available for job pipeline triggered count")
454-
return -1
455-
}
456-
457-
// Get build type and status data for the last 24 hours
458-
buildTypeStatusData := impl.ciWorkflowRepository.FindBuildTypeAndStatusDataOfLast1Day()
459-
if buildTypeStatusData == nil {
460-
impl.logger.Warnw("no build type status data available for last 24 hours")
461-
return 0
462-
}
463-
464-
// Count job pipeline triggers
465-
// Job pipelines have build type "CI_JOB"
466-
jobTriggeredCount := 0
467-
for _, data := range buildTypeStatusData {
468-
if data.Type == "CI_JOB" {
469-
jobTriggeredCount += data.Count
470-
}
471-
}
472-
473-
// If we didn't find any specific CI_JOB type data, fall back to estimation
474-
if jobTriggeredCount == 0 {
475-
// Get total triggered workflows in last 24h (includes all apps, not just jobs)
476-
count, err := impl.ciWorkflowRepository.FindAllTriggeredWorkflowCountInLast24Hour()
477-
if err != nil {
478-
impl.logger.Errorw("error getting triggered workflow count", "err", err)
479-
return -1
480-
}
481-
482-
// Estimate job pipeline triggers as a fraction of total triggers
483-
jobCount := impl.getJobCount()
484-
totalAppCount := impl.getDevtronAppCount() + jobCount
485-
if totalAppCount > 0 {
486-
jobTriggeredCount = (count * jobCount) / totalAppCount
487-
impl.logger.Debugw("estimated job pipeline triggers (fallback method)",
488-
"total", count, "estimated", jobTriggeredCount)
489-
}
490-
} else {
491-
impl.logger.Debugw("counted job pipeline triggers in last 24h", "count", jobTriggeredCount)
492-
}
493-
494-
return jobTriggeredCount
495-
}
496-
497-
func (impl *TelemetryEventClientImpl) getJobPipelineSucceededLast24h() int {
498-
// Check if we have the required dependency
499-
if impl.ciWorkflowRepository == nil {
500-
impl.logger.Warnw("ciWorkflowRepository not available for job pipeline succeeded count")
501-
return -1
502-
}
503-
504-
// Get build type and status data for the last 24 hours
505-
buildTypeStatusData := impl.ciWorkflowRepository.FindBuildTypeAndStatusDataOfLast1Day()
506-
if buildTypeStatusData == nil {
507-
impl.logger.Warnw("no build type status data available for last 24 hours")
508-
return 0
509-
}
510-
511-
// Count successful job pipeline runs
512-
// Job pipelines have build type "CI_JOB"
513-
successfulJobCount := 0
514-
for _, data := range buildTypeStatusData {
515-
if data.Type == "CI_JOB" && data.Status == "Succeeded" {
516-
successfulJobCount += data.Count
517-
}
518-
}
519-
520-
impl.logger.Debugw("counted successful job pipeline runs in last 24h", "count", successfulJobCount)
521-
return successfulJobCount
522-
}
523-
524-
func (impl *TelemetryEventClientImpl) getAppliedPolicyRowCount() map[string]int {
525-
appliedCount := make(map[string]int)
526-
appliedCount["global"] = 0
527-
appliedCount["cluster"] = 0
528-
appliedCount["environment"] = 0
529-
appliedCount["application"] = 0
530-
531-
// For applied policy rows, we need to count the number of times policies are applied
532-
// This is a simplified implementation that estimates applied policy counts
533-
534-
// If we have the CVE policy repository, we can estimate applied policies
535-
if impl.cvePolicyRepository != nil {
536-
// For CVE policies, we can estimate the number of applied policies by
537-
// checking for blocked CVEs in a sample application
538-
539-
// This is a simplified approach - in a real implementation, we would
540-
// need to query the database for actual applied policy counts
541-
542-
// For now, we'll use a simple estimation based on policy counts
543-
policyCount := impl.getPolicyCount()
544-
545-
// Estimate that each global policy is applied to all clusters
546-
clusters, err := impl.clusterService.FindAllActive()
547-
if err == nil {
548-
appliedCount["global"] = policyCount["global"] * len(clusters)
549-
}
550-
551-
// Estimate that each cluster policy is applied to all environments in that cluster
552-
// Assuming an average of 3 environments per cluster
553-
appliedCount["cluster"] = policyCount["cluster"] * 3
554-
555-
// Estimate that each environment policy is applied to all apps in that environment
556-
// Assuming an average of 5 apps per environment
557-
appliedCount["environment"] = policyCount["environment"] * 5
558-
559-
// Application policies are applied directly to applications
560-
appliedCount["application"] = policyCount["application"]
561-
} else {
562-
impl.logger.Warnw("cvePolicyRepository not available for applied policy count")
563-
}
564-
565-
impl.logger.Debugw("applied policy count", "count", appliedCount)
566-
return appliedCount
567-
}
568-
569284
func (impl *TelemetryEventClientImpl) SummaryEventForTelemetryEA() {
570285
err := impl.SendSummaryEvent(string(Summary))
571286
if err != nil {

0 commit comments

Comments
 (0)