-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package pl.allegro.tech.hermes.api; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.Objects; | ||
|
||
public class Stats { | ||
private final TopicStats topicStats; | ||
private final SubscriptionStats subscriptionStats; | ||
|
||
@JsonCreator | ||
public Stats(@JsonProperty("topicStats") TopicStats topicStats, @JsonProperty("subscriptionStats") SubscriptionStats subscriptionStats) { | ||
Check warning on line 13 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/Stats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/Stats.java#L13 <com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck>
Raw output
|
||
this.topicStats = topicStats; | ||
this.subscriptionStats = subscriptionStats; | ||
} | ||
|
||
public TopicStats getTopicStats() { | ||
return topicStats; | ||
} | ||
|
||
public SubscriptionStats getSubscriptionStats() { | ||
return subscriptionStats; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
Check warning on line 28 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/Stats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/Stats.java#L28 <com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck>
Raw output
|
||
if (o == null || getClass() != o.getClass()) return false; | ||
Check warning on line 29 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/Stats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/Stats.java#L29 <com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck>
Raw output
|
||
Stats stats = (Stats) o; | ||
return Objects.equals(topicStats, stats.topicStats) && Objects.equals(subscriptionStats, stats.subscriptionStats); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(topicStats, subscriptionStats); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Stats{" + | ||
Check warning on line 41 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/Stats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/Stats.java#L41 <com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck>
Raw output
|
||
"topicStats=" + topicStats + | ||
Check warning on line 42 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/Stats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/Stats.java#L42 <com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck>
Raw output
|
||
", subscriptionStats=" + subscriptionStats + | ||
Check warning on line 43 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/Stats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/Stats.java#L43 <com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck>
Raw output
|
||
'}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package pl.allegro.tech.hermes.api; | ||
|
||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.Objects; | ||
|
||
public class SubscriptionStats { | ||
private final long subscriptionCount; | ||
private final long trackingEnabledSubscriptionCount; | ||
private final long avroSubscriptionCount; | ||
|
||
@JsonCreator | ||
public SubscriptionStats( | ||
@JsonProperty("subscriptionCount") long subscriptionCount, | ||
@JsonProperty("trackingEnabledSubscriptionCount") long trackingEnabledSubscriptionCount, | ||
@JsonProperty("avroSubscriptionCount") long avroSubscriptionCount) { | ||
this.subscriptionCount = subscriptionCount; | ||
this.trackingEnabledSubscriptionCount = trackingEnabledSubscriptionCount; | ||
this.avroSubscriptionCount = avroSubscriptionCount; | ||
} | ||
|
||
public long getSubscriptionCount() { | ||
return subscriptionCount; | ||
} | ||
|
||
public long getTrackingEnabledSubscriptionCount() { | ||
return trackingEnabledSubscriptionCount; | ||
} | ||
|
||
public long getAvroSubscriptionCount() { | ||
return avroSubscriptionCount; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
Check warning on line 38 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/SubscriptionStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/SubscriptionStats.java#L38 <com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck>
Raw output
|
||
if (o == null || getClass() != o.getClass()) return false; | ||
Check warning on line 39 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/SubscriptionStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/SubscriptionStats.java#L39 <com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck>
Raw output
|
||
SubscriptionStats that = (SubscriptionStats) o; | ||
return subscriptionCount == that.subscriptionCount && trackingEnabledSubscriptionCount == that.trackingEnabledSubscriptionCount && avroSubscriptionCount == that.avroSubscriptionCount; | ||
Check warning on line 41 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/SubscriptionStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/SubscriptionStats.java#L41 <com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck>
Raw output
|
||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(subscriptionCount, trackingEnabledSubscriptionCount, avroSubscriptionCount); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SubscriptionStats{" + | ||
Check warning on line 51 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/SubscriptionStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/SubscriptionStats.java#L51 <com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck>
Raw output
|
||
"subscriptionCount=" + subscriptionCount + | ||
Check warning on line 52 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/SubscriptionStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/SubscriptionStats.java#L52 <com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck>
Raw output
|
||
", trackingEnabledSubscriptionCount=" + trackingEnabledSubscriptionCount + | ||
Check warning on line 53 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/SubscriptionStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/SubscriptionStats.java#L53 <com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck>
Raw output
|
||
", avroSubscriptionCount=" + avroSubscriptionCount + | ||
Check warning on line 54 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/SubscriptionStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/SubscriptionStats.java#L54 <com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck>
Raw output
|
||
'}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package pl.allegro.tech.hermes.api; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.Objects; | ||
|
||
public class TopicStats { | ||
private final long topicCount; | ||
private final long ackAllTopicCount; | ||
private final long trackingEnabledTopicCount; | ||
private final long avroTopicCount; | ||
|
||
@JsonCreator | ||
public TopicStats(@JsonProperty("topicCount") long topicCount, | ||
@JsonProperty("ackAllTopicCount") long ackAllTopicCount, | ||
@JsonProperty("trackingEnabledTopicCount") long trackingEnabledTopicCount, | ||
@JsonProperty("avroTopicCount") long avroTopicCount) { | ||
this.topicCount = topicCount; | ||
this.ackAllTopicCount = ackAllTopicCount; | ||
this.trackingEnabledTopicCount = trackingEnabledTopicCount; | ||
this.avroTopicCount = avroTopicCount; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
Check warning on line 27 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java#L27 <com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck>
Raw output
|
||
if (o == null || getClass() != o.getClass()) return false; | ||
Check warning on line 28 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java#L28 <com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck>
Raw output
|
||
TopicStats that = (TopicStats) o; | ||
return topicCount == that.topicCount && ackAllTopicCount == that.ackAllTopicCount && trackingEnabledTopicCount == that.trackingEnabledTopicCount && avroTopicCount == that.avroTopicCount; | ||
Check warning on line 30 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java#L30 <com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck>
Raw output
|
||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(topicCount, ackAllTopicCount, trackingEnabledTopicCount, avroTopicCount); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "TopicStats{" + | ||
Check warning on line 40 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java#L40 <com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck>
Raw output
|
||
"topicCount=" + topicCount + | ||
Check warning on line 41 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java#L41 <com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck>
Raw output
|
||
", ackAllTopicCount=" + ackAllTopicCount + | ||
Check warning on line 42 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java#L42 <com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck>
Raw output
|
||
", trackingEnabledTopicCount=" + trackingEnabledTopicCount + | ||
Check warning on line 43 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java#L43 <com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck>
Raw output
|
||
", avroTopicCount=" + avroTopicCount + | ||
Check warning on line 44 in hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java GitHub Actions / checkstyle-hermes-api[checkstyle-hermes-api] hermes-api/src/main/java/pl/allegro/tech/hermes/api/TopicStats.java#L44 <com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck>
Raw output
|
||
'}'; | ||
} | ||
|
||
public long getTopicCount() { | ||
return topicCount; | ||
} | ||
|
||
public long getAckAllTopicCount() { | ||
return ackAllTopicCount; | ||
} | ||
|
||
public long getTrackingEnabledTopicCount() { | ||
return trackingEnabledTopicCount; | ||
} | ||
|
||
public long getAvroTopicCount() { | ||
return avroTopicCount; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package pl.allegro.tech.hermes.api.endpoints; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import pl.allegro.tech.hermes.api.Stats; | ||
|
||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; | ||
|
||
@Path("stats") | ||
public interface StatsEndpoint { | ||
@GET | ||
@Produces(APPLICATION_JSON) | ||
Stats getStats(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export interface Stats { | ||
topicStats: TopicStats; | ||
subscriptionStats: SubscriptionStats; | ||
} | ||
|
||
export interface TopicStats { | ||
topicCount: number; | ||
ackAllTopicCount: number; | ||
trackingEnabledTopicCount: number; | ||
avroTopicCount: number; | ||
} | ||
|
||
export interface SubscriptionStats { | ||
subscriptionCount: number; | ||
trackingEnabledSubscriptionCount: number; | ||
avroSubscriptionCount: number; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { afterEach } from 'vitest'; | ||
import { fetchStatsErrorHandler, fetchStatsHandler } from '@/mocks/handlers'; | ||
import { setupServer } from 'msw/node'; | ||
import { statsResponse } from '@/dummy/stats'; | ||
import { useStats } from '@/composables/use-stats/useStats'; | ||
import { waitFor } from '@testing-library/vue'; | ||
|
||
describe('useStats', () => { | ||
const server = setupServer(fetchStatsHandler({ stats: statsResponse })); | ||
|
||
afterEach(() => { | ||
server.resetHandlers(); | ||
}); | ||
|
||
it('should fetch stats details from Hermes API', async () => { | ||
// given | ||
server.listen(); | ||
|
||
// when | ||
const { stats, loading, error } = useStats(); | ||
|
||
// then | ||
expect(loading.value).toBeTruthy(); | ||
|
||
await waitFor(() => { | ||
expect(loading.value).toBeFalsy(); | ||
expect(error.value.fetchError).toBeNull(); | ||
const { | ||
topicCount, | ||
ackAllTopicCount, | ||
ackAllTopicShare, | ||
trackingEnabledTopicCount, | ||
trackingEnabledTopicShare, | ||
avroTopicCount, | ||
avroTopicShare, | ||
subscriptionCount, | ||
trackingEnabledSubscriptionCount, | ||
trackingEnabledSubscriptionShare, | ||
avroSubscriptionCount, | ||
avroSubscriptionShare, | ||
} = stats.value!!; | ||
|
||
expect(topicCount).toEqual(statsResponse.topicStats.topicCount); | ||
expect(ackAllTopicCount).toEqual( | ||
statsResponse.topicStats.ackAllTopicCount, | ||
); | ||
expect(ackAllTopicShare).toBeCloseTo( | ||
share( | ||
statsResponse.topicStats.ackAllTopicCount, | ||
statsResponse.topicStats.topicCount, | ||
), | ||
0.001, | ||
); | ||
expect(avroTopicCount).toEqual(statsResponse.topicStats.avroTopicCount); | ||
expect(avroTopicShare).toBeCloseTo( | ||
share( | ||
statsResponse.topicStats.avroTopicCount, | ||
statsResponse.topicStats.topicCount, | ||
), | ||
0.001, | ||
); | ||
expect(trackingEnabledTopicCount).toEqual( | ||
statsResponse.topicStats.trackingEnabledTopicCount, | ||
); | ||
expect(trackingEnabledTopicShare).toBeCloseTo( | ||
share( | ||
statsResponse.topicStats.trackingEnabledTopicCount, | ||
statsResponse.topicStats.topicCount, | ||
), | ||
0.001, | ||
); | ||
expect(subscriptionCount).toEqual( | ||
statsResponse.subscriptionStats.subscriptionCount, | ||
); | ||
expect(avroSubscriptionCount).toEqual( | ||
statsResponse.subscriptionStats.avroSubscriptionCount, | ||
); | ||
expect(avroSubscriptionShare).toBeCloseTo( | ||
share( | ||
statsResponse.subscriptionStats.avroSubscriptionCount, | ||
statsResponse.subscriptionStats.subscriptionCount, | ||
), | ||
0.001, | ||
); | ||
expect(trackingEnabledSubscriptionCount).toEqual( | ||
statsResponse.subscriptionStats.trackingEnabledSubscriptionCount, | ||
); | ||
expect(trackingEnabledSubscriptionShare).toBeCloseTo( | ||
share( | ||
statsResponse.subscriptionStats.trackingEnabledSubscriptionCount, | ||
statsResponse.subscriptionStats.subscriptionCount, | ||
), | ||
0.001, | ||
); | ||
}); | ||
}); | ||
|
||
it('should set error to true on stats endpoint failure', async () => { | ||
// given | ||
server.use(fetchStatsErrorHandler({ errorCode: 500 })); | ||
server.listen(); | ||
|
||
// when | ||
const { loading, error } = useStats(); | ||
|
||
// then | ||
await waitFor(() => { | ||
expect(loading.value).toBe(false); | ||
expect(error.value.fetchError).not.toBeNull(); | ||
}); | ||
}); | ||
}); | ||
|
||
function share(numerator: number, denominator: number): number { | ||
return (numerator / denominator) * 100; | ||
} |