system.controller.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { Request, Response } from 'express';
  2. import fetch from 'node-fetch';
  3. import config from '../../config';
  4. import TipiCache from '../../config/cache';
  5. import { readJsonFile } from '../fs/fs.helpers';
  6. type CpuData = {
  7. load: number;
  8. };
  9. type DiskData = {
  10. total: number;
  11. used: number;
  12. available: number;
  13. };
  14. type MemoryData = {
  15. total: number;
  16. available: number;
  17. used: number;
  18. };
  19. type SystemInfo = {
  20. cpu: CpuData;
  21. disk: DiskData;
  22. memory: MemoryData;
  23. };
  24. /**
  25. *
  26. * @param req
  27. * @param res
  28. */
  29. const getCpuInfo = async (req: Request, res: Response<CpuData>) => {
  30. const systemInfo: SystemInfo = readJsonFile('/state/system-info.json');
  31. const cpu = systemInfo.cpu;
  32. res.status(200).send({ load: cpu.load });
  33. };
  34. /**
  35. *
  36. * @param req
  37. * @param res
  38. */
  39. const getDiskInfo = async (req: Request, res: Response<DiskData>) => {
  40. const systemInfo: SystemInfo = readJsonFile('/state/system-info.json');
  41. const result: DiskData = systemInfo.disk;
  42. res.status(200).send(result);
  43. };
  44. /**
  45. *
  46. * @param req
  47. * @param res
  48. */
  49. const getMemoryInfo = async (req: Request, res: Response<MemoryData>) => {
  50. const systemInfo: SystemInfo = readJsonFile('/state/system-info.json');
  51. const result: MemoryData = systemInfo.memory;
  52. res.status(200).json(result);
  53. };
  54. const getVersion = async (_: Request, res: Response<{ current: string; latest: string }>) => {
  55. let version = TipiCache.get<string>('latestVersion');
  56. if (!version) {
  57. const response = await fetch('https://api.github.com/repos/meienberger/runtipi/releases/latest');
  58. const json = (await response.json()) as { name: string };
  59. TipiCache.set('latestVersion', json.name);
  60. version = json.name.replace('v', '');
  61. }
  62. res.status(200).send({ current: config.VERSION, latest: version });
  63. };
  64. export default { getCpuInfo, getDiskInfo, getMemoryInfo, getVersion };