Add selfhosted wakatime integration (wakapi) (#276)
This commit is contained in:
@@ -1,44 +1,47 @@
|
|||||||
//Setup
|
//Setup
|
||||||
export default async function({login, q, imports, data, account}, {enabled = false, token} = {}) {
|
export default async function ({ login, q, imports, data, account },{ enabled = false, token } = {}) {
|
||||||
//Plugin execution
|
//Plugin execution
|
||||||
try {
|
try {
|
||||||
//Check if plugin is enabled and requirements are met
|
//Check if plugin is enabled and requirements are met
|
||||||
if ((!enabled)||(!q.wakatime))
|
if (!enabled || !q.wakatime) return null;
|
||||||
return null
|
|
||||||
|
|
||||||
//Load inputs
|
//Load inputs
|
||||||
let {sections, days, limit} = imports.metadata.plugins.wakatime.inputs({data, account, q})
|
let { sections, days, limit, url, user } = imports.metadata.plugins.wakatime.inputs({ data, account, q });
|
||||||
if (!limit)
|
if (!limit) limit = void limit;
|
||||||
limit = void(limit)
|
const range =
|
||||||
const range = {"7":"last_7_days", "30":"last_30_days", "180":"last_6_months", "365":"last_year"}[days] ?? "last_7_days"
|
{
|
||||||
|
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)
|
//Querying api and format result (https://wakatime.com/developers#stats)
|
||||||
console.debug(`metrics/compute/${login}/plugins > wakatime > querying api`)
|
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 {data: { data: stats }} = await imports.axios.get(`${url}/api/v1/users/${user}/stats/${range}?api_key=${token}`);
|
||||||
const result = {
|
const result = {
|
||||||
sections,
|
sections,
|
||||||
days,
|
days,
|
||||||
time:{
|
time: {
|
||||||
total:stats.total_seconds/(60*60),
|
total: stats.total_seconds / (60 * 60),
|
||||||
daily:stats.daily_average/(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),
|
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),
|
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),
|
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),
|
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
|
//Result
|
||||||
return result
|
return result;
|
||||||
}
|
} catch (error) {
|
||||||
//Handle errors
|
//Handle errors
|
||||||
catch (error) {
|
let message = "An error occured";
|
||||||
let message = "An error occured"
|
|
||||||
if (error.isAxiosError) {
|
if (error.isAxiosError) {
|
||||||
const status = error.response?.status
|
const status = error.response?.status;
|
||||||
message = `API returned ${status}`
|
message = `API returned ${status}`;
|
||||||
error = error.response?.data ?? null
|
error = error.response?.data ?? null;
|
||||||
}
|
|
||||||
throw {error:{message, instance:error}}
|
|
||||||
}
|
}
|
||||||
|
throw {error:{message, instance:error }};
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ index: 7
|
|||||||
supports:
|
supports:
|
||||||
- user
|
- user
|
||||||
inputs:
|
inputs:
|
||||||
|
|
||||||
# Enable or disable plugin
|
# Enable or disable plugin
|
||||||
plugin_wakatime:
|
plugin_wakatime:
|
||||||
description: Display WakaTime stats
|
description: Display WakaTime stats
|
||||||
@@ -53,3 +52,18 @@ inputs:
|
|||||||
type: number
|
type: number
|
||||||
default: 5
|
default: 5
|
||||||
min: 0
|
min: 0
|
||||||
|
|
||||||
|
# If you use a selfhosted wakatime instance (wakapi)
|
||||||
|
# that is publicly available place your url here
|
||||||
|
#
|
||||||
|
# If you use the public wakapi instance the url
|
||||||
|
# would be https://wakapi.dev
|
||||||
|
plugin_wakatime_url:
|
||||||
|
description: Address where to reach your Wakatime instance
|
||||||
|
type: string
|
||||||
|
default: http://wakatime.com
|
||||||
|
|
||||||
|
plugin_wakatime_user:
|
||||||
|
description: Your Wakatime user on the selfhosted Wakapi instance
|
||||||
|
type: string
|
||||||
|
default: .user.login
|
||||||
|
|||||||
Reference in New Issue
Block a user