summaryrefslogtreecommitdiff
path: root/web/lib/ui/taginfo.rb
blob: 0f09122d3f4b35ed1f3a22406790fb49a0d34f3c (plain)
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
# web/lib/ui/taginfo.rb
class Taginfo < Sinatra::Base

    def count_texts(data)
        data.values.map{ |value|
            if value.is_a?(Hash)
                count_texts(value)
            else
                1
            end
        }.inject(0, :+)
    end

    def i18n_walk(line, level, path, en, other)
        out = ''
        en.keys.sort.each do |key|
            name = path.sub(/^\./, '') + '.' + key
            name.sub!(/^\./, '')
            if en[key].class == Hash
                if other.nil?
                    out += line.call(level, "<b>#{key}</b>", name, '', '<span style="color: red;">MISSING</span>')
                else
                    out += line.call(level, "<b>#{key}</b>", name, '', '')
                    out += i18n_walk(line, level+1, path + '.' + key, en[key], other[key])
                end
            else
                if other.nil?|| ! other[key]
                    out += line.call(level, key, name, en[key], '<span style="color: red;">MISSING</span>')
                else
                    out += line.call(level, key, name, en[key], other[key])
                end 
            end
        end
        out
    end

    def get_commit
        begin
            @commit = `git rev-parse HEAD`.chop
            @commit_date = Time.parse(`git show -s --format=%ci HEAD`.chop).utc.iso8601
        rescue
            @commit = 'unknown'
            @commit_date = 'unknown'
        end
    end

    get '/taginfo' do
        get_commit
        erb :'taginfo/index'
    end

    get '/taginfo/version' do
        get_commit
        "#{@commit} #{@commit_date}"
    end

    get '/taginfo/status' do
        content_type 'text/plain'
        age_in_days = DateTime.now() - DateTime.parse(@data_until)
        if age_in_days.to_f > 1.5
            halt 400, "data_too_old\n"
        else
            return "ok\n"
        end
    end

    get '/taginfo/config' do
        @title = 'Configuration'
        @section = 'taginfo'
        @section_title = t.taginfo.meta

        @config = TaginfoConfig.sanitized_config

        erb :'taginfo/config'
    end

    get '/taginfo/translations' do
        @title = 'Translations Overview'
        @section = 'taginfo'
        @section_title = t.taginfo.meta

        @num_texts = {}
        r18n.available_locales.each do |lang|
            data = YAML.load_file("i18n/#{lang.code}.yml")
            @num_texts[lang.code] = count_texts(data)
        end

        erb :'taginfo/translations'
    end

    get '/taginfo/i18n' do
        @title = 'Translations of taginfo texts'
        @section = 'taginfo'
        @section_title = t.taginfo.meta
        @lang = params[:lang] || 'de'
        @i18n_en   = YAML.load_file("i18n/en.yml")
        begin
            @i18n_lang = YAML.load_file("i18n/#{@lang}.yml")
        rescue
            @error = "Unknown language: #{@lang}"
        end 

        c = 'even'
        @line = lambda { |level, key, name, en, other|    
            c = (c == '') ? 'even': ''
            "<tr><td class='#{c}' style='padding-left: #{ level * 16 + 6 }px;'><span title='#{ name }'>#{ key }</span></td><td class='#{c}'>#{ en }</td><td class='#{c}'>#{ other }</td></tr>"
        }

        javascript "#{ r18n.locale.code }/taginfo/i18n"
        erb :'taginfo/i18n'
    end

    get '/taginfo/apidoc' do
        @title = t.taginfo.apidoc
        @section = 'taginfo'
        @section_title = t.taginfo.meta
        erb :'taginfo/apidoc'
    end

    get '/taginfo/projects' do
        @title = t.taginfo.projects
        @section = 'taginfo'
        @section_title = t.taginfo.meta

        @projects = @db.select("SELECT * FROM projects.projects ORDER BY lower(name)").execute()

        erb :'taginfo/projects'
    end

    get %r{/taginfo/projects/([a-z_]+)/error_log} do |id|
        @title = "Error log for project #{h(id)}"
        @section = 'taginfo'
        @section_title = t.taginfo.meta

        @data = @db.select("SELECT name, error_log FROM projects.projects").
            condition("id = ?", id).
            execute()[0]

        erb :'taginfo/project_error_log'
    end

end