1package controllers
2
3import (
4	e "repodiff/entities"
5	"repodiff/interactors"
6	"repodiff/repositories"
7	"repodiff/utils"
8)
9
10func DenormalizeData(config e.ApplicationConfig) error {
11	return interactors.NewTaskRunner().ExecuteFunctionsAsync(
12		[]func() error{
13			func() error { return denormalizeViewRecentProject(config) },
14			func() error { return denormalizeDiffRows(config) },
15			func() error { return denormalizeViewRecentCommit(config) },
16		},
17	)
18}
19
20func denormalizeViewRecentProject(config e.ApplicationConfig) error {
21	for _, target := range config.DiffTargets {
22		err := denormalizeViewRecentProjectForTarget(target)
23		if err != nil {
24			return err
25		}
26	}
27	return nil
28}
29
30func denormalizeViewRecentProjectForTarget(target e.DiffTarget) error {
31	mappedTarget, err := getMappedTarget(target)
32	if err != nil {
33		return err
34	}
35	denormalizeRepo, err := repositories.NewScopedDenormalizerRepository(target, mappedTarget)
36	if err != nil {
37		return err
38	}
39
40	projectRepo, err := repositories.NewProjectRepository(mappedTarget)
41	if err != nil {
42		return err
43	}
44
45	diffRows, err := projectRepo.GetMostRecentDifferentials()
46	if err != nil {
47		return err
48	}
49	return denormalizeRepo.DenormalizeToRecentView(diffRows)
50}
51
52func denormalizeDiffRows(config e.ApplicationConfig) error {
53	for _, target := range config.DiffTargets {
54		if err := denormalizeDiffRowsForTarget(target); err != nil {
55			return err
56		}
57	}
58	return nil
59}
60
61func denormalizeDiffRowsForTarget(target e.DiffTarget) error {
62	mappedTarget, err := getMappedTarget(target)
63	if err != nil {
64		return err
65	}
66	denormalizeRepo, err := repositories.NewScopedDenormalizerRepository(target, mappedTarget)
67	if err != nil {
68		return err
69	}
70
71	projectRepo, err := repositories.NewProjectRepository(mappedTarget)
72	if err != nil {
73		return err
74	}
75
76	diffRows, err := projectRepo.GetMostRecentDifferentials()
77	if err != nil {
78		return err
79	}
80	return denormalizeRepo.DenormalizeToChangesOverTime(diffRows)
81}
82
83func getMappedTarget(target e.DiffTarget) (e.MappedDiffTarget, error) {
84	sourceRepo, err := repositories.NewSourceRepository()
85	if err != nil {
86		return e.MappedDiffTarget{}, err
87	}
88	return sourceRepo.DiffTargetToMapped(target)
89}
90
91func denormalizeViewRecentCommit(config e.ApplicationConfig) error {
92	for _, target := range config.DiffTargets {
93		if err := denormalizeCommitRows(target); err != nil {
94			return err
95		}
96	}
97	return denormalizeViewRecentCommitGlobal()
98}
99
100func denormalizeViewRecentCommitGlobal() error {
101	denormalizeRepo, err := repositories.NewGlobalDenormalizerRepository()
102	if err != nil {
103		return err
104	}
105	if err := denormalizeRepo.DenormalizeToTopCommitter(); err != nil {
106		return err
107	}
108	return denormalizeRepo.DenormalizeToTopTechArea()
109}
110
111func denormalizeCommitRows(target e.DiffTarget) error {
112	mappedTarget, err := getMappedTarget(target)
113	if err != nil {
114		return err
115	}
116	denormalizeRepo, err := repositories.NewScopedDenormalizerRepository(target, mappedTarget)
117	if err != nil {
118		return err
119	}
120
121	commitRepo, err := repositories.NewCommitRepository(mappedTarget)
122	if err != nil {
123		return err
124	}
125	commitRows, err := commitRepo.GetMostRecentCommits()
126	if err != nil {
127		return err
128	}
129	commitToTimestamp, err := MaybeNullObjectCommitRepository(
130		mappedTarget,
131	).GetFirstSeenTimestamp(
132		extractCommitHashes(commitRows),
133		utils.TimestampSeconds(),
134	)
135	if err != nil {
136		return err
137	}
138
139	return denormalizeRepo.DenormalizeToRecentCommits(commitRows, commitToTimestamp)
140}
141
142func extractCommitHashes(commitRows []e.AnalyzedCommitRow) []string {
143	hashes := make([]string, len(commitRows))
144	for i, row := range commitRows {
145		hashes[i] = row.Commit
146	}
147	return hashes
148}
149