Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,40 @@ This extension allows you to track the carbon emissions of your code directly fr
- Total CO₂ emissions since tracking started
- Save it into a csv file for further analysis.

## Commands

Use the Command Palette (`Ctrl/Cmd+Shift+P`) and run:

- `Codecarbon: Start tracking emissions`
- `Codecarbon: Stop tracking emissions`
- `Codecarbon: Restart tracking emissions`
- `Codecarbon: Open tracking logs`

## Optional keybindings

If you want keyboard shortcuts, add these to your `keybindings.json`:

```json
[
{
"key": "ctrl+alt+e",
"command": "codecarbon.start"
},
{
"key": "ctrl+alt+shift+e",
"command": "codecarbon.stop"
},
{
"key": "ctrl+alt+r",
"command": "codecarbon.restart"
},
{
"key": "ctrl+alt+l",
"command": "codecarbon.openLogs"
}
]
```

## Requirements

The extension uses `codecarbon` to measure the carbon emissions. This package connects to your hardware via specific APIs to get to know the power usage of your CPU/GPU/RAM. These APIs depend on the brand and OS. See https://mlco2.github.io/codecarbon/methodology.html#power-usage for the needed tools for your specific setup.
Expand Down
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@
"category": "Codecarbon",
"command": "codecarbon.stop"
},
{
"title": "Restart tracking emissions",
"category": "Codecarbon",
"command": "codecarbon.restart"
},
{
"title": "Open tracking logs",
"category": "Codecarbon",
"command": "codecarbon.openLogs"
},
{
"title": "Check codecarbon version",
"category": "Codecarbon",
Expand Down Expand Up @@ -131,4 +141,4 @@
"prettier": "^3.8.1",
"typescript": "^5.9.3"
}
}
}
15 changes: 15 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
context.subscriptions.push(
vscode.commands.registerCommand(COMMANDS.START, () => startTracker()),
vscode.commands.registerCommand(COMMANDS.STOP, () => stopTracker()),
vscode.commands.registerCommand(COMMANDS.RESTART, () => restartTracker()),
vscode.commands.registerCommand(COMMANDS.OPEN_LOGS, () => openTrackingLogs()),
vscode.commands.registerCommand(COMMANDS.CHECK_VERSION, () => checkCodecarbonVersion()),
vscode.commands.registerCommand(COMMANDS.INSTALL_REPAIR, () => installRepairCodecarbon()),
vscode.commands.registerCommand(COMMANDS.OPEN_CONFIG, () => openCodecarbonConfig()),
Expand Down Expand Up @@ -78,6 +80,19 @@ async function stopTracker(): Promise<void> {
}
}

async function restartTracker(): Promise<void> {
logService.log(MESSAGES.RESTARTING);
if (trackerService.isRunning()) {
trackerService.stop();
statusBarManager.setStoppedState();
}
await startTracker();
}

function openTrackingLogs(): void {
logService.getOutputChannel().show(true);
}

async function checkCodecarbonVersion(): Promise<void> {
const pythonPath = ConfigService.getPythonPath();
await pythonService.checkCodecarbonVersion(pythonPath);
Expand Down
7 changes: 3 additions & 4 deletions src/services/trackerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ export class TrackerService {
*/
public async start(): Promise<boolean> {
if (this.startInProgress) {
vscode.window.showInformationMessage(MESSAGES.START_IN_PROGRESS);
this.logService.log(MESSAGES.START_IN_PROGRESS);
return false;
}
if (this.pythonProcess) {
vscode.window.showInformationMessage(MESSAGES.ALREADY_RUNNING);
this.logService.log(MESSAGES.ALREADY_RUNNING);
return false;
}
this.startInProgress = true;
Expand Down Expand Up @@ -85,7 +85,7 @@ export class TrackerService {
*/
public stop(): boolean {
if (!this.pythonProcess) {
vscode.window.showInformationMessage(MESSAGES.NOT_RUNNING);
this.logService.log(MESSAGES.NOT_RUNNING);
return false;
}

Expand All @@ -94,7 +94,6 @@ export class TrackerService {
this.pythonProcess = null;

this.logService.log(MESSAGES.TRACKER_STOPPED);
vscode.window.showInformationMessage(MESSAGES.TRACKER_STOPPED);
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const DEFAULT_STATUS_BAR_TEXT = '$(pulse) Codecarbon';
export const COMMANDS = {
START: 'codecarbon.start',
STOP: 'codecarbon.stop',
RESTART: 'codecarbon.restart',
OPEN_LOGS: 'codecarbon.openLogs',
CHECK_VERSION: 'codecarbon.checkVersion',
INSTALL_REPAIR: 'codecarbon.installRepair',
OPEN_CONFIG: 'codecarbon.openConfig',
Expand All @@ -23,6 +25,7 @@ export const CONFIGURATION_KEYS = {
export const MESSAGES = {
ALREADY_RUNNING: 'Codecarbon tracker is already running.',
NOT_RUNNING: 'Codecarbon tracker is not running.',
RESTARTING: 'Restarting Codecarbon tracker.',
TRACKER_STARTED: 'Codecarbon tracker started',
TRACKER_STOPPED: 'Codecarbon tracker stopped',
EXTENSION_ACTIVATED: 'Codecarbon extension activated',
Expand Down