You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RepoRecentCommits.vue 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <script>
  2. import {SvgIcon} from '../svg.js';
  3. import {
  4. Chart,
  5. Tooltip,
  6. BarElement,
  7. LinearScale,
  8. TimeScale,
  9. } from 'chart.js';
  10. import {GET} from '../modules/fetch.js';
  11. import {Bar} from 'vue-chartjs';
  12. import {
  13. startDaysBetween,
  14. firstStartDateAfterDate,
  15. fillEmptyStartDaysWithZeroes,
  16. } from '../utils/time.js';
  17. import {chartJsColors} from '../utils/color.js';
  18. import {sleep} from '../utils.js';
  19. import 'chartjs-adapter-dayjs-4/dist/chartjs-adapter-dayjs-4.esm';
  20. const {pageData} = window.config;
  21. Chart.defaults.color = chartJsColors.text;
  22. Chart.defaults.borderColor = chartJsColors.border;
  23. Chart.register(
  24. TimeScale,
  25. LinearScale,
  26. BarElement,
  27. Tooltip,
  28. );
  29. export default {
  30. components: {Bar, SvgIcon},
  31. props: {
  32. locale: {
  33. type: Object,
  34. required: true,
  35. },
  36. },
  37. data: () => ({
  38. isLoading: false,
  39. errorText: '',
  40. repoLink: pageData.repoLink || [],
  41. data: [],
  42. }),
  43. mounted() {
  44. this.fetchGraphData();
  45. },
  46. methods: {
  47. async fetchGraphData() {
  48. this.isLoading = true;
  49. try {
  50. let response;
  51. do {
  52. response = await GET(`${this.repoLink}/activity/recent-commits/data`);
  53. if (response.status === 202) {
  54. await sleep(1000); // wait for 1 second before retrying
  55. }
  56. } while (response.status === 202);
  57. if (response.ok) {
  58. const data = await response.json();
  59. const start = Object.values(data)[0].week;
  60. const end = firstStartDateAfterDate(new Date());
  61. const startDays = startDaysBetween(new Date(start), new Date(end));
  62. this.data = fillEmptyStartDaysWithZeroes(startDays, data).slice(-52);
  63. this.errorText = '';
  64. } else {
  65. this.errorText = response.statusText;
  66. }
  67. } catch (err) {
  68. this.errorText = err.message;
  69. } finally {
  70. this.isLoading = false;
  71. }
  72. },
  73. toGraphData(data) {
  74. return {
  75. datasets: [
  76. {
  77. data: data.map((i) => ({x: i.week, y: i.commits})),
  78. label: 'Commits',
  79. backgroundColor: chartJsColors['commits'],
  80. borderWidth: 0,
  81. tension: 0.3,
  82. },
  83. ],
  84. };
  85. },
  86. getOptions() {
  87. return {
  88. responsive: true,
  89. maintainAspectRatio: false,
  90. animation: true,
  91. scales: {
  92. x: {
  93. type: 'time',
  94. grid: {
  95. display: false,
  96. },
  97. time: {
  98. minUnit: 'week',
  99. },
  100. ticks: {
  101. maxRotation: 0,
  102. maxTicksLimit: 52,
  103. },
  104. },
  105. y: {
  106. ticks: {
  107. maxTicksLimit: 6,
  108. },
  109. },
  110. },
  111. };
  112. },
  113. },
  114. };
  115. </script>
  116. <template>
  117. <div>
  118. <div class="ui header tw-flex tw-content-center tw-justify-between">
  119. {{ isLoading ? locale.loadingTitle : errorText ? locale.loadingTitleFailed: "Number of commits in the past year" }}
  120. </div>
  121. <div class="tw-flex ui segment main-graph">
  122. <div v-if="isLoading || errorText !== ''" class="gt-tc tw-m-auto">
  123. <div v-if="isLoading">
  124. <SvgIcon name="octicon-sync" class="gt-mr-3 job-status-rotate"/>
  125. {{ locale.loadingInfo }}
  126. </div>
  127. <div v-else class="text red">
  128. <SvgIcon name="octicon-x-circle-fill"/>
  129. {{ errorText }}
  130. </div>
  131. </div>
  132. <Bar
  133. v-memo="data" v-if="data.length !== 0"
  134. :data="toGraphData(data)" :options="getOptions()"
  135. />
  136. </div>
  137. </div>
  138. </template>
  139. <style scoped>
  140. .main-graph {
  141. height: 250px;
  142. }
  143. </style>