| 1 | // Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by a GPL license that can be found in the LICENSE file. |
| 3 | module main |
| 4 | |
| 5 | import time |
| 6 | |
| 7 | struct Commit { |
| 8 | mut: |
| 9 | id int @[primary; sql: serial] |
| 10 | author_id int |
| 11 | author string |
| 12 | hash string @[unique: 'commit'] |
| 13 | created_at int |
| 14 | repo_id int @[unique: 'commit'] |
| 15 | branch_id int @[unique: 'commit'] |
| 16 | message string |
| 17 | } |
| 18 | |
| 19 | fn (commit Commit) relative() string { |
| 20 | return time.unix(commit.created_at).relative() |
| 21 | } |
| 22 | |
| 23 | fn (commit Commit) short_hash() string { |
| 24 | if commit.hash.len <= 7 { |
| 25 | return commit.hash |
| 26 | } |
| 27 | return commit.hash[..7] |
| 28 | } |
| 29 | |
| 30 | fn (mut app App) commit_exists(repo_id int, branch_id int, hash string) bool { |
| 31 | count := sql app.db { |
| 32 | select count from Commit where repo_id == repo_id && branch_id == branch_id && hash == hash |
| 33 | } or { 0 } |
| 34 | return count > 0 |
| 35 | } |
| 36 | |
| 37 | fn (mut app App) add_commit(repo_id int, branch_id int, last_hash string, author string, author_id int, message string, date int) ! { |
| 38 | new_commit := Commit{ |
| 39 | author_id: author_id |
| 40 | author: author |
| 41 | hash: last_hash |
| 42 | created_at: date |
| 43 | repo_id: repo_id |
| 44 | branch_id: branch_id |
| 45 | message: message |
| 46 | } |
| 47 | |
| 48 | sql app.db { |
| 49 | insert new_commit into Commit |
| 50 | }! |
| 51 | } |
| 52 | |
| 53 | fn (mut app App) find_repo_commits_as_page(repo_id int, branch_id int, offset int) []Commit { |
| 54 | return sql app.db { |
| 55 | select from Commit where repo_id == repo_id && branch_id == branch_id order by created_at desc limit 35 offset offset |
| 56 | } or { []Commit{} } |
| 57 | } |
| 58 | |
| 59 | fn (mut app App) get_repo_commit_count(repo_id int, branch_id int) int { |
| 60 | return sql app.db { |
| 61 | select count from Commit where repo_id == repo_id && branch_id == branch_id |
| 62 | } or { 0 } |
| 63 | } |
| 64 | |
| 65 | fn (mut app App) find_repo_commit_by_hash(repo_id int, hash string) Commit { |
| 66 | commits := sql app.db { |
| 67 | select from Commit where repo_id == repo_id && hash == hash |
| 68 | } or { []Commit{} } |
| 69 | if commits.len == 1 { |
| 70 | return commits[0] |
| 71 | } |
| 72 | return Commit{} |
| 73 | } |
| 74 | |
| 75 | fn (mut app App) find_repo_last_commit(repo_id int, branch_id int) Commit { |
| 76 | commits := sql app.db { |
| 77 | select from Commit where repo_id == repo_id && branch_id == branch_id order by created_at desc limit 1 |
| 78 | } or { []Commit{} } |
| 79 | |
| 80 | if commits.len == 0 { |
| 81 | return Commit{} |
| 82 | } |
| 83 | |
| 84 | return commits.first() |
| 85 | } |
| 86 | |
| 87 | fn (app App) find_repo_last_commit_time(repo_id int) int { |
| 88 | commits := sql app.db { |
| 89 | select from Commit where repo_id == repo_id order by created_at desc limit 1 |
| 90 | } or { return 0 } |
| 91 | if commits.len == 0 { |
| 92 | return 0 |
| 93 | } |
| 94 | return commits[0].created_at |
| 95 | } |
| 96 | |
| 97 | const activity_weeks = 12 |
| 98 | |
| 99 | fn (app App) get_repo_activity_buckets(repo_id int) []int { |
| 100 | week_seconds := 7 * 24 * 3600 |
| 101 | now := int(time.now().unix()) |
| 102 | cutoff := now - activity_weeks * week_seconds |
| 103 | commits := sql app.db { |
| 104 | select from Commit where repo_id == repo_id && created_at >= cutoff |
| 105 | } or { []Commit{} } |
| 106 | mut buckets := []int{len: activity_weeks} |
| 107 | for c in commits { |
| 108 | idx := (c.created_at - cutoff) / week_seconds |
| 109 | if idx >= 0 && idx < activity_weeks { |
| 110 | buckets[idx]++ |
| 111 | } |
| 112 | } |
| 113 | return buckets |
| 114 | } |
| 115 | |
| 116 | // get_user_daily_activity returns commit counts per day for the given user |
| 117 | // over the past `days` days. Index 0 is the oldest day, index `days-1` is today. |
| 118 | fn (app App) get_user_daily_activity(user_id int, days int) []int { |
| 119 | day_seconds := 24 * 3600 |
| 120 | now := time.now() |
| 121 | // Anchor to the start of today (local), so today is always the last bucket. |
| 122 | today_start := i64(time.new(year: now.year, month: now.month, day: now.day).unix()) |
| 123 | cutoff := int(today_start) - (days - 1) * day_seconds |
| 124 | commits := sql app.db { |
| 125 | select from Commit where author_id == user_id && created_at >= cutoff |
| 126 | } or { []Commit{} } |
| 127 | mut buckets := []int{len: days} |
| 128 | for c in commits { |
| 129 | idx := (c.created_at - cutoff) / day_seconds |
| 130 | if idx >= 0 && idx < days { |
| 131 | buckets[idx]++ |
| 132 | } |
| 133 | } |
| 134 | return buckets |
| 135 | } |
| 136 | |