Skip to content

Commit

Permalink
[fix]跨年Shards
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jul 31, 2024
1 parent 03814d1 commit af197ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
15 changes: 13 additions & 2 deletions XCode/Shards/TimeShardPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private ShardModel[] GetModels(DateTime start, DateTime end)
level = StatLevels.Hour;
}

// 根据不仅,把start调整到整点
// 根据步进,把start调整到整点
if (st.TotalDays >= 1)
start = start.Date;
else if (start.Hour >= 1)
Expand All @@ -272,13 +272,24 @@ private ShardModel[] GetModels(DateTime start, DateTime end)

// 根据时间步进级别调整时间,解决每月每年时间不固定的问题
if (level == StatLevels.Year)
{
dt = dt.AddYears(1);
dt = new DateTime(dt.Year, 1, 1);
}
else if (level == StatLevels.Month)
{
dt = dt.AddMonths(1);
dt = new DateTime(dt.Year, dt.Month, 1);
}
else if (level == StatLevels.Day)
dt = dt.AddDays(1);
{
dt = dt.AddDays(1).Date;
}
else if (level == StatLevels.Hour)
{
dt = dt.AddHours(1);
dt = dt.Date.AddHours(dt.Hour);
}
else
dt = dt.Add(st);
}
Expand Down
9 changes: 5 additions & 4 deletions XUnitTest.XCode/EntityTests/ShardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,18 @@ public void 跨年Shards()
var policy = new TimeShardPolicy(Log2._.CreateTime, Log2.Meta.Factory)
{
TablePolicy = "{0}_{1:yyyy}",
Step = TimeSpan.FromDays(365),
};

var start = "2024-05-29".ToDateTime();
var end = start.AddYears(1).AddDays(-7);
var start = "2023-12-31".ToDateTime();
var end = DateTime.Today;
var fi = policy.Field;
var where = fi >= start & fi < end;

var shards = policy.Shards(where);
Assert.NotNull(shards);
Assert.Equal(2, shards.Length);
Assert.Equal("Log2_2024", shards[0].TableName);
Assert.Equal("Log2_2025", shards[1].TableName);
Assert.Equal("Log2_2023", shards[0].TableName);
Assert.Equal("Log2_2024", shards[1].TableName);
}
}

0 comments on commit af197ce

Please sign in to comment.