utils.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { dateTime } from '@grafana/data';
  2. import { config } from '@grafana/runtime';
  3. import { CardState } from './LicenseCard';
  4. import { ERROR_RATE, WARNING_RATE, LIMIT_BY_USERS } from './constants';
  5. import { ActiveUserStats, LicenseToken } from './types';
  6. type Status = { state: CardState; status: string; title: string; message: string };
  7. export const getUserStatMessage = (includedUsers: number, activeUsers?: number): string => {
  8. return (activeUsers ? `${activeUsers} of ` : '') + (includedUsers > 0 ? includedUsers : 'unlimited');
  9. };
  10. const tokenExpired = (token: LicenseToken) => {
  11. return dateTime(token.exp * 1000) < dateTime();
  12. };
  13. const tokenWillExpireSoon = (token: LicenseToken) => {
  14. return dateTime(token.exp * 1000) < dateTime().add((config as any).licensing.tokenExpiryWarnDays, 'd');
  15. };
  16. const tokenWillExpireInDays = (token: LicenseToken) => {
  17. return Math.ceil((token.exp - dateTime().unix()) / 3600 / 24);
  18. };
  19. export const getTokenStatus = (token: LicenseToken | null): Status => {
  20. if (!token) {
  21. return { state: '', status: '', title: '', message: '' };
  22. }
  23. if (tokenExpired(token)) {
  24. return {
  25. state: 'error',
  26. status: 'Expired',
  27. title: 'Token expired',
  28. message: 'Contact support to renew your token, or visit the Cloud portal to learn more.',
  29. };
  30. }
  31. if (tokenWillExpireSoon(token)) {
  32. return {
  33. state: 'warning',
  34. status: ` Expiring in ${tokenWillExpireInDays(token)} day(s)`,
  35. title: 'Token expires soon',
  36. message: `Your token expires in ${tokenWillExpireInDays(token)} day(s). Contact support to renew your token.`,
  37. };
  38. }
  39. return { state: '', status: '', title: '', message: '' };
  40. };
  41. const trialExpired = (token: LicenseToken) => {
  42. return token.trial_exp && dateTime(token.trial_exp * 1000) < dateTime();
  43. };
  44. const trialWillExpireInDays = (token: LicenseToken) => {
  45. if (!token.trial_exp) {
  46. return 0;
  47. }
  48. return Math.ceil((token.trial_exp - dateTime().unix()) / 3600 / 24);
  49. };
  50. export const getTrialStatus = (token: LicenseToken | null): Status => {
  51. if (!token || !token.trial || !token.trial_exp) {
  52. return { state: '', status: '', title: '', message: '' };
  53. }
  54. if (trialExpired(token)) {
  55. return {
  56. state: 'error',
  57. status: 'Expired',
  58. title: 'Trial expired',
  59. message: 'Your trial period has expired, please contact sales to purchase a license.',
  60. };
  61. }
  62. return {
  63. state: 'info',
  64. status: ` Trial expiring in ${trialWillExpireInDays(token)} day(s)`,
  65. title: 'Trial active',
  66. message: `Your trial expires in ${trialWillExpireInDays(
  67. token
  68. )} day(s), please contact sales to purchase a license.`,
  69. };
  70. };
  71. export const getRate = (total: number, value = 0) => {
  72. return (100 * value) / total;
  73. };
  74. export const getUtilStatus = (token: LicenseToken | null, stats?: ActiveUserStats | null): Status => {
  75. let state: CardState = '';
  76. let status = '';
  77. let title = '';
  78. let message = '';
  79. if (!token) {
  80. return {
  81. state,
  82. status,
  83. title,
  84. message,
  85. };
  86. }
  87. if (token.limit_by === LIMIT_BY_USERS) {
  88. const activeUsers = stats?.active_users;
  89. const totalUsers = token.included_users;
  90. let userRate = 0;
  91. if (typeof activeUsers === 'number') {
  92. userRate = getRate(totalUsers, activeUsers);
  93. }
  94. if (userRate > ERROR_RATE) {
  95. state = 'error';
  96. status = 'Quota exceeded: active users';
  97. title = 'User limit exceeded';
  98. message = `There are more than ${totalUsers} active users using Grafana.`;
  99. message += ' Contact support to increase the quotas.';
  100. } else if (userRate >= WARNING_RATE) {
  101. state = 'warning';
  102. if (userRate >= ERROR_RATE) {
  103. status = 'Limit reached';
  104. title = 'User utilization limit reached';
  105. message = `You are at your limit of ${totalUsers} active users.`;
  106. } else {
  107. status = 'Reaching limit';
  108. title = 'User utilization reaching limit';
  109. message = `There are ${activeUsers} active users.`;
  110. message += ` You are approaching your limit of ${totalUsers} active users.`;
  111. }
  112. status += ': active users';
  113. message += ' Contact support to increase the quotas.';
  114. }
  115. }
  116. return {
  117. state,
  118. status,
  119. title,
  120. message,
  121. };
  122. };