Skip to content

Commit

Permalink
add pike protection
Browse files Browse the repository at this point in the history
  • Loading branch information
makamekm committed Jun 10, 2020
1 parent ceb3405 commit 7e246e8
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/app/Dashboard/DashboardService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface DashboardState {
stats: ReturnType<IpcHandler["GET_STATS_DATA"]>;
isLoading: boolean;
limit: number;
calcMaxValue: () => void;
load: () => Promise<void>;
}

Expand Down Expand Up @@ -212,6 +213,34 @@ export const DashboardService = createService<DashboardState>(
)
: [];
},
calcMaxValue() {
let data: typeof state.repositoriesStats[0] = [];
if (state.mode === "repository") {
data = state.repositoriesStats[state.name];
} else if (state.mode === "team") {
data = state.teamStats[state.name];
} else if (state.mode === "user") {
data = state.userStats[state.name];
}
let max = 0;
let total = 0;
const sorted = data
.map(({ value }) => {
max = max > value ? max : value;
total += value;
return value;
})
.sort((a, b) => b - a);
const avg = total / data.length;
let result = 0;
sorted.forEach((value) => {
if (!result && value < avg) {
result = value * 1.1;
}
});
state.maxValue = result;
state.maxValueDelay = state.maxValue;
},
load: async () => {
if (!state.authService.isAuthenticated) {
return;
Expand Down Expand Up @@ -246,6 +275,7 @@ export const DashboardService = createService<DashboardState>(
);
await state.repositoryUserService.load();
await state.messageService.load();
state.calcMaxValue();
state.isLoading = false;
},
}));
Expand All @@ -258,8 +288,8 @@ export const DashboardService = createService<DashboardState>(
useOnChange(state, "limit", state.load);
useOnChange(state.authService, "isAuthenticated", state.load);
useDelay(state, "maxValue", "maxValueDelay", 1000);
useSimpleSyncLocalStorage(state, "maxValue", "maxValue");
useSimpleSyncLocalStorage(state, "maxValueDelay", "maxValueDelay");
// useSimpleSyncLocalStorage(state, "maxValue", "maxValue");
// useSimpleSyncLocalStorage(state, "maxValueDelay", "maxValueDelay");
React.useEffect(() => ipc.channels.ON_DRIVE_UPDATE(state.load));
React.useEffect(() => ipc.channels.ON_COLLECT_FINISH(state.load));
React.useEffect(() => ipc.channels.ON_CONFIG_UPDATE_FINISHED(state.load));
Expand Down
2 changes: 2 additions & 0 deletions src/app/Dashboard/RepositoryDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export const RepositoryDashboard = observer(() => {
<List height={"250px"} />
) : (
<BarActivities
maxValue={state.maxValueDelay}
height={250}
limit={state.limit}
data={state.repositoriesStats[repositoryName] || []}
Expand All @@ -178,6 +179,7 @@ export const RepositoryDashboard = observer(() => {
<List height={"250px"} />
) : (
<LineActivities
maxValue={state.maxValueDelay}
names={state.users}
height={250}
limit={state.limit}
Expand Down
2 changes: 2 additions & 0 deletions src/app/Dashboard/TeamDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export const TeamDashboard = observer(() => {
<List height={"250px"} />
) : (
<BarActivities
maxValue={state.maxValueDelay}
height={250}
limit={state.limit}
data={state.teamStats[teamName] || []}
Expand All @@ -184,6 +185,7 @@ export const TeamDashboard = observer(() => {
<List height={"250px"} />
) : (
<LineActivities
maxValue={state.maxValueDelay}
names={state.users}
height={250}
limit={state.limit}
Expand Down
2 changes: 2 additions & 0 deletions src/app/Dashboard/UserDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export const UserDashboard = observer(() => {
<List height={"250px"} />
) : (
<BarActivities
maxValue={state.maxValueDelay}
height={250}
limit={state.limit}
data={state.userStats[userName] || []}
Expand All @@ -184,6 +185,7 @@ export const UserDashboard = observer(() => {
<List height={"250px"} />
) : (
<LineActivities
maxValue={state.maxValueDelay}
names={state.repositories}
height={250}
limit={state.limit}
Expand Down
3 changes: 3 additions & 0 deletions src/components/Plots/BarActivities.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ export const BarActivities = ({
className,
data,
limit,
maxValue,
}: {
height?: number;
limit: number;
className?: string;
maxValue?: number;
data: {
day: string;
value: number;
Expand Down Expand Up @@ -100,6 +102,7 @@ export const BarActivities = ({
}}
/>
<YAxis
domain={maxValue ? [0, (max) => Math.min(max, maxValue)] : undefined}
tickLine={{
fill: colors["500"],
strokeWidth: 1,
Expand Down
3 changes: 3 additions & 0 deletions src/components/Plots/LineActivities.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ export const LineActivities = ({
names,
data,
limit,
maxValue,
}: {
height?: number;
limit: number;
className?: string;
names: string[];
maxValue?: number;
data: ({
day: string;
} & any)[];
Expand Down Expand Up @@ -105,6 +107,7 @@ export const LineActivities = ({
}}
/>
<YAxis
domain={maxValue ? [0, (max) => Math.min(max, maxValue)] : undefined}
tickLine={{
fill: colors["500"],
strokeWidth: 1,
Expand Down

0 comments on commit 7e246e8

Please sign in to comment.