| 1 | module main |
| 2 | |
| 3 | import veb |
| 4 | |
| 5 | struct LangStat { |
| 6 | id int @[primary; sql: serial] |
| 7 | repo_id int @[unique: 'langstat'] |
| 8 | name string @[unique: 'langstat'] |
| 9 | lines_count int |
| 10 | pct int // out of 1000 |
| 11 | color string |
| 12 | } |
| 13 | |
| 14 | const min_lang_summary_pct = 5 // pct is stored in tenths of a percent, so 5 is 0.5%. |
| 15 | |
| 16 | const test_lang_stats = [ |
| 17 | LangStat{ |
| 18 | name: 'V' |
| 19 | pct: 989 |
| 20 | lines_count: 96657 |
| 21 | color: '#5d87bd' |
| 22 | }, |
| 23 | LangStat{ |
| 24 | name: 'JavaScript' |
| 25 | lines_count: 1131 |
| 26 | color: '#f1e05a' |
| 27 | pct: 11 |
| 28 | }, |
| 29 | ] |
| 30 | |
| 31 | fn (app App) add_lang_stat(lang_stat LangStat) ! { |
| 32 | sql app.db { |
| 33 | insert lang_stat into LangStat |
| 34 | }! |
| 35 | } |
| 36 | |
| 37 | pub fn (l &LangStat) pct_html() veb.RawHtml { |
| 38 | x := f64(l.pct) / 10.0 |
| 39 | sloc := if l.lines_count < 1000 { |
| 40 | l.lines_count.str() |
| 41 | } else { |
| 42 | (l.lines_count / 1000).str() + 'k' |
| 43 | } |
| 44 | |
| 45 | return '<span>${x}%</span> <span class=lang-stat-loc>${sloc} loc</span>' |
| 46 | } |
| 47 | |
| 48 | pub fn (app App) find_repo_lang_stats(repo_id int) []LangStat { |
| 49 | stats := sql app.db { |
| 50 | select from LangStat where repo_id == repo_id order by pct desc |
| 51 | } or { return []LangStat{} } |
| 52 | return stats.filter(it.pct >= min_lang_summary_pct) |
| 53 | } |
| 54 | |
| 55 | fn (app App) remove_repo_lang_stats(repo_id int) ! { |
| 56 | sql app.db { |
| 57 | delete from LangStat where repo_id == repo_id |
| 58 | }! |
| 59 | } |
| 60 | |