@@ -9,7 +9,6 @@ import app.api.Api
99import app.model.Author
1010import app.model.Commit
1111import app.model.Fact
12- import app.model.LocalRepo
1312import app.model.Repo
1413import io.reactivex.Observable
1514import java.time.LocalDateTime
@@ -18,63 +17,92 @@ import java.time.ZoneOffset
1817/* *
1918 * CommitHasher hashes repository and uploads stats to server.
2019 */
21- class FactHasher (private val localRepo : LocalRepo ,
22- private val serverRepo : Repo = Repo (),
23- private val api : Api ) {
20+ class FactHasher (private val serverRepo : Repo = Repo (),
21+ private val api : Api ,
22+ private val emails : HashSet <String >) {
23+ private val fsDayWeek = hashMapOf<String , Array <Int >>()
24+ private val fsDayTime = hashMapOf<String , Array <Int >>()
25+ private val fsRepoDateStart = hashMapOf<String , Long >()
26+ private val fsRepoDateEnd = hashMapOf<String , Long >()
27+ private val fsRepoTeamSize = hashSetOf<String >()
2428
25- private fun postFactsToServer (facts : List <Fact >) {
26- if (facts.isNotEmpty()) {
27- api.postFacts(facts)
28- Logger .debug(" Sent ${facts.size} facts to server" )
29+ init {
30+ for (author in emails) {
31+ fsDayWeek.put(author, Array (7 ) { 0 })
32+ fsDayTime.put(author, Array (24 ) { 0 })
33+ fsRepoDateStart.put(author, - 1 )
34+ fsRepoDateEnd.put(author, - 1 )
2935 }
3036 }
3137
3238 fun updateFromObservable (observable : Observable <Commit >,
3339 onError : (Throwable ) -> Unit ) {
34- val factsDayWeek = hashMapOf<Author , Array <Int >>()
35- val factsDayTime = hashMapOf<Author , Array <Int >>()
36-
37- // TODO(anatoly): Filter hashing by email as in CommitHasher.
3840 observable
41+ .filter { commit -> emails.contains(commit.author.email) }
3942 .subscribe({ commit -> // OnNext.
4043 // Calculate facts.
41- val author = commit.author
42- val factDayWeek = factsDayWeek[author] ? : Array (7 ) { 0 }
43- val factDayTime = factsDayTime[author] ? : Array (24 ) { 0 }
44+ val email = commit.author.email
4445 val timestamp = commit.dateTimestamp
4546 val dateTime = LocalDateTime .ofEpochSecond(timestamp, 0 ,
4647 ZoneOffset .ofTotalSeconds(commit.dateTimeZoneOffset * 60 ))
48+
49+ // DayWeek.
50+ val factDayWeek = fsDayWeek[email] ? : Array (7 ) { 0 }
4751 // The value is numbered from 1 (Monday) to 7 (Sunday).
4852 factDayWeek[dateTime.dayOfWeek.value - 1 ] + = 1
53+ fsDayWeek[email] = factDayWeek
54+
55+ // DayTime.
56+ val factDayTime = fsDayTime[email] ? : Array (24 ) { 0 }
4957 // Hour from 0 to 23.
5058 factDayTime[dateTime.hour] + = 1
51- factsDayWeek[author] = factDayWeek
52- factsDayTime[author] = factDayTime
59+ fsDayTime[email] = factDayTime
60+
61+ // RepoDateStart.
62+ fsRepoDateStart[email] = timestamp
63+
64+ // RepoDateEnd.
65+ if ((fsRepoDateEnd[email] ? : - 1 ) == - 1L ) {
66+ fsRepoDateEnd[email] = timestamp
67+ }
68+
69+ // RepoTeamSize.
70+ fsRepoTeamSize.add(email)
5371 }, onError, { // OnComplete.
5472 try {
55- val facts = mutableListOf<Fact >()
56- factsDayTime.map { (author, list) ->
57- list.forEachIndexed { hour, count ->
58- if (count > 0 ) {
59- facts.add(Fact (serverRepo,
60- FactCodes .COMMITS_DAY_TIME , hour,
61- count.toDouble(), author))
62- }
63- }
64- }
65- factsDayWeek.map { (author, list) ->
66- list.forEachIndexed { day, count ->
67- if (count > 0 ) {
68- facts.add(Fact (serverRepo,
69- FactCodes .COMMITS_DAY_WEEK , day,
70- count.toDouble(), author))
71- }
72- }
73- }
74- postFactsToServer(facts)
73+ postFactsToServer(createFacts())
7574 } catch (e: Throwable ) {
7675 onError(e)
7776 }
7877 })
7978 }
79+
80+ private fun createFacts (): List <Fact > {
81+ val fs = mutableListOf<Fact >()
82+ emails.forEach { email ->
83+ val author = Author (email = email)
84+ fsDayTime[email]?.forEachIndexed { hour, count -> if (count > 0 ) {
85+ fs.add(Fact (serverRepo, FactCodes .COMMITS_DAY_TIME , hour,
86+ count.toString(), author))
87+ }}
88+ fsDayWeek[email]?.forEachIndexed { day, count -> if (count > 0 ) {
89+ fs.add(Fact (serverRepo, FactCodes .COMMITS_DAY_WEEK , day,
90+ count.toString(), author))
91+ }}
92+ fs.add(Fact (serverRepo, FactCodes .REPO_DATE_START , 0 ,
93+ fsRepoDateStart[email].toString(), author))
94+ fs.add(Fact (serverRepo, FactCodes .REPO_DATE_END , 0 ,
95+ fsRepoDateEnd[email].toString(), author))
96+ }
97+ fs.add(Fact (serverRepo, FactCodes .REPO_TEAM_SIZE , 0 ,
98+ fsRepoTeamSize.size.toString()))
99+ return fs
100+ }
101+
102+ private fun postFactsToServer (facts : List <Fact >) {
103+ if (facts.isNotEmpty()) {
104+ api.postFacts(facts)
105+ Logger .debug(" Sent ${facts.size} facts to server" )
106+ }
107+ }
80108}
0 commit comments