Skip to content

Commit

Permalink
Merge pull request #68 from deven-org/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
pixari authored Mar 21, 2023
2 parents 01ab759 + 738e6e6 commit 1f2622b
Show file tree
Hide file tree
Showing 29 changed files with 436 additions and 52 deletions.
2 changes: 1 addition & 1 deletion netlify/functions/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const handler: Handler = async (event: HandlerEvent) => {
try {
const eventSignature = event.headers["x-github-event"] || "unknown";
const body = getEventBody(event);
const metrics = await collectMetricsHandler({ eventSignature, ...body });
await collectMetricsHandler({ eventSignature, ...body });
return {
statusCode: 200,
body: "success",
Expand Down
6 changes: 4 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@types/jest": "^29.4.1",
"@types/node": "^18.13.0",
"@typescript-eslint/eslint-plugin": "^5.55.0",
"cli-table3": "^0.6.3",
"deven-documentation-skeleton": "^2.0.0",
"eslint": "^8.36.0",
"husky": "^8.0.0",
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jest.mock("../core/logger", () => ({
error: jest.fn(),
complete: jest.fn(),
success: jest.fn(),
pending: jest.fn(),
skip: jest.fn(),
},
}));

Expand Down
2 changes: 2 additions & 0 deletions src/core/__tests__/collectMetrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jest.mock("../logger", () => ({
error: jest.fn(),
complete: jest.fn(),
success: jest.fn(),
pending: jest.fn(),
skip: jest.fn(),
},
}));

Expand Down
12 changes: 5 additions & 7 deletions src/core/__tests__/storeData.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import octokit from "../../core/octokit";
import "../../core/collectMetrics";
import "../../core/addSignature";
import {
DataEvent,
DataEventSignature,
EnhancedDataEvent,
} from "../../interfaces";
import { DataEventSignature, EnhancedDataEvent } from "../../interfaces";
import { handler } from "../../handler";
import "../logger";

Expand All @@ -19,6 +15,8 @@ jest.mock("../logger", () => ({
error: jest.fn(),
complete: jest.fn(),
success: jest.fn(),
pending: jest.fn(),
skip: jest.fn(),
},
}));

Expand Down Expand Up @@ -102,7 +100,7 @@ describe("storeData", () => {
"eyJkYXRhRXZlbnRTaWduYXR1cmUiOiJ3b3JrZmxvdy1qb2IiLCJjcmVhdGVkX2F0IjoxMDAsIm91dHB1dCI6eyJmb28iOiJmb28iLCJiYXIiOiJiYXIifSwib3duZXIiOiJvd25lciIsInJlcG8iOiJyZXBvIn0=",
message: "auto(data): add workflow-job for owner/repo",
owner: "deven-org",
path: "raw-data/100.json",
path: "raw-data/owner/repo/100.json",
repo: "telemetry-data",
author: {
email: "author_email",
Expand All @@ -120,7 +118,7 @@ describe("storeData", () => {
"eyJkYXRhRXZlbnRTaWduYXR1cmUiOiJ3b3JrZmxvdy1qb2IiLCJjcmVhdGVkX2F0IjoxMDAsIm91dHB1dCI6eyJmb28iOiJmb28iLCJiYXIiOiJiYXIifSwib3duZXIiOiJvd25lciIsInJlcG8iOiJyZXBvIn0=",
message: "auto(data): add workflow-job for owner/repo",
owner: "deven-org",
path: "raw-data/100.json",
path: "raw-data/owner/repo/100.json",
repo: "telemetry-data",
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/core/addSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const addSignature = (data: any): Promise<DataEvent> => {
? res(signedDataEvent)
: rej(
getRejectionReason({
level: "warn",
level: "skip",
message: LogWarnings.signingEventSignatureNotRecognized,
})
);
Expand Down
31 changes: 22 additions & 9 deletions src/core/collectMetrics.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
import { getRejectionReason } from ".";
import { logger } from "./logger";
import { clone, omit } from "ramda";
import { getRejectionReason, logger } from ".";
import { clone, keys } from "ramda";
import metricsConditions from "../metricsConditions";
import Table from "cli-table3";

import { LogErrors, LogInfos } from "../shared/logMessages";
import { LogSuccess, LogWarnings } from "../shared/logMessages";
import { DataEvent, EnhancedDataEvent } from "../interfaces";

export const collectMetrics = async (
dataEvent: DataEvent
): Promise<EnhancedDataEvent[]> => {
logger.info(LogInfos.startCollectingMetrics);

const collectedMetrics: EnhancedDataEvent[] = [];

for (const [condition, collect] of metricsConditions) {
const clonedDataEvent = clone(dataEvent);
if (condition(clonedDataEvent)) {
collectedMetrics.push(omit(["payload"], await collect(clonedDataEvent)));
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { payload, ...metrics } = (await collect(
clonedDataEvent
)) as DataEvent;

const table = new Table({
style: { head: ["cyan"] },
head: [`Metric for ${metrics.dataEventSignature}`],
colWidths: [40],
});
for (const k of keys(metrics)) {
table.push([k]);
}
collectedMetrics.push(metrics);
logger.success(LogSuccess.metricsCollected, metrics.dataEventSignature);
logger.info("\n" + table.toString());
}
}

if (collectedMetrics.length === 0) {
throw getRejectionReason({
level: "error",
message: LogErrors.collectMetricsSignatureNotRecognized,
level: "skip",
message: LogWarnings.collectMetricsSignatureNotRecognized,
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/createDataEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function createDataEvent<T extends DataEventSignature>(
): DataEvent<T> {
return {
...dataEvent,
created_at: moment().unix(),
created_at: moment().valueOf(),
repo: "",
owner: "",
};
Expand Down
2 changes: 1 addition & 1 deletion src/core/errorCatcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { logger } from "./logger";

export interface ErrorForCatcher {
level: "error" | "warn";
level: "error" | "warn" | "skip";
message: string;
}

Expand Down
10 changes: 9 additions & 1 deletion src/core/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { Signale, SignaleOptions } from "signale";

const options: SignaleOptions = {
stream: process.stdout,
types: {
skip: {
badge: "*",
color: "yellow",
label: "skip",
logLevel: "info",
},
},
};

export const logger = new Signale();
export const logger = new Signale(options);
7 changes: 4 additions & 3 deletions src/core/storeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ export const storeData = async (
const resolvedEnhancedDataEvents = await Promise.all(enhancedDataEvents);

for (const data of resolvedEnhancedDataEvents) {
logger.info(
logger.pending(
`Pushing file to repo: ${process.env.REPO_PATH}/${data.created_at}.json`
);

//const actionType = data.output?.action ? ` - ${data.output.action} ` : "";

try {
await octokit.request("PUT /repos/{owner}/{repo}/contents/{path}", {
owner: process.env.REPO_OWNER as string,
repo: process.env.REPO_NAME as string,
path: `${process.env.REPO_PATH}/${data.created_at}.json`,
path: `${process.env.REPO_PATH}/${data.owner}/${data.repo}/${data.created_at}.json`,
message: `auto(data): add ${data.dataEventSignature} for ${data.owner}/${data.repo}`,
committer: {
name: process.env.COMMITTER_NAME as string,
Expand All @@ -38,6 +40,5 @@ export const storeData = async (
console.log(e);
}
}

return resolvedEnhancedDataEvents;
};
Loading

0 comments on commit 1f2622b

Please sign in to comment.