File: mappers.go

package info (click to toggle)
android-platform-tools 35.0.2-1~exp6
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 211,716 kB
  • sloc: cpp: 995,749; java: 290,495; ansic: 145,647; xml: 58,531; python: 39,608; sh: 14,500; javascript: 5,198; asm: 4,866; makefile: 3,115; yacc: 769; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (315 lines) | stat: -rw-r--r-- 7,316 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package mappers

import (
	"crypto/sha256"
	"database/sql"
	"encoding/hex"
	"fmt"
	"strconv"

	"github.com/pkg/errors"
	"github.com/satori/go.uuid"

	"repodiff/constants"
	e "repodiff/entities"
	"repodiff/interactors"
	"repodiff/utils"
)

const expectedDiffRowLen = 9
const expectedCommitRowLen = 5

func CSVLineToDiffRow(csvColumns []string) (*e.DiffRow, error) {
	if len(csvColumns) != expectedDiffRowLen {
		return nil, errors.New(fmt.Sprintf("Got %d columns but expected %d", len(csvColumns), expectedDiffRowLen))
	}
	intVals, err := batchToInts(csvColumns[4:]...)
	if err != nil {
		return nil, err
	}
	diffStatus, err := constants.GetStatusEnum(csvColumns[3])
	if err != nil {
		return nil, err
	}

	return &e.DiffRow{
		Date:                 csvColumns[0],
		DownstreamProject:    csvColumns[1],
		UpstreamProject:      csvColumns[2],
		DiffStatus:           diffStatus,
		FilesChanged:         intVals[0],
		LineInsertions:       intVals[1],
		LineDeletions:        intVals[2],
		LineChanges:          intVals[3],
		CommitsNotUpstreamed: intVals[4],
		DBInsertTimestamp:    0,
	}, nil
}

func CSVLineToCommitRow(csvColumns []string) (*e.CommitRow, error) {
	if len(csvColumns) != expectedCommitRowLen {
		return nil, errors.New(fmt.Sprintf("Got %d columns but expected %d", len(csvColumns), expectedCommitRowLen))
	}
	return &e.CommitRow{
		Date:              csvColumns[0],
		Commit:            csvColumns[1],
		DownstreamProject: csvColumns[2],
		Author:            csvColumns[3],
		Subject:           csvColumns[4],
	}, nil
}

func batchToInts(intStrings ...string) ([]int, error) {
	ints := make([]int, len(intStrings))
	for i, val := range intStrings {
		var err error
		ints[i], err = strconv.Atoi(val)
		if err != nil {
			return nil, errors.Wrap(err, fmt.Sprintf("Could not convert from %s", val))
		}
	}
	return ints, nil
}

func diffRowToDenormalizedCols(d e.AnalyzedDiffRow, rowIndex int) []interface{} {
	return []interface{}{
		rowIndex,
		d.Date,
		d.DownstreamProject,
		d.UpstreamProject,
		constants.StatusToDisplay[d.DiffStatus],
		d.FilesChanged,
		d.LineInsertions,
		d.LineDeletions,
		d.LineChanges,
		d.CommitsNotUpstreamed,
		constants.ProjectTypeToDisplay[d.Type],
	}
}

func commitRowToDenormalizedCols(commitRow e.AnalyzedCommitRow, firstSeen e.RepoTimestamp, rowIndex int) []interface{} {
	return []interface{}{
		rowIndex,
		commitRow.Commit,
		commitRow.DownstreamProject,
		commitRow.Author,
		commitRow.Subject,
		GetAuthorTechArea(commitRow.Author),
		constants.ProjectTypeToDisplay[commitRow.Type],
		utils.TimestampToDataStudioDatetime(firstSeen),
	}
}

func diffRowToPersistCols(d e.AnalyzedDiffRow, uuidBytes string, timestamp e.RepoTimestamp, rowIndex int) []interface{} {
	return []interface{}{
		timestamp,
		uuidBytes,
		rowIndex,
		d.DownstreamProject,
		d.UpstreamProject,
		d.DiffStatus,
		d.FilesChanged,
		d.LineInsertions,
		d.LineDeletions,
		d.LineChanges,
		d.CommitsNotUpstreamed,
		d.Type,
	}
}

func commitRowToPersistCols(c e.AnalyzedCommitRow, uuidBytes string, timestamp e.RepoTimestamp, rowIndex int) []interface{} {
	return []interface{}{
		timestamp,
		uuidBytes,
		rowIndex,
		c.Commit,
		c.DownstreamProject,
		c.Author,
		interactors.FilterNoUnicode(c.Subject),
		c.Type,
	}
}

func DiffRowsToPersistCols(diffRows []e.AnalyzedDiffRow, timestamp e.RepoTimestamp) [][]interface{} {
	uid := uuid.NewV4()

	rows := make([][]interface{}, len(diffRows))
	for i, diffRow := range diffRows {
		rows[i] = diffRowToPersistCols(
			diffRow,
			string(uid.Bytes()),
			timestamp,
			i,
		)
	}
	return rows
}

func DiffRowsToDenormalizedCols(diffRows []e.AnalyzedDiffRow) [][]interface{} {
	rows := make([][]interface{}, len(diffRows))
	for i, diffRow := range diffRows {
		rows[i] = diffRowToDenormalizedCols(
			diffRow,
			i,
		)
	}
	return rows
}

func CommitRowsToDenormalizedCols(commitRows []e.AnalyzedCommitRow, commitToTimestamp map[string]e.RepoTimestamp) [][]interface{} {
	rows := make([][]interface{}, len(commitRows))
	for i, commitRow := range commitRows {
		rows[i] = commitRowToDenormalizedCols(
			commitRow,
			commitToTimestamp[commitRow.Commit],
			i,
		)
	}
	return rows
}

func DiffRowsToAggregateChangesOverTime(diffRows []e.AnalyzedDiffRow) [][]interface{} {
	if len(diffRows) == 0 {
		return nil
	}
	cols := []interface{}{
		utils.TimestampToDataStudioDatetime(e.RepoTimestamp(diffRows[0].DBInsertTimestamp)),
		getSumOfAttribute(
			diffRows,
			func(d e.AnalyzedDiffRow) int {
				if d.DiffStatus == constants.StatusModified {
					return 1
				}
				return 0
			},
		),
		getSumOfAttribute(
			diffRows,
			func(d e.AnalyzedDiffRow) int {
				return d.LineChanges
			},
		),
		getSumOfAttribute(
			diffRows,
			func(d e.AnalyzedDiffRow) int {
				return d.FilesChanged
			},
		),
	}
	rows := [][]interface{}{
		cols,
	}
	return rows
}

func getSumOfAttribute(diffRows []e.AnalyzedDiffRow, getAttr func(e.AnalyzedDiffRow) int) int {
	var sum int
	for _, d := range diffRows {
		sum += getAttr(d)
	}
	return sum
}

func CommitRowsToPersistCols(commitRows []e.AnalyzedCommitRow, timestamp e.RepoTimestamp) [][]interface{} {
	uid := uuid.NewV4()

	rows := make([][]interface{}, len(commitRows))
	for i, commitRow := range commitRows {
		rows[i] = commitRowToPersistCols(
			commitRow,
			string(uid.Bytes()),
			timestamp,
			i,
		)
	}
	return rows
}

func SQLRowToDiffRow(iterRow *sql.Rows) (e.AnalyzedDiffRow, error) {
	var d e.AnalyzedDiffRow
	var uuidBytes []byte
	var rowIndex int
	err := iterRow.Scan(
		&d.DBInsertTimestamp,
		&uuidBytes,
		&rowIndex,
		&d.DownstreamProject,
		&d.UpstreamProject,
		&d.DiffStatus,
		&d.FilesChanged,
		&d.LineInsertions,
		&d.LineDeletions,
		&d.LineChanges,
		&d.CommitsNotUpstreamed,
		&d.Type,
	)
	d.Date = utils.TimestampToDate(e.RepoTimestamp(d.DBInsertTimestamp))
	return d, err
}

func SQLRowToCommitRow(iterRow *sql.Rows) (e.AnalyzedCommitRow, error) {
	var c e.AnalyzedCommitRow
	var uuidBytes []byte
	var rowIndex int
	var timestamp e.RepoTimestamp
	err := iterRow.Scan(
		&timestamp,
		&uuidBytes,
		&rowIndex,
		&c.Commit,
		&c.DownstreamProject,
		&c.Author,
		&c.Subject,
		&c.Type,
	)
	c.Date = utils.TimestampToDate(timestamp)
	return c, err
}

// SBL needs test coverage
func PrependMappedDiffTarget(target e.MappedDiffTarget, rowsOfCols [][]interface{}) [][]interface{} {
	remapped := make([][]interface{}, len(rowsOfCols))
	prefix := []interface{}{
		target.UpstreamTarget,
		target.DownstreamTarget,
	}
	for i, row := range rowsOfCols {
		remapped[i] = append(
			prefix,
			row...,
		)
	}
	return remapped
}

func AppendDiffTarget(target e.DiffTarget, rowsOfCols [][]interface{}) [][]interface{} {
	remapped := make([][]interface{}, len(rowsOfCols))
	suffix := []interface{}{
		target.Upstream.URL,
		target.Upstream.Branch,
		target.Downstream.URL,
		target.Downstream.Branch,
	}
	for i, row := range rowsOfCols {
		remapped[i] = append(
			row,
			suffix...,
		)
	}
	return remapped
}

func SHA256HexDigest(s string) string {
	byteArray := sha256.Sum256([]byte(s))
	return hex.EncodeToString(
		byteArray[:],
	)
}

func GetAuthorTechArea(authorEMail string) string {
	techAreaIndex, ok := constants.AuthorHashToTechIndex[SHA256HexDigest(authorEMail)]
	if !ok {
		return constants.TechAreaDisplay[constants.Unknown]
	}
	return constants.TechAreaDisplay[techAreaIndex]
}