-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTechnologyStackServices.cs
More file actions
364 lines (305 loc) · 12.5 KB
/
Copy pathTechnologyStackServices.cs
File metadata and controls
364 lines (305 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ServiceStack;
using ServiceStack.OrmLite;
using TechStacks.ServiceModel;
using TechStacks.ServiceModel.Types;
namespace TechStacks.ServiceInterface;
public class TechnologyStackServices : Service
{
public object Get(GetTechnologyStackPreviousVersions request)
{
if (request.Slug == null)
throw new ArgumentNullException(nameof(request.Slug));
if (!long.TryParse(request.Slug, out var id))
{
var techStack = Db.Single<TechnologyStack>(x => x.Slug == request.Slug.ToLower());
id = techStack.Id;
}
return new GetTechnologyStackPreviousVersionsResponse
{
Results = Db.Select(Db.From<TechnologyStackHistory>()
.Where(x => x.TechnologyStackId == id)
.OrderByDescending(x => x.LastModified))
};
}
public object Any(GetPageStats request)
{
var id = $"/{request.Type}/{request.Slug}";
var pageStats = Db.SingleById<PageStats>(id);
return new GetPageStatsResponse
{
Type = request.Type,
Slug = request.Slug,
ViewCount = pageStats?.ViewCount ?? 0,
FavCount = request.Id == null
? pageStats?.FavCount ?? 0
: request.Type == "tech"
? Db.Count<UserFavoriteTechnology>(x => x.TechnologyId == request.Id)
: Db.Count<UserFavoriteTechnologyStack>(x => x.TechnologyStackId == request.Id),
};
}
public object Any(ClearCache request)
{
Cache.FlushAll();
PostServicesBase.PeriodicUpdateTableCaches(Db);
PostServicesBase.ClearOrganizationCache();
Cache.FlushAll();
return "OK";
}
public object Any(HourlyTask request)
{
if (!request.Force)
return new HourlyTaskResponse();
var updatedTechIds = Db.ExecuteSql("UPDATE page_stats AS p SET ref_id = t.id FROM technology AS t WHERE t.slug = p.ref_slug and p.ref_type = 'tech' AND ref_id = 0");
var updatedStackIds = Db.ExecuteSql("UPDATE page_stats AS p SET ref_id = t.id FROM technology_stack AS t WHERE t.slug = p.ref_slug and p.ref_type = 'stack' AND ref_id = 0");
var techFavs = Db.Dictionary<long, long>("SELECT technology_id, count(*) FROM user_favorite_technology GROUP BY technology_id");
foreach (var techFav in techFavs)
{
Db.ExecuteSql("UPDATE page_stats SET fav_count = @favCount WHERE ref_id = @refId and ref_type = 'tech'",
new { refId = techFav.Key, favCount = techFav.Value });
}
var stackFavs = Db.Dictionary<long, long>("SELECT technology_stack_id, count(*) FROM user_favorite_technology_stack GROUP BY technology_stack_id");
foreach (var stackFav in stackFavs)
{
Db.ExecuteSql("UPDATE page_stats SET fav_count = @favCount WHERE ref_id = @refId and ref_type = 'stack'",
new { refId = stackFav.Key, favCount = stackFav.Value });
}
PostServicesBase.PeriodicUpdateTableCaches(Db);
Any(new ClearCache());
return new HourlyTaskResponse
{
Meta = new Dictionary<string, string>
{
{ "updatedTechIds", updatedTechIds.ToString() },
{ "updatedStackIds", updatedStackIds.ToString() },
{ "techFavsCount", techFavs.Count.ToString() },
{ "stackFavsCount", stackFavs.Count.ToString() },
}
};
}
}
[CacheResponse(Duration = 3600)]
public class CachedTechnologyStackServices(IAutoQueryDb autoQuery) : Service
{
//Cached AutoQuery
public async Task<object> Any(FindTechStacks request)
{
using var db = autoQuery.GetDb(request, base.Request);
var q = autoQuery.CreateQuery(request, Request, db);
return await autoQuery.ExecuteAsync(request, q, db);
}
public async Task<object> Any(QueryTechStacks request)
{
using var db = autoQuery.GetDb(request, base.Request);
var q = autoQuery.CreateQuery(request, Request, db);
return await autoQuery.ExecuteAsync(request, q, db);
}
private const int TechStacksAppId = 1;
public object Any(Overview request)
{
if (request.Reload)
Cache.FlushAll();
var topTechByCategory = GetTopTechByCategory();
var map = new Dictionary<string, List<TechnologyInfo>>();
foreach (var tech in topTechByCategory)
{
List<TechnologyInfo> techs = null;
var key = Enum.GetName(typeof(TechnologyTier), tech.Tier);
if (key != null && !map.TryGetValue(key, out techs))
map[key] = techs = new List<TechnologyInfo>();
techs?.Add(tech);
}
foreach (var tier in map.Keys)
{
var list = map[tier];
list.Sort((x, y) => y.StacksCount - x.StacksCount);
if (list.Count > 5)
list.RemoveRange(5, list.Count - 5);
}
var allOrgs = Db.Select<OrganizationInfo>(Db.From<Organization>()
.Where(g => g.Deleted == null));
var allOrgsMap = allOrgs.ToDictionary(x => x.Id);
var orgLabels = Db.Select<OrganizationLabel>();
foreach (var orgLabel in orgLabels)
{
if (allOrgsMap.TryGetValue(orgLabel.OrganizationId, out var org))
{
(org.Labels ?? (org.Labels = new List<LabelInfo>()))
.Add(orgLabel.ConvertTo<LabelInfo>());
}
}
var orgCategories = Db.Select<Category>(x => x.Deleted == null);
foreach (var category in orgCategories)
{
if (allOrgsMap.TryGetValue(category.OrganizationId, out var org))
{
(org.Categories ?? (org.Categories = new List<CategoryInfo>()))
.Add(category.ConvertTo<CategoryInfo>());
}
}
foreach (var entry in allOrgsMap)
{
var org = entry.Value;
org.MembersCount = PostServicesBase.GetOrganizationMembersCount(org.Id);
org.Categories?.Sort((x,y) => string.Compare(x.Name, y.Name, StringComparison.Ordinal));
}
var response = new OverviewResponse
{
Created = DateTime.UtcNow,
LatestTechStacks = Db.GetTechStackDetails(Db.From<TechnologyStack>().OrderByDescending(x => x.LastModified).Limit(20)),
TopUsers = Db.Select<UserInfo>(
@"select u.user_name as UserName, u.default_profile_url as AvatarUrl, COUNT(*) as StacksCount
from technology_stack ts
left join
user_favorite_technology_stack uf on (ts.id = uf.technology_stack_id)
left join
custom_user_auth u on (u.id = ts.owner_id::integer)
group by u.user_name, u.default_profile_url
having count(*) > 0
order by StacksCount desc
limit 20"),
TopTechnologies = topTechByCategory
.OrderByDescending(x => x.StacksCount)
.Take(50)
.ToList(),
PopularTechStacks = Db.Select(
Db.From<TechnologyStack>()
.Join<PageStats>((s, p) => s.Id == p.RefId && p.RefType == "stack")
.OrderByDescending<PageStats>(p => p.ViewCount)
.Limit(12)),
AllOrganizations = allOrgs,
TopTechnologiesByTier = map,
};
//Lighten payload
response.LatestTechStacks.Each(x => {
x.Details = x.DetailsHtml = null;
x.TechnologyChoices.Each(y => {
y.Description = null;
});
});
//Put TechStacks entry first to provide a first good experience
var techStacksApp = response.LatestTechStacks.FirstOrDefault(x => x.Id == TechStacksAppId);
if (techStacksApp != null)
{
response.LatestTechStacks.RemoveAll(x => x.Id == TechStacksAppId);
response.LatestTechStacks.Insert(0, techStacksApp);
}
return response;
}
private List<TechnologyInfo> GetTopTechByCategory(int minCount = 3)
{
var topTechByCategory = Db.Select<TechnologyInfo>(
@"select t.tier, t.slug as Slug, t.name, t.logo_url, COUNT(*) as StacksCount
from technology_choice tc
inner join
technology t on (tc.technology_id = t.id)
group by t.tier, t.slug, t.name, t.logo_url
having COUNT(*) >= {0}
order by 4 desc".Fmt(minCount));
return topTechByCategory;
}
public object Any(AppOverview request)
{
if (request.Reload)
Cache.FlushAll();
var response = new AppOverviewResponse
{
Created = DateTime.UtcNow,
AllTiers = GetAllTiers(),
TopTechnologies = GetTopTechByCategory(minCount: 1)
.OrderByDescending(x => x.StacksCount)
.Take(100)
.ToList(),
};
response.AllTiers.Insert(0, new Option { Title = "[ Top 100 Technologies ]" });
return response;
}
public object Get(GetAllTechnologyStacks request)
{
return new GetAllTechnologyStacksResponse
{
Results = Db.Select(Db.From<TechnologyStack>().OrderByDescending(x => x.LastModified).Take(100)),
Total = Db.Count<TechnologyStack>(),
};
}
public object Get(GetTechnologyStack request)
{
if (string.IsNullOrEmpty(request.Slug))
throw new ArgumentNullException(nameof(request.Slug));
var techStack = int.TryParse(request.Slug, out var id)
? Db.SingleById<TechnologyStack>(id)
: Db.Single<TechnologyStack>(x => x.Slug == request.Slug.ToLower());
if (techStack == null)
throw HttpError.NotFound("Tech stack not found");
var techChoices = Db.LoadSelect(Db.From<TechnologyChoice>()
.Join<Technology>()
.Join<TechnologyStack>()
.Where(x => x.TechnologyStackId == techStack.Id));
var result = techStack.ConvertTo<TechStackDetails>();
if (!string.IsNullOrEmpty(techStack.Details) && string.IsNullOrEmpty(techStack.DetailsHtml))
{
result.DetailsHtml = MarkdownConfig.Transform(techStack.Details);
}
result.TechnologyChoices = techChoices.Map(x => x.ToTechnologyInStack());
var response = new GetTechnologyStackResponse
{
Created = DateTime.UtcNow,
Result = result
};
return response;
}
public object Get(GetTechnologyStackFavoriteDetails request)
{
var tech = int.TryParse(request.Slug, out var id)
? Db.SingleById<TechnologyStack>(id)
: Db.Single<TechnologyStack>(x => x.Slug == request.Slug.ToLower());
if (tech == null)
throw HttpError.NotFound("TechStack not found");
var favoriteCount = Db.Count<UserFavoriteTechnologyStack>(x => x.TechnologyStackId == tech.Id);
return new GetTechnologyStackFavoriteDetailsResponse
{
FavoriteCount = (int)favoriteCount
};
}
public object Any(GetConfig request)
{
var allTiers = GetAllTiers();
return new GetConfigResponse
{
AllTiers = allTiers,
AllPostTypes = GetAllPostTypes(),
AllFlagTypes = GetAllFlagType(),
};
}
public static List<Option> GetAllTiers()
{
return Enum.GetValues(typeof(TechnologyTier)).Map(x =>
new Option
{
Name = x.ToString(),
Title = typeof(TechnologyTier).GetMember(x.ToString())[0].GetDescription(),
Value = (TechnologyTier)x,
});
}
public static List<Option> GetAllPostTypes()
{
return Enum.GetValues(typeof(PostType)).Map(x =>
new Option
{
Name = x.ToString(),
Title = typeof(PostType).GetMember(x.ToString())[0].GetDescription(),
});
}
public static List<Option> GetAllFlagType()
{
return Enum.GetValues(typeof(FlagType)).Map(x =>
new Option
{
Name = x.ToString(),
Title = typeof(FlagType).GetMember(x.ToString())[0].GetDescription(),
});
}
}