DevOps-zhuang / copilot-metric-saver

call github copilot usage and seat API, then save fetched data to file or mysql for persistent save, then anlyze it.
2 stars 3 forks source link

Copilot Saver App

Based on GitHub Copilot Usage and Seat API,provide persistent save and multi-tenant features.

Since GitHubCopilot usage and seat API (see https://docs.github.com/en/rest/copilot/copilot-usage?apiVersion=2022-11-28) just return latest information. like copilot usage only returns latest 28 days usage. and manager API just return the current-latest activity. if we need a long period usage and seat for analysis. it needs a job to periodllyget such data and save it for persistent. so this project is target to meet such requirement.

This App works as backend, it will call GitHubAPI periodlly(each 12 hours by default), then save the fetched data to a file or MySQL for persistent storage, and expose some data by RestAPI, just as similar with GitHubCopilot API itself, the path is same, just update the endpoint by replacing https://api.github.com/ with http://YourServer:3000/api/

New Features

Support for GitHub Copilot Metrics (2024/11/15)

add docker support.

Support for GitHub Copilot Metrics (2024/11/12)

We have recently added support for GitHub Copilot Metrics. Here are some key features:

Features

Known issues

Installation

  1. Clone the repository:

    git clone https://github.com/DevOps-zhuang/copilot-metric-saver.git
    cd copilot-metric-saver
  2. Install dependencies:

    npm install
    npm install -g ts-node typescript
    
  3. Configure the database and other settings in .env. if you prefer to save the output as file format, no needed to update the database setting.

Usage

Running the Server on machine

Start the server:

ts-node src/server.ts

The server will run on http://localhost:3000. And for the overroll API, plese visit http://localhost:3000/api-docs. alt text

Running the Server in Docker

docker build -t copilot-metrics-saver . docker run -p 3000:3000 --env-file ./.env copilot-metrics-saver

API Endpoints

Fetch and Save Usage Data

GET /api/:scopeType/:scopeName/copilot/usage

Parameters:

Example:

curl -H "Authorization: Bearer YOUR_TOKEN" "http://localhost:3000/api/organization/example-org/copilot/usage"

Get Active Tenants

GET /tenants

Returns: A list of active tenants with their scopeType and scopeName.

Example:

curl "http://localhost:3000/tenants"

Project Structure

src/
├── api/
│   ├── GitHubApi.ts          # Contains functions to call GitHubAPIs.
│   ├── TenantServiceFactory.ts # Factory to create tenant service instances.
│   ├── UsageServiceFactory.ts  # Factory to create usage service instances.
│   ├── MySQLTenantStorage.ts   # Implementation of tenant storage using MySQL.
│   └── FileTenantStorage.ts    # Implementation of tenant storage using the file system.
├── model/
│   ├── Tenant.ts             # Tenant model class.
│   └── Metrics.ts            # Metrics model class.
└── server.ts                 # Main server file.

Tenant Model

The Tenant class represents a tenant and includes the following properties:

The Tenant class also includes a method to validate the tenant using the GitHubAPI.

License

This project is licensed under the MIT License. See the LICENSE file for details.