forked from niuniu/llm-intelligence
feat(intraday): add discovery and verification watch pipeline
This commit is contained in:
111
scripts/intraday_discovery_common.go
Normal file
111
scripts/intraday_discovery_common.go
Normal file
@@ -0,0 +1,111 @@
|
||||
//go:build llm_script
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func loadIntradayEnv() {
|
||||
for _, path := range []string{".env.local", ".env"} {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
key, value, ok := strings.Cut(line, "=")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
key = strings.TrimSpace(key)
|
||||
value = strings.Trim(strings.TrimSpace(value), `"'`)
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := os.LookupEnv(key); exists {
|
||||
continue
|
||||
}
|
||||
_ = os.Setenv(key, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func intradayDefaultDSN() string {
|
||||
if dsn := os.Getenv("DATABASE_URL"); dsn != "" {
|
||||
return dsn
|
||||
}
|
||||
return "postgres://long@/llm_intelligence?host=/var/run/postgresql"
|
||||
}
|
||||
|
||||
func intradayDateValue() string {
|
||||
if value := strings.TrimSpace(os.Getenv("REPORT_DATE")); value != "" {
|
||||
return value
|
||||
}
|
||||
return time.Now().Format("2006-01-02")
|
||||
}
|
||||
|
||||
func discoveryTimeoutFromEnv() time.Duration {
|
||||
raw := strings.TrimSpace(os.Getenv("INTRADAY_DISCOVERY_TIMEOUT_SEC"))
|
||||
if raw == "" {
|
||||
return 20 * time.Second
|
||||
}
|
||||
var seconds int
|
||||
if _, err := fmt.Sscanf(raw, "%d", &seconds); err != nil || seconds <= 0 {
|
||||
return 20 * time.Second
|
||||
}
|
||||
return time.Duration(seconds) * time.Second
|
||||
}
|
||||
|
||||
func normalizeIntradayEventType(value string) string {
|
||||
switch strings.TrimSpace(strings.ToLower(value)) {
|
||||
case "price_cut":
|
||||
return "price_cut"
|
||||
case "price_increase":
|
||||
return "price_increase"
|
||||
case "official_release":
|
||||
return "official_release"
|
||||
case "promo_campaign":
|
||||
return "promo_campaign"
|
||||
case "leak_or_rumor":
|
||||
return "leak_or_rumor"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeWord(value string) string {
|
||||
value = strings.ToLower(strings.TrimSpace(value))
|
||||
value = strings.ReplaceAll(value, "_", "-")
|
||||
re := regexp.MustCompile(`[^a-z0-9\-]+`)
|
||||
value = re.ReplaceAllString(value, "-")
|
||||
value = strings.Trim(value, "-")
|
||||
if value == "" {
|
||||
return "unknown"
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func dedupeStrings(values []string) []string {
|
||||
seen := map[string]struct{}{}
|
||||
result := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
trimmed := strings.TrimSpace(value)
|
||||
if trimmed == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := seen[trimmed]; exists {
|
||||
continue
|
||||
}
|
||||
seen[trimmed] = struct{}{}
|
||||
result = append(result, trimmed)
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user