Skip to content

Commit

Permalink
feat: support YearGroupBySupplier / YearMonthGroupBySupplier / `Y…
Browse files Browse the repository at this point in the history
…earMonthDayGroupBySupplier`.
  • Loading branch information
Ahoo-Wang committed May 16, 2024
1 parent 0acf606 commit 8c3e12c
Show file tree
Hide file tree
Showing 11 changed files with 441 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.cosid.segment.grouped.date;

import me.ahoo.cosid.segment.grouped.GroupBySupplier;
import me.ahoo.cosid.segment.grouped.GroupedKey;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

public abstract class AbstractDateGroupBySupplier<D extends TemporalAccessor> implements GroupBySupplier {
protected final DateTimeFormatter formatter;

public AbstractDateGroupBySupplier(DateTimeFormatter formatter) {
this.formatter = formatter;
}

abstract D now();

abstract LocalDateTime lastTimestamp(D date);

@Override
public GroupedKey get() {
D nowDate = now();
String key = formatter.format(nowDate);

LocalDateTime lastTs = lastTimestamp(nowDate);
ZoneId currentZone = ZoneId.systemDefault();
long ttlAt = lastTs.atZone(currentZone).toInstant().toEpochMilli() / 1000;
return new GroupedKey(key, ttlAt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,32 @@
* limitations under the License.
*/

package me.ahoo.cosid.segment.grouped;
package me.ahoo.cosid.segment.grouped.date;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Year;
import java.time.ZoneId;

public enum DateGroupBySupplier implements GroupBySupplier {
YEAR {
@Override
public GroupedKey get() {
Year nowYear = Year.now();
String key = nowYear.toString();

LocalDateTime lastTs = LocalDateTime.of(LocalDate.MAX.withYear(nowYear.getValue()), LocalTime.MAX);
ZoneId currentZone = ZoneId.systemDefault();
long ttlAt = lastTs.atZone(currentZone).toInstant().toEpochMilli() / 1000;
return new GroupedKey(key, ttlAt);
}
import java.time.format.DateTimeFormatter;

public class YearGroupBySupplier extends AbstractDateGroupBySupplier<Year> {

public YearGroupBySupplier(DateTimeFormatter formatter) {
super(formatter);
}

public YearGroupBySupplier(String pattern) {
this(DateTimeFormatter.ofPattern(pattern));
}

@Override
Year now() {
return Year.now();
}

@Override
LocalDateTime lastTimestamp(Year date) {
return LocalDateTime.of(LocalDate.MAX.withYear(date.getValue()), LocalTime.MAX);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.cosid.segment.grouped.date;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class YearMonthDayGroupBySupplier extends AbstractDateGroupBySupplier<LocalDate> {

public YearMonthDayGroupBySupplier(DateTimeFormatter formatter) {
super(formatter);
}

public YearMonthDayGroupBySupplier(String pattern) {
this(DateTimeFormatter.ofPattern(pattern));
}

@Override
LocalDate now() {
return LocalDate.now();
}

@Override
LocalDateTime lastTimestamp(LocalDate date) {
return LocalDateTime.of(date, LocalTime.MAX);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.cosid.segment.grouped.date;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;


public class YearMonthGroupBySupplier extends AbstractDateGroupBySupplier<YearMonth> {
public YearMonthGroupBySupplier(DateTimeFormatter formatter) {
super(formatter);
}

public YearMonthGroupBySupplier(String pattern) {
this(DateTimeFormatter.ofPattern(pattern));
}

@Override
YearMonth now() {
return YearMonth.now();
}

@Override
LocalDateTime lastTimestamp(YearMonth date) {
LocalDate lastDate = LocalDate.MAX.withYear(date.getYear()).withMonth(date.getMonthValue());
return LocalDateTime.of(lastDate, LocalTime.MAX);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,32 @@
* limitations under the License.
*/

package me.ahoo.cosid.segment.grouped;
package me.ahoo.cosid.segment.grouped.date;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import me.ahoo.cosid.segment.grouped.GroupedKey;
import org.junit.jupiter.api.Test;

class DateGroupBySupplierTest {

import java.time.Year;
import java.time.format.DateTimeFormatter;

class YearGroupBySupplierTest {

@Test
void year() {
assertThat(DateGroupBySupplier.YEAR.get(), notNullValue());
String pattern = "yyyy";
YearGroupBySupplier supplier = new YearGroupBySupplier(pattern);
GroupedKey groupedKey = supplier.get();
assertThat(groupedKey.getKey(), equalTo(Year.now().format(DateTimeFormatter.ofPattern(pattern))));
}

@Test
void yearTwoPattern() {
String pattern = "yy";
YearGroupBySupplier supplier = new YearGroupBySupplier(pattern);
GroupedKey groupedKey = supplier.get();
assertThat(groupedKey.getKey(), equalTo(Year.now().format(DateTimeFormatter.ofPattern(pattern))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.cosid.segment.grouped.date;

import me.ahoo.cosid.segment.grouped.GroupedKey;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

import static org.junit.jupiter.api.Assertions.*;

class YearMonthDayGroupBySupplierTest {

@Test
void get() {
String pattern = "yyyyMMdd";
YearMonthDayGroupBySupplier supplier = new YearMonthDayGroupBySupplier(pattern);
GroupedKey groupedKey = supplier.get();
assertEquals(groupedKey.getKey(), LocalDate.now().format(DateTimeFormatter.ofPattern(pattern)));
}

@Test
void getYyMm() {
String pattern = "yyMMDD";
YearMonthDayGroupBySupplier supplier = new YearMonthDayGroupBySupplier(pattern);
GroupedKey groupedKey = supplier.get();
assertEquals(groupedKey.getKey(), LocalDate.now().format(DateTimeFormatter.ofPattern(pattern)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.cosid.segment.grouped.date;

import me.ahoo.cosid.segment.grouped.GroupedKey;
import org.junit.jupiter.api.Test;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

import static org.junit.jupiter.api.Assertions.*;

class YearMonthGroupBySupplierTest {

@Test
void get() {
String pattern = "yyyy-MM";
YearMonthGroupBySupplier supplier = new YearMonthGroupBySupplier(pattern);
GroupedKey groupedKey = supplier.get();
assertEquals(groupedKey.getKey(), YearMonth.now().format(DateTimeFormatter.ofPattern(pattern)));
}

@Test
void getYyMm() {
String pattern = "yyMM";
YearMonthGroupBySupplier supplier = new YearMonthGroupBySupplier(pattern);
GroupedKey groupedKey = supplier.get();
assertEquals(groupedKey.getKey(), YearMonth.now().format(DateTimeFormatter.ofPattern(pattern)));
}
}
Loading

0 comments on commit 8c3e12c

Please sign in to comment.