functions.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. GENERATE_ISSUE_TSVECTOR = """
  2. CREATE OR REPLACE FUNCTION generate_issue_tsvector(jsonb) RETURNS tsvector AS $$
  3. BEGIN
  4. RETURN strip(jsonb_to_tsvector($1, '["string"]'));
  5. EXCEPTION WHEN program_limit_exceeded THEN
  6. RETURN null;
  7. END;
  8. $$ LANGUAGE plpgsql;;
  9. """
  10. UPDATE_ISSUE_INDEX = """
  11. CREATE OR REPLACE PROCEDURE update_issue_index(update_issue_id integer)
  12. LANGUAGE SQL
  13. AS $$
  14. WITH event_agg as (
  15. SELECT COUNT(events_event.event_id) as new_count,
  16. MAX(events_event.created) as new_last_seen,
  17. MAX(events_event.level) as new_level
  18. FROM events_event
  19. WHERE events_event.issue_id=update_issue_id
  20. ), event_vector as (
  21. SELECT strip(COALESCE(generate_issue_tsvector(data), '') || COALESCE(issues_issue.search_vector, '')) as vector
  22. FROM events_event
  23. LEFT JOIN issues_issue on issues_issue.id = events_event.issue_id
  24. WHERE events_event.issue_id=update_issue_id
  25. limit 1
  26. ), event_tags as (
  27. SELECT jsonb_object_agg(y.key, y.values) as new_tags FROM (
  28. SELECT (a).key, array_agg(distinct(a).value) as values
  29. FROM (
  30. SELECT each(tags) as a
  31. FROM events_event
  32. WHERE events_event.issue_id=update_issue_id
  33. ) t GROUP by key
  34. ) y
  35. )
  36. UPDATE issues_issue
  37. SET
  38. count = event_agg.new_count,
  39. last_seen = event_agg.new_last_seen,
  40. level = event_agg.new_level,
  41. search_vector = CASE WHEN search_vector is null or length(search_vector) < 100000 THEN event_vector.vector ELSE search_vector END,
  42. tags = CASE WHEN event_Tags.new_tags is not null THEN event_tags.new_tags ELSE tags END
  43. FROM event_agg, event_vector, event_tags
  44. WHERE issues_issue.id = update_issue_id;
  45. $$;
  46. """