Skip to content

Commit 8361499

Browse files
authored
wip: update facts (#38)
1 parent c83679c commit 8361499

File tree

8 files changed

+36
-30
lines changed

8 files changed

+36
-30
lines changed

src/main/kotlin/app/FactCodes.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package app
2+
3+
object FactCodes {
4+
val COMMITS_DAY_WEEK = 1
5+
val COMMITS_DAY_TIME = 2
6+
val LINE_LONGEVITY = 3
7+
val LINE_LONGEVITY_REPO = 4
8+
}

src/main/kotlin/app/FactKey.kt

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/kotlin/app/hashers/CodeLongevity.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
package app.hashers
55

6-
import app.FactKey
6+
import app.FactCodes
77
import app.Logger
88
import app.api.Api
9-
import app.config.Configurator
109
import app.model.Author
1110
import app.model.LocalRepo
1211
import app.model.Repo
@@ -150,7 +149,7 @@ class CodeLongevity(private val localRepo: LocalRepo,
150149
val repoAvg = if (repoTotal > 0) { repoSum / repoTotal } else 0
151150
val stats = mutableListOf<Fact>()
152151
stats.add(Fact(repo = serverRepo,
153-
key = FactKey.LINE_LONGEVITY_REPO,
152+
code = FactCodes.LINE_LONGEVITY_REPO,
154153
value = repoAvg.toDouble()))
155154
val repoAvgDays = repoAvg / secondsInDay
156155
Logger.info("Repo average code line age is $repoAvgDays days, "
@@ -160,7 +159,7 @@ class CodeLongevity(private val localRepo: LocalRepo,
160159
val total = totals[email] ?: 0
161160
val avg = if (total > 0) { sums[email]!! / total } else 0
162161
stats.add(Fact(repo = serverRepo,
163-
key = FactKey.LINE_LONGEVITY,
162+
code = FactCodes.LINE_LONGEVITY,
164163
value = avg.toDouble(),
165164
author = Author(email = email)))
166165
if (email == localRepo.author.email) {

src/main/kotlin/app/hashers/FactHasher.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
package app.hashers
55

6-
import app.FactKey
6+
import app.FactCodes
77
import app.Logger
88
import app.api.Api
99
import app.model.Author
@@ -59,7 +59,7 @@ class FactHasher(private val localRepo: LocalRepo,
5959
list.forEachIndexed { hour, count ->
6060
if (count > 0) {
6161
facts.add(Fact(serverRepo,
62-
FactKey.COMMITS_DAY_TIME + hour,
62+
FactCodes.COMMITS_DAY_TIME, hour,
6363
count.toDouble(), author))
6464
}
6565
}
@@ -68,7 +68,7 @@ class FactHasher(private val localRepo: LocalRepo,
6868
list.forEachIndexed { day, count ->
6969
if (count > 0) {
7070
facts.add(Fact(serverRepo,
71-
FactKey.COMMITS_DAY_WEEK + day,
71+
FactCodes.COMMITS_DAY_WEEK, day,
7272
count.toDouble(), author))
7373
}
7474
}

src/main/kotlin/app/hashers/RepoHasher.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api,
5757
observable.connect()
5858

5959
// TODO(anatoly): CodeLongevity hash from observable.
60-
CodeLongevity(localRepo, serverRepo, api, git)
60+
CodeLongevity(localRepo, serverRepo, api, git).update()
6161

6262
// Confirm hashing completion.
6363
postRepoToServer()

src/main/kotlin/app/model/Fact.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ import java.security.InvalidParameterException
99
*/
1010
data class Fact(
1111
var repo: Repo = Repo(),
12-
var key: String = "",
12+
var code: Int = 0,
13+
var key: Int = 0,
1314
var value: Double = 0.0,
1415
var author: Author = Author()
1516
) {
1617
@Throws(InvalidParameterException::class)
1718
constructor(proto: Protos.Fact) : this() {
1819
repo = Repo(rehash = proto.repoRehash)
1920
author = Author("", proto.email)
21+
code = proto.code
2022
key = proto.key
21-
value = proto.value
23+
value = proto.value1.toDoubleOrNull() ?: 0.0
2224
}
2325

2426
@Throws(InvalidProtocolBufferException::class)
@@ -30,8 +32,9 @@ data class Fact(
3032
return Protos.Fact.newBuilder()
3133
.setRepoRehash(repo.rehash)
3234
.setEmail(author.email)
35+
.setCode(code)
3336
.setKey(key)
34-
.setValue(value)
37+
.setValue1(value.toString())
3538
.build()
3639
}
3740

src/main/proto/sourcerer.proto

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ message CommitGroup {
5757

5858
// Key-value statistics for fun facts.
5959
message Fact {
60-
string key = 1;
61-
double value = 2;
62-
string email = 3;
63-
string repo_rehash = 4;
60+
int32 code = 1;
61+
int32 key = 2;
62+
string value1 = 3;
63+
string value2 = 4;
64+
string value3 = 5;
65+
string value4 = 6;
66+
string email = 7;
67+
string repo_rehash = 8;
6468
}
6569

6670
// Used to send multiple stats in one request.

src/test/kotlin/test/tests/hashers/FactHasherTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
package test.tests.hashers
55

6-
import app.FactKey
6+
import app.FactCodes
77
import app.api.MockApi
88
import app.hashers.CommitCrawler
99
import app.hashers.FactHasher
@@ -60,9 +60,9 @@ class FactHasherTest : Spek({
6060
.updateFromObservable(observable)
6161

6262
assertEquals(2, facts.size)
63-
assertTrue(facts.contains(Fact(repo, FactKey.COMMITS_DAY_TIME + 13,
63+
assertTrue(facts.contains(Fact(repo, FactCodes.COMMITS_DAY_TIME, 13,
6464
1.0, author1)))
65-
assertTrue(facts.contains(Fact(repo, FactKey.COMMITS_DAY_WEEK + 6,
65+
assertTrue(facts.contains(Fact(repo, FactCodes.COMMITS_DAY_WEEK, 6,
6666
1.0, author1)))
6767
}
6868

@@ -84,13 +84,13 @@ class FactHasherTest : Spek({
8484
.updateFromObservable(observable)
8585

8686
assertEquals(5, facts.size)
87-
assertTrue(facts.contains(Fact(repo, FactKey.COMMITS_DAY_TIME + 18,
87+
assertTrue(facts.contains(Fact(repo, FactCodes.COMMITS_DAY_TIME, 18,
8888
1.0, author2)))
89-
assertTrue(facts.contains(Fact(repo, FactKey.COMMITS_DAY_WEEK + 0,
89+
assertTrue(facts.contains(Fact(repo, FactCodes.COMMITS_DAY_WEEK, 0,
9090
1.0, author2)))
91-
assertTrue(facts.contains(Fact(repo, FactKey.COMMITS_DAY_TIME + 13,
91+
assertTrue(facts.contains(Fact(repo, FactCodes.COMMITS_DAY_TIME, 13,
9292
2.0, author1)))
93-
assertTrue(facts.contains(Fact(repo, FactKey.COMMITS_DAY_WEEK + 0,
93+
assertTrue(facts.contains(Fact(repo, FactCodes.COMMITS_DAY_WEEK, 0,
9494
1.0, author1)))
9595
}
9696

0 commit comments

Comments
 (0)