| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Net;
- using System.Threading.Tasks;
- using CSRedis;
- using dodohold.core;
- using System.Text.Json;
- using YunhuiKit;
- namespace molilian.core
- {
- public partial class TracksCore
- {
- private const string RedisPrefix = ":tracks_v123";
- private const int DailyExpireSeconds = 40 * 86400;
- private const int HourlyExpireSeconds = 7 * 86400; // keep a week of hourly buckets
- private const string DefaultDimensionValue = "";
- private const int LinkCacheExpireSeconds = 30 * 86400;
- private const string TrackRequestLogKey = ":tracks:request:logs";
- private static readonly HashSet<string> SupportEventTypes = new(StringComparer.OrdinalIgnoreCase) { "expose", "click" };
- internal static int ExposeTrackId = 2;
- internal static int ClickTrackId = 1;
- /// <summary>
- /// 生成一个监测链接(仅返回 path,不包含域名),同时落地到 track_links
- /// </summary>
- public static Task<TrackLinkDTO> CreateLinkAsync(string eventType, string typename, string scene, string uniqueId, string description = "", bool forceNew = false)
- {
- eventType = NormalizeEventType(eventType);
- typename = Normalize(typename);
- scene = NormalizeOrAll(scene);
- uniqueId = NormalizeOrAll(uniqueId);
- description = Normalize(description);
- if (!SupportEventTypes.Contains(eventType))
- {
- return Task.FromResult<TrackLinkDTO>(null);
- }
- string cacheKey = $"{RedisPrefix}:link:{eventType}:{typename}:{scene}:{uniqueId}";
- if (!forceNew)
- {
- try
- {
- int cachedId = RedisHelper.Get<int>(cacheKey);
- if (cachedId > 0)
- {
- var cachedLink = RedisHelper.Get<TrackLinkDTO>(GetLinkIdCacheKey(cachedId));
- if (cachedLink != null) return Task.FromResult(cachedLink);
- }
- }
- catch
- {
- // ignore cache errors
- }
- }
- try
- {
- using var conn = DBContext.GetOpenConnection();
- TrackLinkDTO exist = null;
- if (!forceNew)
- {
- exist = new DBContext.Table(conn, "track_links")
- .Get<TrackLinkDTO>("event_type=@event_type AND platform=@platform AND scene=@scene AND unique_id=@unique_id",
- new { event_type = eventType, platform = typename, scene, unique_id = uniqueId });
- }
- if (exist != null)
- {
- exist.description = description;
- string path = BuildPath(exist.id, uniqueId);
- if (string.IsNullOrEmpty(exist.path) || !exist.path.Contains("track_id"))
- {
- exist.path = path;
- new DBContext.Table(conn, "track_links")
- .Add("path", path)
- .Add("description", description)
- .Add("update_time", DateTime.Now)
- .Where("id=@id", new { exist.id })
- .Update();
- }
- _ = RedisHelper.Set(cacheKey, exist.id, LinkCacheExpireSeconds);
- _ = RedisHelper.Set(GetLinkIdCacheKey(exist.id), exist, LinkCacheExpireSeconds);
- return Task.FromResult(exist);
- }
- var item = new TrackLinkDTO
- {
- event_type = eventType,
- platform = typename,
- scene = scene,
- unique_id = uniqueId,
- path = string.Empty,
- description = description,
- create_time = DateTime.Now,
- update_time = DateTime.Now
- };
- var id = conn.Insert(item);
- if (id != null && int.TryParse(id.ToString(), out var linkId))
- {
- item.id = linkId;
- item.path = BuildPath(linkId, uniqueId);
- new DBContext.Table(conn, "track_links")
- .Add("path", item.path)
- .Add("description", description)
- .Add("update_time", DateTime.Now)
- .Where("id=@id", new { item.id })
- .Update();
- }
- _ = RedisHelper.Set(cacheKey, item.id, LinkCacheExpireSeconds);
- _ = RedisHelper.Set(GetLinkIdCacheKey(item.id), item, LinkCacheExpireSeconds);
- return Task.FromResult(item);
- }
- catch (Exception ex)
- {
- _ = new LoggerLibrary("TracksCore", "CreateLink")
- .Info(ex.Message, ex.StackTrace)
- .SaveAsync();
- return Task.FromResult<TrackLinkDTO>(null);
- }
- }
- /// <summary>
- /// 曝光/点击触发:计入 Redis,失败不影响返回
- /// </summary>
- public static Task<bool> TrackAsync(TrackLinkDTO link)
- {
- string dateStr = DateTime.Now.ToString("yyyyMMdd");
- string hourStr = DateTime.Now.ToString("yyyyMMddHH");
- var metrics = new List<(string key, string indexKey, string indexValue, int expire)>
- {
- ($"{RedisPrefix}:daily:{link.event_type}:{link.id}:{dateStr}", $"{RedisPrefix}:daily:index:{dateStr}:track", $"{link.event_type}|{link.id}", DailyExpireSeconds),
- ($"{RedisPrefix}:hour:{link.event_type}:{link.id}:{hourStr}", $"{RedisPrefix}:hour:index:{hourStr}:track", $"{link.event_type}|{link.id}", HourlyExpireSeconds),
- };
- try
- {
- foreach (var metric in metrics)
- {
- RedisHelper.IncrBy(metric.key);
- RedisHelper.Expire(metric.key, metric.expire);
- RedisHelper.SAdd(metric.indexKey, metric.indexValue);
- RedisHelper.Expire(metric.indexKey, metric.expire);
- }
- return Task.FromResult(true);
- }
- catch (Exception ex)
- {
- _ = new LoggerLibrary("TracksCore", "TrackAsync")
- .Info(ex.Message, ex.StackTrace)
- .SaveAsync();
- return Task.FromResult(false);
- }
- }
- /// <summary>
- /// 日报:拉取昨日 Redis 计数并落地到 track_daily_report(默认统计昨天,可传 reportDate)
- /// </summary>
- public static async Task<int> FlushDailyAsync(DateTime targetDate)
- {
- string dateStr = targetDate.ToString("yyyyMMdd");
- string indexKey = $"{RedisPrefix}:daily:index:{dateStr}:track";
- var indexMembers = await RedisHelper.SMembersAsync<string>(indexKey) ?? [];
- if (indexMembers == null || indexMembers.Length == 0) return 0;
- int rows = 0;
- using var conn = DBContext.GetOpenConnection();
- foreach (var member in indexMembers)
- {
- var parts = member.Split('|');
- if (parts.Length != 2) continue;
- string eventType = NormalizeEventType(parts[0]);
- if (!SupportEventTypes.Contains(eventType)) continue;
- if (!int.TryParse(parts[1], out var trackId) || trackId <= 0) continue;
- string countKey = $"{RedisPrefix}:daily:{eventType}:{trackId}:{dateStr}";
- int total = await RedisHelper.GetAsync<int>(countKey);
- if (total <= 0) continue;
- var exist = new DBContext.Table(conn, "track_daily_report")
- .Fields("id")
- .Get<dynamic>("report_date=@report_date AND event_type=@event_type AND track_link_id=@trackId",
- new { report_date = targetDate, event_type = eventType, trackId });
- var update = new DBContext.Table(conn, "track_daily_report")
- .Add("event_count", total)
- .Add("track_link_id", trackId)
- .Add("platform", string.Empty)
- .Add("scene", string.Empty)
- .Add("unique_id", string.Empty)
- .Add("update_time", DateTime.Now);
- if (exist == null)
- {
- update.Add("report_date", targetDate)
- .Add("event_type", eventType)
- .Add("create_time", DateTime.Now)
- .Create();
- }
- else
- {
- update.Where("id=@id", new { exist.id }).Update();
- }
- rows++;
- }
- return rows;
- }
- public static string BuildPath(int trackId, TkDataDTO result)
- {
- if (result == null || result.parse_type != "dp") return string.Empty;
- string unique_id = $"1|{result.itemId}_{result.mktId}";
- return BuildPath(trackId, unique_id);
- }
- public static string BuildPath(int trackId, JdDataDTO result)
- {
- if (result == null || result.parse_type != "dp") return string.Empty;
- string unique_id = $"13|{result.shortLinkurl.UrlEncode()}";
- return BuildPath(trackId, unique_id);
- }
- public static string BuildPath(int trackId, PddDataDTO result)
- {
- if (result == null || result.parse_type != "dp") return string.Empty;
- string unique_id = $"9|{result.shortLinkurl.UrlEncode()}";
- return BuildPath(trackId, unique_id);
- }
- public static string BuildPath(int trackId, string unique_id = "")
- {
- return $"https://api.molilian.com/tracks/track?track_id={trackId}&unique_id={unique_id}";
- }
- public static string GetLinkCacheKey(string eventType, string typename, string scene, string uniqueId)
- {
- eventType = NormalizeEventType(eventType);
- typename = Normalize(typename);
- scene = NormalizeOrAll(scene);
- uniqueId = NormalizeOrAll(uniqueId);
- return $"{RedisPrefix}:link:{eventType}:{typename}:{scene}:{uniqueId}";
- }
- public static string GetLinkIdCacheKey(int trackId)
- {
- return $"{RedisPrefix}:linkid:{trackId}";
- }
- public static async Task<TrackLinkDTO> GetLinkByIdAsync(int trackId)
- {
- if (trackId <= 0) return null;
- string cacheKey = GetLinkIdCacheKey(trackId);
- try
- {
- var cached = RedisHelper.Get<TrackLinkDTO>(cacheKey);
- if (cached != null) return cached;
- }
- catch { }
- try
- {
- var item = new DBContext.Table("track_links").Get<TrackLinkDTO>("id=@id", new { id = trackId });
- if (item != null)
- {
- _ = RedisHelper.Set(cacheKey, item, LinkCacheExpireSeconds);
- }
- return item;
- }
- catch
- {
- return null;
- }
- }
- public static Task<bool> LogTrackRequestAsync(TrackRequestLogDTO dto)
- {
- try
- {
- dto.event_type = NormalizeEventType(dto.event_type);
- dto.platform = Normalize(dto.platform);
- dto.typename = Normalize(dto.typename);
- dto.scene = NormalizeOrAll(dto.scene);
- dto.unique_id = NormalizeOrAll(dto.unique_id);
- dto.ip = Normalize(dto.ip);
- dto.user_agent = Normalize(dto.user_agent);
- dto.referer = Normalize(dto.referer);
- dto.create_time = DateTime.Now;
- RedisHelper.RPush(TrackRequestLogKey, dto);
- return Task.FromResult(true);
- }
- catch
- {
- return Task.FromResult(false);
- }
- }
- public static async Task<int> InsertTrackRequestLogAsync(int limit, YunhuiKit.RedisClient redis)
- {
- int count = 0;
- try
- {
- using var conn = DBContext.GetOpenConnection();
- for (int i = 0; i < limit; i++)
- {
- var entity = await redis.LPopAsync<TrackRequestLogDTO>(TrackRequestLogKey);
- if (entity == null) break;
- try
- {
- if (entity.create_time == default) entity.create_time = DateTime.Now;
- conn.Insert(entity);
- count++;
- }
- catch
- {
- // ignore malformed item
- }
- }
- }
- catch
- {
- return count;
- }
- return count;
- }
- private static string Normalize(string value)
- {
- return (value ?? string.Empty).Trim();
- }
- private static string NormalizeOrAll(string value)
- {
- var result = Normalize(value);
- return string.IsNullOrEmpty(result) ? DefaultDimensionValue : result;
- }
- private static string NormalizeEventType(string value)
- {
- return Normalize(value).ToLowerInvariant();
- }
- private static int GetTrackLinkId(IDbConnection conn, string eventType, string platform, string scene, string uniqueId)
- {
- try
- {
- var record = new DBContext.Table(conn, "track_links")
- .Fields("id")
- .Get<TrackLinkDTO>("event_type=@event_type AND platform=@platform AND scene=@scene AND unique_id=@unique_id",
- new { event_type = eventType, platform, scene, unique_id = uniqueId });
- if (record == null) return 0;
- return record.id;
- }
- catch
- {
- return 0;
- }
- }
- }
- }
|