WakaTime plugin (#90)
This commit is contained in:
36
source/plugins/wakatime/README.md
Normal file
36
source/plugins/wakatime/README.md
Normal file
@@ -0,0 +1,36 @@
|
||||
### ⏰ WakaTime plugin
|
||||
|
||||
The *wakatime* plugin displays statistics from your [WakaTime](https://wakatime.com) account.
|
||||
|
||||
<table>
|
||||
<td align="center">
|
||||
<img src="https://github.com/lowlighter/lowlighter/blob/master/metrics.plugin.wakatime.svg">
|
||||
<img width="900" height="1" alt="">
|
||||
</td>
|
||||
</table>
|
||||
|
||||
<details>
|
||||
<summary>💬 Obtaining a WakaTime token</summary>
|
||||
|
||||
Create a [WakaTime account](https://wakatime.com) and retrieve your API key in your [Account settings](https://wakatime.com/settings/account).
|
||||
|
||||

|
||||
|
||||
Then setup [WakaTime plugins](https://wakatime.com/plugins) to be ready to go!
|
||||
|
||||
</details>
|
||||
|
||||
#### ℹ️ Examples workflows
|
||||
|
||||
[➡️ Available options for this plugin](metadata.yml)
|
||||
|
||||
```yaml
|
||||
- uses: lowlighter/metrics@latest
|
||||
with:
|
||||
# ... other options
|
||||
plugin_wakatime: yes # (🚧 @master feature)
|
||||
plugin_wakatime_token: ${{ secrets.WAKATIME_TOKEN }} # Required
|
||||
plugin_wakatime_days: 7 # Display last week stats
|
||||
plugin_wakatime_sections: time, projects, projects-graphs # Display time and projects sections, along with projects graphs
|
||||
plugin_wakatime_limit: 4 # Show 4 entries per graph
|
||||
```
|
||||
45
source/plugins/wakatime/index.mjs
Normal file
45
source/plugins/wakatime/index.mjs
Normal file
@@ -0,0 +1,45 @@
|
||||
//Setup
|
||||
export default async function ({login, q, imports, data, account}, {enabled = false, token} = {}) {
|
||||
//Plugin execution
|
||||
try {
|
||||
//Check if plugin is enabled and requirements are met
|
||||
if ((!enabled)||(!q.wakatime))
|
||||
return null
|
||||
|
||||
//Load inputs
|
||||
let {sections, days, limit} = imports.metadata.plugins.wakatime.inputs({data, account, q})
|
||||
if (!limit)
|
||||
limit = void(limit)
|
||||
const range = {"7":"last_7_days", "30":"last_30_days", "180":"last_6_months", "365":"last_year"}[days] ?? "last_7_days"
|
||||
|
||||
//Querying api and format result
|
||||
//https://wakatime.com/developers#stats
|
||||
console.debug(`metrics/compute/${login}/plugins > wakatime > querying api`)
|
||||
const {data:{data:stats}} = await imports.axios.get(`https://wakatime.com/api/v1/users/current/stats/${range}?api_key=${token}`)
|
||||
const result = {
|
||||
sections,
|
||||
days,
|
||||
time:{
|
||||
total:stats.total_seconds/(60*60),
|
||||
daily:stats.daily_average/(60*60),
|
||||
},
|
||||
projects:stats.projects.map(({name, percent, total_seconds:total}) => ({name, percent:percent/100, total})).sort((a, b) => b.percent - a.percent).slice(0, limit),
|
||||
languages:stats.languages.map(({name, percent, total_seconds:total}) => ({name, percent:percent/100, total})).sort((a, b) => b.percent - a.percent).slice(0, limit),
|
||||
os:stats.operating_systems.map(({name, percent, total_seconds:total}) => ({name, percent:percent/100, total})).sort((a, b) => b.percent - a.percent).slice(0, limit),
|
||||
editors:stats.editors.map(({name, percent, total_seconds:total}) => ({name, percent:percent/100, total})).sort((a, b) => b.percent - a.percent).slice(0, limit),
|
||||
}
|
||||
|
||||
//Result
|
||||
return result
|
||||
}
|
||||
//Handle errors
|
||||
catch (error) {
|
||||
let message = "An error occured"
|
||||
if (error.isAxiosError) {
|
||||
const status = error.response?.status
|
||||
message = `API returned ${status}`
|
||||
error = error.response?.data ?? null
|
||||
}
|
||||
throw {error:{message, instance:error}}
|
||||
}
|
||||
}
|
||||
53
source/plugins/wakatime/metadata.yml
Normal file
53
source/plugins/wakatime/metadata.yml
Normal file
@@ -0,0 +1,53 @@
|
||||
name: "⏰ WakaTime plugin"
|
||||
cost: N/A
|
||||
supports:
|
||||
- user
|
||||
inputs:
|
||||
|
||||
# Enable or disable plugin
|
||||
plugin_wakatime:
|
||||
description: Display WakaTime stats
|
||||
type: boolean
|
||||
default: no
|
||||
|
||||
# WakaTime API token
|
||||
# See https://wakatime.com/settings/account get your API key
|
||||
plugin_wakatime_token:
|
||||
description: WakaTime API token
|
||||
type: token
|
||||
default: ""
|
||||
|
||||
# Time range to use for displayed stats
|
||||
plugin_wakatime_days:
|
||||
description: WakaTime time range
|
||||
type: string
|
||||
values:
|
||||
- 7 # Last week
|
||||
- 30 # Last month
|
||||
- 180 # Last 6 months
|
||||
- 365 # Last year
|
||||
default: 7
|
||||
|
||||
# Sections to display
|
||||
plugin_wakatime_sections:
|
||||
description: Sections to display
|
||||
type: array
|
||||
values:
|
||||
- time # Show total coding time and daily average
|
||||
- projects # Show most time spent project
|
||||
- projects-graphs # Show most time spent projects graphs
|
||||
- languages # Show most language
|
||||
- languages-graphs # Show languages graphs
|
||||
- editors # Show most used code editor
|
||||
- editors-graphs # Show code editors graphs
|
||||
- os # Show most used operating system
|
||||
- os-graphs # Show code operating systems graphs
|
||||
default: time, projects, projects-graphs, languages, languages-graphs, editors, os
|
||||
|
||||
# Number of entries to display per graph
|
||||
# Set to 0 to disable limitations
|
||||
plugin_wakatime_limit:
|
||||
description: Maximum number of entries to display per graph
|
||||
type: number
|
||||
default: 5
|
||||
min: 0
|
||||
15
source/plugins/wakatime/tests.yml
Normal file
15
source/plugins/wakatime/tests.yml
Normal file
@@ -0,0 +1,15 @@
|
||||
- name: WakaTime plugin (default)
|
||||
uses: lowlighter/metrics@latest
|
||||
with:
|
||||
token: NOT_NEEDED
|
||||
plugin_wakatime_token: MOCKED_TOKEN
|
||||
plugin_wakatime: yes
|
||||
|
||||
- name: WakaTime plugin (complete)
|
||||
uses: lowlighter/metrics@latest
|
||||
with:
|
||||
token: NOT_NEEDED
|
||||
plugin_wakatime_token: MOCKED_TOKEN
|
||||
plugin_wakatime: yes
|
||||
plugin_wakatime_limit: 4
|
||||
plugin_wakatime_sections: time, projects, projects-graphs, languages, languages-graphs, editors, editors-graphs, os, os-graphs
|
||||
Reference in New Issue
Block a user