@backstage-community/plugin-copilot-backend 0.1.5 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/README.md +37 -17
- package/config.d.ts +5 -1
- package/dist/client/GithubClient.cjs.js +21 -1
- package/dist/client/GithubClient.cjs.js.map +1 -1
- package/dist/db/DatabaseHandler.cjs.js +19 -14
- package/dist/db/DatabaseHandler.cjs.js.map +1 -1
- package/dist/service/router.cjs.js +33 -22
- package/dist/service/router.cjs.js.map +1 -1
- package/dist/service/validation/schema.cjs.js +27 -0
- package/dist/service/validation/schema.cjs.js.map +1 -0
- package/dist/service/validation/validateQuery.cjs.js +17 -0
- package/dist/service/validation/validateQuery.cjs.js.map +1 -0
- package/dist/task/EnterpriseTask.cjs.js +53 -0
- package/dist/task/EnterpriseTask.cjs.js.map +1 -0
- package/dist/task/EnterpriseTeamTask.cjs.js +72 -0
- package/dist/task/EnterpriseTeamTask.cjs.js.map +1 -0
- package/dist/task/OrganizationTask.cjs.js +55 -0
- package/dist/task/OrganizationTask.cjs.js.map +1 -0
- package/dist/task/OrganizationTeamTask.cjs.js +72 -0
- package/dist/task/OrganizationTeamTask.cjs.js.map +1 -0
- package/dist/task/TaskManagement.cjs.js +45 -0
- package/dist/task/TaskManagement.cjs.js.map +1 -0
- package/dist/utils/GithubUtils.cjs.js +9 -12
- package/dist/utils/GithubUtils.cjs.js.map +1 -1
- package/dist/utils/batchInsert.cjs.js +11 -0
- package/dist/utils/batchInsert.cjs.js.map +1 -0
- package/dist/utils/metricHelpers.cjs.js +25 -0
- package/dist/utils/metricHelpers.cjs.js.map +1 -0
- package/migrations/202410191705_add_new_columns.js +48 -0
- package/package.json +4 -3
- package/dist/task/Scheduler.cjs.js +0 -59
- package/dist/task/Scheduler.cjs.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,36 @@
|
|
|
1
1
|
# @backstage-community/plugin-copilot-backend
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 7f17c9f: Introduced support for organizations and team metrics visualization in the Copilot plugin.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [7f17c9f]
|
|
12
|
+
- @backstage-community/plugin-copilot-common@0.3.0
|
|
13
|
+
|
|
14
|
+
## 0.1.6
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- f6d006d: Added support for specifying private GitHub tokens dedicated to the Copilot plugin. This is useful if you don't want to use the same token for both the Copilot backend and other GitHub integrations. To do this, you can specify a new GitHub integration using a string as the host:
|
|
19
|
+
|
|
20
|
+
```diff
|
|
21
|
+
integrations:
|
|
22
|
+
github:
|
|
23
|
+
- host: github.com
|
|
24
|
+
token: your_token
|
|
25
|
+
+ - host: your_copilot_private_token
|
|
26
|
+
+ token: your_super_token
|
|
27
|
+
+ apiBaseUrl: https://api.github.com
|
|
28
|
+
copilot:
|
|
29
|
+
- host: github.com
|
|
30
|
+
+ host: your_copilot_private_token
|
|
31
|
+
enterprise: your_enterprise
|
|
32
|
+
```
|
|
33
|
+
|
|
3
34
|
## 0.1.5
|
|
4
35
|
|
|
5
36
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ To configure the plugin using the new backend system:
|
|
|
15
15
|
|
|
16
16
|
const backend = createBackend();
|
|
17
17
|
|
|
18
|
-
backend.add(import('@backstage-community/plugin-copilot'));
|
|
18
|
+
backend.add(import('@backstage-community/plugin-copilot-backend'));
|
|
19
19
|
|
|
20
20
|
backend.start();
|
|
21
21
|
```
|
|
@@ -24,17 +24,17 @@ To configure the plugin using the new backend system:
|
|
|
24
24
|
|
|
25
25
|
To install the plugin using the old method:
|
|
26
26
|
|
|
27
|
-
1. Add the `@backstage-community/plugin-copilot` package to your backend:
|
|
27
|
+
1. Add the `@backstage-community/plugin-copilot-backend` package to your backend:
|
|
28
28
|
|
|
29
29
|
```sh
|
|
30
|
-
yarn --cwd packages/backend add @backstage-community/plugin-copilot
|
|
30
|
+
yarn --cwd packages/backend add @backstage-community/plugin-copilot-backend
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
2. In your `packages/backend/src/plugins/copilot.ts` file, add the following code:
|
|
34
34
|
|
|
35
35
|
```typescript
|
|
36
36
|
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
|
|
37
|
-
import { createRouterFromConfig } from '@backstage-community/plugin-copilot';
|
|
37
|
+
import { createRouterFromConfig } from '@backstage-community/plugin-copilot-backend';
|
|
38
38
|
|
|
39
39
|
export default async function createPlugin(): Promise<void> {
|
|
40
40
|
const schedule: TaskScheduleDefinition = {
|
|
@@ -53,9 +53,7 @@ To install the plugin using the old method:
|
|
|
53
53
|
import { createRouterFromConfig } from './plugins/copilot';
|
|
54
54
|
|
|
55
55
|
async function main() {
|
|
56
|
-
// Backend setup
|
|
57
56
|
const env = createEnv('copilot');
|
|
58
|
-
// Plugin registration
|
|
59
57
|
apiRouter.use('/copilot', await createRouterFromConfig(env));
|
|
60
58
|
}
|
|
61
59
|
```
|
|
@@ -68,14 +66,36 @@ To configure the GitHub Copilot plugin, you need to set the following environmen
|
|
|
68
66
|
|
|
69
67
|
- **`copilot.host`**: The host URL for your GitHub Copilot instance (e.g., `github.com` or `github.enterprise.com`).
|
|
70
68
|
- **`copilot.enterprise`**: The name of your GitHub Enterprise instance (e.g., `my-enterprise`).
|
|
69
|
+
- **`copilot.organization`**: The name of your GitHub Organization (e.g., `my-organization`).
|
|
71
70
|
|
|
72
71
|
These variables are used to configure the plugin and ensure it communicates with the correct GitHub instance.
|
|
73
72
|
|
|
74
73
|
### GitHub Credentials
|
|
75
74
|
|
|
76
|
-
**Important:** The GitHub token,
|
|
75
|
+
**Important:** The GitHub token, necessary for authentication, should be managed within your Backstage integrations configuration. Ensure that your GitHub integration in the Backstage configuration includes the necessary token for the `GithubCredentialsProvider` to function correctly.
|
|
77
76
|
|
|
78
|
-
|
|
77
|
+
### GitHub Token Scopes
|
|
78
|
+
|
|
79
|
+
To ensure the GitHub Copilot plugin operates correctly within your organization or enterprise, your GitHub access token must include specific scopes. These scopes grant the plugin the necessary permissions to interact with your GitHub organization and manage Copilot usage.
|
|
80
|
+
|
|
81
|
+
#### Required Scopes
|
|
82
|
+
|
|
83
|
+
1. **List Teams Endpoint**
|
|
84
|
+
|
|
85
|
+
- **Scope Required:** `read:org`
|
|
86
|
+
- **Purpose:** Allows the plugin to list all teams within your GitHub organization.
|
|
87
|
+
|
|
88
|
+
2. **Copilot Usage**
|
|
89
|
+
- **Scopes Required - enterprise:** `manage_billing:copilot`, `read:enterprise`
|
|
90
|
+
- **Scopes Required - organization:** `manage_billing:copilot`, `read:org`, or `read:enterprise`
|
|
91
|
+
- **Purpose:** Enables the plugin to manage and monitor GitHub Copilot usage within your organization or/and enterprise.
|
|
92
|
+
|
|
93
|
+
#### How to Configure Token Scopes
|
|
94
|
+
|
|
95
|
+
1. **Generate a Personal Access Token (PAT):**
|
|
96
|
+
- Navigate to [GitHub Personal Access Tokens](https://github.com/settings/tokens).
|
|
97
|
+
- Click on **Generate new token**.
|
|
98
|
+
- Select the scopes according to your needs
|
|
79
99
|
|
|
80
100
|
### YAML Configuration Example
|
|
81
101
|
|
|
@@ -90,20 +110,20 @@ copilot:
|
|
|
90
110
|
seconds: 15
|
|
91
111
|
host: YOUR_GITHUB_HOST_HERE
|
|
92
112
|
enterprise: YOUR_ENTERPRISE_NAME_HERE
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
### Generating GitHub Copilot Token
|
|
113
|
+
organization: YOUR_ORGANIZATION_NAME_HERE
|
|
96
114
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
-
|
|
100
|
-
|
|
115
|
+
integrations:
|
|
116
|
+
github:
|
|
117
|
+
- host: YOUR_GITHUB_HOST_HERE
|
|
118
|
+
token: YOUR_GENERATED_TOKEN
|
|
119
|
+
```
|
|
101
120
|
|
|
102
121
|
### API Documentation
|
|
103
122
|
|
|
104
|
-
For more details on using the GitHub Copilot
|
|
123
|
+
For more details on using the GitHub Copilot and Teams APIs, refer to the following documentation:
|
|
105
124
|
|
|
106
|
-
-
|
|
125
|
+
- [GitHub Teams API - List Teams](https://docs.github.com/en/rest/teams/teams?apiVersion=2022-11-28#list-teams)
|
|
126
|
+
- [GitHub Copilot API - Usage](https://docs.github.com/en/rest/copilot/copilot-usage?apiVersion=2022-11-28)
|
|
107
127
|
|
|
108
128
|
## Run
|
|
109
129
|
|
package/config.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2024 The Backstage Authors
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -30,6 +30,10 @@ export interface Config {
|
|
|
30
30
|
* The name of the GitHub enterprise.
|
|
31
31
|
*/
|
|
32
32
|
enterprise?: string;
|
|
33
|
+
/**
|
|
34
|
+
* The name of the GitHub organization.
|
|
35
|
+
*/
|
|
36
|
+
organization?: string;
|
|
33
37
|
/**
|
|
34
38
|
* The host for GitHub Copilot integration.
|
|
35
39
|
*/
|
|
@@ -16,10 +16,30 @@ class GithubClient {
|
|
|
16
16
|
const info = await GithubUtils.getGithubInfo(config);
|
|
17
17
|
return new GithubClient(info);
|
|
18
18
|
}
|
|
19
|
-
async
|
|
19
|
+
async fetchEnterpriseCopilotUsage() {
|
|
20
20
|
const path = `/enterprises/${this.props.enterprise}/copilot/usage`;
|
|
21
21
|
return this.get(path);
|
|
22
22
|
}
|
|
23
|
+
async fetchEnterpriseTeamCopilotUsage(teamId) {
|
|
24
|
+
const path = `/enterprises/${this.props.enterprise}/team/${teamId}/copilot/usage`;
|
|
25
|
+
return this.get(path);
|
|
26
|
+
}
|
|
27
|
+
async fetchEnterpriseTeams() {
|
|
28
|
+
const path = `/enterprises/${this.props.enterprise}/teams`;
|
|
29
|
+
return this.get(path);
|
|
30
|
+
}
|
|
31
|
+
async fetchOrganizationCopilotUsage() {
|
|
32
|
+
const path = `/orgs/${this.props.organization}/copilot/usage`;
|
|
33
|
+
return this.get(path);
|
|
34
|
+
}
|
|
35
|
+
async fetchOrganizationTeamCopilotUsage(teamId) {
|
|
36
|
+
const path = `/orgs/${this.props.organization}/team/${teamId}/copilot/usage`;
|
|
37
|
+
return this.get(path);
|
|
38
|
+
}
|
|
39
|
+
async fetchOrganizationTeams() {
|
|
40
|
+
const path = `/orgs/${this.props.organization}/teams`;
|
|
41
|
+
return this.get(path);
|
|
42
|
+
}
|
|
23
43
|
async get(path) {
|
|
24
44
|
const response = await fetch__default.default(`${this.props.apiBaseUrl}${path}`, {
|
|
25
45
|
headers: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GithubClient.cjs.js","sources":["../../src/client/GithubClient.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ResponseError } from '@backstage/errors';\nimport { Config } from '@backstage/config';\nimport { Metric } from '@backstage-community/plugin-copilot-common';\nimport fetch from 'node-fetch';\nimport { getGithubInfo, GithubInfo } from '../utils/GithubUtils';\n\ninterface GithubApi {\n
|
|
1
|
+
{"version":3,"file":"GithubClient.cjs.js","sources":["../../src/client/GithubClient.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ResponseError } from '@backstage/errors';\nimport { Config } from '@backstage/config';\nimport { Metric, TeamInfo } from '@backstage-community/plugin-copilot-common';\nimport fetch from 'node-fetch';\nimport { getGithubInfo, GithubInfo } from '../utils/GithubUtils';\n\ninterface GithubApi {\n fetchEnterpriseCopilotUsage: () => Promise<Metric[]>;\n fetchEnterpriseTeamCopilotUsage: (teamId: string) => Promise<Metric[]>;\n fetchEnterpriseTeams: () => Promise<TeamInfo[]>;\n fetchOrganizationCopilotUsage: () => Promise<Metric[]>;\n fetchOrganizationTeamCopilotUsage: (teamId: string) => Promise<Metric[]>;\n fetchOrganizationTeams: () => Promise<TeamInfo[]>;\n}\n\nexport class GithubClient implements GithubApi {\n constructor(private readonly props: GithubInfo) {}\n\n static async fromConfig(config: Config) {\n const info = await getGithubInfo(config);\n return new GithubClient(info);\n }\n\n async fetchEnterpriseCopilotUsage(): Promise<Metric[]> {\n const path = `/enterprises/${this.props.enterprise}/copilot/usage`;\n return this.get(path);\n }\n\n async fetchEnterpriseTeamCopilotUsage(teamId: string): Promise<Metric[]> {\n const path = `/enterprises/${this.props.enterprise}/team/${teamId}/copilot/usage`;\n return this.get(path);\n }\n\n async fetchEnterpriseTeams(): Promise<TeamInfo[]> {\n const path = `/enterprises/${this.props.enterprise}/teams`;\n return this.get(path);\n }\n\n async fetchOrganizationCopilotUsage(): Promise<Metric[]> {\n const path = `/orgs/${this.props.organization}/copilot/usage`;\n return this.get(path);\n }\n\n async fetchOrganizationTeamCopilotUsage(teamId: string): Promise<Metric[]> {\n const path = `/orgs/${this.props.organization}/team/${teamId}/copilot/usage`;\n return this.get(path);\n }\n\n async fetchOrganizationTeams(): Promise<TeamInfo[]> {\n const path = `/orgs/${this.props.organization}/teams`;\n return this.get(path);\n }\n\n private async get<T>(path: string): Promise<T> {\n const response = await fetch(`${this.props.apiBaseUrl}${path}`, {\n headers: {\n ...this.props.credentials.headers,\n Accept: 'application/vnd.github+json',\n 'X-GitHub-Api-Version': '2022-11-28',\n },\n });\n\n if (!response.ok) {\n throw await ResponseError.fromResponse(response);\n }\n\n return response.json() as Promise<T>;\n }\n}\n"],"names":["getGithubInfo","fetch","ResponseError"],"mappings":";;;;;;;;;;AA+BO,MAAM,YAAkC,CAAA;AAAA,EAC7C,YAA6B,KAAmB,EAAA;AAAnB,IAAA,IAAA,CAAA,KAAA,GAAA,KAAA,CAAA;AAAA,GAAoB;AAAA,EAEjD,aAAa,WAAW,MAAgB,EAAA;AACtC,IAAM,MAAA,IAAA,GAAO,MAAMA,yBAAA,CAAc,MAAM,CAAA,CAAA;AACvC,IAAO,OAAA,IAAI,aAAa,IAAI,CAAA,CAAA;AAAA,GAC9B;AAAA,EAEA,MAAM,2BAAiD,GAAA;AACrD,IAAA,MAAM,IAAO,GAAA,CAAA,aAAA,EAAgB,IAAK,CAAA,KAAA,CAAM,UAAU,CAAA,cAAA,CAAA,CAAA;AAClD,IAAO,OAAA,IAAA,CAAK,IAAI,IAAI,CAAA,CAAA;AAAA,GACtB;AAAA,EAEA,MAAM,gCAAgC,MAAmC,EAAA;AACvE,IAAA,MAAM,OAAO,CAAgB,aAAA,EAAA,IAAA,CAAK,KAAM,CAAA,UAAU,SAAS,MAAM,CAAA,cAAA,CAAA,CAAA;AACjE,IAAO,OAAA,IAAA,CAAK,IAAI,IAAI,CAAA,CAAA;AAAA,GACtB;AAAA,EAEA,MAAM,oBAA4C,GAAA;AAChD,IAAA,MAAM,IAAO,GAAA,CAAA,aAAA,EAAgB,IAAK,CAAA,KAAA,CAAM,UAAU,CAAA,MAAA,CAAA,CAAA;AAClD,IAAO,OAAA,IAAA,CAAK,IAAI,IAAI,CAAA,CAAA;AAAA,GACtB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andler(client);
|
|
22
22
|
}
|
|
23
|
-
async
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
]);
|
|
28
|
-
return records ?? [];
|
|
29
|
-
}
|
|
30
|
-
async getPeriodRange() {
|
|
31
|
-
const minDate = await this.db("metrics").orderBy("day", "asc").first("day");
|
|
32
|
-
const maxDate = await this.db("metrics").orderBy("day", "desc").first("day");
|
|
23
|
+
async getPeriodRange(type) {
|
|
24
|
+
const query = this.db("metrics").where("type", type);
|
|
25
|
+
const minDate = await query.orderBy("day", "asc").first("day");
|
|
26
|
+
const maxDate = await query.orderBy("day", "desc").first("day");
|
|
33
27
|
if (!minDate?.day || !maxDate?.day) return void 0;
|
|
34
28
|
return { minDate: minDate.day, maxDate: maxDate.day };
|
|
35
29
|
}
|
|
30
|
+
async getTeams(type, startDate, endDate) {
|
|
31
|
+
const result = await this.db("metrics").where("type", type).whereBetween("day", [startDate, endDate]).whereNotNull("team_name").distinct("team_name").orderBy("team_name", "asc").select("team_name");
|
|
32
|
+
return result.map((x) => x.team_name);
|
|
33
|
+
}
|
|
36
34
|
async batchInsert(metrics) {
|
|
37
|
-
await this.db("metrics").insert(metrics).onConflict("day").ignore();
|
|
35
|
+
await this.db("metrics").insert(metrics).onConflict(["day", "type", "team_name"]).ignore();
|
|
38
36
|
}
|
|
39
|
-
async getMostRecentDayFromMetrics() {
|
|
37
|
+
async getMostRecentDayFromMetrics(type, teamName) {
|
|
40
38
|
try {
|
|
41
|
-
const
|
|
42
|
-
return
|
|
39
|
+
const query = await this.db("metrics").where("type", type).where("team_name", teamName ?? null).orderBy("day", "desc").first("day");
|
|
40
|
+
return query ? query.day : void 0;
|
|
43
41
|
} catch (e) {
|
|
44
42
|
return void 0;
|
|
45
43
|
}
|
|
46
44
|
}
|
|
45
|
+
async getMetrics(startDate, endDate, type, teamName) {
|
|
46
|
+
console.log(startDate, endDate, type, teamName);
|
|
47
|
+
if (teamName) {
|
|
48
|
+
return await this.db("metrics").where("type", type).where("team_name", teamName).whereBetween("day", [startDate, endDate]);
|
|
49
|
+
}
|
|
50
|
+
return this.db("metrics").where("type", type).whereNull("team_name").whereBetween("day", [startDate, endDate]);
|
|
51
|
+
}
|
|
47
52
|
}
|
|
48
53
|
|
|
49
54
|
exports.DatabaseHandler = DatabaseHandler;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DatabaseHandler.cjs.js","sources":["../../src/db/DatabaseHandler.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n resolvePackagePath,\n DatabaseService,\n} from '@backstage/backend-plugin-api';\nimport {\n Metric,\n PeriodRange,\n} from '@backstage-community/plugin-copilot-common';\nimport { Knex } from 'knex';\n\nconst migrationsDir = resolvePackagePath(\n '@backstage-community/plugin-copilot-backend',\n 'migrations',\n);\n\ntype Options = {\n database: DatabaseService;\n};\n\nexport type MetricDbRow = Omit<Metric, 'breakdown'> & {\n breakdown: string;\n};\n\nexport class DatabaseHandler {\n static async create(options: Options): Promise<DatabaseHandler> {\n const { database } = options;\n const client = await database.getClient();\n\n if (!database.migrations?.skip) {\n await client.migrate.latest({\n directory: migrationsDir,\n });\n }\n\n return new DatabaseHandler(client);\n }\n\n private constructor(private readonly db: Knex) {}\n\n async
|
|
1
|
+
{"version":3,"file":"DatabaseHandler.cjs.js","sources":["../../src/db/DatabaseHandler.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n resolvePackagePath,\n DatabaseService,\n} from '@backstage/backend-plugin-api';\nimport {\n Metric,\n MetricsType,\n PeriodRange,\n} from '@backstage-community/plugin-copilot-common';\nimport { Knex } from 'knex';\n\nconst migrationsDir = resolvePackagePath(\n '@backstage-community/plugin-copilot-backend',\n 'migrations',\n);\n\ntype Options = {\n database: DatabaseService;\n};\n\nexport type MetricDbRow = Omit<Metric, 'breakdown'> & {\n breakdown: string;\n};\n\nexport class DatabaseHandler {\n static async create(options: Options): Promise<DatabaseHandler> {\n const { database } = options;\n const client = await database.getClient();\n\n if (!database.migrations?.skip) {\n await client.migrate.latest({\n directory: migrationsDir,\n });\n }\n\n return new DatabaseHandler(client);\n }\n\n private constructor(private readonly db: Knex) {}\n\n async getPeriodRange(type: MetricsType): Promise<PeriodRange | undefined> {\n const query = this.db<MetricDbRow>('metrics').where('type', type);\n\n const minDate = await query.orderBy('day', 'asc').first('day');\n const maxDate = await query.orderBy('day', 'desc').first('day');\n\n if (!minDate?.day || !maxDate?.day) return undefined;\n\n return { minDate: minDate.day, maxDate: maxDate.day };\n }\n\n async getTeams(\n type: MetricsType,\n startDate: string,\n endDate: string,\n ): Promise<Array<string | undefined>> {\n const result = await this.db<MetricDbRow>('metrics')\n .where('type', type)\n .whereBetween('day', [startDate, endDate])\n .whereNotNull('team_name')\n .distinct('team_name')\n .orderBy('team_name', 'asc')\n .select('team_name');\n\n return result.map(x => x.team_name);\n }\n\n async batchInsert(metrics: MetricDbRow[]): Promise<void> {\n await this.db<MetricDbRow[]>('metrics')\n .insert(metrics)\n .onConflict(['day', 'type', 'team_name'])\n .ignore();\n }\n\n async getMostRecentDayFromMetrics(\n type: MetricsType,\n teamName?: string,\n ): Promise<string | undefined> {\n try {\n const query = await this.db<MetricDbRow>('metrics')\n .where('type', type)\n .where('team_name', teamName ?? null)\n .orderBy('day', 'desc')\n .first('day');\n return query ? query.day : undefined;\n } catch (e) {\n return undefined;\n }\n }\n\n async getMetrics(\n startDate: string,\n endDate: string,\n type: MetricsType,\n teamName?: string,\n ): Promise<MetricDbRow[]> {\n console.log(startDate, endDate, type, teamName);\n if (teamName) {\n return await this.db<MetricDbRow>('metrics')\n .where('type', type)\n .where('team_name', teamName)\n .whereBetween('day', [startDate, endDate]);\n }\n return this.db<MetricDbRow>('metrics')\n .where('type', type)\n .whereNull('team_name')\n .whereBetween('day', [startDate, endDate]);\n }\n}\n"],"names":["resolvePackagePath"],"mappings":";;;;AA2BA,MAAM,aAAgB,GAAAA,mCAAA;AAAA,EACpB,6CAAA;AAAA,EACA,YAAA;AACF,CAAA,CAAA;AAUO,MAAM,eAAgB,CAAA;AAAA,EAcnB,YAA6B,EAAU,EAAA;AAAV,IAAA,IAAA,CAAA,EAAA,GAAA,EAAA,CAAA;AAAA,GAAW;AAAA,EAbhD,aAAa,OAAO,OAA4C,EAAA;AAC9D,IAAM,MAAA,EAAE,UAAa,GAAA,OAAA,CAAA;AACrB,IAAM,MAAA,MAAA,GAAS,MAAM,QAAA,CAAS,SAAU,EAAA,CAAA;AAExC,IAAI,IAAA,CAAC,QAAS,CAAA,UAAA,EAAY,IAAM,EAAA;AAC9B,MAAM,MAAA,MAAA,CAAO,QAAQ,MAAO,CAAA;AAAA,QAC1B,SAAW,EAAA,aAAA;AAAA,OACZ,CAAA,CAAA;AAAA,KACH;AAEA,IAAO,OAAA,IAAI,gBAAgB,MAAM,CAAA,CAAA;AAAA,GACnC;AAAA,EAIA,MAAM,eAAe,IAAqD,EAAA;AACxE,IAAA,MAAM,QAAQ,IAAK,CAAA,EAAA,CAAgB,SAAS,CAAE,CAAA,KAAA,CAAM,QAAQ,IAAI,CAAA,CAAA;AAEhE,IAAM,MAAA,OAAA,GAAU,MA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gB,SAAS,CAC/C,CAAA,KAAA,CAAM,QAAQ,IAAI,CAAA,CAClB,MAAM,WAAa,EAAA,QAAA,IAAY,IAAI,CACnC,CAAA,OAAA,CAAQ,OAAO,MAAM,CAAA,CACrB,MAAM,KAAK,CAAA,CAAA;AACd,MAAO,OAAA,KAAA,GAAQ,MAAM,GAAM,GAAA,KAAA,CAAA,CAAA;AAAA,aACpB,CAAG,EAAA;AACV,MAAO,OAAA,KAAA,CAAA,CAAA;AAAA,KACT;AAAA,GACF;AAAA,EAEA,MAAM,UAAA,CACJ,SACA,EAAA,OAAA,EACA,MACA,QACwB,EAAA;AACxB,IAAA,OAAA,CAAQ,GAAI,CAAA,SAAA,EAAW,OAAS,EAAA,IAAA,EAAM,QAAQ,CAAA,CAAA;AAC9C,IAAA,IAAI,QAAU,EAAA;AACZ,MAAA,OAAO,MAAM,IAAK,CAAA,EAAA,CAAgB,SAAS,CACxC,CAAA,KAAA,CAAM,QAAQ,IAAI,CAAA,CAClB,KAAM,CAAA,WAAA,EAAa,QAAQ,CAC3B,CAAA,YAAA,CAAa,OAAO,CAAC,SAAA,EAAW,OAAO,CAAC,CAAA,CAAA;AAAA,KAC7C;AACA,IAAA,OAAO,KAAK,EAAgB,CAAA,SAAS,CAClC,CAAA,KAAA,CAAM,QAAQ,IAAI,CAAA,CAClB,SAAU,CAAA,WAAW,EACrB,YAAa,CAAA,KAAA,EAAO,CAAC,SAAA,EAAW,OAAO,CAAC,CAAA,CAAA;AAAA,GAC7C;AACF;;;;"}
|
|
@@ -4,10 +4,12 @@ var express = require('express');
|
|
|
4
4
|
var Router = require('express-promise-router');
|
|
5
5
|
var rootHttpRouter = require('@backstage/backend-defaults/rootHttpRouter');
|
|
6
6
|
var backendPluginApi = require('@backstage/backend-plugin-api');
|
|
7
|
+
var errors = require('@backstage/errors');
|
|
7
8
|
var DatabaseHandler = require('../db/DatabaseHandler.cjs.js');
|
|
8
|
-
var
|
|
9
|
+
var TaskManagement = require('../task/TaskManagement.cjs.js');
|
|
9
10
|
var GithubClient = require('../client/GithubClient.cjs.js');
|
|
10
|
-
var
|
|
11
|
+
var validateQuery = require('./validation/validateQuery.cjs.js');
|
|
12
|
+
var schema = require('./validation/schema.cjs.js');
|
|
11
13
|
|
|
12
14
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
13
15
|
|
|
@@ -40,7 +42,7 @@ async function createRouter(routerOptions, pluginOptions) {
|
|
|
40
42
|
await scheduler.scheduleTask({
|
|
41
43
|
id: "copilot-metrics",
|
|
42
44
|
...schedule ?? defaultSchedule,
|
|
43
|
-
fn: async () => await
|
|
45
|
+
fn: async () => await TaskManagement.default.create({ db, logger, api, config }).runAsync()
|
|
44
46
|
});
|
|
45
47
|
const router = Router__default.default();
|
|
46
48
|
router.use(express__default.default.json());
|
|
@@ -48,29 +50,38 @@ async function createRouter(routerOptions, pluginOptions) {
|
|
|
48
50
|
logger.info("PONG!");
|
|
49
51
|
response.json({ status: "ok" });
|
|
50
52
|
});
|
|
51
|
-
router.get(
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
router.get(
|
|
54
|
+
"/metrics",
|
|
55
|
+
validateQuery.validateQuery(schema.metricsQuerySchema),
|
|
56
|
+
async (req, res) => {
|
|
57
|
+
const { startDate, endDate, type, team } = req.query;
|
|
58
|
+
const result = await db.getMetrics(startDate, endDate, type, team);
|
|
59
|
+
const metrics = result.map((metric) => ({
|
|
60
|
+
...metric,
|
|
61
|
+
breakdown: JSON.parse(metric.breakdown)
|
|
62
|
+
}));
|
|
63
|
+
return res.json(metrics);
|
|
55
64
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
65
|
+
);
|
|
66
|
+
router.get(
|
|
67
|
+
"/metrics/period-range",
|
|
68
|
+
validateQuery.validateQuery(schema.periodRangeQuerySchema),
|
|
69
|
+
async (req, res) => {
|
|
70
|
+
const { type } = req.query;
|
|
71
|
+
const result = await db.getPeriodRange(type);
|
|
72
|
+
if (!result) {
|
|
73
|
+
throw new errors.NotFoundError();
|
|
74
|
+
}
|
|
75
|
+
return res.json(result);
|
|
60
76
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}));
|
|
66
|
-
return response.json(metrics);
|
|
67
|
-
});
|
|
68
|
-
router.get("/metrics/period-range", async (_, response) => {
|
|
69
|
-
const result = await db.getPeriodRange();
|
|
77
|
+
);
|
|
78
|
+
router.get("/teams", validateQuery.validateQuery(schema.teamQuerySchema), async (req, res) => {
|
|
79
|
+
const { type, startDate, endDate } = req.query;
|
|
80
|
+
const result = await db.getTeams(type, startDate, endDate);
|
|
70
81
|
if (!result) {
|
|
71
|
-
|
|
82
|
+
throw new errors.NotFoundError();
|
|
72
83
|
}
|
|
73
|
-
return
|
|
84
|
+
return res.json(result);
|
|
74
85
|
});
|
|
75
86
|
router.use(rootHttpRouter.MiddlewareFactory.create({ config, logger }).error);
|
|
76
87
|
return router;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.cjs.js","sources":["../../src/service/router.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport express from 'express';\nimport Router from 'express-promise-router';\nimport { MiddlewareFactory } from '@backstage/backend-defaults/rootHttpRouter';\nimport {\n DatabaseService,\n LoggerService,\n readSchedulerServiceTaskScheduleDefinitionFromConfig,\n SchedulerService,\n SchedulerServiceTaskScheduleDefinition,\n} from '@backstage/backend-plugin-api';\nimport { Config } from '@backstage/config';\nimport { Metric } from '@backstage-community/plugin-copilot-common';\nimport { DatabaseHandler } from '../db/DatabaseHandler';\nimport
|
|
1
|
+
{"version":3,"file":"router.cjs.js","sources":["../../src/service/router.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport express from 'express';\nimport Router from 'express-promise-router';\nimport { MiddlewareFactory } from '@backstage/backend-defaults/rootHttpRouter';\nimport {\n DatabaseService,\n LoggerService,\n readSchedulerServiceTaskScheduleDefinitionFromConfig,\n SchedulerService,\n SchedulerServiceTaskScheduleDefinition,\n} from '@backstage/backend-plugin-api';\nimport { Config } from '@backstage/config';\nimport { NotFoundError } from '@backstage/errors';\nimport { Metric } from '@backstage-community/plugin-copilot-common';\nimport { DatabaseHandler } from '../db/DatabaseHandler';\nimport TaskManagement from '../task/TaskManagement';\nimport { GithubClient } from '../client/GithubClient';\nimport { validateQuery } from './validation/validateQuery';\nimport {\n MetricsQuery,\n metricsQuerySchema,\n PeriodRangeQuery,\n periodRangeQuerySchema,\n TeamQuery,\n teamQuerySchema,\n} from './validation/schema';\n\n/**\n * Options for configuring the Copilot plugin.\n *\n * @public\n */\nexport interface PluginOptions {\n /**\n * Schedule configuration for the plugin.\n */\n schedule?: SchedulerServiceTaskScheduleDefinition;\n}\n\n/**\n * Options for configuring the router used by the Copilot plugin.\n *\n * @public\n */\nexport interface RouterOptions {\n /**\n * Logger service for the router.\n */\n logger: LoggerService;\n\n /**\n * Database service for the router.\n */\n database: DatabaseService;\n\n /**\n * Scheduler service for the router.\n */\n scheduler: SchedulerService;\n\n /**\n * Configuration for the router.\n */\n config: Config;\n}\n\nconst defaultSchedule: SchedulerServiceTaskScheduleDefinition = {\n frequency: { cron: '0 2 * * *' },\n timeout: { minutes: 15 },\n initialDelay: { minutes: 1 },\n scope: 'local',\n};\n\n/**\n * Creates an Express router configured based on the provided router options and plugin options.\n *\n * This function initializes the router with the appropriate middleware and routes based on the\n * configuration and options provided. It also schedules tasks if scheduling options are provided.\n *\n * @param routerOptions - Options for configuring the router, including services and configuration.\n * @returns A promise that resolves to an Express router instance.\n *\n * @public\n */\nexport async function createRouterFromConfig(routerOptions: RouterOptions) {\n const { config } = routerOptions;\n const pluginOptions: PluginOptions = {\n schedule: defaultSchedule,\n };\n if (config && config.has('copilot.schedule')) {\n pluginOptions.schedule =\n readSchedulerServiceTaskScheduleDefinitionFromConfig(\n config.getConfig('copilot.schedule'),\n );\n }\n return createRouter(routerOptions, pluginOptions);\n}\n\n/** @private */\nasync function createRouter(\n routerOptions: RouterOptions,\n pluginOptions: PluginOptions,\n): Promise<express.Router> {\n const { logger, database, scheduler, config } = routerOptions;\n const { schedule } = pluginOptions;\n\n const db = await DatabaseHandler.create({ database });\n const api = await GithubClient.fromConfig(config);\n\n await scheduler.scheduleTask({\n id: 'copilot-metrics',\n ...(schedule ?? defaultSchedule),\n fn: async () =>\n await TaskManagement.create({ db, logger, api, config }).runAsync(),\n });\n\n const router = Router();\n router.use(express.json());\n\n router.get('/health', (_, response) => {\n logger.info('PONG!');\n response.json({ status: 'ok' });\n });\n\n router.get(\n '/metrics',\n validateQuery(metricsQuerySchema),\n async (req, res) => {\n const { startDate, endDate, type, team } = req.query as MetricsQuery;\n\n const result = await db.getMetrics(startDate, endDate, type, team);\n\n const metrics: Metric[] = result.map(metric => ({\n ...metric,\n breakdown: JSON.parse(metric.breakdown),\n }));\n\n return res.json(metrics);\n },\n );\n\n router.get(\n '/metrics/period-range',\n validateQuery(periodRangeQuerySchema),\n async (req, res) => {\n const { type } = req.query as PeriodRangeQuery;\n const result = await db.getPeriodRange(type);\n\n if (!result) {\n throw new NotFoundError();\n }\n\n return res.json(result);\n },\n );\n\n router.get('/teams', validateQuery(teamQuerySchema), async (req, res) => {\n const { type, startDate, endDate } = req.query as TeamQuery;\n\n const result = await db.getTeams(type, startDate, endDate);\n\n if (!result) {\n throw new NotFoundError();\n }\n\n return res.json(result);\n });\n\n router.use(MiddlewareFactory.create({ config, logger }).error);\n return router;\n}\n"],"names":["readSchedulerServiceTaskScheduleDefinitionFromConfig","DatabaseHandler","GithubClient","TaskManagement","Router","express","validateQuery","metricsQuerySchema","periodRangeQuerySchema","NotFoundError","teamQuerySchema","MiddlewareFactory"],"mappings":";;;;;;;;;;;;;;;;;;AAgFA,MAAM,eAA0D,GAAA;AAAA,EAC9D,SAAA,EAAW,EAAE,IAAA,EAAM,WAAY,EAAA;AAAA,EAC/B,OAAA,EAAS,EAAE,OAAA,EAAS,EAAG,EAAA;AAAA,EACvB,YAAA,EAAc,EAAE,OAAA,EAAS,CAAE,EAAA;AAAA,EAC3B,KAAO,EAAA,OAAA;AACT,CAAA,CAAA;AAaA,eAAsB,uBAAuB,aAA8B,EAAA;AACzE,EAAM,MAAA,EAAE,QAAW,GAAA,aAAA,CAAA;AACnB,EAAA,MAAM,aAA+B,GAAA;AAAA,IACnC,QAAU,EAAA,eAAA;AAAA,GACZ,CAAA;AACA,EAAA,IAAI,MAAU,IAAA,MAAA,CAAO,GAAI,CAAA,kBAAkB,CAAG,EAAA;AAC5C,IAAA,aAAA,CAAc,QACZ,GAAAA,qEAAA;AAAA,MACE,MAAA,CAAO,UAAU,kBAAkB,CAAA;AAAA,KACrC,CAAA;AAAA,GACJ;AACA,EAAO,OAAA,YAAA,CAAa,eAAe,aAAa,CAAA,CAAA;AAClD,CAAA;AAGA,eAAe,YAAA,CACb,eACA,aACyB,EAAA;AACzB,EAAA,MAAM,EAAE,MAAA,EAAQ,QAAU,EAAA,SAAA,EAAW,QAAW,GAAA,aAAA,CAAA;AAChD,EAAM,MAAA,EAAE,UAAa,GAAA,aAAA,CAAA;AA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
|
|
2
|
+
|
|
3
|
+
var zod = require('zod');
|
|
4
|
+
|
|
5
|
+
const metricsTypeSchema = zod.z.enum(["enterprise", "organization"]);
|
|
6
|
+
const isoDateSchema = zod.z.string().refine((date) => !isNaN(Date.parse(date)), {
|
|
7
|
+
message: "Invalid date format"
|
|
8
|
+
});
|
|
9
|
+
const metricsQuerySchema = zod.z.object({
|
|
10
|
+
startDate: isoDateSchema,
|
|
11
|
+
endDate: isoDateSchema,
|
|
12
|
+
type: metricsTypeSchema,
|
|
13
|
+
team: zod.z.string().optional()
|
|
14
|
+
});
|
|
15
|
+
const periodRangeQuerySchema = zod.z.object({
|
|
16
|
+
type: metricsTypeSchema
|
|
17
|
+
});
|
|
18
|
+
const teamQuerySchema = zod.z.object({
|
|
19
|
+
startDate: isoDateSchema,
|
|
20
|
+
endDate: isoDateSchema,
|
|
21
|
+
type: metricsTypeSchema
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
exports.metricsQuerySchema = metricsQuerySchema;
|
|
25
|
+
exports.periodRangeQuerySchema = periodRangeQuerySchema;
|
|
26
|
+
exports.teamQuerySchema = teamQuerySchema;
|
|
27
|
+
//# sourceMappingURL=schema.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.cjs.js","sources":["../../../src/service/validation/schema.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { z } from 'zod';\nimport { MetricsType } from '@backstage-community/plugin-copilot-common';\n\nconst metricsTypeSchema = z.enum(['enterprise', 'organization']);\n\nconst isoDateSchema = z.string().refine(date => !isNaN(Date.parse(date)), {\n message: 'Invalid date format',\n});\n\nexport type MetricsQuery = {\n startDate: string;\n endDate: string;\n type: MetricsType;\n team?: string;\n};\n\nexport type PeriodRangeQuery = {\n type: MetricsType;\n};\n\nexport type TeamQuery = {\n type: MetricsType;\n startDate: string;\n endDate: string;\n};\n\nexport const metricsQuerySchema = z.object({\n startDate: isoDateSchema,\n endDate: isoDateSchema,\n type: metricsTypeSchema,\n team: z.string().optional(),\n});\n\nexport const periodRangeQuerySchema = z.object({\n type: metricsTypeSchema,\n});\n\nexport const teamQuerySchema = z.object({\n startDate: isoDateSchema,\n endDate: isoDateSchema,\n type: metricsTypeSchema,\n});\n"],"names":["z"],"mappings":";;;;AAkBA,MAAM,oBAAoBA,KAAE,CAAA,IAAA,CAAK,CAAC,YAAA,EAAc,cAAc,CAAC,CAAA,CAAA;AAE/D,MAAM,aAAgB,GAAAA,KAAA,CAAE,MAAO,EAAA,CAAE,MAAO,CAAA,CAAA,IAAA,KAAQ,CAAC,KAAA,CAAM,IAAK,CAAA,KAAA,CAAM,IAAI,CAAC,CAAG,EAAA;AAAA,EACxE,OAAS,EAAA,qBAAA;AACX,CAAC,CAAA,CAAA;AAmBY,MAAA,kBAAA,GAAqBA,MAAE,MAAO,CAAA;AAAA,EACzC,SAAW,EAAA,aAAA;AAAA,EACX,OAAS,EAAA,aAAA;AAAA,EACT,IAAM,EAAA,iBAAA;AAAA,EACN,IAAM,EAAAA,KAAA,CAAE,MAAO,EAAA,CAAE,QAAS,EAAA;AAC5B,CAAC,EAAA;AAEY,MAAA,sBAAA,GAAyBA,MAAE,MAAO,CAAA;AAAA,EAC7C,IAAM,EAAA,iBAAA;AACR,CAAC,EAAA;AAEY,MAAA,eAAA,GAAkBA,MAAE,MAAO,CAAA;AAAA,EACtC,SAAW,EAAA,aAAA;AAAA,EACX,OAAS,EAAA,aAAA;AAAA,EACT,IAAM,EAAA,iBAAA;AACR,CAAC;;;;;;"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var errors = require('@backstage/errors');
|
|
4
|
+
|
|
5
|
+
function validateQuery(schema) {
|
|
6
|
+
return (req, _res, next) => {
|
|
7
|
+
const { error, data } = schema.safeParse(req.query);
|
|
8
|
+
if (error) {
|
|
9
|
+
throw new errors.InputError(error.errors[0].message, error);
|
|
10
|
+
}
|
|
11
|
+
req.query = data;
|
|
12
|
+
return next();
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
exports.validateQuery = validateQuery;
|
|
17
|
+
//# sourceMappingURL=validateQuery.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateQuery.cjs.js","sources":["../../../src/service/validation/validateQuery.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { InputError } from '@backstage/errors';\nimport { NextFunction, Request, Response } from 'express';\nimport { ZodSchema } from 'zod';\n\nexport function validateQuery(schema: ZodSchema) {\n return (req: Request, _res: Response, next: NextFunction) => {\n const { error, data } = schema.safeParse(req.query);\n\n if (error) {\n throw new InputError(error.errors[0].message, error);\n }\n\n req.query = data;\n\n return next();\n };\n}\n"],"names":["InputError"],"mappings":";;;;AAmBO,SAAS,cAAc,MAAmB,EAAA;AAC/C,EAAO,OAAA,CAAC,GAAc,EAAA,IAAA,EAAgB,IAAuB,KAAA;AAC3D,IAAA,MAAM,EAAE,KAAO,EAAA,IAAA,KAAS,MAAO,CAAA,SAAA,CAAU,IAAI,KAAK,CAAA,CAAA;AAElD,IAAA,IAAI,KAAO,EAAA;AACT,MAAA,MAAM,IAAIA,iBAAW,CAAA,KAAA,CAAM,OAAO,CAAC,CAAA,CAAE,SAAS,KAAK,CAAA,CAAA;AAAA,KACrD;AAEA,IAAA,GAAA,CAAI,KAAQ,GAAA,IAAA,CAAA;AAEZ,IAAA,OAAO,IAAK,EAAA,CAAA;AAAA,GACd,CAAA;AACF;;;;"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var batchInsert = require('../utils/batchInsert.cjs.js');
|
|
4
|
+
var metricHelpers = require('../utils/metricHelpers.cjs.js');
|
|
5
|
+
|
|
6
|
+
async function discoverEnterpriseMetrics({
|
|
7
|
+
api,
|
|
8
|
+
logger,
|
|
9
|
+
db,
|
|
10
|
+
config
|
|
11
|
+
}) {
|
|
12
|
+
if (!config.getOptionalString("copilot.enterprise")) {
|
|
13
|
+
logger.info(
|
|
14
|
+
"[discoverEnterpriseMetrics] Skipping: Enterprise configuration not found."
|
|
15
|
+
);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const type = "enterprise";
|
|
19
|
+
try {
|
|
20
|
+
const metrics = await api.fetchEnterpriseCopilotUsage();
|
|
21
|
+
logger.info(
|
|
22
|
+
`[discoverEnterpriseMetrics] Fetched ${metrics.length} metrics`
|
|
23
|
+
);
|
|
24
|
+
const lastDay = await db.getMostRecentDayFromMetrics(type);
|
|
25
|
+
logger.info(`[discoverEnterpriseMetrics] Found last day: ${lastDay}`);
|
|
26
|
+
const newMetrics = metricHelpers.filterNewMetrics(metrics, lastDay);
|
|
27
|
+
logger.info(
|
|
28
|
+
`[discoverEnterpriseMetrics] Found ${newMetrics.length} new metrics to insert`
|
|
29
|
+
);
|
|
30
|
+
if (newMetrics.length > 0) {
|
|
31
|
+
await batchInsert.batchInsertInChunks(
|
|
32
|
+
metricHelpers.prepareMetricsForInsert(newMetrics, type),
|
|
33
|
+
30,
|
|
34
|
+
async (chunk) => {
|
|
35
|
+
await db.batchInsert(chunk);
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
logger.info(
|
|
39
|
+
"[discoverEnterpriseMetrics] Inserted new metrics into the database"
|
|
40
|
+
);
|
|
41
|
+
} else {
|
|
42
|
+
logger.info("[discoverEnterpriseMetrics] No new metrics found to insert");
|
|
43
|
+
}
|
|
44
|
+
} catch (error) {
|
|
45
|
+
logger.error(
|
|
46
|
+
`[discoverEnterpriseMetrics] An error occurred while processing Github Copilot metrics: ${error}`
|
|
47
|
+
);
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
exports.discoverEnterpriseMetrics = discoverEnterpriseMetrics;
|
|
53
|
+
//# sourceMappingURL=EnterpriseTask.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnterpriseTask.cjs.js","sources":["../../src/task/EnterpriseTask.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { MetricsType } from '@backstage-community/plugin-copilot-common';\nimport { MetricDbRow } from '../db/DatabaseHandler';\nimport { batchInsertInChunks } from '../utils/batchInsert';\nimport {\n filterNewMetrics,\n prepareMetricsForInsert,\n} from '../utils/metricHelpers';\nimport { TaskOptions } from './TaskManagement';\n\nexport async function discoverEnterpriseMetrics({\n api,\n logger,\n db,\n config,\n}: TaskOptions): Promise<void> {\n if (!config.getOptionalString('copilot.enterprise')) {\n logger.info(\n '[discoverEnterpriseMetrics] Skipping: Enterprise configuration not found.',\n );\n return;\n }\n\n const type: MetricsType = 'enterprise';\n\n try {\n const metrics = await api.fetchEnterpriseCopilotUsage();\n logger.info(\n `[discoverEnterpriseMetrics] Fetched ${metrics.length} metrics`,\n );\n\n const lastDay = await db.getMostRecentDayFromMetrics(type);\n logger.info(`[discoverEnterpriseMetrics] Found last day: ${lastDay}`);\n\n const newMetrics = filterNewMetrics(metrics, lastDay);\n logger.info(\n `[discoverEnterpriseMetrics] Found ${newMetrics.length} new metrics to insert`,\n );\n\n if (newMetrics.length > 0) {\n await batchInsertInChunks<MetricDbRow>(\n prepareMetricsForInsert(newMetrics, type),\n 30,\n async (chunk: MetricDbRow[]) => {\n await db.batchInsert(chunk);\n },\n );\n logger.info(\n '[discoverEnterpriseMetrics] Inserted new metrics into the database',\n );\n } else {\n logger.info('[discoverEnterpriseMetrics] No new metrics found to insert');\n }\n } catch (error) {\n logger.error(\n `[discoverEnterpriseMetrics] An error occurred while processing Github Copilot metrics: ${error}`,\n );\n throw error;\n }\n}\n"],"names":["filterNewMetrics","batchInsertInChunks","prepareMetricsForInsert"],"mappings":";;;;;AAwBA,eAAsB,yBAA0B,CAAA;AAAA,EAC9C,GAAA;AAAA,EACA,MAAA;AAAA,EACA,EAAA;AAAA,EACA,MAAA;AACF,CAA+B,EAAA;AAC7B,EAAA,IAAI,CAAC,MAAA,CAAO,iBAAkB,CAAA,oBAAoB,CAAG,EAAA;AACnD,IAAO,MAAA,CAAA,IAAA;AAAA,MACL,2EAAA;AAAA,KACF,CAAA;AACA,IAAA,OAAA;AAAA,GACF;AAEA,EAAA,MAAM,IAAoB,GAAA,YAAA,CAAA;AAE1B,EAAI,IAAA;AACF,IAAM,MAAA,OAAA,GAAU,MAAM,GAAA,CAAI,2BAA4B,EAAA,CAAA;AACtD,IAAO,MAAA,CAAA,IAAA;AAAA,MACL,CAAA,oCAAA,EAAuC,QAAQ,MAAM,CAAA,QAAA,CAAA;AAAA,KACvD,CAAA;AAEA,IAAA,MAAM,OAAU,GAAA,MAAM,EAAG,CAAA,2BAAA,CAA4B,IAAI,CAAA,CAAA;AACzD,IAAO,MAAA,CAAA,IAAA,CAAK,CAA+C,4CAAA,EAAA,OAAO,CAAE,CAAA,CAAA,CAAA;AAEpE,IAAM,MAAA,UAAA,GAAaA,8BAAiB,CAAA,OAAA,EAAS,OAAO,CAAA,CAAA;AACpD,IAAO,MAAA,CAAA,IAAA;AAAA,MACL,CAAA,kCAAA,EAAqC,WAAW,MAAM,CAAA,sBAAA,CAAA;AAAA,KACxD,CAAA;AAEA,IAAI,IAAA,UAAA,CAAW,SAAS,CAAG,EAAA;AACzB,MAAM,MAAAC,+BAAA;AAAA,QACJC,qCAAA,CAAwB,YAAY,IAAI,CAAA;AAAA,QACxC,EAAA;AAAA,QACA,OAAO,KAAyB,KAAA;AAC9B,UAAM,MAAA,EAAA,CAAG,YAAY,KAAK,CAAA,CAAA;AAAA,SAC5B;AAAA,OACF,CAAA;AACA,MAAO,MAAA,CAAA,IAAA;AAAA,QACL,oEAAA;AAAA,OACF,CAAA;AAAA,KACK,MAAA;AACL,MAAA,MAAA,CAAO,KAAK,4DAA4D,CAAA,CAAA;AAAA,KAC1E;AAAA,WACO,KAAO,EAAA;AACd,IAAO,MAAA,CAAA,KAAA;AAAA,MACL,0FAA0F,KAAK,CAAA,CAAA;AAAA,KACjG,CAAA;AACA,IAAM,MAAA,KAAA,CAAA;AAAA,GACR;AACF;;;;"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var batchInsert = require('../utils/batchInsert.cjs.js');
|
|
4
|
+
var metricHelpers = require('../utils/metricHelpers.cjs.js');
|
|
5
|
+
|
|
6
|
+
async function discoverEnterpriseTeamMetrics({
|
|
7
|
+
api,
|
|
8
|
+
logger,
|
|
9
|
+
db,
|
|
10
|
+
config
|
|
11
|
+
}) {
|
|
12
|
+
if (!config.getOptionalString("copilot.enterprise")) {
|
|
13
|
+
logger.info(
|
|
14
|
+
"[discoverEnterpriseTeamMetrics] Skipping: Enterprise configuration not found."
|
|
15
|
+
);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const type = "enterprise";
|
|
19
|
+
try {
|
|
20
|
+
const teams = await api.fetchEnterpriseTeams();
|
|
21
|
+
logger.info(
|
|
22
|
+
`[discoverEnterpriseTeamMetrics] Fetched ${teams.length} teams`
|
|
23
|
+
);
|
|
24
|
+
for (const team of teams) {
|
|
25
|
+
try {
|
|
26
|
+
logger.info(
|
|
27
|
+
`[discoverEnterpriseTeamMetrics] Fetching metrics for team: ${team.slug}`
|
|
28
|
+
);
|
|
29
|
+
const metrics = await api.fetchEnterpriseTeamCopilotUsage(team.slug);
|
|
30
|
+
logger.info(
|
|
31
|
+
`[discoverEnterpriseTeamMetrics] Fetched ${metrics.length} metrics for team: ${team.slug}`
|
|
32
|
+
);
|
|
33
|
+
const lastDay = await db.getMostRecentDayFromMetrics(type, team.slug);
|
|
34
|
+
logger.info(
|
|
35
|
+
`[discoverEnterpriseTeamMetrics] Found last processed day for team ${team.slug}: ${lastDay}`
|
|
36
|
+
);
|
|
37
|
+
const newMetrics = metricHelpers.filterNewMetrics(metrics, lastDay);
|
|
38
|
+
logger.info(
|
|
39
|
+
`[discoverEnterpriseTeamMetrics] Found ${newMetrics.length} new metrics for team ${team.slug} to insert`
|
|
40
|
+
);
|
|
41
|
+
if (newMetrics.length > 0) {
|
|
42
|
+
await batchInsert.batchInsertInChunks(
|
|
43
|
+
metricHelpers.prepareMetricsForInsert(newMetrics, type, team.slug),
|
|
44
|
+
30,
|
|
45
|
+
async (chunk) => {
|
|
46
|
+
await db.batchInsert(chunk);
|
|
47
|
+
}
|
|
48
|
+
);
|
|
49
|
+
logger.info(
|
|
50
|
+
`[discoverEnterpriseTeamMetrics] Successfully inserted new metrics for team ${team.slug} into the database`
|
|
51
|
+
);
|
|
52
|
+
} else {
|
|
53
|
+
logger.info(
|
|
54
|
+
`[discoverEnterpriseTeamMetrics] No new metrics found for team ${team.slug} to insert`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
} catch (error) {
|
|
58
|
+
logger.error(
|
|
59
|
+
`[discoverEnterpriseTeamMetrics] Error processing metrics for team ${team.slug}: ${error}`
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
} catch (error) {
|
|
64
|
+
logger.error(
|
|
65
|
+
`[discoverEnterpriseTeamMetrics] Error fetching teams: ${error}`
|
|
66
|
+
);
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
exports.discoverEnterpriseTeamMetrics = discoverEnterpriseTeamMetrics;
|
|
72
|
+
//# sourceMappingURL=EnterpriseTeamTask.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnterpriseTeamTask.cjs.js","sources":["../../src/task/EnterpriseTeamTask.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { MetricsType } from '@backstage-community/plugin-copilot-common';\nimport { MetricDbRow } from '../db/DatabaseHandler';\nimport { batchInsertInChunks } from '../utils/batchInsert';\nimport {\n filterNewMetrics,\n prepareMetricsForInsert,\n} from '../utils/metricHelpers';\nimport { TaskOptions } from './TaskManagement';\n\nexport async function discoverEnterpriseTeamMetrics({\n api,\n logger,\n db,\n config,\n}: TaskOptions): Promise<void> {\n if (!config.getOptionalString('copilot.enterprise')) {\n logger.info(\n '[discoverEnterpriseTeamMetrics] Skipping: Enterprise configuration not found.',\n );\n return;\n }\n\n const type: MetricsType = 'enterprise';\n\n try {\n const teams = await api.fetchEnterpriseTeams();\n logger.info(\n `[discoverEnterpriseTeamMetrics] Fetched ${teams.length} teams`,\n );\n\n for (const team of teams) {\n try {\n logger.info(\n `[discoverEnterpriseTeamMetrics] Fetching metrics for team: ${team.slug}`,\n );\n\n const metrics = await api.fetchEnterpriseTeamCopilotUsage(team.slug);\n logger.info(\n `[discoverEnterpriseTeamMetrics] Fetched ${metrics.length} metrics for team: ${team.slug}`,\n );\n\n const lastDay = await db.getMostRecentDayFromMetrics(type, team.slug);\n logger.info(\n `[discoverEnterpriseTeamMetrics] Found last processed day for team ${team.slug}: ${lastDay}`,\n );\n\n const newMetrics = filterNewMetrics(metrics, lastDay);\n logger.info(\n `[discoverEnterpriseTeamMetrics] Found ${newMetrics.length} new metrics for team ${team.slug} to insert`,\n );\n\n if (newMetrics.length > 0) {\n await batchInsertInChunks<MetricDbRow>(\n prepareMetricsForInsert(newMetrics, type, team.slug),\n 30,\n async (chunk: MetricDbRow[]) => {\n await db.batchInsert(chunk);\n },\n );\n logger.info(\n `[discoverEnterpriseTeamMetrics] Successfully inserted new metrics for team ${team.slug} into the database`,\n );\n } else {\n logger.info(\n `[discoverEnterpriseTeamMetrics] No new metrics found for team ${team.slug} to insert`,\n );\n }\n } catch (error) {\n logger.error(\n `[discoverEnterpriseTeamMetrics] Error processing metrics for team ${team.slug}: ${error}`,\n );\n }\n }\n } catch (error) {\n logger.error(\n `[discoverEnterpriseTeamMetrics] Error fetching teams: ${error}`,\n );\n throw error;\n }\n}\n"],"names":["filterNewMetrics","batchInsertInChunks","prepareMetricsForInsert"],"mappings":";;;;;AAwBA,eAAsB,6BAA8B,CAAA;AAAA,EAClD,GAAA;AAAA,EACA,MAAA;AAAA,EACA,EAAA;AAAA,EACA,MAAA;AACF,CAA+B,EAAA;AAC7B,EAAA,IAAI,CAAC,MAAA,CAAO,iBAAkB,CAAA,oBAAoB,CAAG,EAAA;AACnD,IAAO,MAAA,CAAA,IAAA;AAAA,MACL,+EAAA;AAAA,KACF,CAAA;AACA,IAAA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IAAA,CAAK,IAAI,CAAA;AAAA,YACnD,EAAA;AAAA,YACA,OAAO,KAAyB,KAAA;AAC9B,cAAM,MAAA,EAAA,CAAG,YAAY,KAAK,CAAA,CAAA;AAAA,aAC5B;AAAA,WACF,CAAA;AACA,UAAO,MAAA,CAAA,IAAA;AAAA,YACL,CAAA,2EAAA,EAA8E,KAAK,IAAI,CAAA,kBAAA,CAAA;AAAA,WACzF,CAAA;AAAA,SACK,MAAA;AACL,UAAO,MAAA,CAAA,IAAA;AAAA,YACL,CAAA,8DAAA,EAAiE,KAAK,IAAI,CAAA,UAAA,CAAA;AAAA,WAC5E,CAAA;AAAA,SACF;AAAA,eACO,KAAO,EAAA;AACd,QAAO,MAAA,CAAA,KAAA;AAAA,UACL,CAAqE,kEAAA,EAAA,IAAA,CAAK,IAAI,CAAA,EAAA,EAAK,KAAK,CAAA,CAAA;AAAA,SAC1F,CAAA;AAAA,OACF;AAAA,KACF;AAAA,WACO,KAAO,EAAA;AACd,IAAO,MAAA,CAAA,KAAA;AAAA,MACL,yDAAyD,KAAK,CAAA,CAAA;AAAA,KAChE,CAAA;AACA,IAAM,MAAA,KAAA,CAAA;AAAA,GACR;AACF;;;;"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var batchInsert = require('../utils/batchInsert.cjs.js');
|
|
4
|
+
var metricHelpers = require('../utils/metricHelpers.cjs.js');
|
|
5
|
+
|
|
6
|
+
async function discoverOrganizationMetrics({
|
|
7
|
+
api,
|
|
8
|
+
logger,
|
|
9
|
+
db,
|
|
10
|
+
config
|
|
11
|
+
}) {
|
|
12
|
+
if (!config.getOptionalString("copilot.organization")) {
|
|
13
|
+
logger.info(
|
|
14
|
+
"[discoverOrganizationMetrics] Skipping: Organization configuration not found."
|
|
15
|
+
);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const type = "organization";
|
|
19
|
+
try {
|
|
20
|
+
const metrics = await api.fetchOrganizationCopilotUsage();
|
|
21
|
+
logger.info(
|
|
22
|
+
`[discoverOrganizationMetrics] Fetched ${metrics.length} metrics`
|
|
23
|
+
);
|
|
24
|
+
const lastDay = await db.getMostRecentDayFromMetrics(type);
|
|
25
|
+
logger.info(`[discoverOrganizationMetrics] Found last day: ${lastDay}`);
|
|
26
|
+
const newMetrics = metricHelpers.filterNewMetrics(metrics, lastDay);
|
|
27
|
+
logger.info(
|
|
28
|
+
`[discoverOrganizationMetrics] Found ${newMetrics.length} new metrics to insert`
|
|
29
|
+
);
|
|
30
|
+
if (newMetrics.length > 0) {
|
|
31
|
+
await batchInsert.batchInsertInChunks(
|
|
32
|
+
metricHelpers.prepareMetricsForInsert(newMetrics, type),
|
|
33
|
+
30,
|
|
34
|
+
async (chunk) => {
|
|
35
|
+
await db.batchInsert(chunk);
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
logger.info(
|
|
39
|
+
"[discoverOrganizationMetrics] Inserted new metrics into the database"
|
|
40
|
+
);
|
|
41
|
+
} else {
|
|
42
|
+
logger.info(
|
|
43
|
+
"[discoverOrganizationMetrics] No new metrics found to insert"
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
} catch (error) {
|
|
47
|
+
logger.error(
|
|
48
|
+
`[discoverOrganizationMetrics] An error occurred while processing Github Copilot metrics: ${error}`
|
|
49
|
+
);
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
exports.discoverOrganizationMetrics = discoverOrganizationMetrics;
|
|
55
|
+
//# sourceMappingURL=OrganizationTask.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OrganizationTask.cjs.js","sources":["../../src/task/OrganizationTask.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { MetricsType } from '@backstage-community/plugin-copilot-common';\nimport { MetricDbRow } from '../db/DatabaseHandler';\nimport { batchInsertInChunks } from '../utils/batchInsert';\nimport {\n filterNewMetrics,\n prepareMetricsForInsert,\n} from '../utils/metricHelpers';\nimport { TaskOptions } from './TaskManagement';\n\nexport async function discoverOrganizationMetrics({\n api,\n logger,\n db,\n config,\n}: TaskOptions): Promise<void> {\n if (!config.getOptionalString('copilot.organization')) {\n logger.info(\n '[discoverOrganizationMetrics] Skipping: Organization configuration not found.',\n );\n return;\n }\n\n const type: MetricsType = 'organization';\n\n try {\n const metrics = await api.fetchOrganizationCopilotUsage();\n logger.info(\n `[discoverOrganizationMetrics] Fetched ${metrics.length} metrics`,\n );\n\n const lastDay = await db.getMostRecentDayFromMetrics(type);\n logger.info(`[discoverOrganizationMetrics] Found last day: ${lastDay}`);\n\n const newMetrics = filterNewMetrics(metrics, lastDay);\n logger.info(\n `[discoverOrganizationMetrics] Found ${newMetrics.length} new metrics to insert`,\n );\n\n if (newMetrics.length > 0) {\n await batchInsertInChunks<MetricDbRow>(\n prepareMetricsForInsert(newMetrics, type),\n 30,\n async (chunk: MetricDbRow[]) => {\n await db.batchInsert(chunk);\n },\n );\n logger.info(\n '[discoverOrganizationMetrics] Inserted new metrics into the database',\n );\n } else {\n logger.info(\n '[discoverOrganizationMetrics] No new metrics found to insert',\n );\n }\n } catch (error) {\n logger.error(\n `[discoverOrganizationMetrics] An error occurred while processing Github Copilot metrics: ${error}`,\n );\n throw error;\n }\n}\n"],"names":["filterNewMetrics","batchInsertInChunks","prepareMetricsForInsert"],"mappings":";;;;;AAwBA,eAAsB,2BAA4B,CAAA;AAAA,EAChD,GAAA;AAAA,EACA,MAAA;AAAA,EACA,EAAA;AAAA,E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AAA,CAAA,KAAA;AAAA,MACL,4FAA4F,KAAK,CAAA,CAAA;AAAA,KACnG,CAAA;AACA,IAAM,MAAA,KAAA,CAAA;AAAA,GACR;AACF;;;;"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var batchInsert = require('../utils/batchInsert.cjs.js');
|
|
4
|
+
var metricHelpers = require('../utils/metricHelpers.cjs.js');
|
|
5
|
+
|
|
6
|
+
async function discoverOrganizationTeamMetrics({
|
|
7
|
+
api,
|
|
8
|
+
logger,
|
|
9
|
+
db,
|
|
10
|
+
config
|
|
11
|
+
}) {
|
|
12
|
+
if (!config.getOptionalString("copilot.organization")) {
|
|
13
|
+
logger.info(
|
|
14
|
+
"[discoverOrganizationTeamMetrics] Skipping: Organization configuration not found."
|
|
15
|
+
);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const type = "organization";
|
|
19
|
+
try {
|
|
20
|
+
const teams = await api.fetchOrganizationTeams();
|
|
21
|
+
logger.info(
|
|
22
|
+
`[discoverOrganizationTeamMetrics] Fetched ${teams.length} teams`
|
|
23
|
+
);
|
|
24
|
+
for (const team of teams) {
|
|
25
|
+
try {
|
|
26
|
+
logger.info(
|
|
27
|
+
`[discoverOrganizationTeamMetrics] Fetching metrics for team: ${team.slug}`
|
|
28
|
+
);
|
|
29
|
+
const metrics = await api.fetchOrganizationTeamCopilotUsage(team.slug);
|
|
30
|
+
logger.info(
|
|
31
|
+
`[discoverOrganizationTeamMetrics] Fetched ${metrics.length} metrics for team: ${team.slug}`
|
|
32
|
+
);
|
|
33
|
+
const lastDay = await db.getMostRecentDayFromMetrics(type, team.slug);
|
|
34
|
+
logger.info(
|
|
35
|
+
`[discoverOrganizationTeamMetrics] Found last processed day for team ${team.slug}: ${lastDay}`
|
|
36
|
+
);
|
|
37
|
+
const newMetrics = metricHelpers.filterNewMetrics(metrics, lastDay);
|
|
38
|
+
logger.info(
|
|
39
|
+
`[discoverOrganizationTeamMetrics] Found ${newMetrics.length} new metrics for team ${team.slug} to insert`
|
|
40
|
+
);
|
|
41
|
+
if (newMetrics.length > 0) {
|
|
42
|
+
await batchInsert.batchInsertInChunks(
|
|
43
|
+
metricHelpers.prepareMetricsForInsert(newMetrics, type, team.slug),
|
|
44
|
+
30,
|
|
45
|
+
async (chunk) => {
|
|
46
|
+
await db.batchInsert(chunk);
|
|
47
|
+
}
|
|
48
|
+
);
|
|
49
|
+
logger.info(
|
|
50
|
+
`[discoverOrganizationTeamMetrics] Successfully inserted new metrics for team ${team.slug} into the database`
|
|
51
|
+
);
|
|
52
|
+
} else {
|
|
53
|
+
logger.info(
|
|
54
|
+
`[discoverOrganizationTeamMetrics] No new metrics found for team ${team.slug} to insert`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
} catch (error) {
|
|
58
|
+
logger.error(
|
|
59
|
+
`[discoverOrganizationTeamMetrics] Error processing metrics for team ${team.slug}: ${error}`
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
} catch (error) {
|
|
64
|
+
logger.error(
|
|
65
|
+
`[discoverOrganizationTeamMetrics] Error fetching teams: ${error}`
|
|
66
|
+
);
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
exports.discoverOrganizationTeamMetrics = discoverOrganizationTeamMetrics;
|
|
72
|
+
//# sourceMappingURL=OrganizationTeamTask.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OrganizationTeamTask.cjs.js","sources":["../../src/task/OrganizationTeamTask.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { MetricsType } from '@backstage-community/plugin-copilot-common';\nimport { MetricDbRow } from '../db/DatabaseHandler';\nimport { batchInsertInChunks } from '../utils/batchInsert';\nimport {\n filterNewMetrics,\n prepareMetricsForInsert,\n} from '../utils/metricHelpers';\nimport { TaskOptions } from './TaskManagement';\n\nexport async function discoverOrganizationTeamMetrics({\n api,\n logger,\n db,\n config,\n}: TaskOptions): Promise<void> {\n if (!config.getOptionalString('copilot.organization')) {\n logger.info(\n '[discoverOrganizationTeamMetrics] Skipping: Organization configuration not found.',\n );\n return;\n }\n\n const type: MetricsType = 'organization';\n\n try {\n const teams = await api.fetchOrganizationTeams();\n logger.info(\n `[discoverOrganizationTeamMetrics] Fetched ${teams.length} teams`,\n );\n\n for (const team of teams) {\n try {\n logger.info(\n `[discoverOrganizationTeamMetrics] Fetching metrics for team: ${team.slug}`,\n );\n\n const metrics = await api.fetchOrganizationTeamCopilotUsage(team.slug);\n logger.info(\n `[discoverOrganizationTeamMetrics] Fetched ${metrics.length} metrics for team: ${team.slug}`,\n );\n\n const lastDay = await db.getMostRecentDayFromMetrics(type, team.slug);\n logger.info(\n `[discoverOrganizationTeamMetrics] Found last processed day for team ${team.slug}: ${lastDay}`,\n );\n\n const newMetrics = filterNewMetrics(metrics, lastDay);\n logger.info(\n `[discoverOrganizationTeamMetrics] Found ${newMetrics.length} new metrics for team ${team.slug} to insert`,\n );\n\n if (newMetrics.length > 0) {\n await batchInsertInChunks<MetricDbRow>(\n prepareMetricsForInsert(newMetrics, type, team.slug),\n 30,\n async (chunk: MetricDbRow[]) => {\n await db.batchInsert(chunk);\n },\n );\n logger.info(\n `[discoverOrganizationTeamMetrics] Successfully inserted new metrics for team ${team.slug} into the database`,\n );\n } else {\n logger.info(\n `[discoverOrganizationTeamMetrics] No new metrics found for team ${team.slug} to insert`,\n );\n }\n } catch (error) {\n logger.error(\n `[discoverOrganizationTeamMetrics] Error processing metrics for team ${team.slug}: ${error}`,\n );\n }\n }\n } catch (error) {\n logger.error(\n `[discoverOrganizationTeamMetrics] Error fetching teams: ${error}`,\n );\n throw error;\n }\n}\n"],"names":["filterNewMetrics","batchInsertInChunks","prepareMetricsForInsert"],"mappings":";;;;;AAyBA,eAAsB,+BAAgC,CAAA;AAAA,EACpD,GAAA;AAAA,EACA,MAAA;AAAA,EACA,EAAA;AAAA,EACA,MAAA;AACF,CAA+B,EAAA;AAC7B,EAAA,IAAI,CAAC,MAAA,CAAO,iBAAkB,CAAA,sBAAsB,CAAG,EAAA;AACrD,IAAO,MAAA,CAAA,IAAA;AAAA,MACL,mFAAA;AAAA,KACF,CAAA;AACA,IAAA,OAAA;AAAA,GACF;AAEA,EAAA,MAAM,IAAoB,GAAA,cAAA,CAAA;AAE1B,EAAI,IAAA;AACF,IAAM,MAAA,KAAA,GAAQ,MAAM,GAAA,CAAI,sBAAuB,EAAA,CAAA;AAC/C,IAAO,MAAA,CAAA,IAAA;AAAA,MACL,CAAA,0CAAA,EAA6C,MAAM,MAAM,CAAA,MAAA,CAAA;AAAA,KAC3D,CAAA;AAEA,IAAA,KAAA,MAAW,QAAQ,KAAO,EAAA;AACxB,MAAI,IAAA;AACF,QAAO,MAAA,CAAA,IAAA;AAAA,UACL,CAAA,6DAAA,EAAgE,KAAK,IAAI,CAAA,CAAA;AAAA,SAC3E,CAAA;AAEA,QAAA,MAAM,OAAU,GAAA,MAAM,GAAI,CAAA,iCAAA,CAAkC,KAAK,IAAI,CAAA,CAAA;AACrE,QAAO,MAAA,CAAA,IAAA;AAAA,UACL,CAA6C,0CAAA,EAAA,OAAA,CAAQ,MAAM,CAAA,mBAAA,EAAsB,KAAK,IAAI,CAAA,CAAA;AAAA,SAC5F,CAAA;AAEA,QAAA,MAAM,UAAU,MAAM,EAAA,CAAG,2BAA4B,CAAA,IAAA,EAAM,KAAK,IAAI,CAAA,CAAA;AACpE,QAAO,MAAA,CAAA,IAAA;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+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var OrganizationTask = require('./OrganizationTask.cjs.js');
|
|
6
|
+
var OrganizationTeamTask = require('./OrganizationTeamTask.cjs.js');
|
|
7
|
+
var EnterpriseTask = require('./EnterpriseTask.cjs.js');
|
|
8
|
+
var EnterpriseTeamTask = require('./EnterpriseTeamTask.cjs.js');
|
|
9
|
+
|
|
10
|
+
class TaskManagement {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.options = options;
|
|
13
|
+
this.tasks = [
|
|
14
|
+
() => OrganizationTask.discoverOrganizationMetrics(this.options),
|
|
15
|
+
() => OrganizationTeamTask.discoverOrganizationTeamMetrics(this.options),
|
|
16
|
+
() => EnterpriseTask.discoverEnterpriseMetrics(this.options),
|
|
17
|
+
() => EnterpriseTeamTask.discoverEnterpriseTeamMetrics(this.options)
|
|
18
|
+
];
|
|
19
|
+
}
|
|
20
|
+
tasks;
|
|
21
|
+
static create(options) {
|
|
22
|
+
return new TaskManagement(options);
|
|
23
|
+
}
|
|
24
|
+
async runAsync() {
|
|
25
|
+
this.options.logger.info(
|
|
26
|
+
`[TaskManagement] Starting processing of ${this.tasks.length} tasks`
|
|
27
|
+
);
|
|
28
|
+
const taskPromises = this.tasks.map(async (task) => {
|
|
29
|
+
try {
|
|
30
|
+
await task();
|
|
31
|
+
} catch (e) {
|
|
32
|
+
this.options.logger.warn(
|
|
33
|
+
`[TaskManagement] Failed to process task: ${e.message}`
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
await Promise.all(taskPromises);
|
|
38
|
+
this.options.logger.info(
|
|
39
|
+
`[TaskManagement] Completed processing of all tasks`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
exports.default = TaskManagement;
|
|
45
|
+
//# sourceMappingURL=TaskManagement.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TaskManagement.cjs.js","sources":["../../src/task/TaskManagement.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { Config } from '@backstage/config';\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport { GithubClient } from '../client/GithubClient';\nimport { DatabaseHandler } from '../db/DatabaseHandler';\nimport { discoverOrganizationMetrics } from './OrganizationTask';\nimport { discoverOrganizationTeamMetrics } from './OrganizationTeamTask';\nimport { discoverEnterpriseMetrics } from './EnterpriseTask';\nimport { discoverEnterpriseTeamMetrics } from './EnterpriseTeamTask';\n\nexport type TaskOptions = {\n api: GithubClient;\n config: Config;\n logger: LoggerService;\n db: DatabaseHandler;\n};\n\nexport default class TaskManagement {\n private readonly tasks: Array<() => Promise<void>>;\n\n constructor(private readonly options: TaskOptions) {\n this.tasks = [\n () => discoverOrganizationMetrics(this.options),\n () => discoverOrganizationTeamMetrics(this.options),\n () => discoverEnterpriseMetrics(this.options),\n () => discoverEnterpriseTeamMetrics(this.options),\n ];\n }\n\n static create(options: TaskOptions) {\n return new TaskManagement(options);\n }\n\n async runAsync() {\n this.options.logger.info(\n `[TaskManagement] Starting processing of ${this.tasks.length} tasks`,\n );\n\n const taskPromises = this.tasks.map(async task => {\n try {\n await task();\n } catch (e) {\n this.options.logger.warn(\n `[TaskManagement] Failed to process task: ${e.message}`,\n );\n }\n });\n\n await Promise.all(taskPromises);\n\n this.options.logger.info(\n `[TaskManagement] Completed processing of all tasks`,\n );\n }\n}\n"],"names":["discoverOrganizationMetrics","discoverOrganizationTeamMetrics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CAAA;AAAA,GACF;AACF;;;;"}
|
|
@@ -4,15 +4,12 @@ var integration = require('@backstage/integration');
|
|
|
4
4
|
|
|
5
5
|
const getGithubInfo = async (config) => {
|
|
6
6
|
const integrations = integration.ScmIntegrations.fromConfig(config);
|
|
7
|
-
const credentialsProvider = integration.DefaultGithubCredentialsProvider.fromIntegrations(integrations);
|
|
8
7
|
const host = config.getString("copilot.host");
|
|
9
|
-
const enterprise = config.
|
|
8
|
+
const enterprise = config.getOptionalString("copilot.enterprise");
|
|
9
|
+
const organization = config.getOptionalString("copilot.organization");
|
|
10
10
|
if (!host) {
|
|
11
11
|
throw new Error("The host configuration is missing from the config.");
|
|
12
12
|
}
|
|
13
|
-
if (!enterprise) {
|
|
14
|
-
throw new Error("The enterprise configuration is missing from the config.");
|
|
15
|
-
}
|
|
16
13
|
const githubConfig = integrations.github.byHost(host)?.config;
|
|
17
14
|
if (!githubConfig) {
|
|
18
15
|
throw new Error(
|
|
@@ -20,16 +17,16 @@ const getGithubInfo = async (config) => {
|
|
|
20
17
|
);
|
|
21
18
|
}
|
|
22
19
|
const apiBaseUrl = githubConfig.apiBaseUrl ?? "https://api.github.com";
|
|
23
|
-
const credentials =
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
20
|
+
const credentials = {
|
|
21
|
+
type: "token",
|
|
22
|
+
headers: { Authorization: `Bearer ${githubConfig.token}` },
|
|
23
|
+
token: githubConfig.token
|
|
24
|
+
};
|
|
29
25
|
return {
|
|
30
26
|
apiBaseUrl,
|
|
31
27
|
credentials,
|
|
32
|
-
enterprise
|
|
28
|
+
enterprise,
|
|
29
|
+
organization
|
|
33
30
|
};
|
|
34
31
|
};
|
|
35
32
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GithubUtils.cjs.js","sources":["../../src/utils/GithubUtils.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport {
|
|
1
|
+
{"version":3,"file":"GithubUtils.cjs.js","sources":["../../src/utils/GithubUtils.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { GithubCredentials, ScmIntegrations } from '@backstage/integration';\n\nexport type GithubInfo = {\n credentials: GithubCredentials;\n apiBaseUrl: string;\n enterprise?: string;\n organization?: string;\n};\n\nexport const getGithubInfo = async (config: Config): Promise<GithubInfo> => {\n const integrations = ScmIntegrations.fromConfig(config);\n\n const host = config.getString('copilot.host');\n const enterprise = config.getOptionalString('copilot.enterprise');\n const organization = config.getOptionalString('copilot.organization');\n\n if (!host) {\n throw new Error('The host configuration is missing from the config.');\n }\n\n const githubConfig = integrations.github.byHost(host)?.config;\n\n if (!githubConfig) {\n throw new Error(\n `GitHub configuration for host \"${host}\" is missing or incomplete.`,\n );\n }\n\n const apiBaseUrl = githubConfig.apiBaseUrl ?? 'https://api.github.com';\n\n const credentials: GithubCredentials = {\n type: 'token',\n headers: { Authorization: `Bearer ${githubConfig.token}` },\n token: githubConfig.token,\n };\n\n return {\n apiBaseUrl,\n credentials,\n enterprise,\n organization,\n };\n};\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data.length; i += chunkSize) {
|
|
5
|
+
const chunk = data.slice(i, i + chunkSize);
|
|
6
|
+
await batchInsertFunc(chunk);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
exports.batchInsertInChunks = batchInsertInChunks;
|
|
11
|
+
//# sourceMappingURL=batchInsert.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"batchInsert.cjs.js","sources":["../../src/utils/batchInsert.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nexport async function batchInsertInChunks<T>(\n data: T[],\n chunkSize: number,\n batchInsertFunc: (chunk: T[]) => Promise<void>,\n): Promise<void> {\n for (let i = 0; i < data.length; i += chunkSize) {\n const chunk = data.slice(i, i + chunkSize);\n await batchInsertFunc(chunk);\n }\n}\n"],"names":[],"mappings":";;AAesB,eAAA,mBAAA,CACpB,IACA,EAAA,SAAA,EACA,eACe,EAAA;AACf,EAAA,KAAA,IAAS,IAAI,CAAG,EAAA,CAAA,GAAI,IAAK,CAAA,MAAA,EAAQ,KAAK,SAAW,EAAA;AAC/C,IAAA,MAAM,KAAQ,GAAA,IAAA,CAAK,KAAM,CAAA,CAAA,EAAG,IAAI,SAAS,CAAA,CAAA;AACzC,IAAA,MAAM,gBAAgB,KAAK,CAAA,CAAA;AAAA,GAC7B;AACF;;;;"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var luxon = require('luxon');
|
|
4
|
+
|
|
5
|
+
function filterNewMetrics(metrics, lastDay) {
|
|
6
|
+
return metrics.sort(
|
|
7
|
+
(a, b) => luxon.DateTime.fromISO(a.day).toMillis() - luxon.DateTime.fromISO(b.day).toMillis()
|
|
8
|
+
).filter((metric) => {
|
|
9
|
+
const metricDate = luxon.DateTime.fromISO(metric.day);
|
|
10
|
+
const lastDayDate = lastDay ? luxon.DateTime.fromJSDate(new Date(lastDay)) : null;
|
|
11
|
+
return !lastDay || lastDayDate?.isValid && metricDate > lastDayDate;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function prepareMetricsForInsert(metrics, type, team_name) {
|
|
15
|
+
return metrics.map(({ breakdown, ...rest }) => ({
|
|
16
|
+
...rest,
|
|
17
|
+
type,
|
|
18
|
+
team_name,
|
|
19
|
+
breakdown: JSON.stringify(breakdown)
|
|
20
|
+
}));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
exports.filterNewMetrics = filterNewMetrics;
|
|
24
|
+
exports.prepareMetricsForInsert = prepareMetricsForInsert;
|
|
25
|
+
//# sourceMappingURL=metricHelpers.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metricHelpers.cjs.js","sources":["../../src/utils/metricHelpers.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { DateTime } from 'luxon';\nimport { MetricDbRow } from '../db/DatabaseHandler';\nimport {\n Metric,\n MetricsType,\n} from '@backstage-community/plugin-copilot-common';\n\nexport function filterNewMetrics(\n metrics: Metric[],\n lastDay?: string,\n): Metric[] {\n return metrics\n .sort(\n (a, b) =>\n DateTime.fromISO(a.day).toMillis() - DateTime.fromISO(b.day).toMillis(),\n )\n .filter(metric => {\n const metricDate = DateTime.fromISO(metric.day);\n\n const lastDayDate = lastDay\n ? DateTime.fromJSDate(new Date(lastDay))\n : null;\n\n return !lastDay || (lastDayDate?.isValid && metricDate > lastDayDate);\n });\n}\n\nexport function prepareMetricsForInsert(\n metrics: Metric[],\n type: MetricsType,\n team_name?: string,\n): MetricDbRow[] {\n return metrics.map(({ breakdown, ...rest }) => ({\n ...rest,\n type,\n team_name,\n breakdown: JSON.stringify(breakdown),\n })) as MetricDbRow[];\n}\n"],"names":["DateTime"],"mappings":";;;;AAsBgB,SAAA,gBAAA,CACd,SACA,OACU,EAAA;AACV,EAAA,OAAO,OACJ,CAAA,IAAA;AAAA,IACC,CAAC,CAAA,EAAG,CACF,KAAAA,cAAA,CAAS,QAAQ,CAAE,CAAA,GAAG,CAAE,CAAA,QAAA,KAAaA,cAAS,CAAA,OAAA,CAAQ,CAAE,CAAA,GAAG,EAAE,QAAS,EAAA;AAAA,GAC1E,CACC,OAAO,CAAU,MAAA,KAAA;AAChB,IAAA,MAAM,UAAa,GAAAA,cAAA,CAAS,OAAQ,CAAA,MAAA,CAAO,GAAG,CAAA,CAAA;AAE9C,IAAM,MAAA,WAAA,GAAc,UAChBA,cAAS,CAAA,UAAA,CAAW,IAAI,IAAK,CAAA,OAAO,CAAC,CACrC,GAAA,IAAA,CAAA;AAEJ,IAAA,OAAO,CAAC,OAAA,IAAY,WAAa,EAAA,OAAA,IAAW,UAAa,GAAA,WAAA,CAAA;AAAA,GAC1D,CAAA,CAAA;AACL,CAAA;AAEgB,SAAA,uBAAA,CACd,OACA,EAAA,IAAA,EACA,SACe,EAAA;AACf,EAAA,OAAO,QAAQ,GAAI,CAAA,CAAC,EAAE,SAAW,EAAA,GAAG,MAAY,MAAA;AAAA,IAC9C,GAAG,IAAA;AAAA,IACH,IAAA;AAAA,IACA,SAAA;AAAA,IACA,SAAA,EAAW,IAAK,CAAA,SAAA,CAAU,SAAS,CAAA;AAAA,GACnC,CAAA,CAAA,CAAA;AACJ;;;;;"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2024 The Backstage Authors
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* @param {import('knex').Knex} knex
|
|
18
|
+
*/
|
|
19
|
+
exports.up = async function up(knex) {
|
|
20
|
+
await knex.schema.table('metrics', table => {
|
|
21
|
+
table
|
|
22
|
+
.string('type', 50)
|
|
23
|
+
.defaultTo('enterprise')
|
|
24
|
+
.notNullable()
|
|
25
|
+
.comment('Type of the metrics data: enterprise, organization');
|
|
26
|
+
|
|
27
|
+
table.string('team_name', 255).nullable().comment('Name of the team');
|
|
28
|
+
|
|
29
|
+
table.dropPrimary();
|
|
30
|
+
|
|
31
|
+
table.unique(['day', 'type', 'team_name'], 'uk_day_type_team_name');
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {import('knex').Knex} knex
|
|
37
|
+
*/
|
|
38
|
+
exports.down = async function down(knex) {
|
|
39
|
+
await knex.schema.table('metrics', table => {
|
|
40
|
+
table.dropUnique(['day', 'type', 'team_name']);
|
|
41
|
+
|
|
42
|
+
table.dropColumn('type');
|
|
43
|
+
table.dropColumn('team_name');
|
|
44
|
+
|
|
45
|
+
table.primary('day');
|
|
46
|
+
table.index('day', 'idx_metrics_day');
|
|
47
|
+
});
|
|
48
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage-community/plugin-copilot-backend",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"homepage": "https://backstage.io",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/index.cjs.js",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"postpack": "backstage-cli package postpack"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@backstage-community/plugin-copilot-common": "^0.
|
|
42
|
+
"@backstage-community/plugin-copilot-common": "^0.3.0",
|
|
43
43
|
"@backstage/backend-app-api": "^1.0.1",
|
|
44
44
|
"@backstage/backend-defaults": "^0.5.2",
|
|
45
45
|
"@backstage/backend-plugin-api": "^1.0.1",
|
|
@@ -54,7 +54,8 @@
|
|
|
54
54
|
"luxon": "^3.5.0",
|
|
55
55
|
"node-fetch": "^2.6.7",
|
|
56
56
|
"winston": "^3.2.1",
|
|
57
|
-
"yn": "^4.0.0"
|
|
57
|
+
"yn": "^4.0.0",
|
|
58
|
+
"zod": "^3.23.8"
|
|
58
59
|
},
|
|
59
60
|
"devDependencies": {
|
|
60
61
|
"@backstage/backend-test-utils": "^1.0.2",
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var luxon = require('luxon');
|
|
6
|
-
|
|
7
|
-
class Scheduler {
|
|
8
|
-
constructor(options) {
|
|
9
|
-
this.options = options;
|
|
10
|
-
}
|
|
11
|
-
static create(options) {
|
|
12
|
-
return new Scheduler(options);
|
|
13
|
-
}
|
|
14
|
-
async run() {
|
|
15
|
-
try {
|
|
16
|
-
this.options.logger.info("Starting Github Copilot Processor");
|
|
17
|
-
const copilotMetrics = await this.options.api.getCopilotUsageDataForEnterprise();
|
|
18
|
-
this.options.logger.info(`Fetched ${copilotMetrics.length} metrics`);
|
|
19
|
-
const lastDay = await this.options.db.getMostRecentDayFromMetrics();
|
|
20
|
-
this.options.logger.info(`Found last day: ${lastDay}`);
|
|
21
|
-
const diff = copilotMetrics.sort(
|
|
22
|
-
(a, b) => luxon.DateTime.fromISO(a.day).toMillis() - luxon.DateTime.fromISO(b.day).toMillis()
|
|
23
|
-
).filter((metric) => {
|
|
24
|
-
const metricDate = luxon.DateTime.fromISO(metric.day);
|
|
25
|
-
const lastDayDate = lastDay ? luxon.DateTime.fromISO(lastDay) : null;
|
|
26
|
-
return !lastDayDate || metricDate > lastDayDate;
|
|
27
|
-
}).map(({ breakdown, ...rest }) => ({
|
|
28
|
-
...rest,
|
|
29
|
-
breakdown: JSON.stringify(breakdown)
|
|
30
|
-
}));
|
|
31
|
-
this.options.logger.info(`Found ${diff.length} new metrics to insert`);
|
|
32
|
-
if (diff.length > 0) {
|
|
33
|
-
await batchInsertInChunks(
|
|
34
|
-
diff,
|
|
35
|
-
30,
|
|
36
|
-
async (chunk) => {
|
|
37
|
-
await this.options.db.batchInsert(chunk);
|
|
38
|
-
}
|
|
39
|
-
);
|
|
40
|
-
this.options.logger.info("Inserted new metrics into the database");
|
|
41
|
-
} else {
|
|
42
|
-
this.options.logger.info("No new metrics found to insert");
|
|
43
|
-
}
|
|
44
|
-
} catch (error) {
|
|
45
|
-
this.options.logger.error(
|
|
46
|
-
`An error occurred while processing Github Copilot metrics: ${error}`
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
async function batchInsertInChunks(data, chunkSize, batchInsertFunc) {
|
|
52
|
-
for (let i = 0; i < data.length; i += chunkSize) {
|
|
53
|
-
const chunk = data.slice(i, i + chunkSize);
|
|
54
|
-
await batchInsertFunc(chunk);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
exports.default = Scheduler;
|
|
59
|
-
//# sourceMappingURL=Scheduler.cjs.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Scheduler.cjs.js","sources":["../../src/task/Scheduler.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport { GithubClient } from '../client/GithubClient';\nimport { DatabaseHandler, MetricDbRow } from '../db/DatabaseHandler';\nimport { Config } from '@backstage/config';\nimport { DateTime } from 'luxon';\n\ntype Options = {\n api: GithubClient;\n config: Config;\n logger: LoggerService;\n db: DatabaseHandler;\n};\n\nexport default class Scheduler {\n constructor(private readonly options: Options) {}\n\n static create(options: Options) {\n return new Scheduler(options);\n }\n\n async run() {\n try {\n this.options.logger.info('Starting Github Copilot Processor');\n\n const copilotMetrics =\n await this.options.api.getCopilotUsageDataForEnterprise();\n this.options.logger.info(`Fetched ${copilotMetrics.length} metrics`);\n\n const lastDay = await this.options.db.getMostRecentDayFromMetrics();\n this.options.logger.info(`Found last day: ${lastDay}`);\n\n const diff = copilotMetrics\n .sort(\n (a, b) =>\n DateTime.fromISO(a.day).toMillis() -\n DateTime.fromISO(b.day).toMillis(),\n )\n .filter(metric => {\n const metricDate = DateTime.fromISO(metric.day);\n const lastDayDate = lastDay ? DateTime.fromISO(lastDay) : null;\n return !lastDayDate || metricDate > lastDayDate;\n })\n .map(({ breakdown, ...rest }) => ({\n ...rest,\n breakdown: JSON.stringify(breakdown),\n }));\n\n this.options.logger.info(`Found ${diff.length} new metrics to insert`);\n\n if (diff.length > 0) {\n await batchInsertInChunks<MetricDbRow>(\n diff,\n 30,\n async (chunk: MetricDbRow[]) => {\n await this.options.db.batchInsert(chunk);\n },\n );\n this.options.logger.info('Inserted new metrics into the database');\n } else {\n this.options.logger.info('No new metrics found to insert');\n }\n } catch (error) {\n this.options.logger.error(\n `An error occurred while processing Github Copilot metrics: ${error}`,\n );\n }\n }\n}\n\nasync function batchInsertInChunks<T>(\n data: T[],\n chunkSize: number,\n batchInsertFunc: (chunk: T[]) => Promise<void>,\n): Promise<void> {\n for (let i = 0; i < data.length; i += chunkSize) {\n const chunk = data.slice(i, i + chunkSize);\n await batchInsertFunc(chunk);\n }\n}\n"],"names":["DateTime"],"mappings":";;;;;;AA6BA,MAAqB,SAAU,CAAA;AAAA,EAC7B,YAA6B,OAAkB,EAAA;AAAlB,IAAA,IAAA,CAAA,OAAA,GAAA,OAAA,CAAA;AAAA,GAAmB;AAAA,EAEhD,OAAO,OAAO,OAAkB,EAAA;AAC9B,IAAO,OAAA,IAAI,UAAU,OAAO,CAAA,CAAA;AAAA,GAC9B;AAAA,EAEA,MAAM,GAAM,GAAA;AACV,IAAI,IAAA;AACF,MAAK,IAAA,CAAA,OAAA,CAAQ,MAAO,CAAA,IAAA,CAAK,mCAAmC,CAAA,CAAA;AAE5D,MAAA,MAAM,cACJ,GAAA,MAAM,IAAK,CAAA,OAAA,CAAQ,IAAI,gCAAiC,EAAA,CAAA;AAC1D,MAAA,IAAA,CAAK,QAAQ,MAAO,CAAA,IAAA,CAAK,CAAW,QAAA,EAAA,cAAA,CAAe,MAAM,CAAU,QAAA,CAAA,CAAA,CAAA;AAEnE,MAAA,MAAM,OAAU,GAAA,MAAM,IAAK,CAAA,OAAA,CAAQ,GAAG,2BAA4B,EAAA,CAAA;AAClE,MAAA,IAAA,CAAK,OAAQ,CAAA,MAAA,CAAO,IAAK,CAAA,CAAA,gBAAA,EAAmB,OAAO,CAAE,CAAA,CAAA,CAAA;AAErD,MAAA,M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