• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stacklok / codegate-ui / 12707890793

10 Jan 2025 10:36AM CUT coverage: 67.801%. First build
12707890793

Pull #42

github

web-flow
Merge bd60117e5 into d69fac46a
Pull Request #42: chore: add coveralls coverage report

174 of 338 branches covered (51.48%)

Branch coverage included in aggregate %.

384 of 485 relevant lines covered (79.18%)

26.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.42
/src/lib/utils.ts
1
import { Alert, Prompt } from "@/types";
2
import { clsx, type ClassValue } from "clsx";
3
import { isToday, isYesterday } from "date-fns";
4
import { twMerge } from "tailwind-merge";
5

6
const ONE_DAY_MS = 24 * 60 * 60 * 1000;
6✔
7
const SEVEN_DAYS_MS = 7 * ONE_DAY_MS;
6✔
8
const TEEN_DAYS_MS = 14 * ONE_DAY_MS;
6✔
9
const THTY_DAYS_MS = 30 * ONE_DAY_MS;
6✔
10

11
export function cn(...inputs: ClassValue[]) {
12
  return twMerge(clsx(inputs));
3,199✔
13
}
14

15
export function extractTitleFromMessage(message: string) {
16
  try {
15✔
17
    const regex = /^(.*)```[\s\S]*?```(.*)$/s;
15✔
18
    const match = message.match(regex);
15✔
19

20
    if (match) {
15✔
21
      const beforeMarkdown = match[1].trim();
10✔
22
      const afterMarkdown = match[2].trim();
10✔
23
      const title = beforeMarkdown || afterMarkdown;
10✔
24
      return title;
10✔
25
    }
26

27
    return message.trim();
5✔
28
  } catch {
29
    return message.trim();
×
30
  }
31
}
32

33
function getGroup(differenceInMs: number, promptDate: Date): string {
34
  if (isToday(promptDate)) {
14!
35
    return "Today";
×
36
  }
37
  if (isYesterday(promptDate)) {
14!
38
    return "Yesterday";
×
39
  }
40
  if (differenceInMs <= SEVEN_DAYS_MS) {
14!
41
    return "Previous 7 days";
×
42
  }
43
  if (differenceInMs <= TEEN_DAYS_MS) {
14!
44
    return "Previous 14 days";
14✔
45
  }
46
  if (differenceInMs <= THTY_DAYS_MS) {
×
47
    return "Previous 30 days";
×
48
  }
49
  return "Beyond 30 days";
×
50
}
51

52
export function groupPromptsByRelativeDate(prompts: Prompt[]) {
53
  const promptsSorted = prompts.sort(
6✔
54
    (a, b) =>
55
      new Date(b.conversation_timestamp).getTime() -
9✔
56
      new Date(a.conversation_timestamp).getTime(),
57
  );
58

59
  const grouped = promptsSorted.reduce(
6✔
60
    (groups, prompt) => {
61
      const promptDate = new Date(prompt.conversation_timestamp);
14✔
62
      const now = new Date();
14✔
63
      const differenceInMs = now.getTime() - promptDate.getTime();
14✔
64
      const group = getGroup(differenceInMs, promptDate);
14✔
65

66
      if (!groups[group]) {
14✔
67
        groups[group] = [];
5✔
68
      }
69

70
      groups[group].push(prompt);
14✔
71
      return groups;
14✔
72
    },
73
    {} as Record<string, Prompt[]>,
74
  );
75

76
  return grouped;
6✔
77
}
78

79
export function getAllIssues(alerts: Alert[]) {
80
  const groupedTriggerCounts = alerts.reduce<Record<string, number>>(
53✔
81
    (acc, alert) => {
82
      const triggerType = alert.trigger_type;
175✔
83
      if (triggerType) {
175!
84
        acc[triggerType] = (acc[triggerType] || 0) + 1;
175✔
85
      }
86
      return acc;
175✔
87
    },
88
    {},
89
  );
90

91
  const maxCount = Math.max(...Object.values(groupedTriggerCounts));
53✔
92

93
  const sortedTagCounts = Object.entries(groupedTriggerCounts).sort(
53✔
94
    ([, countA], [, countB]) => countB - countA,
42✔
95
  );
96
  return { maxCount, sortedTagCounts };
53✔
97
}
98

99
export function getMaliciousPackages() {
100
  const packageCounts = ([] as { packages: [] }[]).reduce<
×
101
    Record<string, number>
102
  >((acc, prompt) => {
103
    (prompt?.packages ?? []).forEach((pkg) => {
×
104
      acc[pkg] = (acc[pkg] || 0) + 1;
×
105
    });
106
    return acc;
×
107
  }, {});
108

109
  const chartData = Object.entries(packageCounts).map(([pkg, count]) => ({
×
110
    id: pkg,
111
    label: pkg,
112
    value: count,
113
  }));
114

115
  return chartData;
×
116
}
117

118
export function sanitizeQuestionPrompt({
119
  question,
120
  answer,
121
}: {
122
  question: string;
123
  answer: string;
124
}) {
125
  try {
15✔
126
    // it shouldn't be possible to receive the prompt answer without a question
127
    if (!answer) return question;
15!
128

129
    // Check if 'answer' is truthy; if so, try to find and return the text after "Query:"
130
    const index = question.indexOf("Query:");
15✔
131
    if (index !== -1) {
15!
132
      // Return the substring starting right after the first occurrence of "Query:"
133
      // Adding the length of "Query:" to the index to start after it
134
      return question.substring(index + "Query:".length).trim();
×
135
    }
136
    return question;
15✔
137
  } catch (error) {
138
    // Log the error and return the original question as a fallback
139
    console.error("Error processing the question:", error);
×
140
    return question;
×
141
  }
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc