computeAvgKBFileBytes returns the average byte size of *.md files in kbDir. Returns 0 if the directory is missing or empty.
(kbDir string)
| 166 | // computeAvgKBFileBytes returns the average byte size of *.md files in kbDir. |
| 167 | // Returns 0 if the directory is missing or empty. |
| 168 | func computeAvgKBFileBytes(kbDir string) int64 { |
| 169 | entries, err := os.ReadDir(kbDir) |
| 170 | if err != nil { |
| 171 | return 0 |
| 172 | } |
| 173 | var total int64 |
| 174 | var count int |
| 175 | for _, e := range entries { |
| 176 | if e.IsDir() || !strings.HasSuffix(e.Name(), ".md") { |
| 177 | continue |
| 178 | } |
| 179 | fi, err := e.Info() |
| 180 | if err != nil { |
| 181 | continue |
| 182 | } |
| 183 | total += fi.Size() |
| 184 | count++ |
| 185 | } |
| 186 | if count == 0 { |
| 187 | return 0 |
| 188 | } |
| 189 | return total / int64(count) |
| 190 | } |
| 191 | |
| 192 | // fileSize returns the byte size of the file at path, or 0 if it does not exist. |
| 193 | func fileSize(path string) int64 { |