Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
5.0 kB
2
Indexable
func (repo *monitoringRepoImpl) GetMonitoringList(ctx context.Context, req dto.GetLivenessListRequest) error {
	ctx, span := otel.Tracer("monitoringRepo").Start(ctx, "GetMonitoringList")
	defer span.End()
	// var finalQuery string
	var whereFilter string
	var joinFilter string
	// var orderFilter string
	// var result string

	baseQuery := "SELECT anti_spoofing_log.*, face_image.* FROM anti_spoofing_log"
	joinFilter = ` JOIN face_image
		ON (
    			(
        			anti_spoofing_log.session_id = face_image.session_id
        			AND anti_spoofing_log.log_type != 'face_comparison_im_2'
        			AND anti_spoofing_log.log_type != 'face_comparison_im_1'
    			)
    		OR
    			(
    			    anti_spoofing_log.session_id = face_image.session_id
    			    AND anti_spoofing_log.log_type = 'face_comparison_im_1'
    			    AND face_image.endpoint_name = 'compare_1'
    			)
    		OR
		   		(
    			    anti_spoofing_log.session_id = face_image.session_id
    			    AND anti_spoofing_log.log_type = 'face_comparison_im_2'
    			    AND face_image.endpoint_name = 'compare_2'
    			)
			)
		`
	whereFilter = fmt.Sprintf(`
		WHERE anti_spoofing_log.created_at BETWEEN '%s' AND '%s'`,
		*req.ListFilter.StartDate,
		*req.ListFilter.EndDate,
	)

	if !util.Deref(req.ListFilter.IsLabeled) {
		joinFilter += `
		LEFT JOIN liveness_labeling ON anti_spoofing_log.id = liveness_labeling.anti_spoofing_log_id
		`
		whereFilter += `
		AND liveness_labeling.anti_spoofing_log_id IS NULL
		`
	} else if *req.ListFilter.IsLabeled {
		joinFilter += `
		JOIN liveness_labeling ON anti_spoofing_log.id = liveness_labeling.anti_spoofing_log_id
		`
	} else if req.ListFilter.IsLabeled == nil {
		joinFilter += `
		LEFT JOIN liveness_labeling ON anti_spoofing_log.id = liveness_labeling.anti_spoofing_log_id
		`
	}

	v := reflect.ValueOf(&req.LabelFilter).Elem()
	t := reflect.TypeOf(&req.LabelFilter).Elem()

	for i := 0; i < v.NumField(); i++ {
		field := v.Field(i)
		tag := t.Field(i).Tag.Get("query")
		if tag != "" && field.IsValid() && !field.IsNil() {
			switch field.Kind() {
			case reflect.Ptr:
				elem := field.Elem()
				if elem.Kind() == reflect.String {
					whereFilter += fmt.Sprintf(` AND liveness_labeling.%s = '%s'`, tag, elem.String())
				}
				if elem.Kind() == reflect.Bool {
					whereFilter += fmt.Sprintf(` AND liveness_labeling.%s = %t`, tag, elem.Bool())
				}
			default:
				whereFilter += fmt.Sprintf(`AND liveness_labeling.%s = '%v'`, tag, field.Interface())
			}
		}
	}

	if req.ListFilter.HasGroundTruth == nil {
		joinFilter += `
		LEFT JOIN liveness_ground_truth ON anti_spoofing_log.id = liveness_ground_truth.anti_spoofing_log_id
		`
	} else if !*req.ListFilter.HasGroundTruth {
		joinFilter += `
		LEFT JOIN liveness_ground_truth ON anti_spoofing_log.id = liveness_ground_truth.anti_spoofing_log_id
		`
		whereFilter += `
		AND liveness_ground_truth.anti_spoofing_log_id IS NULL
		`
	} else if *req.ListFilter.HasGroundTruth {
		joinFilter += `
		LEFT JOIN liveness_ground_truth ON anti_spoofing_log.id = liveness_ground_truth.anti_spoofing_log_id
		`
	}

	if req.ListFilter.LivenessGroundTruth != nil {
		whereFilter += fmt.Sprintf(`
		AND liveness_ground_truth = %t
		`, *req.ListFilter.LivenessGroundTruth)
	}

	if req.ListFilter.IsCorrectPrediction != nil {
		if *req.ListFilter.IsCorrectPrediction {
			whereFilter += `
			liveness_ground_truth.liveness_ground_truth = liveness_ground_truth.liveness_result_snapshot
			`
		} else if !*req.ListFilter.IsCorrectPrediction {
			whereFilter += `
			liveness_ground_truth.liveness_ground_truth != liveness_ground_truth.liveness_result_snapshot
			`
		}
	}

	var search string
	if req.ListFilter.SessionID != nil && *req.ListFilter.SessionID != "" {
		search = fmt.Sprintf("%%%s%%", *req.ListFilter.SessionID)
		whereFilter += fmt.Sprintf(`
		AND anti_spoofing_log.session_id ILIKE %s
		`, search)
	}
	if req.ListFilter.ApplicationID != nil && *req.ListFilter.ApplicationID != "" {
		search = fmt.Sprintf("%%%s%%", *req.ListFilter.ApplicationID)
		whereFilter += fmt.Sprintf(`
		AND anti_spoofing_log.application_id ILIKE '%s'
		`, search)
	}

	if len(req.ListFilter.EndpointName) > 0 {
		whereFilter += fmt.Sprintf(`
		AND face_image.endpoint_name IN (%s) 
		`, strings.Join(req.ListFilter.EndpointName, ","))
	}

	if req.ListFilter.FasStatus != nil {
		whereFilter += fmt.Sprintf(`
		CAST(anti_spoofing_log.results->'liveness'->>'status' AS BOOLEAN) = %t
		`, *req.ListFilter.FasStatus)
	}

	if req.ListFilter.IsTest != nil {
		whereFilter += fmt.Sprintf(`
		AND anti_spoofing_log.is_test = %t
		`, *req.ListFilter.IsTest)
	}
	result := make(map[string]interface{})

	finalQuery := baseQuery+joinFilter+whereFilter+" ORDER BY anti_spoofing_log.id ASC;" 
	print(finalQuery)
	err := repo.DB.QueryRow(
		ctx, finalQuery,
	).Scan(&result)

	if err != nil {
		log.Println(err)
		return err
	}

	for key, value := range result {
        log.Println("%s: %v\n", key, value)
    }
	log.Println(baseQuery + joinFilter + whereFilter)

	return nil
}
Leave a Comment