forked from olivia-ai/olivia
/
coverage.go
151 lines (122 loc) 路 4.92 KB
/
coverage.go
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
143
144
145
146
147
148
149
150
151
package analysis
import (
"encoding/json"
"net/http"
"github.com/NerdDoc/server/locales"
"github.com/NerdDoc/server/modules"
"github.com/NerdDoc/server/util"
)
var (
defaultModules []modules.Module
defaultIntents []Intent
defaultMessages []util.Message
)
// LocaleCoverage is the element for the coverage of each language
type LocaleCoverage struct {
Tag string `json:"locale_tag"`
Language string `json:"language"`
Coverage Coverage `json:"coverage"`
}
// Coverage is the coverage for a single language which contains the coverage details of each section
type Coverage struct {
Modules CoverageDetails `json:"modules"`
Intents CoverageDetails `json:"intents"`
Messages CoverageDetails `json:"messages"`
}
// CoverageDetails are the details of items not covered and the coverage percentage
type CoverageDetails struct {
NotCovered []string `json:"not_covered"`
Coverage int `json:"coverage"`
}
// GetCoverage encodes the coverage of each language in json
func GetCoverage(writer http.ResponseWriter, _ *http.Request) {
allowedHeaders := "Accept, Content-Type, Content-Length, Accept-Encoding, Authorization,Olivia-Token"
writer.Header().Set("Access-Control-Allow-Origin", "*")
writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
writer.Header().Set("Access-Control-Allow-Headers", allowedHeaders)
writer.Header().Set("Access-Control-Expose-Headers", "Authorization")
defaultMessages, defaultIntents, defaultModules =
util.GetMessages("en"), GetIntents("en"), modules.GetModules("en")
var coverage []LocaleCoverage
// Calculate coverage for each language
for _, locale := range locales.Locales {
if locale.Tag == "en" {
continue
}
coverage = append(coverage, LocaleCoverage{
Tag: locale.Tag,
Language: locales.GetNameByTag(locale.Tag),
Coverage: Coverage{
Modules: getModuleCoverage(locale.Tag),
Intents: getIntentCoverage(locale.Tag),
Messages: getMessageCoverage(locale.Tag),
},
})
}
json.NewEncoder(writer).Encode(coverage)
}
// getMessageCoverage returns an array of not covered messages and the percentage of message that
// aren't translated in the given locale.
func getMessageCoverage(locale string) CoverageDetails {
var notCoveredMessages []string
// Iterate through the default messages which are the english ones to verify if a message isn't
// translated in the given locale.
for _, defaultMessage := range defaultMessages {
message := util.GetMessageByTag(defaultMessage.Tag, locale)
// Add the current module tag to the list of not-covered-modules
if message.Tag != defaultMessage.Tag {
notCoveredMessages = append(notCoveredMessages, defaultMessage.Tag)
}
}
// Calculate the percentage of modules that aren't translated in the given locale
coverage := calculateCoverage(len(notCoveredMessages), len(defaultMessages))
return CoverageDetails{
NotCovered: notCoveredMessages,
Coverage: coverage,
}
}
// getIntentCoverage returns an array of not covered intents and the percentage of intents that aren't
// translated in the given locale.
func getIntentCoverage(locale string) CoverageDetails {
var notCoveredIntents []string
// Iterate through the default intents which are the english ones to verify if an intent isn't
// translated in the given locale.
for _, defaultIntent := range defaultIntents {
intent := GetIntentByTag(defaultIntent.Tag, locale)
// Add the current module tag to the list of not-covered-modules
if intent.Tag != defaultIntent.Tag {
notCoveredIntents = append(notCoveredIntents, defaultIntent.Tag)
}
}
// Calculate the percentage of modules that aren't translated in the given locale
coverage := calculateCoverage(len(notCoveredIntents), len(defaultModules))
return CoverageDetails{
NotCovered: notCoveredIntents,
Coverage: coverage,
}
}
// getModuleCoverage returns an array of not covered modules and the percentage of modules that aren't
// translated in the given locale.
func getModuleCoverage(locale string) CoverageDetails {
var notCoveredModules []string
// Iterate through the default modules which are the english ones to verify if a module isn't
// translated in the given locale.
for _, defaultModule := range defaultModules {
module := modules.GetModuleByTag(defaultModule.Tag, locale)
// Add the current module tag to the list of not-covered-modules
if module.Tag != defaultModule.Tag {
notCoveredModules = append(notCoveredModules, defaultModule.Tag)
}
}
// Calculate the percentage of modules that aren't translated in the given locale
coverage := calculateCoverage(len(notCoveredModules), len(defaultModules))
return CoverageDetails{
NotCovered: notCoveredModules,
Coverage: coverage,
}
}
// calculateCoverage returns the coverage calculated with the given length of not covered
// items and the default items length
func calculateCoverage(notCoveredLength, defaultLength int) int {
return 100 * (defaultLength - notCoveredLength) / defaultLength
}