diff --git a/source/plugins/wakatime/index.mjs b/source/plugins/wakatime/index.mjs index 26089b61..bc83b38d 100644 --- a/source/plugins/wakatime/index.mjs +++ b/source/plugins/wakatime/index.mjs @@ -1,44 +1,47 @@ //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 +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" + //Load inputs + let { sections, days, limit, url, user } = 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), - } + //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(`${url}/api/v1/users/${user}/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 - } + //Result + return result; + } catch (error) { //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}} - } + 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 }}; } +} diff --git a/source/plugins/wakatime/metadata.yml b/source/plugins/wakatime/metadata.yml index 009924b5..ced8ab89 100644 --- a/source/plugins/wakatime/metadata.yml +++ b/source/plugins/wakatime/metadata.yml @@ -5,7 +5,6 @@ index: 7 supports: - user inputs: - # Enable or disable plugin plugin_wakatime: description: Display WakaTime stats @@ -24,10 +23,10 @@ inputs: description: WakaTime time range type: string values: - - 7 # Last week - - 30 # Last month - - 180 # Last 6 months - - 365 # Last year + - 7 # Last week + - 30 # Last month + - 180 # Last 6 months + - 365 # Last year default: 7 # Sections to display @@ -35,15 +34,15 @@ inputs: 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 + - 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 @@ -53,3 +52,18 @@ inputs: type: number default: 5 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