func configureCollectFromTeamCity()

in cmd/tc-collector/main.go [42:132]


func configureCollectFromTeamCity() error {
	clickHouseUrl := env.Get("CLICKHOUSE", "127.0.0.1:9000")
	sinceDate := flag.String("since", "", "The date to force collecting since")
	flag.Parse()

	var since time.Time
	if *sinceDate != "" {
		var err error
		since, err = dateparse.ParseStrict(*sinceDate)
		if err != nil {
			return fmt.Errorf("cannot parse since date: %w", err)
		}
	}

	var config CollectorConfiguration
	rawJson, err := util.GetEnvOrFile("CONFIG", "/etc/config/config.json")
	if err != nil {
		return err
	}

	rawJson = strings.TrimSpace(rawJson)
	if rawJson == "" {
		return errors.New("file /etc/config/config.json is empty or env CONFIG is not set")
	}

	err = json.Unmarshal([]byte(rawJson), &config)
	if err != nil {
		return errors.New("cannot parse json: " + rawJson)
	}

	httpClient := &http.Client{
		Timeout: 60 * time.Second,
		Transport: &http.Transport{
			MaxIdleConns:        10,
			MaxIdleConnsPerHost: 10,
		},
	}
	httpClient.CheckRedirect = checkRedirectFunc

	taskContext, cancel := util.CreateCommandContext()
	defer cancel()

	for _, chunk := range config.BuildConfigurations {
		if taskContext.Err() != nil {
			break
		}

		var buildConfigurationIds []string
		switch {
		case chunk.Database == "ij":
			osList := []string{"Linux", "Windows", "MacM2"}
			for _, configuration := range chunk.Configurations {
				if hasOSSuffix(osList, configuration) {
					buildConfigurationIds = append(buildConfigurationIds, configuration)
				} else {
					for _, osName := range osList {
						buildConfigurationIds = append(buildConfigurationIds, configuration+osName)
					}
				}
			}
		default:
			for _, configuration := range chunk.Configurations {
				collector := &Collector{
					serverUrl:  config.TeamcityUrl + "/app/rest",
					httpClient: httpClient,
				}
				configurations, err := collector.getSnapshots(taskContext, configuration)
				slog.Info("get snapshots", "configurations", configurations)
				if err != nil {
					slog.Warn("cannot get snapshots", "err", err)
				}
				buildConfigurationIds = append(buildConfigurationIds, configurations...)
			}
		}

		err = collectFromTeamCity(taskContext, clickHouseUrl, config.TeamcityUrl, chunk.Database, buildConfigurationIds, since, httpClient)
		if err != nil {
			return err
		}
	}

	natsUrl := os.Getenv("NATS")
	if natsUrl != "" {
		err = doNotifyServer(natsUrl)
		if err != nil {
			return err
		}
	}

	return nil
}