Files
bj_power/bj_power_mes/internal/backup/util.go
T

132 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package backup
import (
"crypto/rand"
"fmt"
"io"
"reflect"
"strings"
"time"
)
// randReader 安全随机源。
func randReader() io.Reader { return rand.Reader }
// sliceToAny 把任意切片([]*ent.Xxx)转成 []any,便于统一 JSON 序列化。
func sliceToAny(slice any) []any {
if slice == nil {
return nil
}
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice {
return nil
}
out := make([]any, v.Len())
for i := 0; i < v.Len(); i++ {
out[i] = v.Index(i).Interface()
}
return out
}
// complement 计算 [start,end] 在被若干区间并集覆盖后的缺失段(闭区间,秒)。
func complement(intervals [][2]int64, start, end int64) []MissingSeg {
if start > end {
return nil
}
// 归一化并排序
ivs := make([][2]int64, 0, len(intervals))
for _, iv := range intervals {
if iv[0] <= iv[1] && iv[1] >= start && iv[0] <= end {
ivs = append(ivs, [2]int64{maxInt64(iv[0], start), minInt64(iv[1], end)})
}
}
sortIntervals(ivs)
missing := make([]MissingSeg, 0)
cur := start
for _, iv := range ivs {
if iv[0] > cur {
missing = append(missing, MissingSeg{Start: cur, End: iv[0] - 1})
}
if iv[1] > cur {
cur = iv[1] + 1
}
}
if cur <= end {
missing = append(missing, MissingSeg{Start: cur, End: end})
}
return missing
}
func sortIntervals(ivs [][2]int64) {
for i := 0; i < len(ivs); i++ {
for j := i + 1; j < len(ivs); j++ {
if ivs[j][0] < ivs[i][0] {
ivs[i], ivs[j] = ivs[j], ivs[i]
}
}
}
}
func maxInt64(a, b int64) int64 {
if a > b {
return a
}
return b
}
func minInt64(a, b int64) int64 {
if a < b {
return a
}
return b
}
func formatMissing(segs []MissingSeg) string {
parts := make([]string, 0, len(segs))
for _, s := range segs {
parts = append(parts, fmt.Sprintf("%s ~ %s",
time.Unix(s.Start, 0).Format("2006-01-02"), time.Unix(s.End, 0).Format("2006-01-02")))
}
return strings.Join(parts, ";")
}
func summaryDeleted(deleted, skipped []ItemResult) string {
var b strings.Builder
for _, d := range deleted {
fmt.Fprintf(&b, "%s 删除 %d 行;", d.Name, d.Rows)
}
if len(skipped) > 0 {
b.WriteString(" 跳过共享附件(对端未备份):")
for _, s := range skipped {
fmt.Fprintf(&b, "%s %d 个;", s.Name, s.Rows)
}
}
return strings.TrimRight(b.String(), ";")
}
// buildIndexHTML 生成备份清单页(仅清单 + 校验,不内嵌预览)。
func buildIndexHTML(title string, manifest map[string]any, checksum string) string {
items, _ := manifest["items"].([]ItemResult)
var rows strings.Builder
for _, it := range items {
fmt.Fprintf(&rows, "<tr><td>%s</td><td>%d</td><td>%s</td></tr>\n", it.Name, it.Rows, humanBytes(it.Bytes))
}
if checksum == "" {
if c, ok := manifest["checksum"].(string); ok {
checksum = c
}
}
cs := checksum
if cs == "" {
cs = "(见 backup_record.checksum)"
}
return fmt.Sprintf(`<!doctype html><html lang="zh"><head><meta charset="utf-8">
<title>%s</title><style>body{font-family:-apple-system,Segoe UI,Roboto,sans-serif;margin:32px;color:#222}
h2{margin-bottom:8px}h3{margin-top:24px}table{border-collapse:collapse;margin-top:8px}td,th{border:1px solid #ddd;padding:6px 12px;text-align:left}
.meta{color:#666;font-size:13px}</style></head><body>
<h2>%s</h2>
<p class="meta">范围:%s ~ %s | 操作人:%s | 校验和(sha256):%s</p>
<h3>数据项</h3><table><tr><th>数据项</th><th>行数</th><th>字节</th></tr>%s</table>
<p class="meta">本文件仅作清单与校验用途,不内嵌任何数据预览。完整数据见包内各 data.jsonl 与 attachments/ 目录。</p>
</body></html>`, title, title, manifest["rangeStartText"], manifest["rangeEndText"], manifest["operator"], cs, rows.String())
}