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

stacklok / codegate-ui / 12826888982

17 Jan 2025 10:05AM CUT coverage: 68.391% (+1.2%) from 67.241%
12826888982

Pull #99

github

web-flow
Merge e9242c8bc into 32def3dae
Pull Request #99: fix: fix data fetching bug

205 of 379 branches covered (54.09%)

Branch coverage included in aggregate %.

390 of 491 relevant lines covered (79.43%)

27.11 hits per line

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

70.87
/src/lib/utils.ts
1
import { AlertConversation, Conversation } from "@/api/generated/types.gen";
2
import { MaliciousPkgType, TriggerType } from "@/types";
3
import { isToday, isYesterday } from "date-fns";
4

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

10
export function extractTitleFromMessage(message: string) {
11
  try {
3✔
12
    const regex = /^(.*)```[\s\S]*?```(.*)$/s;
3✔
13
    const match = message.match(regex);
3✔
14

15
    if (match !== null && match !== undefined) {
3✔
16
      const beforeMarkdown = match[1]?.trim();
1✔
17
      const afterMarkdown = match[2]?.trim();
1✔
18
      const title = beforeMarkdown || afterMarkdown;
1✔
19
      return title;
1✔
20
    }
21

22
    return message.trim();
2✔
23
  } catch {
24
    return message.trim();
×
25
  }
26
}
27

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

47
export function groupPromptsByRelativeDate(prompts: Conversation[]) {
48
  const promptsSorted = prompts.sort(
2✔
49
    (a, b) =>
50
      new Date(b.conversation_timestamp).getTime() -
×
51
      new Date(a.conversation_timestamp).getTime(),
52
  );
53

54
  const grouped = promptsSorted.reduce(
2✔
55
    (groups, prompt) => {
56
      const promptDate = new Date(prompt.conversation_timestamp);
2✔
57
      const now = new Date();
2✔
58
      const differenceInMs = now.getTime() - promptDate.getTime();
2✔
59
      const group = getGroup(differenceInMs, promptDate);
2✔
60

61
      if (!groups[group]) {
2!
62
        groups[group] = [];
2✔
63
      }
64

65
      (groups[group] ?? []).push(prompt);
2!
66
      return groups;
2✔
67
    },
68
    {} as Record<string, Conversation[]>,
69
  );
70

71
  return grouped;
2✔
72
}
73

74
export function getAllIssues(alerts: AlertConversation[]) {
75
  const groupedTriggerCounts = alerts.reduce<Record<string, number>>(
65✔
76
    (acc, alert) => {
77
      const triggerType: TriggerType = alert.trigger_type;
138✔
78
      if (triggerType) {
138!
79
        acc[triggerType] = (acc[triggerType] || 0) + 1;
138✔
80
      }
81
      return acc;
138✔
82
    },
83
    {},
84
  );
85

86
  const maxCount = Math.max(...Object.values(groupedTriggerCounts));
65✔
87

88
  const sortedTagCounts = Object.entries(groupedTriggerCounts).sort(
65✔
89
    ([, countA], [, countB]) => countB - countA,
56✔
90
  );
91
  return { maxCount, sortedTagCounts };
65✔
92
}
93

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

104
  const chartData = Object.entries(packageCounts).map(([pkg, count]) => ({
×
105
    id: pkg,
106
    label: pkg,
107
    value: count,
108
  }));
109

110
  return chartData;
×
111
}
112

113
export function sanitizeQuestionPrompt({
114
  question,
115
  answer,
116
}: {
117
  question: string;
118
  answer: string;
119
}) {
120
  try {
3✔
121
    // it shouldn't be possible to receive the prompt answer without a question
122
    if (!answer) return question;
3!
123

124
    // Check if 'answer' is truthy; if so, try to find and return the text after "Query:"
125
    const index = question.indexOf("Query:");
3✔
126
    if (index !== -1) {
3!
127
      // Return the substring starting right after the first occurrence of "Query:"
128
      // Adding the length of "Query:" to the index to start after it
129
      return question.substring(index + "Query:".length).trim();
×
130
    }
131
    return question;
3✔
132
  } catch (error) {
133
    // Log the error and return the original question as a fallback
134
    console.error("Error processing the question:", error);
×
135
    return question;
×
136
  }
137
}
138

139
export function getMaliciousPackage(
140
  value: AlertConversation["trigger_string"],
141
): string | (MaliciousPkgType & { [key: string]: string }) | null {
142
  if (typeof value === "string") {
310✔
143
    return value;
208✔
144
  }
145

146
  if (typeof value === "object" && value !== null) {
102✔
147
    return value as MaliciousPkgType;
94✔
148
  }
149

150
  return null;
8✔
151
}
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